Ë
    g^(h *  ã                   ó°   — d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
 d dlZd dlmZ e G d„ d«      «       Z G d„ de	«      Z G d	„ d
«      Z G d„ d«      Zy)é    N)Úcontextmanager)Ú	dataclassÚfield)ÚAnyÚCallableÚ
NamedTupleÚOptionalc                   óœ   — e Zd ZU dZ eej                  j                  ¬«      Zej                  j                  e	d<   dZ
eeeef      e	d<   y)ÚTracingConfiga±  
    This represents a symbolic tracing configuration.

    Args:
        tracer (torch.fx.Tracer): An instance of :class:`torch.fx.Tracer` to
            use for symbolic tracing. The default value is the native
            :class:`torch.fx.Tracer` constructed with default arguments.
            However, the user may want to pass a different value such as the
            ``HFTracer`` for models in the HuggingFace Transformers_ library.
            .. _Transformers: https://huggingface.co/docs/transformers/index
        concrete_args (Optional[Dict[str, Any]]): Concrete arguments that
            should not be treated as ``torch.fx.Proxy`` when tracing the
            module ``forward()``. Passing ``concrete_args`` allows partially
            specializing the forward, e.g. to remove control flow or data
            structures. This ``concrete_args`` here is the same argument used
            in :meth:`~torch.fx.Tracer.trace`.
    )Údefault_factoryÚtracerNÚconcrete_args)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtorchÚfxÚTracerr   Ú__annotations__r   r	   ÚdictÚstrr   © ó    úa/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/distributed/fsdp/_trace_utils.pyr   r      s?   … ññ$ $°E·H±H·O±OÔD€FˆE�H‰H�O‰OÓDØ.2€M�8˜D  c ™NÑ+Ô2r   r   c                   ó^   — e Zd ZU dZej
                  ed<   eee	ej                  f      ed<   y)Ú_ParamUsageInfoa6  
    This is used for ``_ExecutionInfo.module_to_param_usage_infos`` to record
    execution information. The ``dict`` maps modules to a list of these
    ``_ParamUsageInfo`` instances, where each instance represents a group of
    parameters used together.

    Specifically, for each module key in the ``dict``, each instance of this
    class represents either:
    (1) the module and some sublist of its ``named_parameters()`` used
    together in execution (see ``_patched_create_proxy()``), or
    (2) a submodule and all of ``submodule.named_parameters()`` (see
    ``_patched_call_module()``).

    Type (1) corresponds to directly using parameters in ops without calling
    ``forward()``, and type (2) corresponds to calling ``forward()``. The
    mapped-to lists in the ``dict`` follow the execution order.
    ÚmoduleÚnamed_paramsN)r   r   r   r   ÚnnÚModuler   ÚlistÚtupler   Ú	Parameterr   r   r   r   r   #   s,   … ñð$ �I‰IÓØ�u˜S "§,¡,Ð.Ñ/Ñ0Ô0r   r   c                   ó4   — e Zd ZdZdej
                  ddfd„Zy)Ú_ExecutionInfoa1  
    This represents the execution order information from the forward pass.

    Attributes:
        curr_module (nn.Module): Current module being traced.
        module_forward_order (List[nn.Module]): The modules in (pre-)forward
            order, i.e. the order in which their ``forward()`` methods are
            called. Each call to a module's ``forward()`` corresponds to one
            element in the list.
        module_to_param_usage_infos (Dict[nn.Module, List[_ParamUsageInfo]]):
            Maps a module to a list of module execution infos. See
            :class:`_ParamUsageInfo` for details.
        param_forward_order (List[nn.Parameter]): The parameters in forward
            execution order, where only a parameter's first participation is
            included.
        visited_params (Set[nn.Parameter]): The parameters visited so far
            during the trace. This is only used during tracing for fast
            membership check. Invariant: The parameters in
            ``param_forward_order`` are exactly those in ``visited_params``.
    Úroot_moduleÚreturnNc                 ó`   — || _         |g| _        |g i| _        g | _        t	        «       | _        y ©N)Úcurr_moduleÚmodule_forward_orderÚmodule_to_param_usage_infosÚparam_forward_orderÚsetÚvisited_params)Úselfr'   s     r   Ú__init__z_ExecutionInfo.__init__P   s8   € Ø&1ˆÔØ6A°]ˆÔ!à˜ðT
ˆÔ(ð 8:ˆÔ Ü14³ˆÕr   )r   r   r   r   r    r!   r2   r   r   r   r&   r&   :   s   „ ñð*7 B§I¡Ið 7°$ô 7r   r&   c                   óö  — e Zd Zdd„Zedej                  j                  dej                  fd„«       Z
deded	ej                  d
edeedf   deeef   defd„Z	 	 	 ddededeeej$                  f   dedej                  j&                  j(                  deedf   deeef   dee   dee   deeej                  j,                  gej                  j.                  f      dej                  j.                  fd„Zy)Ú_ExecOrderTracerr(   Nc                 ó   — d | _         y r*   )Ú	exec_info)r1   s    r   r2   z_ExecOrderTracer.__init__[   s	   € Ø37ˆ�r   r   r'   c              #   óœ  K  — t        |«      | _        |j                  }|j                  }t	        j
                  | j                  || j                  «      |_        t        |j                  «       «      }t	        j
                  | j                  || j                  |«      |_        	 d –— ||_        ||_        y # ||_        ||_        w xY w­wr*   )
r&   r6   Úcall_moduleÚcreate_proxyÚ	functoolsÚpartialÚ_patched_call_moduler   Únamed_parametersÚ_patched_create_proxy)r1   r   r'   Úorig_call_moduleÚorig_create_proxyÚfqn_to_params         r   Úpatch_tracerz_ExecOrderTracer.patch_tracer^   s¸   è ø€ ä'¨Ó4ˆŒØ!×-Ñ-ÐØ"×/Ñ/ÐÜ&×.Ñ.Ø×%Ñ%Ð'7¸¿¹ó
ˆÔô ˜K×8Ñ8Ó:Ó;ˆÜ'×/Ñ/Ø×&Ñ&ØØ�N‰NØó	
ˆÔð	4Ûà!1ˆFÔØ"3ˆFÕøð "2ˆFÔØ"3ˆFÕüs   ‚B#CÂ&B9 Â*CÂ9C	Ã	Cr8   r6   r   ÚforwardÚargs.Úkwargsc                 ól  — |j                   j                  |«       t        |j                  «       «      }|j                  }|rG||j
                  v sJ d«       ‚|j
                  |j                     j                  t        ||«      «       |}	||_        g |j
                  |<    |||||«      }
|	|_        |
S )a  
        Overrides ``call_module`` to save execution information to
        ``exec_info``. Note that ``call_module`` is called during symbolic
        tracing for each non-root module.

        Args:
            call_module (Callable): Original ``call_module`` to override.
            exec_info (_ExecutionInfo): Used to record execution information.
            module (nn.Module): Module corresponding to this ``call_module``.
            forward (Callable): ``forward()`` method of ``module`` to be called
                for this ``call_module``.
            args (Tuple[Any, ...]): Positional arguments for ``forward``.
            kwargs (Dict[str, Any]): Keyword arguments for ``forward``.

        Returns:
            Same return value as ``call_module``.
        zPThe current module should have already been processed by a patched `call_module`)r,   Úappendr"   r=   r+   r-   r   )r1   r8   r6   r   rC   rD   rE   r   r+   Úprev_curr_moduleÚoutputs              r   r<   z%_ExecOrderTracer._patched_call_modules   s¿   € ð6 	×&Ñ&×-Ñ-¨fÔ5Ü˜F×3Ñ3Ó5Ó6ˆØ×+Ñ+ˆÙØ )×"GÑ"GÑGð ØbóÐGð ×1Ñ1°)×2GÑ2GÑH×OÑOÜ ¨Ó5ôð 'ÐØ &ˆ	ÔØ8:ˆ	×-Ñ-¨fÑ5Ù˜V W¨d°FÓ;ˆØ 0ˆ	ÔØˆr   r9   rA   ÚkindÚtargetÚnameÚ	type_exprÚproxy_factory_fnc           	      ód  —  |||||||	|
«      }|j                   }|dv rú|�ög }|D ]Å  }t        |t        j                  j                  «      sŒ(|j
                  j                  |v sŒA||j
                  j                     }|j                  |j
                  j                  |f«       ||j                  vsŒ�|j                  j                  |«       |j                  j                  |«       ŒÇ |r(|j                  |   j                  t        ||«      «       |S |dk(  r’t        |j                  «       «      }|r(|j                  |   j                  t        ||«      «       |D ]J  \  }}||j                  vsŒ|j                  j                  |«       |j                  j                  |«       ŒL |S )a�  
        Overrides ``create_proxy`` to save execution information to
        ``exec_info``. Note that ``create_proxy`` is called during symbolic
        tracing for each leaf function/method/module.

        Args:
            create_proxy (Callable): Original ``create_proxy`` to override.
            exec_info (_ExecutionInfo): Used to record execution information.
            fqn_to_param (Dict[str, nn.Parameter]): ``dict`` version of the
                root module's ``named_parameters()`` with FQN as key and
                parameter as value.
            kind (str): Kind of the target method ('call_function',
                'call_method', 'get_attr', 'call_module', 'placeholder', or
                'output'). See :class:`torch.fx.Graph` for details. This is
                passed to ``create_proxy``.
            target (torch.fx.node.Target): Contains the string name of the
                function/method/module. This is passed to ``create_proxy``.
            args (Tuple[Any, ...]): Positional arguments for the function/
                method/module. This is passed to ``create_proxy``.
            kwargs (Dict[str, Any]): Keyword arguments for the function/method/
                module. This is passed to ``create_proxy``
            name (Optional[str]): An optional string name for the ``Node``
                created in ``create_proxy``. This is passed to
                ``create_proxy``.
            type_expr (Optional[Any]): An optional type annotation representing
                the Python type that the output of the node has. This is passed
                to ``create_proxy``.
            proxy_factory_fn (Callable[[torch.fx.Node], torch.fx.Proxy]):
                An alternative proxy constructor used in ``create_proxy``. This
                is passed to ``create_proxy``.

        Returns:
            torch.fx.Proxy: Created ``Node`` wrapped in a ``Proxy`` object.
        )Úcall_functionÚcall_methodr8   )r+   Ú
isinstancer   r   ÚProxyÚnoderK   rG   r0   Úaddr.   r-   r   r"   r=   )r1   r9   r6   rA   rJ   rK   rD   rE   rL   rM   rN   Úproxyr+   r   ÚargÚparamÚ_s                    r   r>   z&_ExecOrderTracer._patched_create_proxyŸ   sœ  € ñ` Ø�&˜$ ¨¨iÐ9Ió
ˆð  ×+Ñ+ˆØÐ3Ñ3ØÐØ?A�Øò 	H�Cä" 3¬¯©¯©Õ7ØŸH™HŸO™O¨|Ò;à ,¨S¯X©X¯_©_Ñ =˜Ø$×+Ñ+¨S¯X©X¯_©_¸eÐ,DÔEØ ¨	×(@Ñ(@Ò@Ø%×4Ñ4×8Ñ8¸Ô?Ø%×9Ñ9×@Ñ@ÀÕGð	Hñ  Ø×9Ñ9¸+ÑF×MÑMÜ'¨°\ÓBôð ˆð �]Ò"Ü × <Ñ <Ó >Ó?ˆLÙØ×5Ñ5°kÑB×IÑIÜ# K°Ó>ôð )ò @‘��5Ø 	× 8Ñ 8Ò8Ø×,Ñ,×0Ñ0°Ô7Ø×1Ñ1×8Ñ8¸Õ?ð@ð ˆr   )r(   N)NNN)r   r   r   r2   r   r   r   r   r    r!   rB   r   r&   r#   r   r   r   r<   r$   rT   ÚTargetr	   ÚNoderS   r>   r   r   r   r4   r4   Z   sx  „ ó8ð ð4 5§8¡8§?¡?ð 4ÀÇÁò 4ó ð4ð(*àð*ð "ð*ð
 —	‘	ð*ð ð*ð �C˜�H‰oð*ð �S˜#�X‘ð*ð 
ó*ðl #Ø#'ØPTñOàðOð "ðOð ˜3 §¡Ð,Ñ-ð	Oð ðOð —‘—‘×$Ñ$ðOð �C˜�H‰oðOð �S˜#�X‘ðOð �s‰mðOð ˜C‘=ðOð # 8¨U¯X©X¯]©]¨O¸U¿X¹X¿^¹^Ð,KÑ#LÑMðOð 
�‰�‰ôOr   r4   )r:   Ú
contextlibr   Údataclassesr   r   Útypingr   r   r   r	   r   Útorch.nnr    r   r   r&   r4   r   r   r   ú<module>r`      sV   ðã Ý %ß (ß 6Ó 6ã Ý ð ÷3ð 3ó ð3ô.1�jô 1÷.7ñ 7÷@Tò Tr   