o
    i_                  	   @   s  d Z ddlZddlZddlZddlZddlZddlZddl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lmZ ddlmZmZ ddlmZmZmZ dd	lm Z m!Z!m"Z"m#Z# dd
l$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z: e a;eej<j=_>eej<j?_>ej<j?Z?de?_ e8e?d dd Z@e@e?_Ae%reBdddgZCndd ZCdeC_ dd ZDdd ZEdd  ZFG d!d" d"ZGG d#d$ d$eGZHG d%d& d&eIZJG d'd( d(ZKG d)d* d*eLZMd+d, ZNG d-d. d.ZOd/e6d0ePd1ejQj/fd2d3ZRe%rg d4ZSG d5d6 d6ZTeSD ]ZUd7d8 ZVeWeTeUeV q2G d9d: d:e/eJd;ZXG d<d= d=eXZYeYjZ[ D ]&\Z\Z]e^e]she_e]e`shqWe\ad>stebeXe\rvqWeWeXe\e] qWd?d@ Zch dAZddBdC ZeecejQj/D ]'\Z\Zfe\ad>se\gdDrqe\eYjZvre\edvreWeYefjheee\ qnG dEd6 d6ZTG dFd: d:ejQj/ZXG dGd= d=eXZYdHdI ZidJdK ZjdLdM ZkdqdNdOZl				drdPeee ee
ee f df fdQdRZmdSdT ZndUdV ZodWdX ZpdYdZ Zqd[d\ Zrd]d^ Zsej<jtZte8etd dsd`ePdaeudbeudcePfdddeZvG dfdg dgZwG dhdi diZxG djdk dkZydldm Zzeezdn eej{do ee1dp ee2dp ee3dp dS )tzTorchScript

This module contains functionality to support the JIT's scripting frontend, notably:
    - torch.jit.script

This is not intended to be imported directly; please use the exposed
functionalities in `torch.jit`.
    N)AnyCallableDictListSetTupleUnion)classes)_qualified_name)_register_builtin)
_graph_for_script_method_graph_for)JitTypeTraceConfigJitTypeTraceStoremonkeytype_trace)_compile_and_register_classinfer_methods_to_compileScriptMethodStubwrap_cpp_module)_enabled_set_jit_function_cache_set_jit_overload_cache_try_get_jit_cached_function_try_get_jit_cached_overloads)get_default_argsget_jit_class_defget_jit_def)Module)has_torch_functionhas_torch_function_unaryhas_torch_function_variadic)PackageExporterPackageImporter)
set_module   )validate_map_locationz
Functionally equivalent to a :class:`ScriptModule`, but represents a single
function and does not have any attributes or Parameters.
z	torch.jitc                 C   
   t d)Nz ScriptFunction cannot be pickledpicklePickleErrorcls r,   `/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torch/jit/_script.py_reduceD      
r.   	Attributevaluetypec                 C   s   | S Nr,   )r1   r2   r,   r,   r-   r0   O   s   a  
    This method is a pass-through function that returns `value`, mostly
    used to indicate to the TorchScript compiler that the left-hand side
    expression is a class instance attribute with type of `type`. Note that
    `torch.jit.Attribute` should only be used in `__init__` method of `jit.ScriptModule`
    subclasses.

    Though TorchScript can infer correct type for most Python expressions, there are some cases where
    type inference can be wrong, including:

    - Empty containers like `[]` and `{}`, which TorchScript assumes to be container of `Tensor`
    - Optional types like `Optional[T]` but assigned a valid value of type `T`, TorchScript would assume
      it is type `T` rather than `Optional[T]`

    In eager mode, it is simply a pass-through function that returns `value`
    without other implications.

    Example:

    .. testcode::

        import torch
        from typing import Dict

        class AttributeModule(torch.jit.ScriptModule):
            def __init__(self):
                super().__init__()
                self.foo = torch.jit.Attribute(0.1, float)

                # we should be able to use self.foo as a float here
                assert 0.0 < self.foo

                self.names_ages = torch.jit.Attribute({}, Dict[str, int])
                self.names_ages["someone"] = 20
                assert isinstance(self.names_ages["someone"], int)

        m = AttributeModule()
        # m will contain two attributes
        # 1. foo of type float
        # 2. names_ages of type Dict[str, int]

    .. testcleanup::

        del AttributeModule
        del m

    Note: it's now preferred to instead use type annotations instead of `torch.jit.Attribute`:

    .. testcode::

        import torch
        from typing import Dict

        class AttributeModule(torch.nn.Module):
            names: Dict[str, int]

            def __init__(self):
                super().__init__()
                self.names = {}

        m = AttributeModule()

    .. testcleanup::

        del AttributeModule
        del m

    Args:
        value: An initial value to be assigned to attribute.
        type: A Python type

    Returns:
        Returns `value`
c                   C   s   t S r3   )type_trace_dbr,   r,   r,   r-   _get_type_trace_db      r5   c                 C   s   t | |d S r3   )getattr)r+   namer,   r,   r-   _get_function_from_type      r9   c                 C   s$   t | drdt| v pt | dS d S )N	__class____dict__	__slots__)hasattrdirr*   r,   r,   r-   _is_new_style_class   s   
r@   c                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )OrderedDictWrapperc                 C   
   || _ d S r3   )_c)selfrC   r,   r,   r-   __init__   r/   zOrderedDictWrapper.__init__c                 C      dd |   D S )Nc                 S   s   g | ]\}}|qS r,   r,   .0kvr,   r,   r-   
<listcomp>       z+OrderedDictWrapper.keys.<locals>.<listcomp>itemsrD   r,   r,   r-   keys      zOrderedDictWrapper.keysc                 C   rF   )Nc                 S      g | ]\}}|qS r,   r,   rG   r,   r,   r-   rK      rL   z-OrderedDictWrapper.values.<locals>.<listcomp>rM   rO   r,   r,   r-   values   rQ   zOrderedDictWrapper.valuesc                 C   s   t |  S r3   )lenrS   rO   r,   r,   r-   __len__   r:   zOrderedDictWrapper.__len__c                 C   s   t d)Nz6cannot delete methods or parameters of a script moduleRuntimeErrorrD   rI   r,   r,   r-   __delitem__   s   zOrderedDictWrapper.__delitem__c                 C   
   | j  S r3   )rC   rN   rO   r,   r,   r-   rN      r/   zOrderedDictWrapper.itemsc                 C   s(   || vrt d| | j|| d S )NzICan't add a new parameter after ScriptModule construction. Tried to add ')rW   rC   setattrrD   rI   rJ   r,   r,   r-   __setitem__   s
   zOrderedDictWrapper.__setitem__c                 C   s   | j |S r3   )rC   containsrX   r,   r,   r-   __contains__   r:   zOrderedDictWrapper.__contains__c                 C   s   || vrt || j|S r3   )KeyErrorrC   r7   rX   r,   r,   r-   __getitem__   s   zOrderedDictWrapper.__getitem__N)__name__
__module____qualname__rE   rP   rS   rU   rY   rN   r]   r_   ra   r,   r,   r,   r-   rA      s    rA   c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )OrderedModuleDictc                    s   t  tj| || _d S r3   )superrE   torch_C
ModuleDict_python_modules)rD   moduleZpython_dictr;   r,   r-   rE      s   
zOrderedModuleDict.__init__c                 C   s   | j  }|S r3   )rj   rN   rD   rr,   r,   r-   rN      s   
zOrderedModuleDict.itemsc                 C   s
   || j v S r3   rj   rX   r,   r,   r-   r_      r/   zOrderedModuleDict.__contains__c                 C   s:   t |tr| j|| || j|< d S td| d| )NzgCannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing module 'z': )
isinstanceScriptModulerC   r[   rj   rW   r\   r,   r,   r-   r]      s   

zOrderedModuleDict.__setitem__c                 C   
   | j | S r3   ro   rX   r,   r,   r-   ra     r/   zOrderedModuleDict.__getitem__)	rb   rc   rd   rE   rN   r_   r]   ra   __classcell__r,   r,   rl   r-   re      s    
re   c                       s   e Zd Z fddZ  ZS )
ScriptMetac           	         s   i  _ tt dd _t|D ]"}t|di  D ]	\}}| j |< qt|dt } j| _qt| D ]\}}t|t	rNt
 | | j |jj< q9t ddr]t |||S t ddd	 t fd
d}| _t ||| d S )NZ__constants__r,   _methods_constants_set_disable_script_metaFrE   c                 S      d S r3   r,   rO   r,   r,   r-   <lambda>*  s    z%ScriptMeta.__init__.<locals>.<lambda>c           	         s   t  j}| g|R i | t  j|k}t|  krWdd }tjjj| || d| jd< | jj	}|
 D ]}t| | q7| D ]	\}}t| | qCdD ]	}t| | qOd S d S )Nc                 S   s2   t | }t|drdd t|j D S t| S )Nru   c                 S   rR   r,   r,   rG   r,   r,   r-   rK   7  rL   zUScriptMeta.__init__.<locals>.init_then_script.<locals>.make_stubs.<locals>.<listcomp>)r2   r>   sortedru   rN   r   )rk   r+   r,   r,   r-   
make_stubs4  s   
zAScriptMeta.__init__.<locals>.init_then_script.<locals>.make_stubs)Zshare_types_actual_script_module)_parameters_buffers_modules)rT   ru   r2   rg   jit
_recursivecreate_script_moduler<   r|   _concrete_typeZget_attributesdelattrZget_modules)	rD   argskwargsZnum_methodsZadded_methods_in_initr{   Zconcrete_typer8   _r+   Zoriginal_initr,   r-   init_then_script,  s(   
		z-ScriptMeta.__init__.<locals>.init_then_script)ru   setr7   rv   reversedrN   unionrz   rp   r   r   Zoriginal_methodrb   rf   rE   	functoolswraps)	r+   r8   basesattrsbaserI   rJ   Zbase_constantsr   rl   r   r-   rE     s&   

zScriptMeta.__init__rb   rc   rd   rE   rs   r,   r,   rl   r-   rt     s    rt   c                   @   s   e Zd Zdd ZdS )_CachedForwardc                 C   
   |  dS )Nforward)__getattr__)rD   objr+   r,   r,   r-   __get__Q  r/   z_CachedForward.__get__N)rb   rc   rd   r   r,   r,   r,   r-   r   P  s    r   c                   @      e Zd ZdS )ScriptWarningNrb   rc   rd   r,   r,   r,   r-   r   U      r   c                 C   s0   t s| S tjdd}t| | jdd}t||| S )N   Z	frames_uprq   )	self_name)r   _jit_internal!createResolutionCallbackFromFramer   rb   r   )fn_rcbastr,   r,   r-   script_methodY  s
   r   c                   @   s   e Zd Zdd Zdd ZdS )ConstMapc                 C   rB   r3   const_mapping)rD   r   r,   r,   r-   rE   n  r/   zConstMap.__init__c                 C   rr   r3   r   rD   attrr,   r,   r-   r   q  r/   zConstMap.__getattr__N)rb   rc   rd   rE   r   r,   r,   r,   r-   r   m  s    r   importerscript_module_idreturnc                 C   sH   t | jtjjstdtj }tj|| j| jt	| j
|}t|S )z
    Called by ``torch.package.PackageImporter``'s Pickler's ``persistent_load`` function.
    Performs work of loading and returning a ScriptModule from a ``torch.package`` archive.
    z{Loading ScriptObjects from a PackageImporter created from a directory is not supported. Use a package archive file instead.)rp   Z
zip_readerrg   rh   ZPyTorchFileReaderrW   CompilationUnitZ_import_ir_module_from_packageZstorage_contextr%   Zlast_map_locationr   )r   r   cu
cpp_moduler,   r,   r-   unpackage_script_moduleu  s   
r   )__iter__rU   __neg____mul__r_   __add____sub____pow____truediv____mod____ne____eq____lt____gt____le____ge____and____or____xor__ra   r]   __call____int__	__float____bool____str__	__enter____exit__c                       sP   e Zd ZdZ fddZ fddZ fddZdd	 Zd
d Zdd Z	  Z
S )RecursiveScriptClassa  
        An analogue of RecursiveScriptModule for regular objects that are not modules.
        This class is a wrapper around a torch._C.ScriptObject that represents an instance
        of a TorchScript class and allows it to be used in Python.

        Attributes:
            _c [torch._C.ScriptObject]: The C++ object to which attribute lookups and method
                calls are forwarded.
            _props [Dict[str, property]]: A dictionary of properties fetched from self._c and
                exposed on this wrppaer.
        c                    s>   t    d| jd< || _dd | j D | _d| jd< d S )NT_initializingc                 S   s   i | ]}|j t|j|jqS r,   )r8   propertygettersetter)rH   propr,   r,   r-   
<dictcomp>  s    z1RecursiveScriptClass.__init__.<locals>.<dictcomp>F)rf   rE   r<   rC   Z_properties_props)rD   Z	cpp_classrl   r,   r-   rE     s   

zRecursiveScriptClass.__init__c                    sD   d| j v r| j d rt |S || jv r| j|  S t| j|S Nr   )r<   rf   r   r   fgetr7   rC   r   rl   r,   r-   r     s
   
z RecursiveScriptClass.__getattr__c                    sN   d| j v r| j d rt ||S || jv r| j| |S t| j|| d S r   )r<   rf   __setattr__r   fsetr[   rC   rD   r   r1   rl   r,   r-   r     s
   
z RecursiveScriptClass.__setattr__c                 O   s*   | j |s	t | |}||i |S r3   )rC   _has_method	TypeErrorr   rD   method_namer   r   self_methodr,   r,   r-   forward_magic_method  s   
z)RecursiveScriptClass.forward_magic_methodc                 C   r&   )NzScriptClasses cannot be pickledr'   rO   r,   r,   r-   __getstate__  r/   z!RecursiveScriptClass.__getstate__c                 C   s$   | j dr| d|S | d|S )N__iadd__r   )rC   r   r   )rD   otherr,   r,   r-   r     s   zRecursiveScriptClass.__iadd__)rb   rc   rd   __doc__rE   r   r   r   r   r   rs   r,   r,   rl   r-   r     s    	r   c                 O   s   | j tg|R i |S r3   )r   r   rD   r   r   r,   r,   r-   method_template     r   c                       sv   e Zd ZU dZg dZ fddZe Zede	f e
d<  fddZ fd	d
Zdd Zdd ZdefddZ  ZS )rq   z
        A wrapper around C++ ``torch::jit::Module``. ``ScriptModule``\s
        contain methods, attributes, parameters, and
        constants. These can be accessed the same way as on a normal ``nn.Module``.
        )codecode_with_constantsgraphinlined_graphoriginal_namec                       t    d S r3   rf   rE   rO   rl   r,   r-   rE        ScriptModule.__init__.r   c                    s"   d| j vrt |S t| j|S )Nr|   )r<   rf   r   r7   r|   r   rl   r,   r-   r     s   
zScriptModule.__getattr__c                    sZ   d| j vr$t|trd| jj vri | j_|j| j|< |j}t ||S t	| j
|| d S )Nr|   __annotations__)r<   rp   r0   r;   r   r2   r1   rf   r   r[   r|   r   rl   r,   r-   r     s   

zScriptModule.__setattr__c                 C   sJ   d| j v r| j|S tjdd}tj|}t||d | j	|
 j
< d S )Nr|   r$   r   )r<   r|   definer   r   rg   rh   Z_parse_source_defr   ru   r8   )rD   srcrcbr   r,   r,   r-   r   %  s
   
zScriptModule.definec                 C   rZ   r3   )r|   _replicate_for_data_parallelrO   r,   r,   r-   r   ;  r/   z)ScriptModule._replicate_for_data_parallelexporterc                 C   s&   |  }|j| jt| t|ffS )a}  
            Called by ``torch.package.PackageExporter``'s Pickler's ``persistent_id`` when
            saving TorchScript objects. Performs act of saving a ScriptModule inside of
            a ``torch.package`` archive.

            Returns method to load the ScriptModule from a ``torch.package.PackageImporter``'s
            Pickler's ``persistent_load`` function.
            )Zget_unique_idZscript_module_serializer	serializerC   intr   )rD   r   r   r,   r,   r-   __reduce_package__>  s   	
zScriptModule.__reduce_package__)rb   rc   rd   r   Z__jit_unused_properties__rE   r   r   r   r   r   r   r   r   r   r!   r   rs   r,   r,   rl   r-   rq     s   
 rq   )	metaclassc                       s,  e Zd ZdZdZ fddZedd Zedd Zd	d
 Z	e
dd Ze
dd Ze
dd Ze
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Ze
d!d" Zd#d$ Z fd%d&Z fd'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Z  fd7d8Z!d9d: Z"d;d< Z#  Z$S )=RecursiveScriptModulea#  
        The core data structure in TorchScript is the ``ScriptModule``. It is an
        analogue of torch's ``nn.Module`` and represents an entire model as a tree of
        submodules. Like normal modules, each individual module in a ``ScriptModule`` can
        have submodules, parameters, and methods. In ``nn.Module``\s methods are implemented
        as Python functions, but in ``ScriptModule``\s methods are implemented as
        TorchScript functions, a statically-typed subset of Python that contains all
        of PyTorch's built-in Tensor operations. This difference allows your
        ``ScriptModule``\s code to run without the need for a Python interpreter.

        ``ScriptModule``\s should not be created manually, instead use
        either :func:`tracing <torch.jit.trace>` or :func:`scripting <torch.jit.script>`.
        Tracing and scripting can be applied incrementally and :ref:`composed as necessary <Types>`.

        * Tracing records the tensor operations as executed with a set of example inputs and uses these
          operations to construct a computation graph. You can use the full dynamic behavior of Python with tracing,
          but values other than Tensors and control flow aren't captured in the graph.

        * Scripting inspects the Python code of the model
          and compiles it to TorchScript. Scripting allows the use of many `types`_ of values and supports dynamic control flow.
          Many, but not all features of Python are supported by the compiler, so changes to the source code may be necessary.
        Tc                    s(   d| j d< || _t   t| d d S )NTr   Ztraining)r<   rC   rf   rE   r   )rD   r   rl   r,   r-   rE   g  s   

RecursiveScriptModule.__init__c                 C   s   t | }|| t | |S )a  
            Construct a RecursiveScriptModule that's ready for use. PyTorch
            code should use this to construct a RecursiveScriptModule instead
            of instead of calling `__init__` directly, as it makes sure the
            object is properly finalized (and in the future, we may take
            control of how the RecursiveScriptModule instance is created).

            Args:
                cpp_module:  The C++ Module that will hold the actual state of
                             this RecursiveScriptModule instance.
                init_fn:  Lambda that initializes the RecursiveScriptModule passed to it.
            )r   _finalize_scriptmodule)r   init_fnscript_moduler,   r,   r-   
_constructp  s   
z RecursiveScriptModule._constructc                 C   sB   t tj| j| _t tj| j| _t| j| j	| _	d| _
d S )NF)rA   rg   rh   ParameterDictrC   r}   
BufferDictr~   re   r   r   r   r,   r,   r-   r     s   
z,RecursiveScriptModule._finalize_scriptmodulec                 C   s   |  | tjj| j | _i }tj| j	 D ]
\}}t
|||< qt| j|| _ttj| j| _ttj| j| _dd | j	 D | _d| jd< dS )z
            Re-construct an instance of RecursiveScriptModule using an instance of a C++ module.

            Args:
                cpp_module: The C++ module that this RecursiveScriptModule will be rebuilt around.
            c                 S   s$   i | ]\}}t |tjjs||qS r,   )rp   rg   rh   ScriptMethodrG   r,   r,   r-   r     s    z6RecursiveScriptModule._reconstruct.<locals>.<dictcomp>Fr   N)rE   rg   rh   ZConcreteModuleTypeZfrom_jit_typerC   _typer   ri   rN   r   re   r   rA   r   r}   r   r~   r<   )rD   r   modulesr8   r,   r,   r-   _reconstruct  s   
z"RecursiveScriptModule._reconstructc                 C   s   | j djS )z
            Returns a string representation of the internal graph for the
            ``forward`` method. See :ref:`interpreting-graphs` for details.
            r   )rC   _get_methodr   rO   r,   r,   r-   r     s   zRecursiveScriptModule.graphc                 C      | j jS )z
            Returns a string representation of the internal graph for the
            ``forward`` method. This graph will be preprocessed to inline all function and method calls.
            See :ref:`interpreting-graphs` for details.
            )r   r   rO   r,   r,   r-   r        z#RecursiveScriptModule.inlined_graphc                 C   r  )z
            Returns a pretty-printed representation (as valid Python syntax) of
            the internal graph for the ``forward`` method. See
            :ref:`inspecting-code` for details.
            )r   r   rO   r,   r,   r-   r     r  zRecursiveScriptModule.codec                 C   s   | j j}|d t|d fS )a  
            Returns a tuple of:

            [0] a pretty-printed representation (as valid Python syntax) of
            the internal graph for the ``forward`` method. See `code`.
            [1] a ConstMap following the CONSTANT.cN format of the output in [0].
            The indices in the [0] output are keys to the underlying constant's values.

            See :ref:`inspecting-code` for details.
            r   r$   )r   r   r   rm   r,   r,   r-   r     s   z)RecursiveScriptModule.code_with_constantsc                 K   s   | j jt|fi |S )aO  
            save(f, _extra_files={})

            See :func:`torch.jit.save <torch.jit.save>` witch accepts a file-like object.
            This function, torch.save(), converts the object to a string, treating it as a path.
            DO NOT confuse these two functions when it comes to the 'f' parameter functionality.
            )rC   savestr)rD   fr   r,   r,   r-   r    s   zRecursiveScriptModule.savec                 O      | j j|i |S )az  
            _save_for_lite_interpreter(f)

            Add (or update) the bytecode session to the script model. The updated model is used
            in lite interpreter for mobile applications.

            Args:
                f: a string containing a file name.
                _extra_files: Map from filename to contents which will be stored as part of 'f'.

            )rC   Z_save_for_mobiler   r,   r,   r-   _save_for_lite_interpreter  s   z0RecursiveScriptModule._save_for_lite_interpreterc                 O   r
  r3   )rC   Z_save_to_buffer_for_mobiler   r,   r,   r-   $_save_to_buffer_for_lite_interpreter  rQ   z:RecursiveScriptModule._save_to_buffer_for_lite_interpreterc                 O   r
  r3   )rC   save_to_bufferr   r,   r,   r-   r    rQ   z$RecursiveScriptModule.save_to_bufferc                 O   rZ   r3   )rC   get_debug_stater   r,   r,   r-   r    r/   z%RecursiveScriptModule.get_debug_statec                 C   s   d| j  S )Nzoriginal_name=)r   rO   r,   r,   r-   
extra_repr  r:   z RecursiveScriptModule.extra_reprc                 O   s   | j j| g|R i |S r3   )r   	graph_forr   r,   r,   r-   r     s   zRecursiveScriptModule.graph_forc                 C   s0   t | t| j  krdS t| j  S )N )r2   r  rC   r  r8   rO   r,   r,   r-   r     s   z#RecursiveScriptModule.original_namec                 C   s"   t jdd}| j| j|| d S )Nr$   r   )r   r   rC   Z_definer   )rD   r   r   r,   r,   r-   r   	  s   	zRecursiveScriptModule.definec                    s   d| j vr	td| jrt |S || jv r| j| S | j|r(| j|S | j	|r;| j
|}|| j |< |S t |S )Nr   zKScriptModule has not been initialized, did you forget to call super's init?)r<   rW   r   rf   r   r   rC   r>   r7   r   r  )rD   r   r   rl   r,   r-   r     s   



z!RecursiveScriptModule.__getattr__c                    s   | j r
t ||S || jv r|| j|< d S | j|r%| j|| d S t| dr>|| j 	 v r>t
d| d| dt ||S )Nr   z+Cannot mutate TorchScript constant value: 'z'. Value: '')r   rf   r   r   rC   r>   r[   r   Zget_constantsrP   AttributeErrorr   rl   r,   r-   r   -  s   
z!RecursiveScriptModule.__setattr__c                 C   s   t jjt| jS r3   )rg   r   r   r   copyrC   rO   r,   r,   r-   __copy__H  s   zRecursiveScriptModule.__copy__c                 C   s   t jjt| j|S r3   )rg   r   r   r   r  deepcopyrC   )rD   memor,   r,   r-   __deepcopy__K  r   z"RecursiveScriptModule.__deepcopy__c                 O   s4   t | |}t |dd t t|krt ||i |S )N__func__)r7   r   NotImplementedErrorr   r,   r,   r-   r   R  s   
z*RecursiveScriptModule.forward_magic_methodc                 C   r   )Nr   r   rO   r,   r,   r-   r   Z  r/   zRecursiveScriptModule.__iter__c                 C      |  d|S )Nra   r  )rD   idxr,   r,   r-   ra   ]  r:   z!RecursiveScriptModule.__getitem__c                 C   r   )NrU   r  rO   r,   r,   r-   rU   `  r/   zRecursiveScriptModule.__len__c                 C   r  )Nr_   r  )rD   keyr,   r,   r-   r_   c  r:   z"RecursiveScriptModule.__contains__c                    s&   | j }|jttdkrt   S | S )N__dir__)r  r  r9   r   rf   rD   r   rl   r,   r-   r  h  s   
zRecursiveScriptModule.__dir__c                 C   s    | j }|jttdkrdS | S )Nr   T)r   r  r9   r   r   r,   r,   r-   r   t  s   zRecursiveScriptModule.__bool__c                 C   s   dd }t | j |S )Nc                 S   rx   r3   r,   r   r,   r,   r-   r     r6   zCRecursiveScriptModule._replicate_for_data_parallel.<locals>.init_fn)r   r   rC   r   )rD   r   r,   r,   r-   r   }  s   
z2RecursiveScriptModule._replicate_for_data_parallel)%rb   rc   rd   r   rw   rE   staticmethodr   r   r  r   r   r   r   r   r  r  r  r  r  r  r  r   r   r   r   r  r  r   r   ra   rU   r_   r  r   r   rs   r,   r,   rl   r-   r   K  sN    	

 





	r   __c                    s   dd l   j|  fdddS )Nr   c                    s     | p	 | S r3   )
isfunctionismethodxinspectr,   r-   ry     rL   z_get_methods.<locals>.<lambda>)	predicate)r(  
getmembersr*   r,   r'  r-   _get_methods  s   r+  >%   childrendoubleZ
state_dictZ_tracing_namebuffersZget_extra_stateZnamed_buffersZ_save_to_state_dictZ_load_from_state_dictevalZregister_parameterZnamed_childrenZ	zero_gradZhalfcuda_applyfloatZ
add_moduleZset_extra_stateZ	_get_nametrainr  Zregister_moduleZshare_memoryZnamed_modulesapplyZ_slow_forwardr   cpu
parametersZload_state_dictr  Z_named_memberstor2   Zregister_bufferZnamed_parametersc                    s    fdd}|S )Nc                    s   t  d )Nz" is not supported on ScriptModulesrV   r   r8   r,   r-   fail  r:   z_make_fail.<locals>.failr,   )r8   r9  r,   r8  r-   
_make_fail  s   r:  Z
_call_implc                   @   r   )r   Nr   r,   r,   r,   r-   r     r   c                          e Zd Zd fdd	Z  ZS )rq   Nc                    r   r3   r   rD   argrl   r,   r-   rE     r   r   r3   r   r,   r,   rl   r-   rq         c                       r;  )r   Nc                    r   r3   r   r<  rl   r,   r-   rE     r   r   r3   r   r,   r,   rl   r-   r     r>  c                 C   s   t | tjjs	| S t| }||v r|t|  S t| dr |  n| } | ||< i }| j D ]5\}}|dkrJ| D ]\}}t	||||< q9|||< q-t |tjjr^t |t
s^t	||||< q-|||< q-| D ]	\}}|| j|< qg| S )N__prepare_scriptable__r   )rp   rg   nnr   idr>   r?  r<   rN   !call_prepare_scriptable_func_implrq   )r   r  obj_idZnew_obj_dictr8   Z
sub_modulerI   rJ   r,   r,   r-   rB    s*   

rB  c                 C   s   i }t | |S r3   )rB  )r   r  r,   r,   r-   call_prepare_scriptable_func	  s   
rD  c                 C      t j| S )a  
    Create a ``torch._C.ScriptDict`` instance with the data from ``obj``.

    Args:
        obj (dict): The Python dictionary that is used to initialize the ``ScriptDict``
                    returned by this function.

    Returns:
        An instance of ``torch._C.ScriptDict`` that has the same data as ``obj``
        and can be passed between Python and TorchScript with reference semantics and
        zero copy overhead.
    )rg   rh   Z
ScriptDict)r   r,   r,   r-   create_script_dict  s   rF  c                 C   rE  )a  
    Create a ``torch._C.ScriptList`` instance with the data from ``obj``.
    Args:
        obj (dict): The Python list that is used to initialize the ``ScriptList``
                    returned by this function.
    Returns:
        An instance of ``torch._C.ScriptList`` that has the same data as ``obj``
        and can be passed between Python and TorchScript with reference semantics and
        zero copy overhead.
    )rg   rh   Z
ScriptList)r   Z	type_hintr,   r,   r-   create_script_list  s   rG  example_inputsc                 C   s  t s| S |durtd t| tr| S t| tr| S t| tr"| S |rrt at	rmt
t}t	|3 t|trJ| D ]\}}|D ]}||  qAq;nt|trY|D ]}	| |	  qQntdW d   n1 sgw   Y  ntd t| tjjrt| } tjj| tjjjS t| dr|  n| } t| trt| S t| trt| S t| rt| }
t | tjjrt!d|  dt | t"j#r| S t$| st!dt%| & d	krt!d
|du rt'(|d }t)| ||
 | S t*| st+| rLt| }
t| dr	| j,} t'-| }t| drt!d| j. t/|  t0| }|r#|S t1| | j2}|du r3t'-| }tj34|
||t5| }| j6|_6| |_7t8| | |S tjj9| S )a  
    Scripting a function or ``nn.Module`` will inspect the source code, compile
    it as TorchScript code using the TorchScript compiler, and return a :class:`ScriptModule` or
    :class:`ScriptFunction`. TorchScript itself is a subset of the Python language, so not all
    features in Python work, but we provide enough functionality to compute on
    tensors and do control-dependent operations. For a complete guide, see the
    :ref:`language-reference`.

    Scripting a dictionary or list copies the data inside it into a TorchScript instance than can be
    subsequently passed by reference between Python and TorchScript with zero copy overhead.

    ``torch.jit.script`` can be used as a function for modules, functions, dictionaries and lists
     and as a decorator ``@torch.jit.script`` for :ref:`torchscript-classes` and functions.

    Args:
        obj (Callable, class, or nn.Module):  The ``nn.Module``, function, class type,
                                                  dictionary, or list to compile.
        example_inputs (Union[List[Tuple], Dict[Callable, List[Tuple]], None]): Provide example inputs
            to annotate the arguments for a function or ``nn.Module``.

    Returns:
        If ``obj`` is ``nn.Module``, ``script`` returns
        a :class:`ScriptModule` object. The returned :class:`ScriptModule` will
        have the same set of sub-modules and parameters as the
        original ``nn.Module``. If ``obj`` is a standalone function,
        a :class:`ScriptFunction` will be returned. If ``obj`` is a ``dict``, then
        ``script`` returns an instance of `torch._C.ScriptDict`. If ``obj`` is a ``list``,
        then ``script`` returns an instance of `torch._C.ScriptList`.

    **Scripting a function**
        The ``@torch.jit.script`` decorator will construct a :class:`ScriptFunction`
        by compiling the body of the function.

        Example (scripting a function):

        .. testcode::

            import torch

            @torch.jit.script
            def foo(x, y):
                if x.max() > y.max():
                    r = x
                else:
                    r = y
                return r

            print(type(foo))  # torch.jit.ScriptFunction

            # See the compiled graph as Python code
            print(foo.code)

            # Call the function using the TorchScript interpreter
            foo(torch.ones(2, 2), torch.ones(2, 2))

        .. testoutput::
            :hide:

            ...

    ****Scripting a function using example_inputs**
        Example inputs can be used to annotate a function arguments.

        Example (annotating a function before scripting):

        .. testcode::

            import torch

            def test_sum(a, b):
                return a + b

            # Annotate the arguments to be int
            scripted_fn = torch.jit.script(test_sum, example_inputs=[(3, 4)])

            print(type(scripted_fn))  # torch.jit.ScriptFunction

            # See the compiled graph as Python code
            print(scripted_fn.code)

            # Call the function using the TorchScript interpreter
            scripted_fn(20, 100)

        .. testoutput::
            :hide:

            ...

    **Scripting an nn.Module**
        Scripting an ``nn.Module`` by default will compile the ``forward`` method and recursively
        compile any methods, submodules, and functions called by ``forward``. If a ``nn.Module`` only uses
        features supported in TorchScript, no changes to the original module code should be necessary. ``script``
        will construct :class:`ScriptModule` that has copies of the attributes, parameters, and methods of
        the original module.

        Example (scripting a simple module with a Parameter):

        .. testcode::

            import torch

            class MyModule(torch.nn.Module):
                def __init__(self, N, M):
                    super().__init__()
                    # This parameter will be copied to the new ScriptModule
                    self.weight = torch.nn.Parameter(torch.rand(N, M))

                    # When this submodule is used, it will be compiled
                    self.linear = torch.nn.Linear(N, M)

                def forward(self, input):
                    output = self.weight.mv(input)

                    # This calls the `forward` method of the `nn.Linear` module, which will
                    # cause the `self.linear` submodule to be compiled to a `ScriptModule` here
                    output = self.linear(output)
                    return output

            scripted_module = torch.jit.script(MyModule(2, 3))

        Example (scripting a module with traced submodules):

        .. testcode::

            import torch
            import torch.nn as nn
            import torch.nn.functional as F

            class MyModule(nn.Module):
                def __init__(self):
                    super().__init__()
                    # torch.jit.trace produces a ScriptModule's conv1 and conv2
                    self.conv1 = torch.jit.trace(nn.Conv2d(1, 20, 5), torch.rand(1, 1, 16, 16))
                    self.conv2 = torch.jit.trace(nn.Conv2d(20, 20, 5), torch.rand(1, 20, 16, 16))

                def forward(self, input):
                    input = F.relu(self.conv1(input))
                    input = F.relu(self.conv2(input))
                    return input

            scripted_module = torch.jit.script(MyModule())

        To compile a method other than ``forward`` (and recursively compile anything it calls), add
        the :func:`@torch.jit.export <torch.jit.export>` decorator to the method. To opt out of compilation
        use :func:`@torch.jit.ignore <torch.jit.ignore>` or :func:`@torch.jit.unused <torch.jit.unused>`.

        Example (an exported and ignored method in a module)::

            import torch
            import torch.nn as nn

            class MyModule(nn.Module):
                def __init__(self):
                    super().__init__()

                @torch.jit.export
                def some_entry_point(self, input):
                    return input + 10

                @torch.jit.ignore
                def python_only_fn(self, input):
                    # This function won't be compiled, so any
                    # Python APIs can be used
                    import pdb
                    pdb.set_trace()

                def forward(self, input):
                    if self.training:
                        self.python_only_fn(input)
                    return input * 99

            scripted_module = torch.jit.script(MyModule())
            print(scripted_module.some_entry_point(torch.randn(2, 2)))
            print(scripted_module(torch.randn(2, 2)))

        Example ( Annotating forward of nn.Module using example_inputs)::

            import torch
            import torch.nn as nn
            from typing import NamedTuple

            class MyModule(NamedTuple):
            result: List[int]

            class TestNNModule(torch.nn.Module):
                def forward(self, a) -> MyModule:
                    result = MyModule(result=a)
                    return result

            pdt_model = TestNNModule()

            # Runs the pdt_model in eager model with the inputs provided and annotates the arguments of forward
            scripted_model = torch.jit.script(pdt_model, example_inputs={pdt_model: [([10, 20, ], ), ], })

            # Run the scripted_model with actual inputs
            print(scripted_model([20]))
    Nz]`optimize` is deprecated and has no effect. Use `with torch.jit.optimized_execution() insteadzError: Unable to infer types. Please format the inputs to type `List[Tuple]` or `Dict[Callable, List[Tuple]]` to be run with MonkeyType.zWarning: monkeytype is not installed. Please install https://github.com/Instagram/MonkeyType to enable Profile-Directed Typing in TorchScript. Refer to https://github.com/Instagram/MonkeyType/blob/master/README.rst to install MonkeyType. r?  zType 'zO' cannot be compiled since it inherits from nn.Module, pass an instance insteadzLTorchScript classes must be new-style classes. Please inherit from 'object'.r   z\TorchScript classes does not support inheritance yet. Please directly inherit from 'object'.r$   Z__script_if_tracing_wrapper__script_unsupportedzTorchScript error: ):r   warningswarnrp   r   rq   ScriptFunctionr   r4   r   r   r   rN   r   
ValueErrorrg   r@  r   rD  r   r   r   r   r>   r?  dictrF  listrG  r(  isclassr
   
issubclassrW   enumEnumr@   rT   mror   r   r   r#  r$  Z__original_fn#createResolutionCallbackFromClosurerI  "_check_directly_compile_overloadedr   r   rb   rh   Z_jit_script_compiler   r   Z_torchdynamo_inliner   Zcreate_script_class)r   optimizeZ
_frames_upr   rH  Zmonkeytype_configrk   Zexample_inputZexampleZexamplesqualified_nameZmaybe_already_compiled_fnr   r   r,   r,   r-   script,  s    N
















rY  c                 C   s@   |  D ]\}}|| vs| | |krtjj|d| qd S )NzDefault parameters on overloads do not affect the runtime so they must equal to the default parameter on the implementation function. Found on parameter )rN   rg   r   ZfrontendZFrontendError)Zimpl_defaultsoverload_defaultslocr8   Zoverload_valuer,   r,   r-   _check_overload_defaultsv  s   r\  c           
      C   sz   t | | j }tjj| d d t| }t ||j}t	| }t	|}t
|}t|||  tj||||||}	|	S r3   )r   rb   declrg   r   annotationsZget_signaturer(  r$  r   r   rU  r\  rangerh   Z_jit_script_compile_overload)
overload_fn	qual_nameZimpl_fnZoverload_declZoverload_signatureZimpl_astrZ  Zimplementation_defaultsr   r   r,   r,   r-   _compile_function_with_overload  s(   

rb  c                 C   s   t | }t| }t|}|d u r|S | |v rttd| g }|D ]}|t|||  q#|r5|| }t| | t	| |S )Nfunction)
r   r
   r   _get_fn_overloadsrW   Z,get_overload_no_implementation_error_messageappendrb  r   Z_clear_fn_overloads)r   Zexisting_compiled_fnsra  Zuncompiled_overloadsZcompiled_fnsr`  r,   r,   r-   _get_overloads  s&   




rf  c                 C   s.   t | }t|st| rtd| dd S )Nz	Function z cannot be directly compiled because it is overloaded. It must be used in a context of a function where its inputs can determine which overload to call.)r
   r   rd  r   rW   )r   ra  r,   r,   r-   rV    s   
rV  c                 C   s   t | s	tdt| stdt| tjjot| 	 dk}|s.t| 	 dkr.tdt
| }td}t| | j}tj||||}|| _| S )Nz$interface must be applied to a classz1TorchScript interfaces must inherit from 'object'   r   zmTorchScript interface does not support inheritance yet. Please directly inherit from 'object' or 'nn.Module'.r$   )r(  rP  rW   r@   rQ  rg   r@  r   rT   rT  r
   r   r   r   rb   rh   Z_jit_script_interface_compileZ__torch_script_interface__)r   Zis_module_interfacerX  r   r   Zmangled_classnamer,   r,   r-   	interface  s"   

rh  c                 C   s,   t | }tj||}t| }t| ||S r3   )r
   rg   rh   Z	CallStackr   Z'createResolutionCallbackForClassMethodsr   )r   r[  Z
_qual_nameZerror_stackr   r,   r,   r-   _recursive_compile_class  s   
ri   spaddingoffsetcharc                    s<   |t | kr|t | 8 }d fddt|| D |  S )Nr  c                    s   g | ]} qS r,   r,   )rH   r   rn  r,   r-   rK     s    zpad.<locals>.<listcomp>)rT   joinr_  )rk  rl  rm  rn  r,   ro  r-   pad  s   $rq  c                   @   s>   e Zd ZddededefddZded	efd
dZdd ZdS )_ScriptProfileColumn   r   header	alignmentrm  c                 C   s   || _ || _|| _i | _d S r3   )rt  ru  rm  rows)rD   rt  ru  rm  r,   r,   r-   rE     s   
z_ScriptProfileColumn.__init__linenor1   c                 C   s   || j |< d S r3   )rv  )rD   rw  r1   r,   r,   r-   add_row  r   z_ScriptProfileColumn.add_rowc                    s   t j}g }j D ]\}}t|}|||f tt ||}qjdkr5|j    j 8  nd  fdd|D }tj j	|fS )Nr   c                    s"   g | ]\}}|t | jfqS r,   )rq  rm  )rH   r  cellrl  rD   r,   r-   rK        " z4_ScriptProfileColumn.materialize.<locals>.<listcomp>)
rT   rt  rv  rN   r  re  maxru  rq  rm  )rD   
max_lengthrv  r  r1   ry  r,   rz  r-   materialize  s   


z _ScriptProfileColumn.materializeN)rs  r   )	rb   rc   rd   r  r   rE   r   rx  r~  r,   r,   r,   r-   rr    s    rr  c                   @   s.   e Zd Zdee dee fddZdd ZdS )_ScriptProfileTablecolssource_rangec                 C   s   || _ || _d S r3   )r  r  )rD   r  r  r,   r,   r-   rE     s   
z_ScriptProfileTable.__init__c           
      C   s   g }g }d}| j D ]}| \}}||7 }||t|f q	|| |tdt|dd | jD ]'}d}|D ]\}}||}	|	d u rP|tdt|7 }q9||	7 }q9|| q3d|S )Nr  r   =
)	r  r~  re  rN  rq  rT   r  getrp  )
rD   outputscellsZheader_buffercolrt  rv  lineZ
row_bufferry  r,   r,   r-   dump_string  s$   





z_ScriptProfileTable.dump_stringN)rb   rc   rd   r   rr  r   rE   r  r,   r,   r,   r-   r    s    r  c                   @   s:   e Zd Zdd Zdd Zdd Zdefdd	Zd
d ZdS )_ScriptProfilec                 C   s   t j | _d S r3   )r	   Z	profilingr  profilerO   r,   r,   r-   rE   )     z_ScriptProfile.__init__c                 C      | j   d S r3   )r  enablerO   r,   r,   r-   r  ,  r   z_ScriptProfile.enablec                 C   r  r3   )r  disablerO   r,   r,   r-   r  /  r   z_ScriptProfile.disabler   c                    s   g }| j  D ]}| }|  }tdd |D   fdd|D }| }|t| }t||}t	d}t	d}	t	d}
t	ddd	}|
 }|D ]+}||| |||||   ||}|d urw|	||  |
||  qLt||	|
|gt|}||  qd
|S )Nc                 S   s"   g | ]}t |t |d  qS )rj  )rT   lstriprH   r  r,   r,   r-   rK   7  r{  z._ScriptProfile.dump_string.<locals>.<listcomp>c                    s   g | ]}| d  qS r3   r,   r  dedentr,   r-   rK   8  s    zLine #ZHitsz	Time (ns)zLine Contentsr   r$   z

)r  Z_dump_statssourcetext
splitlinesminZstarting_linenorT   r_  rr  Zline_maprx  r  countZduration_nsr  rO  re  r  rp  )rD   r  source_statsZ
source_refZsource_lines
start_lineend_liner  rw  hitstime_nsZline_contentsstatsr  stattabler,   r  r-   r  2  s6   


z_ScriptProfile.dump_stringc                 C   s   t |   d S r3   )printr  rO   r,   r,   r-   dumpP  r  z_ScriptProfile.dumpN)	rb   rc   rd   rE   r  r  r  r  r  r,   r,   r,   r-   r  (  s    r  c                 C   s   | d usJ d| S )NzUnwrapping null optionalr,   r%  r,   r,   r-   _unwrap_optionalT  s   r  zaten::_unwrap_optionalzaten::is_scriptingzaten::has_torch_functionr3   )Nr   NN)r   rj  )|r   collectionsr  rR  r   r(  r(   rJ  typingr   r   r   r   r   r   r   rg   Ztorch._jit_internalr   Ztorch._classesr	   r
   Ztorch.jit._builtinsr   Ztorch.jit._fuserr   r   Ztorch.jit._monkeytype_configr   r   r   Ztorch.jit._recursiver   r   r   r   Ztorch.jit._stater   r   r   r   r   Ztorch.jit.frontendr   r   r   Ztorch.nnr   Ztorch.overridesr   r   r    Ztorch.packager!   r"   Ztorch.utilsr#   Z_serializationr%   r4   rh   r   r  rL  r.   
__reduce__
namedtupler0   r5   r9   r@   rA   re   r2   rt   r   Warningr   r   r   r  r@  r   Z_magic_methodsr   r   r   r[   rq   r   r<   rN   r8   itemcallablerp   r   
startswithr>   r+  Z_compiled_methods_allowlistr:  methodendswithrb   rB  rD  rF  rG  rY  r\  rb  rf  rV  rh  ri  r   r   rq  rr  r  r  r  Zis_scriptingr,   r,   r,   r-   <module>   s    $


L#1=
>
R  D(
$

  L
	
,


