Ë
    g^(hÑl  ã                   óv  — d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZ d dlmc mZ d dlmZ  ej2                  «       serd dlmZ d dlmZ d dlmZmZm Z  d dl!m"Z" d	ejF                  d
eejH                     deejJ                     dedejF                  f
d„Z&	 	 dAddd
eejH                     deejJ                     dejF                  fd„Z' G d„ de(«      Z)ddddddddœdede	de	de	d
eejH                     deejJ                     de*dede+e,df   de*d e*de-e.ef   fd!„Z/dddddd"œd#e-e.ef   d
eejH                     deejJ                     de*de+e,df   de*de-e.ef   fd$„Z0ddd%œd#e-e.ef   de+e,df   de*de-e.ef   fd&„Z1 ejd                  «       	 	 dBd#e-e.ef   d'e-e.ef   d e*de*de-e.ef   f
d(„«       Z3 ejd                  «       	 dCd#e-e.ef   d)e*d*e*de-e.ef   fd+„«       Z4d#e-e.ef   d,e-e.ef   de*fd-„Z5 G d.„ d/e«      Z6	 dDd0e-e.ef   d1e-e.ef   d2e7e.   dejJ                  d
eejH                     ddfd3„Z8	 dDd1e-e.ef   d2e7e.   dejJ                  d
eejH                     ddf
d4„Z9	 	 	 dEd0e-e.ef   d1e-e.ef   dejJ                  d
eejH                     d5e*de*ddfd6„Z:	 dDd0e-e.ef   d1e-e.ef   dejJ                  d
eejH                     ddf
d7„Z;ee.e,f   Z<e+e<df   Z=e-e.e=f   Z>e-e.ef   Z?ee<ef   Z@d#e?d8e	e=egdf   ddfd9„ZAd#e?de+e?e>f   fd:„ZBd;e?d<e=d=eddfd>„ZCd#e?d?e>de?fd@„ZDy)Fé    N)ÚMappingÚMutableMapping)ÚAnyÚCallableÚcastÚ
NamedTupleÚOptionalÚTYPE_CHECKINGÚUnion)ÚAsyncCollectiveTensor)Údistributed_c10d)ÚShardedTensor)Údistribute_tensorÚDTensorÚ	Replicate)Ú%compute_local_shape_and_global_offsetÚobjÚpgÚdeviceÚcompanion_objÚreturnc                 ó   — | S ©N© ©r   r   r   r   s       úa/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/distributed/_state_dict_utils.pyÚ_identity_funcr      s	   € ð €Jó    Úsharded_tensorr   c                 ór  — |€t        j                  «       }t        j                  |«      }| j	                  «       }| j                  «       d   }| j                  «       j                  «       }t        j                  ||z  «      |z  |z  }|€t        j                  |«      n|}|r‚|d   j                  j                  «       }	|	j                  j                  |j                  k7  r|	j                  |«      }	||	j                  «       z
  }
|
dkD  r;t        j                   |	d|
g«      }	n"t#        j$                  || j&                  |¬«      }	t#        j(                  ||z  |	j&                  |¬«      }t        j*                  ||	|¬«       |j-                  dd|«      j/                  | j                  «       «      }|S )Nr   )Údtyper   )Úgroup)r   Ú_get_default_groupÚdistÚget_world_sizeÚlocal_shardsÚsizeÚnumelÚmathÚceilÚ_get_pg_default_deviceÚtensorÚflattenr   ÚtypeÚtoÚFÚpadÚtorchÚzerosr!   ÚemptyÚall_gather_into_tensorÚnarrowÚreshape)r   r   r   Ú
world_sizeÚshardsÚ
dim_0_sizeÚtensor_numelÚ
chunk_sizeÚ	pg_deviceÚlocal_tensorÚnum_paddingr,   s               r   Ú_all_gather_sharded_tensorr@      s†  € ð
 
€zÜ×0Ñ0Ó2ˆÜ×$Ñ$ RÓ(€JØ×(Ñ(Ó*€FØ×$Ñ$Ó& qÑ)€JØ!×&Ñ&Ó(×.Ñ.Ó0€LÜ—‘˜:¨
Ñ2Ó3°lÑBÀjÑP€Jà7=°~Ô×/Ñ/°Ô3È6ð ñ Ø˜a‘y×'Ñ'×/Ñ/Ó1ˆØ×Ñ×#Ñ# y§~¡~Ò5Ø'Ÿ?™?¨9Ó5ˆLØ  <×#5Ñ#5Ó#7Ñ7ˆØ˜Š?ÜŸ5™5 °°;Ð/?Ó@‰Lä—{‘{Ø˜n×2Ñ2¸9ô
ˆô �[‰[Ø�ZÑØ× Ñ Øô€Fô
 	×Ñ ¨¸BÕ?à�]‰]˜1˜a Ó.×6Ñ6°~×7JÑ7JÓ7LÓM€FØ€Mr   c                   ó   — e Zd Zy)ÚCompanionMismatchN)Ú__name__Ú
__module__Ú__qualname__r   r   r   rB   rB   E   s   „ Ør   rB   Fr   T©r   r   Úcpu_offloadr   Ú
ranks_onlyÚ
type_checkÚnon_blockingÚiter_objectÚsharded_tensor_funcÚdtensor_funcÚtensor_funcrG   rH   .rI   rJ   c                ó\  — t        j                  d«      }t        | t        «      r || |||«      }�n.t        | t        «      r || |||«      }�nt        | t         j
                  «      r || |||«      }�nêt        | t        t        t        t        t        j                  f«      s| €| }�nµt        | t        «      r×|�–t        |t        «      r3t        |j                  «       «      t        | j                  «       «      k7  rSt        |t        «      rdn5dt        |j                  «       «      ›dt        | j                  «       «      ›�}t        |«      ‚| j!                  «       D ��ci c]#  \  }}|t#        ||||||||�||   nd||	|
¬«      “Œ% }}}nÎt        | t$        t&        f«      r‰|�3t        |t$        t&        f«      rt)        |«      t)        | «      k7  rt        ‚t+        | «      D ��cg c]"  \  }}t#        ||||||||�||   nd||	|
¬«      ‘Œ$ }}}t        | t&        «      r;t'        |«      }n/|	st-        j.                  | «      }nt1        dt3        | «      › �«      ‚|rt5        j6                  |«      |v r’t        |t         j
                  «      rv|r|€|j9                  |«      }|�_t        |t        «      r:t        |t        «      sJ ‚|j:                  j=                  |j:                  |
¬«       n|j=                  ||
¬«       |}|S t        |t        «      ri nd}|S c c}}w c c}}w )	ae  Iterate through the state dict, applying the given functions to each tensor type.

    Args:
        iter_object (Any): the target state_dict.
        sharded_tensor_func (Callable): the function to apply to ShardedTensor
        dtensor_func (Callable): the function to apply to DTensor
        tensor_func (Callable): the function to apply to Tensor
        pg (Optional[dist.ProcessGroup]): process group passed to tensor functions
        device (Optional[torch.device]): device passed to tensor functions
        cpu_offload (bool): whether to offload the tensors to CPU memory. This option is ignored
            if a companion_obj is supplied.
        companion_obj (Any): A companion object to the state dict. If this object
            is supplied, we attempt to copy the tensor to the companion object.
        ranks_only (Tuple[int, ...]): if this tuple is empty, all ranks will
            have the same state_dicts. Otherwise only ranks that in ``ranks_only``
            have the same state_dicts. Other ranks will get empty state_dicts.
        type_check (bool): check if the instance data type is a supported type
            that can be saved by DCP.  The current supported data types are
            torch.Tensor, DTensor, int, float, str, list, dict, None.
        non_blocking (bool): whether to use non-blocking copy when copying to the companion object.
    ÚcpuNÚ zset(companion_obj.keys())=z set(iter_object.keys())=rF   zUnexpected value type )rJ   )r2   r   Ú
isinstancer   r   ÚTensorÚintÚfloatÚstrÚbytesÚioÚBytesIOÚdictÚsetÚkeysrB   ÚitemsÚ_iterate_state_dictÚlistÚtupleÚlenÚ	enumerateÚcopyÚdeepcopyÚ
ValueErrorr.   r$   Úget_rankr/   Ú_local_tensorÚcopy_)rK   rL   rM   rN   r   r   rG   r   rH   rI   rJ   Ú
cpu_deviceÚretÚmsgÚkeyÚvalueÚidxÚvs                     r   r^   r^   I   s  € ôH —‘˜eÓ$€JÜ�+œ}Ô-Ù! +¨r°6¸=ÓIŠÜ	�K¤Ô	)Ù˜;¨¨F°MÓBŠÜ	�K¤§¡Ô	.Ù˜+ r¨6°=ÓAŠä�;¤¤e¬S´%¼¿¹Ð DÔEØÐàŠÜ	�K¤Ô	&ØÐ$Ü˜=¬$Ô/Ü�=×%Ñ%Ó'Ó(¬C°×0@Ñ0@Ó0BÓ,CÒCô ˜m¬TÔ2ñ à2œ˜M×.Ñ.Ó0Ó1Ð3Ð3M´S¸×9IÑ9IÓ9KÓ5LÐ4NÐOð ô
 $ CÓ(Ð(ð  *×/Ñ/Ó1÷
ñ ��Uð Ô$ØØ#ØØØØØ'Ø4AÐ4M˜m¨CÒ0ÐSWØ%Ø%Ø)ôñ ð
ˆò 
ô  
�K¤$¬ Ô	/ØÐ$Ü˜=¬4´¨-Ô8Ü�=Ó!¤S¨Ó%5Ò5ä#Ð#ô  $ KÓ0÷
ñ ��Qô  ØØ#ØØØØØ'Ø4AÐ4M˜m¨CÒ0ÐSWØ%Ø%Ø)öð
ˆñ 
ô  �k¤5Ô)Ü˜“*‰CÙÜ�m‰m˜KÓ(‰äÐ1´$°{Ó2CÐ1DÐEÓFÐFáœŸ™ rÓ*¨jÑ8Ü�cœ5Ÿ<™<Ô(Ù˜}Ð4Ø—f‘f˜ZÓ(�àÐ(Ü˜m¬WÔ5Ü% c¬7Ô3Ð3Ð3Ø!×/Ñ/×5Ñ5Ø×)Ñ)¸ð 6õ ð "×'Ñ'¨¸,Ð'ÔGØ#�ð €Jô ˜s¤DÔ)‰b¨tˆà€Jùó
ùó.
s   Å'(L"Ç,'L(©r   r   rG   rH   rI   Ú
state_dictc                ó>   — d„ }d„ }t        | ||t        |||||¬«	      S )a)  
    Given a state_dict, this API gathers all the ShardedTensors or DTensors in
    the state_dict.


    Args:
        state_dict (Dict[str, Any]): the target sharded state_dict.
        pg (Optional[dist.ProcessGroup]): the process group that is used to
            gather ShardedTensor. Note that gathering a DTensor will use
            the DeviceMesh. So this argument will be ignored when gathering a
            DTensor.
        device: (Optional[torch.device]): the device that is used to
            perform allgather for ShardedTensor. Note that gathering a DTensor
            will use the DeviceMesh. So this argument will be ignored when
            gathering a DTensor.
        cpu_offload (bool): whether to offload the tensors to CPU memory. The
            default value is False.
        ranks_only: (Tuple[int, ...]): if this tuple is empty, all ranks will
            have the same state_dicts. Otherwise only ranks that in ``ranks_only``
            have the same state_dicts. Other ranks will get empty state_dicts.
        type_check: (bool): check if the instance data type is a supported type
            that can be saved by DCP.  The current supported data types are
            torch.Tensor, DTensor, int, float, str, list, dict, None.

    Returns:
        The gathered state dictionary.
    c                 ó  — t        j                  d«      }t        | ||«      }| j                  «       r'| j                  «       d   j                  j                  n|}|j                  |k7  r|j                  |«      } | S |} | S )NrP   r   )r2   r   r@   r&   r,   r/   )rm   r   r   r   ri   Úoutput_tensorÚlocal_shard_devices          r   rL   z/_gather_state_dict.<locals>.sharded_tensor_funcì   sŠ   € ô —\‘\ %Ó(ˆ
Ü2°5¸"¸fÓEˆð ×!Ñ!Ô#ð ×ÑÓ  Ñ#×*Ñ*×1Ò1àð 	ð
 ×ÑÐ#5Ò5Ø!×$Ñ$Ð%7Ó8ˆEð ˆð "ˆEØˆr   c                 ó|  — | j                   | j                  j                  k7  r%| j                  | j                  j                  «      } | j                  D �cg c]  }t        «       ‘Œ }}| j                  | j                  |¬«      } | j                  «       } t        | t        «      r| j                  «       } | S c c}w )N)Údevice_meshÚ
placements)r   rw   Údevice_typer/   rx   r   ÚredistributeÚto_localrR   r   Úwait)rm   r   r   r   Ú_rx   s         r   rM   z(_gather_state_dict.<locals>.dtensor_funcý   s    € Ø�<‰<˜5×,Ñ,×8Ñ8Ò8Ø—H‘H˜U×.Ñ.×:Ñ:Ó;ˆEð ,1×+;Ñ+;Ö< a”i•kÐ<ˆ
Ð<Ø×"Ñ"Ø×)Ñ)Ø!ð #ó 
ˆð —‘Ó ˆÜ�eÔ2Ô3Ø—J‘J“LˆEØˆùò =s   ÁB9rp   ©r^   r   )rq   r   r   rG   rH   rI   rL   rM   s           r   Ú_gather_state_dictr   Ç   s7   € òJò"ô* ØØØÜØØØØØô
ð 
r   )rH   rI   c                óF   — t        | t        t        t        ddd||¬«	      }|S )a“  
    Given a state_dict, this API offload all the tensors to CPU memory.

    Args:
        state_dict (Dict[str, Any]): the target state_dict.
        pg (Optional[dist.ProcessGroup]): the process group that is used to
            gather ShardedTensor. Note that gathering a DTensor will use
            the DeviceMesh. So this argument will be ignored when gathering a
            DTensor.
        ranks_only: (Tuple[int, ...]): if this tuple is empty, all ranks will
            have the same state_dicts. Otherwise only ranks that in ``ranks_only``
            have the same state_dicts. Other ranks will get empty state_dicts.
        type_check: (bool): check if the instance data type is a supported type
            that can be saved by DCP.  The current supported data types are
            torch.Tensor, DTensor, int, float, str, list, dict, None.

    Returns:
        The gathered state dictionary.
    NTrp   r~   )rq   rH   rI   rj   s       r   Ú_offload_state_dict_to_cpur�     s0   € ô4 ØÜÜÜØØØØØô
€Cð €Jr   Úcopy_state_dictc                 óF   — t        | t        t        t        dddd|||¬«      S )a§  
    Copies all tensors in a given state dict into a different state_dict with the
    same structure. Additionally, a copied state dict with the same value references
    is returned. Editing the keys on this state dict will not affect the
    passed in copy_state_dict (but the value references are the same).

    .. warning::
        It is expected by this function that state_dict and copy_state_dict share
        the same structure and data types.

    .. warning::
        The current supported data types are
            torch.Tensor, DTensor, int, float, str, list, dict, None.

    Args:
        state_dict (Dict[str, Any]): the target state_dict.
        copy_state_dict (Dict[str, Any]):
            The state dict we are copying into. This state_dict must have exactly
             the same structure as the source `state_dict`.
        non_blocking: (bool): Whether copy ops should be performed asynchronously
        type_check (bool): check if the instance data type is a supported type
            that can be saved by DCP. The current supported data types are
            torch.Tensor, DTensor, int, float, str, list, dict, None.

    Returns:
        State Dict copy
    NFr   )r   r   rG   rH   r   rI   rJ   r~   )rq   r‚   rJ   rI   s       r   Ú_copy_state_dictr„   G  s3   € ôF ØÜÜÜØØØØØ%ØØ!ôð r   Ú
pin_memoryÚshare_memoryc                 ó~  ‡‡‡— dt         j                  dt        t        j                     dt        t         j
                     dt        dt         j                  f
ˆˆfd„Šdt        dt        t        j                     dt        t         j
                     dt        dt        f
ˆfd„}t        | t        |‰ddd	d
d	¬«	      }|S )aó  
    Given a state_dict, create another state_dict with the same structure and elements.
    However, all tensors in the returned state_dict are new tensors on CPU. These
    tensors can be placed on pin_memory or share_memory based on the provided arguments.

    .. warning::
        Setting both `pin_memory` and `share_memory` to True significantly increases the
        latency of this method because of the nuances which require us to register memory
        as pinned directly as opposed to relying on the pin_memory cache allocator. This
        option should only be used for long lived tensors which are required to be shared.
        This is not the case as long as at least one of `pin_memory` or `share_memory` is
         set to False.

    r   r   r   r}   r   c                 ó*  •— t        | j                  «       «      dk(  r!t        j                  d| j                  ¬«      S ‰rØt        j
                  t        | j                  «       «      d| j                  iŽ}|j                  «       }‰rŽd„ }t        j                  |||«       t        t        j                  j                  «       j                  |j                  «       |j                  «       |j!                  «       z  d«      «      }|dk(  s
J d|› �«       ‚|S ‰rDt        j
                  t        | j                  «       «      d| j                  iŽj#                  «       S t        j
                  t        | j                  «       «      d| j                  iŽS )Nr   )r!   r!   c                 óª   — t        t        j                  j                  «       j	                  | j                  «       «      «      }|dk(  s
J d|› �«       ‚y )Nr   z0Unpinning shared memory failed with error-code: )rT   r2   ÚcudaÚcudartÚcudaHostUnregisterÚdata_ptr)ÚtÚsuccs     r   Úunpin_memoryzA_create_cpu_state_dict.<locals>.tensor_func.<locals>.unpin_memoryš  sG   € ÜœuŸz™z×0Ñ0Ó2×EÑEÀaÇjÁjÃlÓSÓT�DØ 1š9ð ØJÈ4È&ÐQó™9r   é   z.Pinning shared memory failed with error-code: )ra   r'   r2   r,   r!   r4   r`   Úshare_memory_ÚweakrefÚfinalizerT   rŠ   r‹   ÚcudaHostRegisterr�   r(   Úelement_sizer…   )	r   r   r   r}   rŽ   r�   r�   r…   r†   s	          €€r   rN   z+_create_cpu_state_dict.<locals>.tensor_funcŒ  s7  ø€ ô ˆs�x‰x‹z‹?˜aÒÜ—<‘< ¨¯©Ô3Ð3áÜ—‘œU 3§8¡8£:Ó.Ð@°c·i±iÑ@ˆAØ—‘Ó!ˆAÙòô × Ñ   L°!Ô4ÜÜ—J‘J×%Ñ%Ó'×8Ñ8ØŸ
™
›ØŸ™›	 A§N¡NÓ$4Ñ4Øóó�ð ˜q’yð ØDÀTÀFÐKó�yð ˆHÙÜ—;‘;¤ c§h¡h£jÓ 1ÐC¸¿¹ÑC×NÑNÓPÐPä—;‘;¤ c§h¡h£jÓ 1ÐC¸¿¹ÑCÐCr   c                 ó(  •— t        | j                  «       «      dk(  r| S | j                  t        j                  d«      k7  r!t	        t
        | j                  d¬«      «      }nt        j                  | «      } ‰|j                  ||d «      |_	        |S )Nr   rP   )r   )
ra   r'   r   r2   r   r   r/   rc   rd   rg   )r   r   r   r}   rj   rN   s        €r   rM   z,_create_cpu_state_dict.<locals>.dtensor_func±  st   ø€ ô ˆs�x‰x‹z‹?˜aÒØˆJà�:‰:œŸ™ eÓ,Ò,Ü”w §¡¨e Ó 4Ó5‰Cä—-‘- Ó$ˆCÙ'¨×(9Ñ(9¸2¸vÀtÓLˆÔØˆ
r   NFr   rp   )
r2   rS   r	   r$   ÚProcessGroupr   r   r   r^   r   )rq   r…   r†   rM   rj   rN   s    ``  @r   Ú_create_cpu_state_dictr™   y  sÎ   ú€ ð&#DÜ�\‰\ð#Dä”T×&Ñ&Ñ'ð#Dô œŸ™Ñ&ð#Dô ð	#Dô
 
�‰ö#DðJÜðä”T×&Ñ&Ñ'ðô œŸ™Ñ&ðô ð	ô
 
õô  ØÜØØØØØØØô
€Cð €Jr   Úcompared_state_dictc                 ó  — dt         j                  dt        t        j                     dt        t         j
                     dt        dt         j                  f
d„}	 t        | t        t        |dddd	|d¬
«
       y# t        $ r Y yw xY w)a6  
    Given two state_dicts, check if the structures are the same. And
    if a [key, tensor] pair exist in one state_dict there must be
    the a corresponding pait, [key, other_tensor], in the other state_dict,
    where tensor and other_tensor have the same size and dtype.

    Return the check result.
    r   r   r   r   r   c                 ó†   — |j                   | j                   k7  s!|j                  «       | j                  «       k7  rt        ‚| S r   )r!   r'   rB   r   s       r   rN   z1_check_state_dict_similarity.<locals>.tensor_funcÜ  s7   € ð ×Ñ #§)¡)Ò+¨}×/AÑ/AÓ/CÀsÇxÁxÃzÒ/QÜ#Ð#Øˆ
r   NFr   )r   r   rG   rH   r   rI   T)
r2   rS   r	   r$   r˜   r   r   r^   r   rB   )rq   rš   rN   s      r   Ú_check_state_dict_similarityr�   Ï  s•   € ðÜ�\‰\ðä”T×&Ñ&Ñ'ðô œŸ™Ñ&ðô ð	ô
 
�‰óðÜØÜÜØØØØØØ-Øõ	
ð øô ò Ùðús   ÁA8 Á8	BÂBc                   óJ   — e Zd ZU ej                  ed<   ej                  ed<   y)Ú_TensorInfor'   r!   N)rC   rD   rE   r2   ÚSizeÚ__annotations__r!   r   r   r   rŸ   rŸ   ù  s   … Ø
�*‰*ÓØ�;‰;Ôr   rŸ   Úfull_state_dictÚlocal_state_dictr\   c                 óŽ  — g }|D ]Î  }t        j                  «       dk(  rA| |   }t        |t        j                  «      sJ ‚|j                  «       j                  |«      }n1| |   }	t        j                  |	j                  ||	j                  ¬«      }|j                  |«       |j                  |d «      }
|
€Œ²t        |
t        «      r|
|f||<   ŒÊ|||<   ŒÐ |€t         j                  j                  «       }t        |«      dkD  rt        j                   ||dd«       nt        j"                  |d   d|¬«       t%        ||||«       y )Nr   )r'   r   r!   r‘   iô  ©Úsrcr"   )r$   rf   rR   r2   rS   Údetachr/   r4   r'   r!   ÚappendÚgetr   r   r#   ra   Ú_broadcast_coalescedÚ	broadcastÚ_distribute_tensors)r¢   r£   r\   r   r   Útensorsrl   Ú
full_stateÚfull_tensorÚtensor_infoÚlocal_states              r   Ú_broadcast_tensorsr²   þ  s6  € ð €GØò 0ˆÜ�=‰=‹?˜aÒØ(¨Ñ-ˆJÜ˜j¬%¯,©,Ô7Ð7Ð7Ø$×+Ñ+Ó-×0Ñ0°Ó8‰Kà)¨#Ñ.ˆKÜŸ+™+Ø ×%Ñ%ØØ!×'Ñ'ôˆKð
 	�‰�{Ô#Ø&×*Ñ*¨3°Ó5ˆØÐØÜ˜¤WÔ-Ø%0°+Ð$>Ð˜SÒ!à$/Ð˜SÒ!ð'0ð* 
€zÜ×"Ñ"×5Ñ5Ó7ˆä
ˆ7ƒ|�aÒÜ×!Ñ! " g¨s°AÕ6ä�‰�w˜q‘z q°Õ3äÐ(¨$°¸Õ;r   c           
      ó¶  — |€t         j                  j                  «       }|D �]-  }| j                  |d «      }|�t	        j
                  |«      rŒ.|d   }|d   }t        |j                  |j                  |j                  «      \  }}	t        ||	«      D �
�cg c]  \  }
}t        |||
z   «      ‘Œ }}
}|j                  rh||   j                  «       j                  «       }t        j                   ||j                  |j                  |j                  |j#                  «       ¬«      }n$|}|j%                  «       j'                  ||   «       || |<   �Œ0 y c c}}
w )Nr   r‘   )ÚshapeÚstride)r$   r   r#   r©   r2   Ú	is_tensorr   r´   rw   rx   ÚzipÚsliceÚis_metar§   Úcloner   Ú
from_localrµ   r{   rh   )r£   r\   r   r   rl   Ú_local_stater±   r¯   r´   ÚoffsetÚ	cur_shapeÚ
cur_offsetÚslicesr>   rj   s                  r   r¬   r¬   &  sT  € ð 
€zÜ×"Ñ"×5Ñ5Ó7ˆØó $ˆØ'×+Ñ+¨C°Ó6ˆØÐ¤5§?¡?°<Ô#@Øà" 1‘oˆØ" 1‘oˆä=Ø×Ñ˜{×6Ñ6¸×8NÑ8Nó
‰ˆˆvô
 *-¨U°FÓ);÷
á%�	˜:ô �*˜j¨9Ñ4Õ5ð
ˆñ 
ð ×Òà& vÑ.×5Ñ5Ó7×=Ñ=Ó?ˆLô ×$Ñ$ØØ×'Ñ'Ø×&Ñ&Ø!×'Ñ'Ø"×)Ñ)Ó+ô‰Cð ˆCà�L‰L‹N× Ñ  ¨VÑ!4Ô5Ø #Ð˜Óñ?$ùó
s   ÂEÚstrictc                 óè  — i }t        j                  «       dk(  r�| j                  «       D ]n  \  }}t        j                  |«      s|||<   Œ!|j                  «       dk(  r|j                  «       ||<   ŒHt        |j                  «       |j                  «      ||<   Œp |g}	t        j                  |	d|¬«       |	d   }g }
t        |j                  «       «      }t        «       }|j                  «       D ]­  \  }}|j                  |«       t        |t        «      s
||v r|||<   Œ1t        j                  «       dk(  r| |   ||<   |
j                  |«       t!        |
«      dk\  sŒpt#        |||
||«       |r|
D ]  }||   j                  «       ||<   Œ |
j%                  «        Œ¯ |r||z
  x}r|D ]  }|j'                  |«       Œ |
r0t#        |||
||«       |r|
D ]  }||   j                  «       ||<   Œ y y y )Nr   r¥   r‘   )r$   rf   r]   r2   r¶   ÚdimrP   rŸ   r'   r!   Úbroadcast_object_listr[   r\   ÚaddrR   r¨   ra   r²   ÚclearÚpop)r¢   r£   r   r   rÁ   rG   rj   rl   rm   Úbroadcast_listr\   Úlocal_state_dict_keysÚglobal_keysÚmissing_keyss                 r   Ú_broadcast_state_dictrÌ   P  s  € ð €CÜ‡}�}ƒ˜!ÒØ)×/Ñ/Ó1ò 	B‰JˆC�Ü—?‘? 5Ô)Ø ��C’Ø—‘“ Ò!Ø Ÿ9™9›;��C’ä& u§z¡z£|°U·[±[ÓA��C’ð	Bð �U€NÜ×Ñ˜~°1¸BÕ?Ø
˜Ñ
€Cà€DÜÐ 0× 5Ñ 5Ó 7Ó8ÐÜ“%€KØ—i‘i“kò ‰
ˆˆUØ�‰˜ÔÜ˜%¤Ô-ØÐ&Ñ&Ø(-Ð  Ñ%Øä�=‰=‹?˜aÒØ& sÑ+ˆC�‰Hà�‰�CÔäˆt‹9˜‹>Ü˜sÐ$4°d¸FÀBÔGÙØò H�CØ,<¸SÑ,A×,EÑ,EÓ,GÐ$ SÒ)ðHà�J‰J�Lð#ñ& Ø1°KÑ?Ð@ˆ<Ð@Ø#ò *�Ø ×$Ñ$ SÕ)ð*ñ Ü˜3Ð 0°$¸ÀÔCÙØò D�Ø(8¸Ñ(=×(AÑ(AÓ(CÐ  Ò%ñDð ð r   c                 ó  — | j                  «       D ]ñ  \  }}|| vrŒt        j                  |«      s|||<   Œ&|j                  «       dk(  r|j	                  «       ||<   ŒMt        |t        j                  «      sJ ‚|j                  |d «      }|€Œ~t        |t        «      rBt        |j                  «       j                  |«      |j                  |j                  «      ||<   ŒÐ|j                  «       j                  |«      ||<   Œó y )Nr   )r]   r2   r¶   rÃ   rP   rR   rS   r©   r   r   r§   r/   rw   rx   )r¢   r£   r   r   rl   rm   r±   s          r   Ú_distribute_state_dictrÎ   ‹  sð   € ð &×+Ñ+Ó-ò B‰
ˆˆUØ�oÑ%ØÜ�‰˜uÔ%Ø$)Ð˜SÒ!Ø�Y‰Y‹[˜AÒØ$)§I¡I£KÐ˜SÒ!ä˜e¤U§\¡\Ô2Ð2Ð2Ø*×.Ñ.¨s°DÓ9ˆKØÐ"ØÜ˜K¬Ô1Ü(9Ø—L‘L“N×%Ñ% fÓ-Ø×+Ñ+Ø×*Ñ*ó)Ð  Ò%ð ).¯©«×(9Ñ(9¸&Ó(AÐ  Ò%ñ'Br   Úvisitorc                 óˆ   ‡‡— dt         dt        ddfˆˆfd„Š| j                  «       D ]  \  }} ‰t        |«      f|«       Œ y)zÓ
    Invoke ``visitor`` for each value recursively in ``state_dict``.
    Mapping, list, and tuple will be flattened and other value types are treated
    as the terminal values and will invoke ``visitor``.
    Úpathrm   r   Nc                 ó  •— t        |t        «      r/|j                  «       D ]  \  }} ‰| t        |«      fz   |«       Œ y t        |t        t
        f«      r!t        |«      D ]  \  }} ‰| |fz   |«       Œ y  ‰| |«       y r   )rR   r   r]   rV   r_   r`   rb   )rÑ   rm   Úkro   ÚiÚ_traverse_objrÏ   s        €€r   rÕ   z+_traverse_state_dict.<locals>._traverse_obj¾  s}   ø€ Ü�eœWÔ%ØŸ™›ò 3‘��1Ù˜d¤c¨!£f YÑ.°Õ2ñ3ä˜¤¤e˜}Ô-Ü! %Ó(ò .‘��1Ù˜d a T™k¨1Õ-ñ.ñ �D˜%Õ r   )ÚOBJ_PATHr   r]   rV   )rq   rÏ   rl   rm   rÕ   s    `  @r   Ú_traverse_state_dictr×   ´  sK   ù€ ð!œHð !¬Sð !°Tö !ð !×&Ñ&Ó(ò *‰
ˆˆUÙ”s˜3“x�k 5Õ)ñ*r   c                 óX   ‡‡— i Ši Šdt         dt        ddfˆˆfd„}t        | |«       ‰‰fS )a‹  
    Flatten ``state_dict`` made of nested dicts and lists into a top level dictionary.

    Use ``unflatten_state_dict`` to revert this process.
    Returns:
        A tuple with the flatten state_dict and a mapping from original to new state_dict.
    N.B. The new keys are derived from the object paths, joined by dot.
        For example: ``{ 'a': {'b':...}}`` results in the key `a.b`.
    rÑ   rm   r   Nc                 ó|   •— dj                  t        t        | «      «      }|‰v rt        d|› �«      ‚|‰|<   | ‰|<   y )Nú.zduplicated flatten key )ÚjoinÚmaprV   re   )rÑ   rm   Únew_fqnÚ	flattenedÚmappingss      €€r   Ú	flat_copyz&_flatten_state_dict.<locals>.flat_copyÛ  sF   ø€ Ø—(‘(œ3œs D›>Ó*ˆØ�iÑÜÐ6°w°iÐ@ÓAÐAØ"ˆ	�'ÑØ ˆ�Òr   )rÖ   r   r×   )rq   rà   rÞ   rß   s     @@r   Ú_flatten_state_dictrá   Ì  s@   ù€ ð "$€IØ "€Hð!œð !¬ð !°ö !ô ˜ YÔ/Ø�hÐÐr   Ú	root_dictrÑ   rm   c                 óÖ  — t        t        | «      }dt        t           dt        ddfd„}t        dt        |«      «      D ]n  }||dz
     }||   }t        |«      t        k(  ri ng }t        |t        «      r!t        t        |j                  ||«      «      }ŒW |||«       ||   €|||<   ||   }Œp |d   }t        |«      t        k(  r |t        t        t           |«      |«       |||<   y)z>Set ``value`` in ``root_dict`` along the ``path`` object path.Úlstrn   r   Nc                 ób   — t        | «      |k  r!| j                  d «       t        | «      |k  rŒ y y r   )ra   r¨   )rä   rn   s     r   Úextend_listz!_set_element.<locals>.extend_listê  s&   € Ü�#‹h˜#ŠoØ�J‰J�tÔô �#‹h˜#�or   r‘   éÿÿÿÿ)r   ÚCONTAINER_TYPEr_   r   rT   Úrangera   r.   rV   rR   r   Ú
setdefault)	râ   rÑ   rm   Úcur_containerræ   rÔ   Úprev_keyrl   Údef_vals	            r   Ú_set_elementrî   æ  sø   € äœ¨Ó3€Mðœœc™ð ¬ð °ó ô �1”c˜$“iÓ ò 4ˆØ˜˜A™‘;ˆØ�1‰gˆÜ:>¸s»)ÄsÒ:J±BÐPRˆä�m¤WÔ-Ü Ü × 8Ñ 8¸À7Ó Kó‰Mñ ˜ xÔ0Ø˜XÑ&Ð.Ø*1�˜hÑ'Ø)¨(Ñ3‰Mð4ð ˆr‰(€CÜˆCƒy”CÒÙ”Dœœc™ MÓ2°CÔ8à€M�#Òr   Úmappingc                 óZ   — i }| j                  «       D ]  \  }}t        |||   |«       Œ |S )zaRestore the original nested state_dict according to ``mapping`` and the flattened ``state_dict``.)r]   rî   )rq   rï   Únestedrl   rm   s        r   Ú_unflatten_state_dictrò     s:   € ð !€FØ ×&Ñ&Ó(ò 2‰
ˆˆUÜ�V˜W S™\¨5Õ1ð2à€Mr   )NN)FT)FFr   )NFF)Erc   rX   r)   r“   Úcollections.abcr   r   Útypingr   r   r   r   r	   r
   r   r2   Útorch.distributedÚdistributedr$   Útorch.nn.functionalÚnnÚ
functionalr0   Ú)torch.distributed._functional_collectivesr   Úis_availabler   Ú'torch.distributed._shard.sharded_tensorr   Útorch.distributed.tensorr   r   r   Útorch.distributed.tensor._utilsr   rS   r˜   r   r   r@   Ú	ExceptionrB   Úboolr`   rT   rZ   rV   r^   r   r�   Úno_gradr„   r™   r�   rŸ   r_   r²   r¬   rÌ   rÎ   Ú	PATH_ITEMrÖ   ÚFLATTEN_MAPPINGÚSTATE_DICT_TYPErè   r×   rá   rî   rò   r   r   r   ú<module>r     sî  ðã Û 	Û Û ß 3ß R× RÑ Rã Ý  ß Ð Ý Kð €4×ÑÔ™-Ý2ÝEßNÑNÝUðØ	�‰ðà�×"Ñ"Ñ#ðð �U—\‘\Ñ"ðð ð	ð
 ‡\�\óð '+Ø%)ñ#Ø#ð#à�×"Ñ"Ñ#ð#ð �U—\‘\Ñ"ð#ð ‡\�\ó	#ôL	˜	ô 	ð '+Ø%)ØØØ"$ØØò{Øð{à!ð{ð ð{ð ð	{ð 	�×"Ñ"Ñ#ð{ð �U—\‘\Ñ"ð{ð ð{ð ð{ð �c˜3�h‘ð{ð ð{ð ð{ð 
ˆ#ˆsˆ(�^ó{ðB '+Ø%)ØØ"$ØòUØ�S˜#�X‘ðUð 	�×"Ñ"Ñ#ðUð �U—\‘\Ñ"ð	Uð
 ðUð �c˜3�h‘ðUð ðUð 
ˆ#ˆsˆ(�^óUðv #%Øò	%Ø�S˜#�X‘ð%ð �c˜3�h‘ð%ð ð	%ð
 
ˆ#ˆsˆ(�^ó%ðP €‡�ƒð Øñ	.Ø�S˜#�X‘ð.à˜#˜s˜(‘^ð.ð ð.ð ð	.ð
 
ˆ#ˆsˆ(�^ò.ó ð.ðb €‡�ƒàOTñRØ�S˜#�X‘ðRØ,0ðRØHLðRà	ˆ#ˆsˆ(�^òRó ðRðj'Ø�S˜#�X‘ð'à˜c 3˜h™ð'ð 
ó'ôT�*ô ð '+ñ%<Ø˜#˜s˜(‘^ð%<à˜3 ˜8‘nð%<ð ˆs‰)ð%<ð �L‰Lð	%<ð
 	�×"Ñ"Ñ#ð%<ð 
ó%<ðX '+ñ	'$Ø˜3 ˜8‘nð'$à
ˆs‰)ð'$ð �L‰Lð'$ð 	�×"Ñ"Ñ#ð	'$ð
 
ó'$ð\ '+ØØñ8DØ˜#˜s˜(‘^ð8Dà˜3 ˜8‘nð8Dð �L‰Lð8Dð 	�×"Ñ"Ñ#ð	8Dð
 ð8Dð ð8Dð 
ó8Dð~ '+ñ	BØ˜#˜s˜(‘^ðBà˜3 ˜8‘nðBð �L‰LðBð 	�×"Ñ"Ñ#ð	Bð
 
óBðD �#�s�(‰O€	Ø�˜C�Ñ €Ø�s˜H�}Ñ%€Ø�s˜C�x‘.€Ø 	¨3 Ñ/€ð*Øð*à�x �o tÐ+Ñ,ð*ð 
ó*ð0Øðà
ˆ?˜OÐ+Ñ,óð4˜Oð °8ð ÀCð ÈDó ð<ØðØ*9ðàôr   