
    [Th~4                     2   S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	J
r
Jr  S SKrS SKJr  S SKJr  S SKJr  / rS\S\S	\\\S
4   \\S
4   4   4S jrS\	\R.                     S\S\S	\\\4   4S jrS\\S
4   S\\S
4   S	\\\S
4   \\\4   4   4S jr\" S\\\5      r\" S\R:                  \5      r\
S\S\R>                  S\ S	\\   4S j5       r!\
S\S\R>                  S\ S	\\   4S j5       r!S r!S=S\S\S\ S	S4S jjr"S\R:                  S\RF                  S	S4S jr$S\R:                  4S  jr%\" S!5      r&\" S"\\\\'\\\5      r(\
S#\\R:                  /\&4   S$\R:                  S	\&4S% j5       r)\
S#\\R:                  /\4   S$\(S	\(4S& j5       r)S' r)S\\S
4   S\	\\\4      S\R>                  S\ S	\\\S
4   \\\\4   S
4   4   4
S( jr* S>S)\RV                  S*\\R:                     S+\	S,   4S- jjr, S=S.\RZ                  S)\RV                  S/\.S0\.S1\\   S2\ S	S4S3 jjr/S)\RV                  S4\\R:                     S/\.S0\.S	S4
S5 jr0S6\\\4   S7\S8\S	S4S9 jr1S\R:                  S	\ 4S: jr2S;\\RZ                     S	\\RZ                     4S< jr3g)?    N)OrderedDict)	Container)AnyCallableOptionaloverloadTypeVar)nn)PackedSequenceargskwargsreturn.c                      / n[        U 5      nUR                  5        H'  u  pEUR                  U5        UR                  U5        M)     [        U5      [        U5      4$ )aJ  
Turn argument list into separate key list and value list (unpack_kwargs does the opposite).

Inspiration: https://github.com/facebookresearch/fairscale/blob/eeb6684/fairscale/internal/containers.py#L70
Usage::

    kwarg_keys, flat_args = pack_kwargs(1, 2, a=3, b=4)
    assert kwarg_keys == ("a", "b")
    assert flat_args == (1, 2, 3, 4)
    args, kwargs = unpack_kwargs(kwarg_keys, flat_args)
    assert args == (1, 2)
    assert kwargs == {"a": 3, "b": 4}
Returns:
    Tuple[Tuple[Any, ...], Tuple[str, ...]]: The first tuple element gives
    gives both positional args and kwarg values, where the positional args
    proceed kwarg values and kwarg values are ordered consistently with the
    kwarg keys. The second tuple element gives the kwarg keys.
    The second tuple element's length is at most the first tuple element's length.
)listitemsappendtuple)r   r   
kwarg_keys	flat_argskvs         O/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributed/utils.py_pack_kwargsr      sW    ( J:I!  U:...    dtypec                    ^  T c  X4$ S[         R                  S[         R                  4U 4S jjn[        X15      [        X25      4$ )z
Cast floating point tensors in ``args`` and ``kwargs`` to ``input_dtype``.

This respects the existing ``requires_grad`` on the tensors.
xr   c                    > [         R                  " U 5      (       a  U R                  T:X  a  U $ U R                  T5      $ N)torchis_floating_pointr   to)r   r   s    r   cast_fn%_cast_forward_inputs.<locals>.cast_fn;   s2    &&q))QWW-=HttE{r   )r    Tensor_apply_to_tensors)r   r   r   r#   s   `   r   _cast_forward_inputsr'   .   sE     }|5<< ELL 
 g,.?.PQQr   r   r   c           	          [        U5      [        U 5      ::  d   S[        U5       S[        U 5       35       e[        U5      S:X  a  U 0 4$ U S[        U5      *  n[        [        X[        U5      * S 5      5      nX#4$ )zSee _pack_kwargs.ztoo many keys z vs. r   N)lendictzip)r   r   r   r   s       r   _unpack_kwargsr,   C   s     z?c)n, 
Z)s9~.>?, :!"}'J'(D#jS_,<,>"?@AF<r   STinputstarget_device!use_side_stream_for_tensor_copiesc                     g r    r/   r0   r1   s      r   _recursive_tor5   U   s     r   c                     g r   r3   r4   s      r   r5   r5   [   s     r   c                 @   ^^^ UUU4S jm T" U 5      nSmU$ ! Smf = f)z-Recursively moves input to the target_device.c                 "  > [        U [        R                  [        45      (       Ga}  [        U [        5      (       a  U R                  R
                  OU R
                  nUT
:X  a  U 4$ T(       d  U R                  T
5      4$ [        [        UR                  S 5      nUR                  S:X  d  Uc  U R                  T
5      4$ SSK	J
n  U" T
5      nUR                  U5         U R                  T
5      nS S S 5        UR                  T
R                  5         UR                  5       nUR                  U5        [        U [        5      (       a  WR                  R                  U5        O2[        W[        R                  5      (       d   eUR                  U5        S S S 5        U4$ SSKJn  U" U 5      (       a/  [%        ['        TU 5      6  Vs/ s H  n[        U 5      " U6 PM     sn$ [        U [(        5      (       a*  [+        U 5      S:  a  [-        [%        ['        TU 5      6 5      $ [        U [,        5      (       a;  [+        U 5      S:  a,  [%        ['        TU 5      6  V	s/ s H  n	[-        U	5      PM     sn	$ [        U [.        5      (       aO  [+        U 5      S:  a@  [%        ['        TU R1                  5       5      6  V	s/ s H  n	[        U 5      " U	5      PM     sn	$ U /$ ! , (       d  f       GN= f! , (       d  f       W4$ = fs  snf s  sn	f s  sn	f )Ncpur   )_get_stream_is_namedtuple)
isinstancer    r%   r   datadevicer"   getattrtypetorch.nn.parallel._functionsr:   streamindexcurrent_streamwait_streamrecord_stream torch.nn.parallel.scatter_gatherr<   r+   mapr   r)   r   r*   r   )objr?   
device_modr:   rC   outputrE   r<   r   ir0   to_mapr1   s             r   rN   _recursive_to.<locals>.to_mapd   sh   cELL.9::(23(G(GSXX__SZZF&v4}-// %UFKK>
;;%':+=FF=133D %]3&&v. VVM2F /  &&}':':;%/%>%>%@N"..v6 "#~6611.A)&%,,????,,^< < y C#14c&#6F1GH1GDIt$1GHHc5!!c#hlS-.//c4  SX\%(#fc*:%;<%;DG%;<<c4  SX\*-s6399;/G*HI*HQDIaL*HIIu5 /. <; y 
 I =Is+    KBK/LL<L
K,/
K?Nr3   )r/   r0   r1   resrN   s    `` @r   r5   r5   a   s*    ,^VnJ s    condsraise_assertion_errorc                 x    U (       d3  [        U5        [        R                  " 5         U(       a  [        U5      egg)zwAlternate to ``assert`` when in the backward context to print the error message ``s`` since otherwise, it is swallowed.N)print	tracebackprint_stackAssertionError)rQ   rR   rS   s      r   	_p_assertrY      s0    a  ## ! r   tensorsizec                    [         R                  " 5          [         R                  R                  R	                  5       (       d  U R                  5       R                  5       UR                  5       :H  nU(       dZ  U R                  5       R                  5       n[        US:H  S5        U R                  5       R                  UR                  5       5        SSS5        g! , (       d  f       g= f)z
Allocate storage for ``tensor`` with the given size.

Returns:
    bool: ``True`` if this method allocated storage and ``False`` if the
    storage was already allocated.
r   zCTensor storage should have been resized to be 0 but got PLACEHOLDErN)
r    no_graddistributed_functional_collectivesis_torchdynamo_compiling_typed_storage_sizenumelrY   _resize_)rZ   r[   already_allocatedtensor_storage_sizes       r   _alloc_storagerg      s     
  88QQSS & 5 5 7 = = ?4::< O$&,&;&;&=&C&C&E#'1,Y %%'00> 
s   B>C
C+c           
         [         R                  " 5          [         R                  R                  R	                  5       (       d  U R                  5       R                  5       S:H  nU(       dy  [        U R                  5       S:H  SU R                  5        SU R                  5       R                  5        SU R                   35        U R                  5       R                  S5        SSS5        g! , (       d  f       g= f)z
Frees the underlying storage of ``tensor``.

Returns:
    bool: ``True`` if the method freed the storage and ``False`` if the
    storage was already freed.
r   zVFreeing a tensor's storage is unsafe when it is not the sole occupant
storage offset: z
storage size: z
tensor shape: N)r    r]   r^   r_   r`   ra   rb   rY   storage_offsetshaperd   )rZ   already_freeds     r   _free_storagerl      s     
  88QQSS"11399;q@M ))+q0''-'<'<'>&? @%%+%:%:%<%B%B%D$E F%%+\\N4 %%'003 
s   CC..
C<QRfn	containerc                     g r   r3   ro   rp   s     r   r&   r&      s     	r   c                     g r   r3   rr   s     r   r&   r&      s    MPr   c                 $   ^ ^ UU 4S jmT" U5      $ )zFRecursively apply to all tensor in different kinds of container types.c           
        > SSK Jn  [        U [        R                  5      (       a  T
" U 5      $ [        U S5      (       av  [        R                  " U 5      n[        R                  " U5       Vs0 s H(  o3R                  T	" [        X#R                  5      5      _M*     nn[        R                  " U40 UD6$ [        U [        5      (       a5  U R                  5       nU R                  5        H  u  pgT	" U5      XV'   M     U$ [        U [        5      (       a  T	" U R                  5        U $ [        U [         5      (       a,  U R                  5        VVs0 s H  u  pgUT	" U5      _M     snn$ U" U 5      (       a  U	4S jU  5       n[#        U 5      " U6 $ [        U [$        [&        [(        45      (       a  [#        U 5      " U	4S jU  5       5      $ U $ s  snf s  snnf )Nr   r;   __dataclass_fields__c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r3   .0elapplys     r   	<genexpr>3_apply_to_tensors.<locals>.apply.<locals>.<genexpr>   s     )q599q   c              3   4   >#    U  H  nT" U5      v   M     g 7fr   r3   rx   s     r   r|   r}      s     1q599qr~   )rH   r<   r=   r    r%   hasattrdataclassesreplacefieldsnamer@   r   	__class__r   r   r>   r*   rA   r   r   set)r   r<   dcfchangesodkeyvaluerP   r{   ro   s            r   r{    _apply_to_tensors.<locals>.apply   sy   Ca&&a5LQ.//$$Q'B<G<N<Nr<R<Rqgb&&122<R   &&r5W55;''Bggi
, (I>**!&&MH4  89	B	*#Cu%	BBA)q)C7C= D%-..71q111H) Cs   ./GGr3   )ro   rp   r{   s   ` @r   r&   r&      s    : r   c                    U (       a  [        XU5      O/ nU(       a  [        XU5      O/ n[        U5      [        U5      :  aA  UR                  [        [        U5      [        U 5      -
  5       Vs/ s H  nSPM     sn5        OX[        U5      [        U5      :  a@  UR                  [        [        U5      [        U5      -
  5       Vs/ s H  n0 PM     sn5        [	        U5      [	        U5      4$ s  snf s  snf )Nr3   )r5   r)   extendranger   )r/   r   r0   r1   moved_inputsmoved_kwargs_s          r   
_to_kwargsr      s      	f-NO   	f-NO 
 <3|,,s</@3v;/N)OP)OAR)OPQ	\	S.	.s</@3|CT/T)UV)UAR)UVWl 333 QVs   0C2	C7process_grouptensorsloggerzdist.Loggerc                 0    [         R                  " XU5      $ r   )dist_verify_params_across_processes)r   r   r   s      r   $_verify_param_shape_across_processesr     s    
 //OOr   modulebroadcast_bucket_sizesrcparams_and_buffers_to_ignorebroadcast_buffersc                 *   / nU R                  5        H+  u  pxXt;  d  M  UR                  UR                  5       5        M-     U(       a?  U R                  5        H+  u  pyXt;  d  M  UR                  U	R                  5       5        M-     [	        XX#5        g)aO  
Sync ``module``'s parameters and buffers state.

Syncs ``module``'s parameters and buffers state so that all ranks contain
the same module state across all ranks. Note that this API assumes that all
parameter shapes are consistent before running the synchronization. This can
be checked with ``_verify_param_shape_across_processes``.
N)named_parametersr   detachnamed_buffers_sync_params_and_buffers)
r   r   r   r   r   r   module_statesr   parambuffers
             r   _sync_module_statesr     sy      )+M..03  0 1 "002LD7$$V]]_5 3 ];PVr   r   c                 R    [        U5      S:  a  [        R                  " XX#5        gg)zfSynchronize ``module_states`` (list of tensors) across all processes by broadcasting them from rank 0.r   N)r)   r   _broadcast_coalesced)r   r   r   r   s       r   r   r   :  s*     =A!!*?	
 r   
state_dict
old_prefix
new_prefixc                     X:X  a  [        S5      e[        U R                  5       5       H3  nUR                  U5      (       d  M  X#[	        U5      S -   nX   X'   X	 M5     g)a  
Replace all keys that match a given old_prefix with a new_prefix (in-place).

Usage::

    state_dict = {"layer.xyz": torch.tensor(1)}
    replace_by_prefix_(state_dict, "layer.", "module.layer.")
    assert state_dict == {"module.layer.xyz": torch.tensor(1)}
z*old_prefix and new_prefix must be distinctN)
ValueErrorr   keys
startswithr)   )r   r   r   r   new_keys        r   _replace_by_prefixr   G  sa     EFFJOO%&~~j))3z?#455(o
O 'r   c                 D    U R                  5       R                  5       S:  $ )Nr   )untyped_storagedata_ptr)rZ   s    r   _data_ptr_allocatedr   _  s    !!#,,.22r   modulesc                    / nU  Vs0 s H  o"[        UR                  5       5      _M     nnU  HQ  nSnUR                  5        H  u  p&XBL=(       a    XF;   nU(       d  M  Sn  O   U(       d  M@  UR                  U5        MS     U$ s  snf )z
Returns the modules in ``modules`` that are root modules (i.e.
parent-less) with respect to the set ``modules``. In other words, these
are the modules in ``modules`` that are the not child of any other
module in ``modules``.
TF)r   r   r   r   )r   root_modulesr   module_to_modulescandidate_moduleis_root_module_modulesis_child_modules           r   _get_root_modulesr   c  s     %'L4;:4;&FNN$%%G  : $ 1 7 7 9F .O3C3O  !& !: > 01 $ :s   #B)Tr   )4r   rV   collectionsr   collections.abcr   typingr   r   r   r   r	   r    torch.distributedr^   r   r
   torch.nn.utils.rnnr   __all__r   strr   r   r'   r*   r,   r   r-   r%   r.   r?   boolr5   rY   Sizerg   rl   rm   r   rn   r&   r   ProcessGroupr   Moduleintr   r   r   r   r   r3   r   r   <module>r      s-     # % = =     - / /s /uU38_eCQTHo5U/V /:REKK RR R 38_	R*S#X,1#s(O
5c?DcN*+ CtU#C~. 
#llOS	!W 

 
#llOS
1X 

6r$C $C $ $ $?5<< ?uzz ?d ?(4%,, 4, CLCtUCncJ 
%,,"#05 

 
 P(ELL>3#67 PA P! P 
 P F4#s(O4T#s(^$4 <<4 (,	4
 5c?E$sCx.#"566744 '+P$$P%,,P ]#P #WIIW$$W W 
	W
 #,C.W W 
W:

$$

%

 

 
	


 


S#X  
	03 3 3tBII 4		? r   