Ë
    g^(hˆ(  ã            
       óØ  — 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j8                  eej8                     «      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j8                  dee"e#ef      fd„Z$y)é    N)ÚOrderedDict)Úwraps)ÚCallableÚGenericÚOptionalÚProtocol)ÚConcatenateÚ	ParamSpecÚTypeVar)Ú_State)Ú_get_root_modulesÚ_TT)Ú	covariantÚ_Pc                 óF   — | › dt        t        j                  «       «      › �S )NÚ_)ÚstrÚuuidÚuuid4)Ústrings    úd/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/distributed/_composable/contract.pyÚgenerate_state_keyr      s   € ØˆX�Q”sœ4Ÿ:™:›<Ó(Ð)Ð*Ð*ó    c                   ó   — e Zd Zy)ÚRegistryItemN)Ú__name__Ú
__module__Ú__qualname__© r   r   r   r      s   „ Ør   r   Ú_TStater   )Úboundr   Ú_Mc                   ól   — e Zd Zdej                  dej
                  defd„Zdej                  de
fd„Zy)Ú_ContractFnÚargsÚkwargsÚreturnc                  ó   — y ©Nr   )Úselfr%   r&   s      r   Ú__call__z_ContractFn.__call__'   ó   � r   Úmodulec                  ó   — y r)   r   )r*   r-   s     r   Ústatez_ContractFn.state)   r,   r   N)r   r   r   r   r%   r&   r   r+   ÚnnÚModuler    r/   r   r   r   r$   r$   &   s-   „ ØF˜bŸg™gÐF°·±ÐF¸rÓFà6˜BŸI™IÐ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 �]f  } t        «       }
t        «       }| j                  j                  t        |
«      }t        |t        «      st        d|› �«      ‚| j                  j                  t        |«      }t        |t        «      st        d|› �«      ‚‰|v s‰j                  |v rt        d‰j                  › d| › �«      ‚|j                  ‰|«       |j                  ‰j                  |«       |j                  t        | j                  «       «      «       |j                  t        | j!                  «       «      «       |	j                  t        | j#                  «       «      «       �Œi  ‰|g|¢­i |¤Ž}|€|}t        |t        j                  «      r|g}nt        t	        |«      «      }g }g }g }|D ]z  } |j                  t        | j                  «       «      «       |j                  t        | j!                  «       «      «       |j                  t        | j#                  «       «      «       Œ| t%        |	«      }t%        |«      }||k7  rt        ‰j                  › d|› d|› d�«      ‚dt        t&           d	t        t&           d
t&        fd„}t)        ||«      D ]=  \  }} |t	        |j+                  «       «      t	        |j+                  «       «      d«       Œ? t)        ||«      D ]=  \  }} |t	        |j+                  «       «      t	        |j+                  «       «      d«       Œ? t)        |	|«      D ]=  \  }} |t	        |j+                  «       «      t	        |j+                  «       «      d«       Œ? |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y 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)r7   r8   r9   Úorig_fqn_setÚnew_fqn_setÚ	orig_onlyÚnew_onlys          r   Ú	check_fqnz;contract.<locals>.inner.<locals>.wrapper.<locals>.check_fqn°   s—   € Ø Ò(Øä,/°	«N¼CÀ»M˜k�Ø(¨;Ñ6�	Ø&¨Ñ5�Ü�y”>¤S¨¤]Ü&Ø$˜+ð2Ø2;°ð =-Ø-5¨Jð8óð ô 'Ø$˜+ð*à*3¨ð 5%Ø%- Jð	0óð r   zChecking parameters: zChecking buffers: zChecking modules: )Ú
isinstancer0   r1   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Úmodulesr/   Ú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_modulesrB   Úorig_named_paramsÚnew_named_paramsÚorig_named_buffersÚnew_named_buffersÚorig_named_modulesÚnew_named_modulesr4   r2   s                               €€r   Úwrapperz(contract.<locals>.inner.<locals>.wrapper_   sÞ  ø€ ð  ˆJä˜&¤"§)¡)Ô,Ø!˜(‘ô
 ,¬D°«LÓ9�Ù“KˆEÜ(›NˆMð
 DFÐ!ØDFÐ"ØACÐ"à!ó S�Ü<G»MÐ!Ü<G»MÐ Ø4:·O±O×4NÑ4NÜÐ0ó5�	ô " )¬TÔ2Ü(ØGÈ	À{ÐSóð ð 5;·O±O×4NÑ4NÜ Ð"2ó5�ô " (¬DÔ1Ü(ØIÈ(ÈÐTóð ð ˜9Ñ$¨¯©¸Ñ(AÜ(ð(Ø(,¯© ð 7.Ø.4¨Xð7óð ð
 ×$Ñ$ T¨5Ô1Ø×#Ñ# D§M¡M°=ÔAà%×,Ñ,¬[¸×9PÑ9PÓ9RÓ-SÔTØ&×-Ñ-¬k¸&×:NÑ:NÓ:PÓ.QÔRØ&×-Ñ-¬k¸&×:NÑ:NÓ:PÓ.QÖRð9Sñ< ˜:Ð7¨Ò7°Ñ7ˆGØˆØ$�ä˜'¤2§9¡9Ô-Ø#* )‘ä"3´D¸Ó4DÓ"E�àBDÐ ØCEÐ!Ø@BÐ!Ø)ò R�Ø$×+Ñ+¬K¸×8OÑ8OÓ8QÓ,RÔSØ%×,Ñ,¬[¸×9MÑ9MÓ9OÓ-PÔQØ%×,Ñ,¬[¸×9MÑ9MÓ9OÓ-PÕQðRô
  #Ð#9Ó:ÐÜ!Ð"7Ó8ˆOØ ?Ò2Ü$Ø—}‘}�oð &Ø/Ð0ð 1 Ø /Ð0°ð:óð ð¤T¬#¡Yð ¼$¼s¹)ð ÔPSó ô. 8;Ø%Ð';ó8ò Ñ3Ð!Ð#3ñ ÜÐ*×/Ñ/Ó1Ó2ÜÐ)×.Ñ.Ó0Ó1Ø+õðô :=Ø&Ð(=ó:ò Ñ5Ð"Ð$5ñ ÜÐ+×0Ñ0Ó2Ó3ÜÐ*×/Ñ/Ó1Ó2Ø(õðô :=Ø&Ð(=ó:ò Ñ5Ð"Ð$5ñ ÜÐ+×0Ñ0Ó2Ó3ÜÐ*×/Ñ/Ó1Ó2Ø(õðð ˆNr   c                 ób   •— | j                   j                  t        i «      j                  ‰«      S r)   )rE   rF   rG   Úget)r-   r4   s    €r   Ú	get_statez*contract.<locals>.inner.<locals>.get_stateå   s+   ø€ Ø—?‘?×-Ñ-ÜØó÷ ‰c�$‹iðr   )	r   r"   r   r%   r&   r0   r1   r   r/   )r4   rh   rk   r2   s   `  €r   Úinnerzcontract.<locals>.inner[   ss   ù€ ô 
ˆt‹ðC	ÜðC	ä—7‘7ðC	ô —i‘iðC	ô õ	C	ó 
ðC	ðJ	œbŸi™ið 	¬Fõ 	ð "ˆŒàˆr   )r   r   r	   r"   r   r$   r    )r2   rl   s   ` r   Úcontractrm   ,   sa   ø€ ô^ ˆ9ÓðQÜ”{¤2¤r 6Ñ*¬BÐ.Ñ/ðQä	”[¤¤R Ñ(¬"¬gÐ5Ñ	6ô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)ÚgetattrrJ   )r-   s    r   Ú_get_registryrp   ò   s   € ô �6œ<¨Ó.Ð.r   )Ú__composable_api_state_key)%r   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r   Útyping_extensionsr	   r
   r   ÚtorchÚtorch.nnr0   Ú#torch.distributed._composable_stater   Útorch.distributed.utilsr   r   r   r   rG   rJ   r   r    r1   rD   r"   r$   Útyperm   rH   r   rp   r   r   r   ú<module>r{      s$  ðã Ý #Ý ß 8Ó 8ß =Ñ =ã Ý Ý 6Ý 5ñ ˆT˜TÔ"€Ùˆtƒ_€ó+ñ Ó €	Ù!Ó#€÷	ñ 	ñ �) 8°tÔ
<€ÙˆT�2—9‘9˜d 2§9¡9™oÓ.€ô7�(˜G B¨¨G OÑ4ô 7ð  &ñCØ�G‰}ðCàØˆk˜"˜b˜&Ñ! 2Ð%Ñ&Ð'Ø�˜B ˜FÑ# R¨Ð0Ñ1ð3ñóCðL/˜"Ÿ)™)ð /¨°°c¸<Ð6GÑ1HÑ(Iô /r   