Ë
    P/æjJ%  ã                   óö   — 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j2                  de	eeef      fd„Zy)é    N)ÚOrderedDict©Úwraps)ÚCallableÚDictÚListÚOptionalÚSequenceÚTypeÚUnion)Ú_State)Ú_get_root_modulesc                 óF   — | › dt        t        j                  «       «      › �S )NÚ_)ÚstrÚuuidÚuuid4)Ústrings    úi/root/repos/pocs/video-evals/.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   Ú	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 �]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)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—   € Ø Ò(Øä,/°	«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: )Ú
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_modulesr/   Ú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Ó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 )N)r4   r5   r6   Úget)r!   rZ   s    €r   Ú	get_statez*contract.<locals>.inner.<locals>.get_stateË   s/   ø€ Ø—?‘?×-Ñ-ÜØó÷ ‰cØóðr   )r   r   r1   r2   r
   r	   r   rD   )rZ   r[   r^   r   s   `  €r   Úinnerzcontract.<locals>.innerG   sx   ù€ ä	ˆt‹ð	Üœ"Ÿ)™)¤X¬b¯i©iÑ%8Ð8Ñ9ð	ä”b—i‘iÑ õ	ó 
ð	ðB	œbŸi™ið 	¬H´VÑ,<õ 	ð "ˆŒàˆr   r   )r   r_   s   ` r   Úcontractr`      s'   ø€ ôT ˆ9Óó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)Úgetattrr9   )r!   s    r   Ú_get_registryrc   Ú   s   € ô �6œ<¨Ó.Ð.r   )Ú__composable_api_state_key)r   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r	   r
   r   r   ÚtorchÚtorch.nnr1   Ú#torch.distributed._composable_stater   Útorch.distributed.utilsr   r   r6   r9   r   r`   r2   r   rc   r   r   r   ú<module>rl      s‚   ðã Ý #Ý ß H× HÑ Hã Ý Ý 6Ý 5ó+ñ Ó €	Ù!Ó#€÷	ñ 	ð (.ñ z˜˜V™ó zðz/˜"Ÿ)™)ð /¨°°c¸<Ð6GÑ1HÑ(Iô /r   