
    &Vji(              
       (   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 d dl
mZmZmZ d dlZd dlmZ d dlmZ d dlmZ  edd	
          Z ed          ZddZ e            Z e            Z G d d          Z eddd	          Z edej        eej                           Z G d de	eeeef                   Zefde e         deeeeef         ef         geeeef         eef         f         fdZ!dej        dee"e#ef                  fdZ$dS )    N)OrderedDict)wraps)CallableGenericOptionalProtocol)Concatenate	ParamSpecTypeVar)_State)_get_root_modules_TT)	covariant_P__composable_api_state_keyc                 L    |  dt          t          j                               S )N_)struuiduuid4)strings    `/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/distributed/_composable/contract.pygenerate_state_keyr      s$    **s4:<<((***    c                       e Zd ZdS )RegistryItemN)__name__
__module____qualname__ r   r   r   r      s        Dr   r   _TStater   )boundr   _Mc                   P    e Zd Zdej        dej        defdZdej	        de
fdZdS )_ContractFnargskwargsreturnc                     d S Nr    )selfr&   r'   s      r   __call__z_ContractFn.__call__'         r   modulec                     d S r*   r    )r+   r.   s     r   statez_ContractFn.state)   r-   r   N)r   r   r   r   r&   r'   r   r,   nnModuler!   r0   r    r   r   r%   r%   &   sG        FbgFFrFFFF6BI6'666666r   r%   	state_clsr(   c                      t                     dt          t          t          t          f         t          f         dt
          t          t          t          f         t          t          f         f fd            }|S )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                     dt          dt          j        dt          j        dt          f fd            }dt
          j        dt          f fd}||_        |S )Nr.   r&   r'   r(   c                 	   | }t          | t          j                  r| g}nt          t	          |                     }             }t                      }g }g }g }	|D ]} t                      }
t                      }| j                            t          |
          }t          |t                    st          d|           | j                            t          |          }t          |t                    st          d|           |v s	j        |v rt          dj         d|            |                    |           |                    j        |           |                    t          |                                                      |                    t          |                                                      |	                    t          |                                                       |g|R i |}||}t          |t          j                  r|g}nt          t	          |                    }g }g }g }|D ]} |                    t          |                                                      |                    t          |                                                      |                    t          |                                                      t%          |	          }t%          |          }||k    rt          j         d| d| d          dt          t&                   d	t          t&                   d
t&          fd}t)          ||          D ]P\  }} |t	          |                                          t	          |                                          d           Qt)          ||          D ]P\  }} |t	          |                                          t	          |                                          d           Qt)          |	|          D ]P\  }} |t	          |                                          t	          |                                          d           Q|S )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                     | |k    rd S t          |           t          |          }}||z
  }||z
  }t          |          st          |          rt          | d| d|           t          | d| d|           )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)r8   r9   r:   orig_fqn_setnew_fqn_set	orig_onlynew_onlys          r   	check_fqnz;contract.<locals>.inner.<locals>.wrapper.<locals>.check_fqn   s    ((F,/	NNCMMk(;6	&5y>> S]] &$ 8 82;8 8 .68 8   '$ 0 0 +40 0 &.	0 0  r   zChecking parameters: zChecking buffers: zChecking modules: )
isinstancer1   r2   r   listr   r   __dict__
setdefault	STATE_KEYdictAssertionErrorREGISTRY_KEYr   appendnamed_parametersnamed_buffersnamed_modulesr=   r   zipkeys)r.   r&   r'   
inp_modulemodulesr0   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_modulesrC   orig_named_paramsnew_named_paramsorig_named_buffersnew_named_buffersorig_named_modulesnew_named_modulesr5   r3   s                               r   wrapperz(contract.<locals>.inner.<locals>.wrapper_   s     J&"),, :!(
 ,DLL99IKKE(NNM
 DF!DF"AC"! S S<GMM!<GMM 4:O4N4N05 5	 ")T22 (S	SS   5;O4N4N "25 5 "(D11 (T(TT   9$$(A(A(7(,7 7.47 7  
 $$T5111##DM=AAA%,,[9P9P9R9R-S-STTT&--k&:N:N:P:P.Q.QRRR&--k&:N:N:P:P.Q.QRRRRd:777777G$'29-- F#*)"3D4D4D"E"EBD CE!@B!) R R$++K8O8O8Q8Q,R,RSSS%,,[9M9M9O9O-P-PQQQ%,,[9M9M9O9O-P-PQQQQ"#9::!"788O?22$} : :/: : /: : :  T#Y $s) PS    . 8;%';8 8  3!#3 	*//1122)..0011+   
 :=&(=: :  5"$5 	+002233*//1122(   
 :=&(=: :  5"$5 	+002233*//1122(    Nr   c                 j    | j                             t          i                                         S r*   )rF   rG   rH   get)r.   r5   s    r   	get_statez*contract.<locals>.inner.<locals>.get_state   s0    ?--  c$iir   )	r   r#   r   r&   r'   r1   r2   r   r0   )r5   ri   rl   r3   s   `  r   innerzcontract.<locals>.inner[   s     
tC	C	7C	 iC	 	C	 C	 C	 C	 C	 C	 
C	J	bi 	F 	 	 	 	 	 	 "r   )r   r   r	   r#   r   r%   r!   )r3   rm   s   ` r   contractrn   ,   s    ^ 9Q{2r6*B./Q	[R("g5	6Q Q Q Q Q Qf Lr   r.   c                 .    t          | t          d          S )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)getattrrK   )r.   s    r   _get_registryrq      s     6<...r   )r   )%r   collectionsr   	functoolsr   typingr   r   r   r   typing_extensionsr	   r
   r   torchtorch.nnr1   #torch.distributed._composable_stater   torch.distributed.utilsr   r   r   r   rH   rK   r   r!   r2   rE   r#   r%   typern   rI   r   rq   r    r   r   <module>r{      sL    # # # # # #       8 8 8 8 8 8 8 8 8 8 8 8 = = = = = = = = = =        6 6 6 6 6 6 5 5 5 5 5 5 WTT"""Yt__+ + + +   	!!##	 	 	 	 	 	 	 	 ')8t
<
<
<WT29d29o..7 7 7 7 7(GBGO4 7 7 7  &C CG}Ck"b&!2%&'BF#R013C C C CL/") /c<6G1H(I / / / / / /r   