
    [Th(              
          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	  S SK
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  \" SS	S
9r\" S5      rSS jr\" 5       r\" 5       r " S S5      r\" SSS	S9r\" S\R8                  \\R8                     5      r " S S\	\\\\4   5      r\4S\ \   S\\\\\4   \4   /\\\\4   \\4   4   4S jjr!S\R8                  S\\"\#\4      4S jr$g)    N)OrderedDict)wraps)CallableGenericOptionalProtocol)Concatenate	ParamSpecTypeVar)_State)_get_root_modules_TT)	covariant_Pc                 H    U  S[        [        R                  " 5       5       3$ )N_)struuiduuid4)strings    ^/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributed/_composable/contract.pygenerate_state_keyr      s    XQs4::<()**    c                       \ rS rSrSrg)RegistryItem    N)__name__
__module____qualname____firstlineno____static_attributes__r   r   r   r   r      s    r   r   _TStater   )boundr   _Mc                   x    \ rS rSrS\R
                  S\R                  S\4S jrS\	R                  S\4S jrSrg	)
_ContractFn&   argskwargsreturnc                     g Nr   )selfr)   r*   s      r   __call___ContractFn.__call__'   s    3r   modulec                     g r-   r   )r.   r1   s     r   state_ContractFn.state)   s    3r   r   N)r   r   r    r!   r   r)   r*   r   r/   nnModuler#   r3   r"   r   r   r   r'   r'   &   s-    FbggFFrF6BII6'6r   r'   	state_clsr+   c                    ^  [        T 5      S[        [        [        [        4   [        4   S[
        [        [        [        4   [        [        4   4U 4S jj5       nU$ )a  
Decorate a function as a composable distributed API, where the first
argument of the function must be an :class:`nn.Module` instance or sequence
of :class:`nn.Module` instances.

The decorator verifies that the decorated function does not modify
fully-qualified names (FQNs) for parameters, buffers, or modules. The
decorated function can return different module instances than the input
modules; the FQN invariant will be enforced following the input order.

When a function ``func`` is decorated by ``@contract()``, a
``.state(module: nn.Module)`` method will be installed to the decorated
function. Then you can retrieve and modify the state on a module by calling
``func.state(module)``.

Example::
    >>> # xdoctest: +SKIP
    >>> import torch.nn as nn
    >>>
    >>> class MyModel(nn.Module):
    >>>     def __init__(self) -> None:
    >>>         super().__init__()
    >>>         self.l1 = nn.Linear(10, 10)
    >>>         self.l2 = nn.Linear(10, 10)
    >>>
    >>>     def forward(self, x):
    >>>         return self.l2(self.l1(x))
    >>>
    >>> @contract()
    >>> def my_feature(module: nn.Module) -> nn.Module:
    >>>     my_feature.state(module).some_state = "any value"
    >>>     return module
    >>>
    >>> model = MyModel()
    >>> my_feature(model.l1)
    >>> assert my_feature.state(model.l1).some_state == "any value"
    >>> my_feature(model.l2)
    >>> model(torch.randn(2, 10)).sum().backward()
funcr+   c           	         >^  [        T 5      S[        S[        R                  S[        R                  S[        4U U4S jj5       nS[
        R                  S[        4U 4S jjnX!l        U$ )Nr1   r)   r*   r+   c                   > U n[        U [        R                  5      (       a  U /nO[        [	        U 5      5      nT" 5       n[        5       n/ n/ n/ n	U GHu  n [        5       n
[        5       nU R                  R                  [        U
5      n[        U[        5      (       d  [        SU 35      eU R                  R                  [        U5      n[        U[        5      (       d  [        SU 35      eTU;   d  TR                  U;   a  [        STR                   SU  35      eUR                  TU5        UR                  TR                  U5        UR                  [        U R                  5       5      5        UR                  [        U R!                  5       5      5        U	R                  [        U R#                  5       5      5        GMx     T" U/UQ70 UD6nUc  Un[        U[        R                  5      (       a  U/nO[        [	        U5      5      n/ n/ n/ nU H{  n UR                  [        U R                  5       5      5        UR                  [        U R!                  5       5      5        UR                  [        U R#                  5       5      5        M}     [%        U	5      n[%        U5      nUU:w  a  [        TR                   SU SU S35      eS[        [&           S	[        [&           S
[&        4S jn[)        UU5       H>  u  nnU" [	        UR+                  5       5      [	        UR+                  5       5      S5        M@     [)        UU5       H>  u  nnU" [	        UR+                  5       5      [	        UR+                  5       5      S5        M@     [)        U	U5       H>  u  nnU" [	        UR+                  5       5      [	        UR+                  5       5      S5        M@     U$ )Nz-Distributed composable API states corrupted: z/Distributed composable API registry corrupted: zOEach distinct composable distributed API can only be applied to a module once. z3 has already been applied to the following module:
zB should return the same number of modules as input modulesInputs: z modules
Outputs: z modules	orig_fqnsnew_fqns	check_keyc                     X:X  a  g [        U 5      [        U5      pCX4-
  nXC-
  n[        U5      (       d  [        U5      (       a  [        U SU SU 35      e[        U SU SU 35      e)NzVComposable distributed API implementations cannot modify FQNs.
FQNs only in original: z
FQNs only in new: z[Composable distributed API implementations cannot modify the order of FQNs.
Original FQNs: z
New FQNs: )setlenRuntimeError)r<   r=   r>   orig_fqn_setnew_fqn_set	orig_onlynew_onlys          r   	check_fqn;contract.<locals>.inner.<locals>.wrapper.<locals>.check_fqn   s    (,/	NCMk(6	&5y>>S]]&$+22; =--5J8  '$+**3 5%%-J	0 r   zChecking parameters: zChecking buffers: zChecking modules: )
isinstancer5   r6   r   listr   r   __dict__
setdefault	STATE_KEYdictAssertionErrorREGISTRY_KEYr   appendnamed_parametersnamed_buffersnamed_modulesrA   r   zipkeys)r1   r)   r*   
inp_modulemodulesr3   registry_itemall_orig_named_paramsall_orig_named_buffersall_orig_named_modulesdefault_all_statedefault_registry	all_stateregistryupdatedupdated_modulesall_new_named_paramsall_new_named_buffersall_new_named_modulesnum_orig_modulesnum_new_modulesrG   orig_named_paramsnew_named_paramsorig_named_buffersnew_named_buffersorig_named_modulesnew_named_modulesr9   r7   s                               r   wrapper(contract.<locals>.inner.<locals>.wrapper_   s     J&")),,!(
 ,DL9KE(NM
 DF!DF"AC"!<GM!<GM 4:OO4N4N05	 ")T22(G	{S  5;OO4N4N "25 "(D11(I(T  9$(A(((, 7..4X7 
 $$T51##DMM=A%,,[9P9P9R-ST&--k&:N:N:P.QR&--k&:N:N:P.QR9 "< :777G$'299--#*)"3D4D"EBD CE!@B!)$++K8O8O8Q,RS%,,[9M9M9O-PQ%,,[9M9M9O-PQ *
  ##9:!"78O?2$}}o &/0 1  /0: T#Y $s) PS . 8;%';83!#3 *//12)..01+8 :=&(=:5"$5 +0023*//12(: :=&(=:5"$5 +0023*//12(: Nr   c                 b   > U R                   R                  [        0 5      R                  T5      $ r-   )rK   rL   rM   get)r1   r9   s    r   	get_state*contract.<locals>.inner.<locals>.get_state   s+    ??-- c$ir   )	r   r%   r   r)   r*   r5   r6   r   r3   )r9   rn   rr   r7   s   `  r   innercontract.<locals>.inner[   sq     
tC	C	77C	 iiC	 	C	 
C	J	bii 	F 	 "r   )r   r   r	   r%   r   r'   r#   )r7   rt   s   ` r   contractrv   ,   sa    ^ 9Q{2r6*B./Q	[R("g5	6Q Qf Lr   r1   c                 $    [        U [        S5      $ )z
Get an ``OrderedDict`` of composable APIs that have been applied to the
``module``, indexed by the API name. If no API has been applied, then this
returns ``None``.
N)getattrrP   )r1   s    r   _get_registryry      s     6<..r   )__composable_api_state_key)%r   collectionsr   	functoolsr   typingr   r   r   r   typing_extensionsr	   r
   r   torchtorch.nnr5   #torch.distributed._composable_stater   torch.distributed.utilsr   r   r   r   rM   rP   r   r#   r6   rJ   r%   r'   typerv   rN   r   ry   r   r   r   <module>r      s$    #  8 8 = =   6 5 TT"t_+  	!#	 	 )8t
<T299d299o.7(GBGO4 7  &CG}Ck"b&!2%&'BF#R013CL/")) /c<6G1H(I /r   