U
    d9                  %   @   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mZmZm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$m%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.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z: ddl;m<Z< e: a=e)ej>j?_@e(ej>jA_@ej>jAZAdeA_ eeAd dd ZBeBeA_CereDdddgZEndd ZEdeE_ dd ZFdd  ZGd!d" ZHG d#d$ d$eIZJG d%d& d&eJZKG d'd( d(eLZMG d)d* d*eIZNG d+d, d,eOZPd-d. ZQG d/d0 d0ZRe4eSejTjd1d2d3ZUerd4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOgZVG dPdQ dQeIZWeVD ]ZXdRdS ZYeZeWeXeY qG dTdU dUe!eMeZ[G dVdW dWe[Z\e\j]^ D ]L\Z_Z`eae`sebe`ecsqe_ddXseee[e_r:qeZe[e_e` qdYdZ Zfd[d\d]d^d_d`dadbdcddddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~h%Zgdd ZhefejTjD ]B\Z_Zie_ddXr̐qe_e\j]kre_egkreZe\eijjehe_ qn4G ddQ dQeIZWG ddU dUejTjZ[G ddW dWe[Z\dd Zkdd Zldd ZmdddZndeee e
eee f df dddZodd Zpdd Zqdd Zrdd Zsdd Ztdd Zuej>jvZveevd deSeweweSdddZxG dd dZyG dd dZzG dd dZ{dd Z|ee|d eej}d ee/d ee0d ee1d dS )zTorchScript

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DictListSetTupleUnionCallable)
set_module)ScriptMethodStubwrap_cpp_moduleinfer_methods_to_compile_compile_and_register_class)Module)_enabled)_register_builtin)with_metaclass)get_jit_defget_default_argsget_jit_class_def)_qualified_name)
_graph_for_script_method_graph_for)_try_get_jit_cached_function_try_get_jit_cached_overloads_set_jit_function_cache_set_jit_overload_cache)has_torch_functionhas_torch_function_unaryhas_torch_function_variadic)PackageExporterPackageImporter   )validate_map_location)monkeytype_traceJitTypeTraceConfigJitTypeTraceStore)classesz
Functionally equivalent to a :class:`ScriptModule`, but represents a single
function and does not have any attributes or Parameters.
z	torch.jitc                 C   s   t dd S )Nz ScriptFunction cannot be pickledpicklePickleErrorcls r,   5/tmp/pip-unpacked-wheel-ua33x9lu/torch/jit/_script.py_reduce=   s    r.   	Attributevaluetypec                 C   s   | S Nr,   )r0   r1   r,   r,   r-   r/   G   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(AttributeModule, self).__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.Annotate`:

    .. testcode::

        import torch
        from typing import Dict

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

            def __init__(self):
                super(AttributeModule, self).__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 r2   )type_trace_dbr,   r,   r,   r-   _get_type_trace_db   s    r4   c                 C   s   t | |d S r2   )getattr)r+   namer,   r,   r-   _get_function_from_type   s    r7   c                 C   s$   t | dr dt| k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   s
   || _ d S r2   )_c)selfr?   r,   r,   r-   __init__   s    zOrderedDictWrapper.__init__c                 C   s   dd |   D S )Nc                 S   s   g | ]\}}|qS r,   r,   .0kvr,   r,   r-   
<listcomp>   s     z+OrderedDictWrapper.keys.<locals>.<listcomp>itemsr@   r,   r,   r-   keys   s    zOrderedDictWrapper.keysc                 C   s   dd |   D S )Nc                 S   s   g | ]\}}|qS r,   r,   rB   r,   r,   r-   rF      s     z-OrderedDictWrapper.values.<locals>.<listcomp>rG   rI   r,   r,   r-   values   s    zOrderedDictWrapper.valuesc                 C   s   t |  S r2   )lenrK   rI   r,   r,   r-   __len__   s    zOrderedDictWrapper.__len__c                 C   s   t dd S )Nz6cannot delete methods or parameters of a script moduleRuntimeErrorr@   rD   r,   r,   r-   __delitem__   s    zOrderedDictWrapper.__delitem__c                 C   s
   | j  S r2   )r?   rH   rI   r,   r,   r-   rH      s    zOrderedDictWrapper.itemsc                 C   s(   || krt d|| j|| d S )NzKCan't add a new parameter after ScriptModule construction. Tried to add '{})rO   formatr?   setattrr@   rD   rE   r,   r,   r-   __setitem__   s    zOrderedDictWrapper.__setitem__c                 C   s   | j |S r2   )r?   containsrP   r,   r,   r-   __contains__   s    zOrderedDictWrapper.__contains__c                 C   s   || krt || j|S r2   )KeyErrorr?   r5   rP   r,   r,   r-   __getitem__   s    zOrderedDictWrapper.__getitem__N)__name__
__module____qualname__rA   rJ   rK   rM   rQ   rH   rU   rW   rY   r,   r,   r,   r-   r>      s   r>   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| tj| || _d S r2   )superr]   rA   torch_C
ModuleDict_python_modules)r@   moduleZpython_dictr8   r,   r-   rA      s    zOrderedModuleDict.__init__c                 C   s   | j  }|S r2   )rb   rH   r@   rr,   r,   r-   rH      s    
zOrderedModuleDict.itemsc                 C   s
   || j kS r2   rb   rP   r,   r,   r-   rW      s    zOrderedModuleDict.__contains__c                 C   s8   t |tr$| j|| || j|< ntd||d S )NznCannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing module '{}': {})
isinstanceScriptModuler?   rS   rb   rO   rR   rT   r,   r,   r-   rU      s    

 zOrderedModuleDict.__setitem__c                 C   s
   | j | S r2   rg   rP   r,   r,   r-   rY      s    zOrderedModuleDict.__getitem__)	rZ   r[   r\   rA   rH   rW   rU   rY   __classcell__r,   r,   rd   r-   r]      s
   
r]   c                       s   e Zd Z fddZ  ZS )
ScriptMetac           	         s  i  _ tt dd _t|D ]D}t|di  D ]\}}| j |< q4t|dt } j| _q t| D ]*\}}t|t	rrt
 | | j |jj< qrt ddrtt |||S t ddd	 t fd
d}| _tt ||| d S )NZ__constants__r,   _methods_constants_set_disable_script_metaFrA   c                 S   s   d S r2   r,   rI   r,   r,   r-   <lambda>       z%ScriptMeta.__init__.<locals>.<lambda>c           	         s   t  j}| f|| t  j|k}t|  krdd }tjjj| || d| jd< | jj	}|
 D ]}t| | qh| D ]\}}t| | qdD ]}t| | qd S )Nc                 S   s6   t | }t|dr*dd t|j D S t| S d S )Nrl   c                 S   s   g | ]\}}|qS r,   r,   rB   r,   r,   r-   rF   -  s     zUScriptMeta.__init__.<locals>.init_then_script.<locals>.make_stubs.<locals>.<listcomp>)r1   r;   sortedrl   rH   r   )rc   r+   r,   r,   r-   
make_stubs*  s    
zAScriptMeta.__init__.<locals>.init_then_script.<locals>.make_stubs)Zshare_types_actual_script_module)_parameters_buffers_modules)rL   rl   r1   r_   jit
_recursivecreate_script_moduler9   rs   _concrete_typeZget_attributesdelattrZget_modules)	r@   argskwargsZnum_methodsZadded_methods_in_initrr   Zconcrete_typer6   _r+   Zoriginal_initr,   r-   init_then_script"  s     
	z-ScriptMeta.__init__.<locals>.init_then_script)rl   setr5   rm   reversedrH   unionrq   rh   r
   r{   Zoriginal_methodrZ   r^   rk   rA   	functoolswraps)	r+   r6   basesattrsbaserD   rE   Zbase_constantsr   rd   r   r-   rA   
  s$    

zScriptMeta.__init__rZ   r[   r\   rA   rj   r,   r,   rd   r-   rk   	  s   rk   c                   @   s   e Zd Zdd ZdS )_CachedForwardc                 C   s
   |  dS )Nforward)__getattr__)r@   objr+   r,   r,   r-   __get__E  s    z_CachedForward.__get__N)rZ   r[   r\   r   r,   r,   r,   r-   r   D  s   r   c                   @   s   e Zd ZdS )ScriptWarningN)rZ   r[   r\   r,   r,   r,   r-   r   I  s   r   c                 C   s0   t s| S tjdd}t| | jdd}t||| S )N   Z	frames_upri   )Z	self_name)r   _jit_internal!createResolutionCallbackFromFramer   rZ   r
   )fn_rcbastr,   r,   r-   script_methodM  s
    r   c                   @   s   e Zd Zdd Zdd ZdS )ConstMapc                 C   s
   || _ d S r2   const_mapping)r@   r   r,   r,   r-   rA   b  s    zConstMap.__init__c                 C   s
   | j | S r2   r   r@   attrr,   r,   r-   r   e  s    zConstMap.__getattr__N)rZ   r[   r\   rA   r   r,   r,   r,   r-   r   a  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.)rh   Z
zip_readerr_   r`   ZPyTorchFileReaderrO   CompilationUnitZ_import_ir_module_from_packageZstorage_contextr"   Zlast_map_locationr   )r   r   Zcu
cpp_moduler,   r,   r-   unpackage_script_modulei  s    
r   __iter__rM   __neg____mul__rW   __add____sub____pow____truediv____mod____ne____eq____lt____gt____le____ge____and____or____xor__rY   rU   __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                    sB   t 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,   )r6   propertygettersetter)rC   propr,   r,   r-   
<dictcomp>  s      z1RecursiveScriptClass.__init__.<locals>.<dictcomp>F)r^   r   rA   r9   r?   Z_properties_props)r@   Z	cpp_classrd   r,   r-   rA     s
    
RecursiveScriptClass.__init__c                    sH   d| j kr$| j d r$tt| |S || jkr<| j|  S t| j|S Nr   )r9   r^   r   r   r   fgetr5   r?   r   rd   r,   r-   r     s
    
z RecursiveScriptClass.__getattr__c                    sR   d| j kr&| j d r&tt| ||S || jkr@| j| |S t| j|| d S r   )r9   r^   r   __setattr__r   fsetrS   r?   r@   r   r0   rd   r,   r-   r     s
    
z RecursiveScriptClass.__setattr__c                 O   s&   | j |st | |}|||S r2   )r?   _has_method	TypeErrorr   r@   method_namer|   r}   self_methodr,   r,   r-   forward_magic_method  s    
z)RecursiveScriptClass.forward_magic_methodc                 C   s   t dd S )NzScriptClasses cannot be pickledr'   rI   r,   r,   r-   __getstate__  s    z!RecursiveScriptClass.__getstate__c                 C   s(   | j dr| d|S | d|S d S )N__iadd__r   )r?   r   r   )r@   otherr,   r,   r-   r     s    zRecursiveScriptClass.__iadd__)rZ   r[   r\   __doc__rA   r   r   r   r   r   rj   r,   r,   rd   r-   r     s   
	r   c                 O   s   | j tf||S r2   )r   r   r@   r|   r}   r,   r,   r-   method_template  s    r   c                       sj   e Zd ZdZdddddgZ fddZe Z fd	d
Z fddZ	dd Z
dd ZedddZ  ZS )ri   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                    s   t t|   d S r2   )r^   ri   rA   rI   rd   r,   r-   rA     s    ScriptModule.__init__c                    s&   d| j krtt| |S t| j|S )Nrs   )r9   r^   ri   r   r5   rs   r   rd   r,   r-   r     s    
zScriptModule.__getattr__c                    s^   d| j krLt|tr:d| jj kr(i | j_|j| j|< |j}tt| 	||S t
| j|| d S )Nrs   __annotations__)r9   rh   r/   r8   r   r1   r0   r^   ri   r   rS   rs   r   rd   r,   r-   r     s    

zScriptModule.__setattr__c                 C   sJ   d| j kr| j|S tjdd}tj|}t||d | j	|
 j
< d S )Nrs   r!   r   )r9   rs   definer   r   r_   r`   Z_parse_source_defr
   rl   r6   )r@   srcrcbr   r,   r,   r-   r     s
    
zScriptModule.definec                 C   s
   | j  S r2   )rs   _replicate_for_data_parallelrI   r,   r,   r-   r   #  s    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	serializer?   intr   )r@   r   r   r,   r,   r-   __reduce_package__&  s    	zScriptModule.__reduce_package__)rZ   r[   r\   r   Z__jit_unused_properties__rA   r   r   r   r   r   r   r   r   rj   r,   r,   rd   r-   ri     s   ri   c                       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|   t| d d S )NTr   Ztraining)r9   r?   r^   r   rA   r{   )r@   r   rd   r,   r-   rA   O  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-   
_constructX  s    
z RecursiveScriptModule._constructc                 C   sB   t tj| j| _t tj| j| _t| j| j	| _	d| _
d S )NF)r>   r_   r`   ParameterDictr?   rt   
BufferDictru   r]   rv   r   r   r,   r,   r-   r   n  s     z,RecursiveScriptModule._finalize_scriptmodulec                 C   s   |  | tjj| j | _i }tj| j	 D ]\}}t
|||< q6t| 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,   )rh   r_   r`   ScriptMethodrB   r,   r,   r-   r     s    z6RecursiveScriptModule._reconstruct.<locals>.<dictcomp>Fr   N)rA   r_   r`   ZConcreteModuleTypeZfrom_jit_typer?   _typerz   ra   rH   r   r]   rv   r>   r   rt   r   ru   r9   )r@   r   modulesr6   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   )r?   _get_methodr   rI   r,   r,   r-   r     s    zRecursiveScriptModule.graphc                 C   s   | 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   rI   r,   r,   r-   r     s    z#RecursiveScriptModule.inlined_graphc                 C   s   | j jS )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   rI   r,   r,   r-   r     s    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   re   r,   r,   r-   r     s    z)RecursiveScriptModule.code_with_constantsc                 K   s   | j jt|f|S )zx
            save(f, _extra_files={})

            See :func:`torch.jit.save <torch.jit.save>` for details.
            )r?   savestr)r@   fr}   r,   r,   r-   r     s    zRecursiveScriptModule.savec                 O   s   | j j||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'.

            )r?   Z_save_for_mobiler   r,   r,   r-   _save_for_lite_interpreter  s    z0RecursiveScriptModule._save_for_lite_interpreterc                 O   s   | j j||S r2   )r?   Z_save_to_buffer_for_mobiler   r,   r,   r-   $_save_to_buffer_for_lite_interpreter  s    z:RecursiveScriptModule._save_to_buffer_for_lite_interpreterc                 O   s   | j j||S r2   )r?   save_to_bufferr   r,   r,   r-   r     s    z$RecursiveScriptModule.save_to_bufferc                 O   s
   | j  S r2   )r?   get_debug_stater   r,   r,   r-   r     s    z%RecursiveScriptModule.get_debug_statec                 C   s   d | jS )Nzoriginal_name={})rR   r   rI   r,   r,   r-   
extra_repr  s    z RecursiveScriptModule.extra_reprc                 O   s   | j j| f||S r2   )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 )r1   r   r?   r   r6   rI   r,   r,   r-   r     s    z#RecursiveScriptModule.original_namec                 C   s"   t jdd}| j| j|| d S )Nr!   r   )r   r   r?   Z_definerz   )r@   r   r   r,   r,   r-   r     s    	zRecursiveScriptModule.definec                    s   d| j krtd| jr(tt| |S || jkr<| j| S | j|rT| j	|S | j
|rz| j|}|| j |< |S tt| |S )Nr   zKScriptModule has not been initialized, did you forget to call super's init?)r9   rO   r   r^   r   r   rv   r?   r;   r5   r   r   )r@   r   r   rd   r,   r-   r     s    



z!RecursiveScriptModule.__getattr__c                    s   | j rtt| ||S || jkr.|| j|< n\| j|rJ| j|| n@t| drx|| j	 
 krxtd||ntt| ||S d S )Nrz   z;Cannot mutate TorchScript constant value: '{}'. Value: '{}')r   r^   r   r   rv   r?   r;   rS   rz   Zget_constantsrJ   AttributeErrorrR   r   rd   r,   r-   r     s"    
 z!RecursiveScriptModule.__setattr__c                 C   s   t jjt| jS r2   )r_   rw   rx   r   copyr?   rI   r,   r,   r-   __copy__0  s    zRecursiveScriptModule.__copy__c                 C   s   t jjt| j|S r2   )r_   rw   rx   r   r   deepcopyr?   )r@   memor,   r,   r-   __deepcopy__3  s    z"RecursiveScriptModule.__deepcopy__c                 O   s0   t | |}t |dd t t|kr&t |||S )N__func__)r5   r   NotImplementedErrorr   r,   r,   r-   r   :  s    
 z*RecursiveScriptModule.forward_magic_methodc                 C   s
   |  dS )Nr   r   rI   r,   r,   r-   r   B  s    zRecursiveScriptModule.__iter__c                 C   s   |  d|S )NrY   r  )r@   idxr,   r,   r-   rY   E  s    z!RecursiveScriptModule.__getitem__c                 C   s
   |  dS )NrM   r  rI   r,   r,   r-   rM   H  s    zRecursiveScriptModule.__len__c                 C   s   |  d|S )NrW   r  )r@   keyr,   r,   r-   rW   K  s    z"RecursiveScriptModule.__contains__c                    s*   | j }|jttdkr$tt|   S | S )N__dir__)r  r  r7   r   r^   r@   r   rd   r,   r-   r  P  s     zRecursiveScriptModule.__dir__c                 C   s    | j }|jttdkrdS | S )Nr   T)r   r  r7   r   r  r,   r,   r-   r   [  s     zRecursiveScriptModule.__bool__c                 C   s   dd }t | j |S )Nc                 S   s   d S r2   r,   r   r,   r,   r-   r   f  s    zCRecursiveScriptModule._replicate_for_data_parallel.<locals>.init_fn)r   r   r?   r   )r@   r   r,   r,   r-   r   c  s
     z2RecursiveScriptModule._replicate_for_data_parallel)%rZ   r[   r\   r   rn   rA   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   rY   rM   rW   r  r   r   rj   r,   r,   rd   r-   r   3  sL   	

 




r   __c                    s   dd l   j|  fdddS )Nr   c                    s     | p | S r2   )
isfunctionismethodxinspectr,   r-   ro     rp   z_get_methods.<locals>.<lambda>)	predicate)r  
getmembersr*   r,   r  r-   _get_methods}  s
     
r  r   Zregister_bufferZregister_parameterZregister_moduleZ
add_module_applyapplyZcudacputofloatdoubleZhalfZ
state_dictZ_save_to_state_dictZload_state_dictZ_load_from_state_dictZ_named_members
parametersZnamed_parametersbuffersZnamed_bufferschildrenZnamed_childrenr   Znamed_modulesZ	zero_gradZshare_memoryZ	_get_namer   Z_slow_forwardZ_tracing_nameevalZtrainZget_extra_stateZset_extra_statec                    s    fdd}|S )Nc                    s   t  d d S )Nz" is not supported on ScriptModulesrN   r   r6   r,   r-   fail  s    z_make_fail.<locals>.failr,   )r6   r  r,   r  r-   
_make_fail  s    r   c                       s   e Zd Z fddZ  ZS )r   c                    s   t    d S r2   r^   rA   rI   rd   r,   r-   rA     s    r   r   r,   r,   rd   r-   r     s   c                       s   e Zd Zd fdd	Z  ZS )ri   Nc                    s   t    d S r2   r!  r@   argrd   r,   r-   rA     s    r   )Nr   r,   r,   rd   r-   ri     s   c                       s   e Zd Zd fdd	Z  ZS )r   Nc                    s   t    d S r2   r!  r"  rd   r,   r-   rA     s    r   )Nr   r,   r,   rd   r-   r     s   c                 C   s   t | tjjs| S t| }||kr.|t|  S t| dr@|  n| } | ||< i }| j D ]j\}}|dkr| D ]\}}t	||||< qr|||< qZt |tjjrt |t
st	||||< qZ|||< qZ| D ]\}}|| j|< q| S )N__prepare_scriptable__rv   )rh   r_   nnr   idr;   r$  r9   rH   !call_prepare_scriptable_func_implri   )r   r   Zobj_idZnew_obj_dictr6   Z
sub_modulerD   rE   r,   r,   r-   r'    s&    

r'  c                 C   s   i }t | |S r2   )r'  )r   r   r,   r,   r-   call_prepare_scriptable_func  s    r(  c                 C   s   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.
    )r_   r`   Z
ScriptDict)r   r,   r,   r-   create_script_dict  s    r)  c                 C   s   t j| S )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.
    )r_   r`   Z
ScriptList)r   Z	type_hintr,   r,   r-   create_script_list  s    r*  )example_inputsc              	   C   s  t s| S |dk	rtd t| tr(| S t| tr6| S t| trD| S |rt at	rt
t}t	|\ t|tr| D ]\}}|D ]}||  qqvn&t|tr|D ]}	| |	  qntdW 5 Q R X n
td t| tjjrt| } tjj| tjjjS t| trt| S t| tr$t| S t| rt| }
t| tjjrVtd | t| t!j"rh| S t#| sztdt$| % dkrtd|dkrt&'|d	 }t(| ||
 | S t)| st*| rxt| }
t+| d
r| j,} t&-| }t+| drtd| j. t/|  t0| }|r,|S t1| | j2}|dkrLt&-| }tj34|
||t5| }| j6|_6t7| | |S tjj8| S d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(MyModule, self).__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(MyModule, self).__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(MyModule, self).__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. zWType '{}' 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: )9r   warningswarnrh   r   ri   ScriptFunctionr%   r3   r#   r$   r   rH   r   
ValueErrorr_   r%  r   r(  rw   rx   ry   r   dictr)  listr*  r  isclassr   
issubclassrO   rR   enumEnumr=   rL   mror   r   r   r  r  r;   Z__original_fn#createResolutionCallbackFromClosurer,  "_check_directly_compile_overloadedr   r   rZ   r`   Z_jit_script_compiler   r   r   Zcreate_script_class)r   optimizeZ
_frames_upr   r+  Zmonkeytype_configrc   Zexample_inputZexampleZexamplesqualified_nameZmaybe_already_compiled_fnr   r   r,   r,   r-   script  s     I






 




   
r<  c                 C   sB   |  D ]4\}}|| ks$| | |krtjj|d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 {name}r  )rH   r_   rw   ZfrontendZFrontendErrorrR   )Zimpl_defaultsoverload_defaultslocr6   Zoverload_valuer,   r,   r-   _check_overload_defaultsN  s    r?  c           
      C   sz   t | | j }tjj| d d t| }t ||j}t	| }t	|}t
|}t|||  tj||||||}	|	S r2   )r   rZ   declr_   rw   annotationsZget_signaturer  r  r   r   r8  r?  ranger`   Z_jit_script_compile_overload)
overload_fn	qual_nameZimpl_fnZoverload_declZoverload_signatureZimpl_astr=  Zimplementation_defaultsr   r   r,   r,   r-   _compile_function_with_overloadY  s2       
  rE  c                 C   s   t | }t| }t|}|d kr&|S | |kr>ttd| g }|D ]}|t|||  qF|rj|| }t| | t	| |S )Nfunction)
r   r   r   _get_fn_overloadsrO   Z,get_overload_no_implementation_error_messageappendrE  r   Z_clear_fn_overloads)r   Zexisting_compiled_fnsrD  Zuncompiled_overloadsZcompiled_fnsrC  r,   r,   r-   _get_overloadsp  s(    
 


rI  c                 C   s,   t | }t|st| r(td|d S )NzFunction {} 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   rG  r   rO   rR   )r   rD  r,   r,   r-   r9    s    r9  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  r3  rO   r=   r4  r_   r%  r   rL   r7  r   r   r   r   rZ   r`   Z_jit_script_interface_compileZ__torch_script_interface__)r   Zis_module_interfacer;  r   r   Zmangled_classnamer,   r,   r-   	interface  s(    

   rK  c                 C   s,   t | }tj||}t| }t| ||S r2   )r   r_   r`   Z	CallStackr   Z'createResolutionCallbackForClassMethodsr   )r   r>  Z
_qual_nameZerror_stackr   r,   r,   r-   _recursive_compile_class  s    
rL   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,   )rC   r~   rR  r,   r-   rF     s     zpad.<locals>.<listcomp>)rL   joinrB  rN  r,   rS  r-   pad  s    rU  c                   @   s8   e Zd ZdeeedddZeedddZd	d
 ZdS )_ScriptProfileColumn   r   )header	alignmentrQ  c                 C   s   || _ || _|| _i | _d S r2   )rX  rY  rQ  rows)r@   rX  rY  rQ  r,   r,   r-   rA     s    z_ScriptProfileColumn.__init__)linenor0   c                 C   s   || j |< d S r2   )rZ  )r@   r[  r0   r,   r,   r-   add_row  s    z_ScriptProfileColumn.add_rowc                    s   t j}g }j D ],\}}t|}|||f tt ||}qjdkrj|j    j 8  nd  fdd|D }tj j	|fS )Nr   c                    s"   g | ]\}}|t | jfqS r,   )rU  rQ  )rC   r  cellrP  r@   r,   r-   rF     s     z4_ScriptProfileColumn.materialize.<locals>.<listcomp>)
rL   rX  rZ  rH   r   rH  maxrY  rU  rQ  )r@   
max_lengthrZ  r  r0   r]  r,   r^  r-   materialize  s    


z _ScriptProfileColumn.materializeN)rW  r   )	rZ   r[   r\   r   r   rA   r   r\  ra  r,   r,   r,   r-   rV    s   rV  c                   @   s,   e Zd Zee ee dddZdd ZdS )_ScriptProfileTablecolssource_rangec                 C   s   || _ || _d S r2   rc  )r@   rd  re  r,   r,   r-   rA     s    z_ScriptProfileTable.__init__c           
      C   s   g }g }d}| j D ]*}| \}}||7 }||t|f q|| |tdt|dd | jD ]N}d}|D ]6\}}||}	|	d kr|tdt|7 }qr||	7 }qr|| qfd|S )Nr   r   =
)	rd  ra  rH  r1  rU  rL   re  getrT  )
r@   outputscellsZheader_buffercolrX  rZ  lineZ
row_bufferr]  r,   r,   r-   dump_string  s$    




z_ScriptProfileTable.dump_stringN)rZ   r[   r\   r   rV  r   rA   rm  r,   r,   r,   r-   rb    s   rb  c                   @   s:   e Zd Zdd Zdd Zdd Zeddd	Zd
d ZdS )_ScriptProfilec                 C   s   t j | _d S r2   )r&   Z	profilingrn  profilerI   r,   r,   r-   rA     s    z_ScriptProfile.__init__c                 C   s   | j   d S r2   )ro  enablerI   r,   r,   r-   rp    s    z_ScriptProfile.enablec                 C   s   | j   d S r2   )ro  disablerI   r,   r,   r-   rq    s    z_ScriptProfile.disable)r   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 ]V}||| |||||   ||}|d k	r|	||  |
||  qt||	|
|gt|}||  qd
|S )Nc                 S   s"   g | ]}t |t |d  qS )rM  )rL   lstriprC   rl  r,   r,   r-   rF     s     z._ScriptProfile.dump_string.<locals>.<listcomp>c                    s   g | ]}| d  qS r2   r,   rs  dedentr,   r-   rF     s     zLine #ZHitsz	Time (ns)zLine Contentsr   r!   z

)ro  Z_dump_statssourcetext
splitlinesminZstarting_linenorL   rB  rV  Zline_mapr\  rh  countZduration_nsrb  r2  rH  rm  rT  )r@   ri  source_statsZ
source_refZsource_linesZ
start_lineZend_linere  r[  hitstime_nsZline_contentsstatsrl  stattabler,   rt  r-   rm    s0    

z_ScriptProfile.dump_stringc                 C   s   t |   d S r2   )printrm  rI   r,   r,   r-   dump$  s    z_ScriptProfile.dumpN)	rZ   r[   r\   rA   rp  rq  r   rm  r  r,   r,   r,   r-   rn    s
   rn  c                 C   s   | d k	st d| S )NzUnwrapping null optional)AssertionErrorr  r,   r,   r-   _unwrap_optional(  s    r  zaten::_unwrap_optionalzaten::is_scriptingzaten::has_torch_function)N)Nr   NN)r   rM  )~r   r   collectionsr5  r  r   r(   r-  typingr   r   r   r   r   r   r   r_   Ztorch._jit_internalr   Ztorch.utilsr	   Ztorch.jit._recursiver
   r   r   r   Ztorch.nnr   Ztorch.jit._stater   Ztorch.jit._builtinsr   Z
torch._sixr   Ztorch.jit.frontendr   r   r   r   Ztorch.jit._fuserr   r   r   r   r   r   Ztorch.overridesr   r   r   Ztorch.packager   r    Z_serializationr"   Ztorch.jit._monkeytype_configr#   r$   r%   Ztorch._classesr&   r3   r`   r   r   r/  r.   
__reduce__
namedtupler/   r4   r7   r=   objectr>   r]   r1   rk   r   Warningr   r   r   r   r%  r   Z_magic_methodsr   r   r   rS   ri   r   r9   rH   r6   itemcallablerh   r   
startswithr;   r  Z_compiled_methods_allowlistr   methodrZ   r'  r(  r)  r*  r<  r?  rE  rI  r9  rK  rL  r   r   rU  rV  rb  rn  r  Zis_scriptingr,   r,   r,   r-   <module>   sX  $


K$1;;
L  B	("
    A

*


