
    Ph                         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	m
Z
 d dlmZ d dlmZ ddZ e       Z e       Z G d d      Zefd	e
e   fd
Zdej(                  de	eeef      fdZy)    N)OrderedDictwraps)CallableDictListOptionalType)_Statec                 F    |  dt        t        j                                S )N_)struuiduuid4)strings    qC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/_composable/contract.pygenerate_state_keyr   
   s    XQs4::<()**    c                       e Zd Zy)RegistryItemN)__name__
__module____qualname__ r   r   r   r      s    r   r   	state_clsc                 .     t                fd       }|S )aJ  
    Decorate a function as a composable distributed API, where the first
    argument of the function must be an :class:`nn.Module` instance. The
    decorator verifies that the wrapped function does not modify parameter,
    buffer or sub-module fully-qualified names (FQN).

    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):
        >>>         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()
    c                      t               dt        j                  dt        t        j                     f fd       }dt        j                  dt        t           f fd}||_        |S )Nmodulereturnc                 "   t               }| j                  j                  t        |      }t	        |t
              sJ d       t               }| j                  j                  t        |      }t	        |t
              sJ d       |vrj                  |vsJ dj                   d|         |j                                 |j                  j                  t                      t        | j                               }t        | j                  d            }t        | j                  d            }	 | g|i |}
|
| }
t        |
j                               }t        |
j                  d            }t        |
j                  d            }t	        |
t        j                        sJ dt        |
              dt        t            d	t        t            d
t         fd} |t#        |j%                               t#        |j%                               d        |t#        |j%                               t#        |j%                               d        |t#        |	j%                               t#        |j%                               d       |
S )Nz+Distributed composable API states corruptedz-Distributed composable API registry corruptedzOEach distinct composable distributed API can only be applied to a module once. z3 has already been applied to the following module.
F)remove_duplicatezPOutput of composable distributed APIs must be either None or nn.Module, but got 	orig_fqnsnew_fqns	check_keyc                     | |k(  ry t        |       t        |      }}||z
  }||z
  }t        |      st        |      rt        | d| d|       t        | d| d|       )NzVComposable distributed API implementations cannot modify FQNs.
Only in original FQNs: z,
Only in new FQNs: 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_fqnz;contract.<locals>.inner.<locals>.wrapper.<locals>.check_fqn}   s    (,/	NCMk(;6	&5y>S]&$+22; =--5J	8  '$+**3 5%%-J	0 r   zCheck parameters, zCheck buffer, zCheck modules, )r   __dict__
setdefault	STATE_KEY
isinstancedictREGISTRY_KEYr   r   named_parametersnamed_buffersnamed_modulesnnModuletyper   r   listkeys)r   argskwargsdefault_all_state	all_statedefault_registryregistryorig_named_paramsorig_named_buffersorig_named_modulesupdatednew_named_paramsnew_named_buffersnew_named_modulesr-   funcr   s                  r   wrapperz(contract.<locals>.inner.<locals>.wrapperB   s    9D060J0J,1I 4 =<= 
 9D060J0J.1H $ ?>? 
 y(T]](-J   $ /&&,X/J   y{3|~> +F,C,C,E F!,$$e$<" "-$$e$<" 63D3F3G *7+C+C+EF +%%u%=! !,%%u%=! gryy1 &&*7m_61
T#Y $s) PS 0 &++-.%**,-$
 ',,./&++-. 
 ',,./&++-.! Nr   c                 b    | j                   j                  t        i       j                        S )N)r.   r/   r0   get)r   rI   s    r   	get_statez*contract.<locals>.inner.<locals>.get_state   s/    ??-- cr   )r   r7   r8   r	   r   state)rI   rJ   rM   r   s   `  r   innerzcontract.<locals>.inner@   sc    	th	BII h	8BII;N h	 
h	T	bii 	HV,< 	 "r   r   )r   rO   s   ` r   contractrP      s'    L 9v vp Lr   r   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)getattrr3   )r   s    r   _get_registryrS      s     6<..r   )__composable_api_state_key)r   collectionsr   	functoolsr   typingr   r   r   r	   r
   torch.nnr7   #torch.distributed._composable_stater   r   r0   r3   r   rP   r8   r   rS   r   r   r   <module>rZ      sy     #  7 7  6+  	!#	 	 (. _V _D/")) /c<6G1H(I /r   