§
    Ÿ²ÅjD&  ã                   ó  — 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
mZmZ d dlZd dl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dS )é    N)ÚOrderedDict©Úwraps)ÚCallableÚDictÚListÚOptionalÚSequenceÚTypeÚUnion)Ú_State)Ú_get_root_modulesÚ__composable_api_state_keyc                 óL   — | › dt          t          j        ¦   «         ¦  «        › �S )NÚ_)ÚstrÚuuidÚuuid4)Ústrings    úWC:\Projects\Historical\venv\Lib\site-packages\torch/distributed/_composable/contract.pyÚgenerate_state_keyr      s$   € ØÐ*Ð*•s�4œ:™<œ<Ñ(Ô(Ð*Ð*Ð*ó    c                   ó   — e Zd ZdS )ÚRegistryItemN)Ú__name__Ú
__module__Ú__qualname__© r   r   r   r      s   € € € € € Ø€Dr   r   Ú	state_clsc                 ó<   ‡ — t          ‰ ¦  «        ˆ 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()
    c                 ó"  •‡ — t          ‰ ¦  «        dt          t          j        t          t          j                 f         dt
          t          j                 fˆ ˆfd„¦   «         }dt          j        dt
          t                   fˆ fd„}||_        |S )NÚmoduleÚreturnc                 ó	  •— | }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)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Ð   € Ø Ò(Ð(Ø�Få,/°	©N¬N½CÀ¹M¼M˜k�Ø(¨;Ñ6�	Ø&¨Ñ5�Ý�y‘>”>ð ¥S¨¡]¤]ð Ý&Ø$ð 8ð 8à2;ð8ð 8ð .6ð8ð 8ñô ð õ 'Ø$ð 0ð 0ð +4ð0ð 0ð &.ð	0ð 0ñô ð r   zChecking parameters: zChecking buffers: zChecking modules: )Ú
isinstanceÚnnÚModuler   Úlistr   r   Ú__dict__Ú
setdefaultÚ	STATE_KEYÚdictÚAssertionErrorÚREGISTRY_KEYr   ÚappendÚnamed_parametersÚnamed_buffersÚnamed_modulesr*   r   r   ÚzipÚkeys)r"   ÚargsÚkwargsÚ
inp_moduleÚmodulesÚstateÚ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_modulesr0   Úorig_named_paramsÚnew_named_paramsÚorig_named_buffersÚnew_named_buffersÚorig_named_modulesÚnew_named_modulesÚfuncr   s                               €€r   Úwrapperz(contract.<locals>.inner.<locals>.wrapperI   s  ø€ ð  ˆJÝ˜&¥"¤)Ñ,Ô,ð :Ø!˜(��õ
 ,­D°©L¬LÑ9Ô9�Ø�I‘K”KˆEÝ(™NœNˆMð
 DFÐ!ØDFÐ"ØACÐ"à!ð Sñ S�Ý<G¹M¼MÐ!Ý<G¹M¼MÐ Ø4:´O×4NÒ4NÝÐ0ñ5ô 5�	õ " )­TÑ2Ô2ð Ý(ØSÈ	ÐSÐSñô ð ð 5;´O×4NÒ4NÝ Ð"2ñ5ô 5�õ " (­DÑ1Ô1ð Ý(ØTÈ(ÐTÐTñô ð ð ˜9Ð$Ð$¨¬¸Ð(AÐ(AÝ(ð7Ø(,¬ð7ð 7à.4ð7ð 7ñô ð ð
 ×$Ò$ T¨5Ñ1Ô1Ð1Ø×#Ò# D¤M°=ÑAÔAÐAà%×,Ò,­[¸×9PÒ9PÑ9RÔ9RÑ-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Ð7ˆGØˆØ$�Ý˜'¥2¤9Ñ-Ô-ð FØ#* )��å"3µD¸Ñ4DÔ4DÑ"EÔ"E�àBDÐ ØCEÐ!Ø@BÐ!Ø)ð Rð R�Ø$×+Ò+­K¸×8OÒ8OÑ8QÔ8QÑ,RÔ,RÑSÔSÐSØ%×,Ò,­[¸×9MÒ9MÑ9OÔ9OÑ-PÔ-PÑQÔQÐQØ%×,Ò,­[¸×9MÒ9MÑ9OÔ9OÑ-PÔ-PÑQÔQÐQÐQå"Ð#9Ñ:Ô:ÐÝ!Ð"7Ñ8Ô8ˆOØ ?Ò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 )N)r5   r6   r7   Úget)r"   r[   s    €r   Ú	get_statez*contract.<locals>.inner.<locals>.get_stateË   s7   ø€ Ø”?×-Ò-ÝØñô ÷ ŠcØñô ðr   )r   r   r2   r3   r
   r	   r   rE   )r[   r\   r_   r   s   `  €r   Úinnerzcontract.<locals>.innerG   s¬   øø€ å	ˆt‰Œð	Ý�"œ)¥X­b¬iÔ%8Ð8Ô9ð	å•b”iÔ ð	ð 	ð 	ð 	ð 	ð 	ñ 
Œð	ðB	�bœið 	­HµVÔ,<ð 	ð 	ð 	ð 	ð 	ð 	ð "ˆŒàˆr   r   )r   r`   s   ` r   Úcontractra      s?   ø€ õT ˆ9ÑÔðMð Mð Mð Mñ ÔðMð^ €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)Úgetattrr:   )r"   s    r   Ú_get_registryrd   Ú   s   € õ �6�<¨Ñ.Ô.Ð.r   )r   )r   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r	   r
   r   r   ÚtorchÚtorch.nnr2   Ú#torch.distributed._composable_stater   Útorch.distributed.utilsr   r   r7   r:   r   ra   r3   r   rd   r   r   r   ú<module>rl      sn  ðà €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø Ð Ð Ð Ð Ð Ø HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ Hà €€€Ø Ð Ð Ð Ð Ð Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5ð+ð +ð +ð +ð ÐÑ Ô €	Ø!Ð!Ñ#Ô#€ð	ð 	ð 	ð 	ð 	ñ 	ô 	ð 	ð (.ð zð z˜˜Vœð zð zð zð zðz/˜"œ)ð /¨°°c¸<Ð6GÔ1HÔ(Ið /ð /ð /ð /ð /ð /r   