+
    $LjB                         ^ RI t ^ RIt^ RIHt ^ RIHtHtHtHt ^ RI	H
t
HtHtHt ^ RIHt ]'       d   ^ RIHt ] ! R R4      4       tR# )	    N)	dataclass)TYPE_CHECKINGLiteralOptionalUnion)DeepSpeedSequenceParallelConfigDistributedTypeTorchContextParallelConfigTorchTensorParallelConfig)is_torch_version)Acceleratorc                     a  ] tR t^!t o RtRtRtRtRtRt	Rt
RtRtRtRtRtR tR t]R 4       t]R 4       t]R 4       t]R 4       t]R	 4       t]R
 4       t]R 4       t]R 4       t]R 4       t]R 4       t]R 4       t]R 4       t]R 4       t]R 4       t V 3R lR lt!R V 3R lR llt"V 3R lR lt#R t$V 3R lR lt%V 3R lR lt&V 3R lt'Rt(V t)R# )!ParallelismConfigaM  
A dataclass to configure parallelisms applied to the model. Inspired by torchtitan's `ParallelDims`
https://github.com/pytorch/torchtitan/blob/main/torchtitan/distributed/parallel_dims.py

Args:
    dp_replicate_size (`int`, defaults to `1`):
        The size of the data parallel group. If `dp_replicate_size` is set to 1, the data parallel replication
        group will not be used.
    dp_shard_size (`int`, defaults to `1`):
        The size of the model shard group. If `dp_replicate_size > 1` and `tp_size > 1`, `dp_shard_size` must also
        be greater than 1, as composing DDP + TP is currently not supported.
    tp_size (`int`, defaults to `1`):
        The size of the tensor parallel group. If `tp_size` is set to `1`, the tensor parallel group will not be
        used.
    tp_handler (`~utils.TorchTensorParallelConfig`, defaults to `None`):
        The handler for the tensor parallel group.
    cp_size (`int`, defaults to `1`):
        The size of the context parallel group. Currently not supported, but reserved for future use and enabled
        for downstream libraries.
    cp_backend (`str`, defaults to `torch`):
        Which CP backend to use: `torch` (FSDP2)
    sp_size (`int`, defaults to `1`):
        The size of the sequence parallel group.
    sp_backend (`str`, defaults to `deepspeed`):
        Which SP backend to use:`deepspeed` (ALST/Ulysses)

You may obtain different distributed data parallel paradigms by configuring `dp_replicate_size` and `dp_shard_size`
together:
    - `dp_replicate_size == 1` and `dp_shard_size > 1`, we obtain Fully Sharded Data Parallel (FSDP).
    - `dp_replicate_size > 1` and `dp_shard_size > 1`, we obtain Hybrid Sharded Data Parallel (HSDP).
    - `dp_replicate_size > 1` and `dp_shard_size == 1` is an invalid configuration, to use pure DP, use
      `DistributedDataParallelKwargs` instead.

Nc                   R V P                    RV P                   RV P                   RV P                   RV P                   RV P
                   RV P                   RV P                   RV P                   R	V P                   R
2# )z'ParallelismConfig(
 	dp_replicate_size=z,
	dp_shard_size=z,
	tp_size=z,
	cp_size=z,
	cp_backend=z,
	sp_size=z,
	sp_backend=z,
	total_size=z
	tp_handler=z,
	cp_handler=z)
)
dp_replicate_sizedp_shard_sizetp_sizecp_size
cp_backendsp_size
sp_backend
total_size
tp_handler
cp_handlerselfs   &I/app/.local/lib/python3.14/site-packages/accelerate/parallelism_config.py__repr__ParallelismConfig.__repr__U   s    ##'#9#9": ;#112 3 ' ' OO, - ' OO, - OO, - OO, - OO,C
1	
    c                   ^ RI pR.pTP                  V P                  P                  4        UUu/ uF=  w  r4W29  g   K  T\	        VR4      '       d   VP                  VP                  4      MTbK?  	  upp4       R# u uppi )r   Ndevice_mesh__dict__)copydeepcopyr#   itemshasattr)r   r$   _non_serializable_fieldskvs   &    r   to_jsonParallelismConfig.to_jsond   ss    $1?  !MM//11DA4 N:0F0F4==,AM1	
s
   
A=
4A=
c                z    . pV P                   '       d   VR.,          pV P                  '       d   VR.,          pV# )zENames of enabled dimensions across which data parallelism is applied.dp_replicatedp_shard)dp_replicate_enableddp_shard_enabledr   dimss   & r   dp_dim_namesParallelismConfig.dp_dim_namesq   s?     $$$^$$D   ZL Dr    c                    . pV P                   '       d   VR.,          pV P                  '       d   VR.,          pV P                  '       d   VR.,          pV# )z]Names of enabled dimensions which will receive the same batch (non-data parallel dimensions).tpcpsp)
tp_enabled
cp_enabled
sp_enabledr2   s   & r   non_dp_dim_names"ParallelismConfig.non_dp_dim_names{   sJ     ???TFND???TFND???TFNDr    c                z    . pV P                   '       d   VR.,          pV P                  '       d   VR.,          pV# )zlNames of enabled dimensions which will be flattened into a joint mesh across which is model sharded in FSDP.r/   r8   )r1   r;   r2   s   & r   dp_shard_cp_dim_names'ParallelismConfig.dp_shard_cp_dim_names   s:        ZL D???TFNDr    c                    . pV P                   '       d   VR.,          pV P                  '       d   VR.,          pV P                  '       d   VR.,          pV# )z@Names of enabled dimensions across which loss should be averagedr.   r/   r8   )r0   r1   r;   r2   s   & r   dp_cp_dim_names!ParallelismConfig.dp_cp_dim_names   sS     $$$^$$D   ZL D???TFNDr    c                V    . pV P                   '       d   VR.,          pVR.,          pV# )z^Names of enabled dimensions across which FSDP is applied, including data parallel replication.r.   dp_shard_cp)r0   r2   s   & r   fsdp_dim_names ParallelismConfig.fsdp_dim_names   s3     $$$^$$Dr    c                    V P                   V P                  ,          V P                  ,          V P                  ,          V P                  ,          # )zSThe total size of the parallelism configuration, which is the product of all sizes.)r   r   r   r   r   r   s   &r   r   ParallelismConfig.total_size   s9     %%(:(::T\\IDLLX[_[g[gggr    c                ^    V P                   V P                  ,          V P                  ,          # )zhThe size of the non-data parallel dimensions, which is the product of tensor and context parallel sizes.)r   r   r   r   s   &r   non_data_parallel_size(ParallelismConfig.non_data_parallel_size   s      ||dll*T\\99r    c                <    V P                   V P                  ,          # )z_The size of the data parallel dimensions, which is the product of data parallel replication and)r   r   r   s   &r   data_parallel_size$ParallelismConfig.data_parallel_size   s     %%(:(:::r    c                     V P                   ^8  # )zKTrue if data parallel replication is enabled, i.e. `dp_replicate_size > 1`.)r   r   s   &r   r0   &ParallelismConfig.dp_replicate_enabled   s     %%))r    c                     V P                   ^8  # )zDTrue if data parallel sharding is enabled, i.e. `dp_shard_size > 1`.)r   r   s   &r   r1   "ParallelismConfig.dp_shard_enabled   s     !!A%%r    c                     V P                   ^8  # )z:True if tensor parallelism is enabled, i.e. `tp_size > 1`.)r   r   s   &r   r:   ParallelismConfig.tp_enabled        ||ar    c                     V P                   ^8  # )z;True if context parallelism is enabled, i.e. `cp_size > 1`.)r   r   s   &r   r;   ParallelismConfig.cp_enabled   rW   r    c                     V P                   ^8  # )z;True if context parallelism is enabled, i.e. `sp_size > 1`.)r   r   s   &r   r<   ParallelismConfig.sp_enabled   rW   r    c                <    V P                   V P                  ,           # )z$Names of all active mesh dimensions.)r4   r=   r   s   &r   active_mesh_dims"ParallelismConfig.active_mesh_dims   s       4#8#888r    c                    < V ^8  d   QhRS[ /#    device_type)str)format__classdict__s   "r   __annotate__ParallelismConfig.__annotate__   s     ! !S !r    c                *   V P                   R8X  d   V P                  ^8  d   R# \        RR4      '       d   ^ RIHp M\        R4      hV P                  4       p\        V4      ^ 8X  d   R# Vw  rEV! VVVR7      pV P                  '       d"   W`P                  ,          P                  R4       V P                  '       d"   W`P                  ,          P                  R	4       V P                  '       d"   W`P                  ,          P                  R
4       V# )a  Builds a device mesh for the given device type based on the parallelism configuration.
This method will also create required joint meshes (e.g. `dp_shard_cp`, `dp_cp`, `dp`).

Args:
    device_type (`str`): The type of device for which to build the mesh, e
	deepspeedNz>=z2.2.0)init_device_meshz4Building a device_mesh requires to have torch>=2.2.0)mesh_dim_namesdprF   dp_cp)r   r   r   torch.distributed.device_meshrj   RuntimeError	_get_meshlenr4   _flattenr@   rC   )r   rb   rj   meshrk   
mesh_shaper"   s   &&     r   build_device_mesh#ParallelismConfig.build_device_mesh   s     ??k)dllQ.>D'**FUVV~~t9>%)"&)

 ))*33D9%%%223<<]K,,-66w?r    c                0   < V ^8  d   QhRS[ S[,          /# r`   )r   rc   )rd   re   s   "r   rf   rg      s        8C=  r    c                "   V P                   f2   Ve#   V P                  V4      V n         V P                   # \        R4      hVeB   V P                   P                  V8w  d'   \        RV P                   P                   RV R24      hV P                   # )Nz@You need to pass a device_type e.g cuda to build the device meshz4The device_mesh is already created with device type z@. However, you are trying to get a device mesh with device_type z<. Please check if you correctly initialized your device_mesh)r"   ru   
ValueErrorrb   )r   rb   s   &&r   get_device_mesh!ParallelismConfig.get_device_mesh   s    #&#'#9#9+#F   !!cdd&##//;>$NtO_O_OkOkNl  mm  ny  mz  zv  w  r    c                ^   < V ^8  d   QhRS[ S[ S[R3,          S[ S[R3,          3,          /# )ra   return.)tupleintrc   )rd   re   s   "r   rf   rg     s.     ) )5sCx%S/!AB )r    c                   a V P                    Uu/ uF  qV P                  V,          bK  	  pp. ROo\        VP                  4       V3R lR7      p\	        \        V!  4      # u upi )zQGenerate mesh shape and dimension names for torch.distributed.init_device_mesh().c                 4   < SP                  V ^ ,          4      # )r   )index)x
mesh_orders   &r   <lambda>-ParallelismConfig._get_mesh.<locals>.<lambda>  s    :++AaD1r    )key)r.   r/   r8   r9   r7   )r]   _sizessortedr&   r~   zip)r   parallelism	mesh_dimssorted_itemsr   s   &   @r   rp   ParallelismConfig._get_mesh  sf     OSNcNcdNc{$++k"::Nc	d D
OO2
 S,'(( es   A#c           
     D	   V P                   f/   \        \        P                  P	                  RR4      4      V n         V P
                  f/   \        \        P                  P	                  RR4      4      V n        V P                  f/   \        \        P                  P	                  RR4      4      V n        V P                  f/   \        \        P                  P	                  RR4      4      V n        V P                  f&   \        P                  P	                  RR4      V n        V P                  f/   \        \        P                  P	                  RR4      4      V n	        V P                  f&   \        P                  P	                  R	R
4      V n
        V P                  ^8  d   V P                  f   \        4       V n        V P                  ^8  d   V P                  f   \        4       V n        M}\        \        R7      p\!        V P                  WP                  ,          4      '       gB   \#        RV P                   RWP                  ,           R\%        V P                  4       24      hV P                  ^8  d   V P&                  f   \)        4       V n        V P                   ^8  d   \#        RV P                    24      hV P
                  ^8  d   \#        RV P
                   24      hV P                  ^8  d   \#        RV P                   24      hV P                  ^8  d   \#        RV P                   24      hR.pV P                  V9  d   \#        RV RV P                   24      hV P                  ^8  d   \#        RV P                   24      hR
.pV P                  V9  d   \#        RV RV P                   24      hV P                  ^8  d8   V P                  ^8  d'   \#        RV P                   RV P                   R24      hV P                  ^8  g   V P                  ^8  d.   V P                   ^8  d   V P
                  ^8X  d   \#        R4      hRV P                   RV P
                  RV P                  RV P                  RV P                  /V n        R # ) N$PARALLELISM_CONFIG_DP_REPLICATE_SIZE1 PARALLELISM_CONFIG_DP_SHARD_SIZEPARALLELISM_CONFIG_TP_SIZEPARALLELISM_CONFIG_CP_SIZEPARALLELISM_CONFIG_CP_BACKENDtorchPARALLELISM_CONFIG_SP_SIZEPARALLELISM_CONFIG_SP_BACKENDri   )r   zParallelismConfig's cp_backend=z
 requires z, but cp_handler was set to z.dp_replicate_size must be at least 1, but got z*dp_shard_size must be at least 1, but got z$tp_size must be at least 1, but got z$cp_size must be at least 1, but got zcp_backend must be one of z
, but got z$sp_size must be at least 1, but got zsp_backend must be one of z[Context Parallelism (CP) and Sequence Parallelism (SP) are mutually exclusive. Got cp_size=z and sp_size=z+. Please set either cp_size=1 or sp_size=1.aC  Tensor/Context parallelism (tp/cp_size > 1) cannot be used with pure data parallelism (dp_replicate_size > 1 and dp_shard_size == 1). Please set dp_shard_size > 1 and dp_replicate_size == 1 to compose FSDP + TP/CP for 2D parallel, or set dp_replicate_size == 1 and dp_shard_size > 1 to compose HSDP + TP/CP for 3D parallel.r.   r/   r7   r8   r9   )r   r   osenvirongetr   r   r   r   r   r   r   r   r   r
   dict
isinstancery   type
sp_handlerr   r   )r   cp_backends_config_mapvalid_cp_backendsvalid_sp_backendss   &   r   __post_init__ParallelismConfig.__post_init__  s   !!)%(8^`c)d%eD"%!$RZZ^^4VX[%\!]D<<rzz~~.JCPQDL<<rzz~~.JCPQDL??" jjnn-LgVDO<<rzz~~.JCPQDL??" jjnn-LkZDO<<!&";"=<<!&"<">)-4*& "$//3I//3Z[[$9$//9J*Ukl{l{U|T}  ~Z  [_  `d  `o  `o  [p  Zq  r  <<!&"A"C!!A%MdNdNdMefgg!I$J\J\I]^__<<!CDLL>RSS<<!CDLL>RSS$I??"339:K9LJW[WfWfVghii<<!CDLL>RSS(M??"339:K9LJW[WfWfVghii <<!q 0#||nM$,, H<<  LL1q 0d6L6Lq6PUYUgUgklUlo  D22**$,,$,,$,,
r    c                &   < V ^8  d   QhRS[ RS[/# )ra   r   size)rc   r   )rd   re   s   "r   rf   rg   ^  s     3 3S 3 3r    c                    WP                   P                  4       9   g#   Q R V P                   P                  4        24       hW P                   V&   \        W R2V4       R# )zParallelism must be one of _sizeN)r   keyssetattr)r   r   r   s   &&&r   	_set_sizeParallelismConfig._set_size^  sV    kk..00d4OPTP[P[P`P`PbOc2dd0#'K U+T2r    c                   < V ^8  d   QhRR/# )ra   acceleratorr    )rd   re   s   "r   rf   rg   c  s     + + +r    c                   \        4       pVP                  '       g   V P                  ^8X  d   R# V P                  ^8X  d   V P                  RVP                  4       V P
                  R8X  d   V P                  ^8  d   MAV P                  VP                  8w  d'   \        RV P                   RVP                   R24      hV P                  ^8  d]   VP                  '       gK   VP                  '       g9   VP                  \        P                  8X  g   \        RVP                   R24      hV P                  P                  4        F9  w  r4V^8X  g   K  \        W R	2R4      f   K!  VP                  R
V RV R24       K;  	  V'       dH   VP                   '       d4   \"        P$                  ! RRP'                  V4      ,           \(        4       R# R# R# )   Nr.   ri   zParallelismConfig total_size (z ) does not match num_processes (zJ). Please adjust dp_replicate_size/ dp_shard_size/tp_size/cp_size/sp_size.zParallelismConfig is only compatible DistributedType.FSDP (version 2) or DistributedType.Multi{Device} or DistributedType.DEEPSPEED, but got ._handlerzParallelismConfig.z_handler is set, but z0_size is set to 1. This handler will be ignored.z.ParallelismConfig has the following warnings:

)setmulti_devicer   r   num_processesr   r   ry   is_fsdp2distributed_typer	   	DEEPSPEEDr   r&   getattraddis_main_processwarningswarnjoinUserWarning)r   r   	_warningsr   r   s   &&   r   _validate_accelerator'ParallelismConfig._validate_acceleratorc  s   E	'''DOOq,@ ??aNN>;+D+DE ??k)dllQ.>__ 9 9900A B""-";";!< =9:  ??Q   '''++/H/HH b  cn  c  c  b@  @A  B  "&!2!2!4KqyWT](+CTJV(5J;-  XH  I "5 444MMADIIiDXX 59r    c                :  < V ^8  d   Qh/ S[ S[,          ;R&   S[ S[,          ;R&   S[ S[,          ;R&   S[ S[,          ;R&   S[R,          ;R&   S[ S[,          ;R&   S[R,          ;R	&   S[R
S[3,          ;R&   S[R
S[3,          ;R&   S[R
S[3,          ;R&   # )ra   r   r   r   r   r   r   r   ri   r   Nr   r   r   )r   r   r   r   r   r
   r   )rd   re   s   "r   rf   rg   !   s     J  }+K L C='M N c]!O P c]!Q R  'S T c]!U V $+W \ d556=] ^ d667>_ ` d;;<Ca r    )r   r   r   r   r"   r   r   r   r   r   r   r   )N)*__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r   r   r"   r   r+   propertyr4   r=   r@   rC   rG   r   rL   rO   r0   r1   r:   r;   r<   r]   ru   rz   rp   r   r   r   __annotate_func____static_attributes____classdictcell__)re   s   @r   r   r   !   s    !F (,#'M!G!G#'J!G'+J :>J:>J?CJK

   	 	   	 	   h h : : ; ; * * & &             9 9! !F   ) )J
X3 3
+ +E
  r    r   )r   r   dataclassesr   typingr   r   r   r   accelerate.utils.dataclassesr   r	   r
   r   accelerate.utils.versionsr   
accelerater   r   r   r    r   <module>r      sI    
  ! : :  7 & l l lr    