U
    §Ê¹j_ó  ã                %   @   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%�r¤e 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.ƒZOe6ePejQj/d/œd0d1„ZRe%�rêd2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMgZSG dNdO„ dOƒZTeSD ]ZUdPdQ„ ZVeWeTeUeVƒ �q–G dRdS„ dSe/eJdT�ZXG dUdV„ dVeXƒZYeYjZ [¡ D ]L\Z\Z]e^e]ƒ�se_e]e`ƒ�s�qàe\ adW¡�sàebeXe\ƒ�r�qàeWeXe\e]ƒ �qàdXdY„ ZcdZd[d\d]d^d_d`dadbdcddddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}h%Zdd~d„ ZeecejQj/ƒD ]N\Z\Zfe\ adW¡�s˜e\ gd€¡�r¼�q˜e\eYjZk�r˜e\edk�r˜eWeYefjheee\ƒƒ �q˜n2G d�dO„ dOƒZTG d‚dS„ dSejQj/ƒZXG dƒdV„ dVeXƒZYd„d…„ Zid†d‡„ Zjdˆd‰„ ZkdªdŠd‹„Zld«eee ee
ee f df dŒœd�dŽ„Zmd�d�„ Znd‘d’„ Zod“d”„ Zpd•d–„ Zqd—d˜„ Zrd™dš„ Zsej<jtZte8etdƒ d¬ePeueuePdœœd�dž„ZvG dŸd „ d ƒZwG d¡d¢„ d¢ƒZxG d£d¤„ d¤ƒZyd¥d¦„ Zzeezd§ƒ eej{d¨ƒ ee1d©ƒ ee2d©ƒ ee3d©ƒ dS )­zßTorchScript

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   s   t  d¡‚d S )Nz ScriptFunction cannot be pickled©ÚpickleÚPickleError©Úcls© r+   úP/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/jit/_script.pyÚ_reduceD   s    r-   Ú	AttributeÚvalueÚtypec                 C   s   | S ©Nr+   )r/   r0   r+   r+   r,   r.   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 r1   )Útype_trace_dbr+   r+   r+   r,   Ú_get_type_trace_dbŸ   s    r3   c                 C   s   t | |d ƒS r1   )Úgetattr)r*   Únamer+   r+   r,   Ú_get_function_from_type¥   s    r6   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 r1   )Ú_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+   rA   r+   r+   r,   rE   Ç   s     z-OrderedDictWrapper.values.<locals>.<listcomp>rF   rH   r+   r+   r,   ÚvaluesÆ   s    zOrderedDictWrapper.valuesc                 C   s   t |  ¡ ƒS r1   )ÚlenrJ   rH   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?   rC   r+   r+   r,   Ú__delitem__Ì   s    zOrderedDictWrapper.__delitem__c                 C   s
   | j  ¡ S r1   )r>   rG   rH   r+   r+   r,   rG   Ï   s    zOrderedDictWrapper.itemsc                 C   s(   || krt d|› �ƒ‚| j ||¡ d S )NzICan't add a new parameter after ScriptModule construction. Tried to add ')rN   r>   Úsetattr©r?   rC   rD   r+   r+   r,   Ú__setitem__Ò   s
    ÿzOrderedDictWrapper.__setitem__c                 C   s   | j  |¡S r1   )r>   ÚcontainsrO   r+   r+   r,   Ú__contains__Ù   s    zOrderedDictWrapper.__contains__c                 C   s   || krt |ƒ‚| j |¡S r1   )ÚKeyErrorr>   r4   rO   r+   r+   r,   Ú__getitem__Ü   s    zOrderedDictWrapper.__getitem__N)Ú__name__Ú
__module__Ú__qualname__r@   rI   rJ   rL   rP   rG   rS   rU   rW   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j |¡¡ || _d S r1   )Úsuperr@   ÚtorchÚ_CÚ
ModuleDictÚ_python_modules)r?   ÚmoduleZpython_dict©r7   r+   r,   r@   ã   s    zOrderedModuleDict.__init__c                 C   s   | j  ¡ }|S r1   )r`   rG   ©r?   Úrr+   r+   r,   rG   í   s    
zOrderedModuleDict.itemsc                 C   s
   || j kS r1   ©r`   rO   r+   r+   r,   rU   ñ   s    zOrderedModuleDict.__contains__c                 C   s<   t |tƒr$| j ||¡ || j|< ntd|› d|› �ƒ‚d S )NzgCannot re-assign modules in a ScriptModule with non-scripted module, tried to replace existing module 'z': )Ú
isinstanceÚScriptModuler>   rQ   r`   rN   rR   r+   r+   r,   rS   ô   s    

ÿzOrderedModuleDict.__setitem__c                 C   s
   | j | S r1   re   rO   r+   r+   r,   rW     s    zOrderedModuleDict.__getitem__)	rX   rY   rZ   r@   rG   rU   rS   rW   Ú__classcell__r+   r+   rb   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ƒrºtƒ  |||¡S tˆ ddd	„ ƒ‰t ˆ¡‡ ‡fd
d„ƒ}|ˆ _tƒ  |||¡ d S )NZ__constants__r+   Ú_methodsÚ_constants_setÚ_disable_script_metaFr@   c                 S   s   d S r1   r+   rH   r+   r+   r,   Ú<lambda>*  ó    z%ScriptMeta.__init__.<locals>.<lambda>c           	         s¬   t ˆ jƒ}ˆ| f|ž|Ž t ˆ jƒ|k}t| ƒˆ kr¨dd„ }tjjj| || d�| jd< | jj	}| 
¡ D ]}t| |ƒ qh| ¡ D ]\}}t| |ƒ q€dD ]}t| |ƒ q˜d S )Nc                 S   s6   t | ƒ}t|dƒr*dd„ t|j ¡ ƒD ƒS t| ƒS d S )Nrj   c                 S   s   g | ]\}}|‘qS r+   r+   rA   r+   r+   r,   rE   7  s     zUScriptMeta.__init__.<locals>.init_then_script.<locals>.make_stubs.<locals>.<listcomp>)r0   r:   Úsortedrj   rG   r   )ra   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)rK   rj   r0   r]   ÚjitÚ
_recursiveÚcreate_script_moduler8   rq   Ú_concrete_typeZget_attributesÚdelattrZget_modules)	r?   ÚargsÚkwargsZnum_methodsZadded_methods_in_initrp   Zconcrete_typer5   Ú_©r*   Zoriginal_initr+   r,   Úinit_then_script,  s(    
	  ÿþÿ	z-ScriptMeta.__init__.<locals>.init_then_script)rj   Úsetr4   rk   ÚreversedrG   Úunionro   rf   r   ry   Zoriginal_methodrX   r\   r@   Ú	functoolsÚwraps)	r*   r5   ÚbasesÚattrsÚbaserC   rD   Zbase_constantsr~   rb   r}   r,   r@     s$    

zScriptMeta.__init__©rX   rY   rZ   r@   rh   r+   r+   rb   r,   ri     s   ri   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__Q  s    z_CachedForward.__get__N)rX   rY   rZ   rŒ   r+   r+   r+   r,   rˆ   P  s   rˆ   c                   @   s   e Zd ZdS )ÚScriptWarningN©rX   rY   rZ   r+   r+   r+   r,   r�   U  s   r�   c                 C   s0   t s| S tjdd�}t| | jdd�}t||| ƒS )Né   ©Z	frames_uprg   )Ú	self_name)r   Ú_jit_internalÚ!createResolutionCallbackFromFramer   rX   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   s
   || _ d S r1   ©Úconst_mapping)r?   rš   r+   r+   r,   r@   n  s    zConstMap.__init__c                 C   s
   | j | S r1   r™   ©r?   Úattrr+   r+   r,   rŠ   q  s    zConstMap.__getattr__N)rX   rY   rZ   r@   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.)rf   Z
zip_readerr]   r^   ZPyTorchFileReaderrN   Ú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__rL   Ú__neg__Ú__mul__rU   Ú__add__Ú__sub__Ú__pow__Ú__truediv__Ú__mod__Ú__ne__Ú__eq__Ú__lt__Ú__gt__Ú__le__Ú__ge__Ú__and__Ú__or__Ú__xor__rW   rS   Ú__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+   )r5   ÚpropertyÚgetterÚsetter)rB   Úpropr+   r+   r,   Ú
<dictcomp>¿  s   ÿ z1RecursiveScriptClass.__init__.<locals>.<dictcomp>F)r\   r@   r8   r>   Z_propertiesÚ_props)r?   Z	cpp_classrb   r+   r,   r@   ¹  s    

þzRecursiveScriptClass.__init__c                    sD   d| j kr | j d r tƒ  |¡S || jkr8| j|  ¡ S t| j|ƒS ©Nr½   )r8   r\   rŠ   rÃ   Úfgetr4   r>   r›   rb   r+   r,   rŠ   Æ  s
    
z RecursiveScriptClass.__getattr__c                    sN   d| j kr"| j d r"tƒ  ||¡S || jkr<| j|  |¡S t| j||ƒ d S rÄ   )r8   r\   Ú__setattr__rÃ   ÚfsetrQ   r>   ©r?   rœ   r/   rb   r+   r,   rÆ   Ï  s
    
z RecursiveScriptClass.__setattr__c                 O   s&   | j  |¡stƒ ‚|  |¡}|||ŽS r1   )r>   Ú_has_methodÚ	TypeErrorrŠ   ©r?   Úmethod_namerz   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&   rH   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__)rX   rY   rZ   Ú__doc__r@   rŠ   rÆ   rÎ   rÏ   rÐ   rh   r+   r+   rb   r,   r¼   ¬  s   	r¼   c                 O   s   | j tf|ž|ŽS r1   )rÎ   rÌ   ©r?   rz   r{   r+   r+   r,   Úmethod_templateì  s    rÔ   c                       s|   e Zd ZU dZdddddg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edœdd„Z‡  ZS )rg   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 ƒ  ¡  d S r1   ©r\   r@   rH   rb   r+   r,   r@     s    úScriptModule.__init__.r‰   c                    s"   d| j krtƒ  |¡S t| j|ƒS )Nrq   )r8   r\   rŠ   r4   rq   r›   rb   r+   r,   rŠ     s    
zScriptModule.__getattr__c                    sZ   d| j krHt|tƒr:d| jj kr(i | j_|j| j|< |j}tƒ  ||¡S t	| j
||ƒ d S )Nrq   Ú__annotations__)r8   rf   r.   r7   rÜ   r0   r/   r\   rÆ   rQ   rq   rÈ   rb   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 )Nrq   r$   r�   )r8   rq   Údefiner’   r“   r]   r^   Z_parse_source_defr   rj   r5   )r?   ÚsrcÚrcbr–   r+   r+   r,   rÝ   %  s
    
zScriptModule.definec                 C   s
   | j  ¡ S r1   )rq   Ú_replicate_for_data_parallelrH   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__)rX   rY   rZ   rÒ   Z__jit_unused_properties__r@   rˆ   r‰   r   r   rÜ   rŠ   rÆ   rÝ   rà   r!   rä   rh   r+   r+   rb   r,   rg   ù  s   
ûrg   )Ú	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)r8   r>   r\   r@   ry   )r?   r¢   rb   r+   r,   r@   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)r=   r]   r^   ÚParameterDictr>   rr   Ú
BufferDictrs   r[   rt   r½   ©rê   r+   r+   r,   rè   †  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+   )rf   r]   r^   ÚScriptMethodrA   r+   r+   r,   rÂ   ¬  s   þ z6RecursiveScriptModule._reconstruct.<locals>.<dictcomp>Fr½   N)r@   r]   r^   ZConcreteModuleTypeZfrom_jit_typer>   Ú_typerx   r_   rG   r   r[   rt   r=   rì   rr   rí   rs   r8   )r?   r¢   Úmodulesr5   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×   rH   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Ø   rH   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Õ   rH   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˜   rc   r+   r+   r,   rÖ   Í  s    z)RecursiveScriptModule.code_with_constantsc                 K   s   | j jt|ƒf|Ž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.
            )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 r1   )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 r1   )r>   Úsave_to_bufferrÓ   r+   r+   r,   rù   ÷  s    z$RecursiveScriptModule.save_to_bufferc                 O   s
   | j  ¡ S r1   )r>   Úget_debug_staterÓ   r+   r+   r,   rú   ú  s    z%RecursiveScriptModule.get_debug_statec                 C   s   d| j › �S )Nzoriginal_name=)rÙ   rH   r+   r+   r,   Ú
extra_reprý  s    z RecursiveScriptModule.extra_reprc                 O   s   | j j| f|ž|ŽS r1   )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Ú )r0   rõ   r>   rð   r5   rH   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_definerx   )r?   rÞ   rß   r+   r+   r,   rÝ   	  s    	zRecursiveScriptModule.definec                    s‚   d| j krtdƒ‚| jr$tƒ  |¡S || jkr8| j| S | j |¡rP| j |¡S | j 	|¡rv| j 
|¡}|| j |< |S tƒ  |¡S )Nr½   zKScriptModule has not been initialized, did you forget to call super's init?)r8   rN   r½   r\   rŠ   rt   r>   r:   r4   rÉ   ró   )r?   rœ   r—   rb   r+   r,   rŠ     s    
ÿ


z!RecursiveScriptModule.__getattr__c                    sŒ   | j rtƒ  ||¡S || jkr*|| j|< n^| j |¡rF| j ||¡ nBt| dƒrz|| j ¡  	¡ krzt
d|› d|› d�ƒ‚ntƒ  ||¡S d S )Nrx   z+Cannot mutate TorchScript constant value: 'z'. Value: 'ú')r½   r\   rÆ   rt   r>   r:   rQ   rx   Zget_constantsrI   ÚAttributeErrorrÈ   rb   r+   r,   rÆ   -  s    
ÿþÿz!RecursiveScriptModule.__setattr__c                 C   s   t jj t | j¡¡S r1   )r]   ru   rv   r   Úcopyr>   rH   r+   r+   r,   Ú__copy__H  s    zRecursiveScriptModule.__copy__c                 C   s   t jj t | j|¡¡S r1   )r]   ru   rv   r   r   Údeepcopyr>   )r?   Úmemor+   r+   r,   Ú__deepcopy__K  s    z"RecursiveScriptModule.__deepcopy__c                 O   s0   t | |ƒ}t |dd ƒt t|ƒkr&tƒ ‚|||ŽS )NÚ__func__)r4   ræ   ÚNotImplementedErrorrË   r+   r+   r,   rÎ   R  s    
 ÿz*RecursiveScriptModule.forward_magic_methodc                 C   s
   |   d¡S )Nr¤   ©rÎ   rH   r+   r+   r,   r¤   Z  s    zRecursiveScriptModule.__iter__c                 C   s   |   d|¡S )NrW   r  )r?   Úidxr+   r+   r,   rW   ]  s    z!RecursiveScriptModule.__getitem__c                 C   s
   |   d¡S )NrL   r  rH   r+   r+   r,   rL   `  s    zRecursiveScriptModule.__len__c                 C   s   |   d|¡S )NrU   r  )r?   Úkeyr+   r+   r,   rU   c  s    z"RecursiveScriptModule.__contains__c                    s&   | j }|jttdƒkr tƒ   ¡ S |ƒ S )NÚ__dir__)r
  r  r6   ræ   r\   ©r?   rÍ   rb   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  r6   ræ   r  r+   r+   r,   r¸   t  s    ÿÿzRecursiveScriptModule.__bool__c                 C   s   dd„ }t  | j ¡ |¡S )Nc                 S   s   d S r1   r+   rî   r+   r+   r,   ré   €  s    zCRecursiveScriptModule._replicate_for_data_parallel.<locals>.init_fn)ræ   rë   r>   rà   )r?   ré   r+   r+   r,   rà   }  s
     ÿz2RecursiveScriptModule._replicate_for_data_parallel)%rX   rY   rZ   rÒ   rl   r@   Ústaticmethodrë   rè   rò   r¾   r×   rØ   rÕ   rÖ   rô   r÷   rø   rù   rú   rû   rü   rÙ   rÝ   rŠ   rÆ   r  r  rÎ   r¤   rW   rL   rU   r
  r¸   rà   rh   r+   r+   rb   r,   ræ   K  sL   	

 





	ræ   Ú__c                    s   dd l ‰ ˆ j| ‡ fdd„d�S )Nr   c                    s   ˆ   | ¡pˆ  | ¡S r1   )Ú
isfunctionÚismethod©Úx©Úinspectr+   r,   rm   œ  rn   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ÚapplyÚ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ÚevalÚtrainZget_extra_stateZset_extra_statec                    s   ‡ fdd„}|S )Nc                    s   t ˆ d ƒ‚d S )Nz" is not supported on ScriptModulesrM   rÓ   ©r5   r+   r,   ÚfailÈ  s    z_make_fail.<locals>.failr+   )r5   r$  r+   r#  r,   Ú
_make_failÇ  s    r%  Z
_call_implc                   @   s   e Zd ZdS )r¼   NrŽ   r+   r+   r+   r,   r¼   Ù  s   c                       s   e Zd Zd‡ fdd„	Z‡  ZS )rg   Nc                    s   t ƒ  ¡  d S r1   rÚ   ©r?   Úargrb   r+   r,   r@   Ý  s    rÛ   )Nr‡   r+   r+   rb   r,   rg   Ü  s   c                       s   e Zd Zd‡ fdd„	Z‡  ZS )ræ   Nc                    s   t ƒ  ¡  d S r1   rÚ   r&  rb   r+   r,   r@   á  s    rç   )Nr‡   r+   r+   rb   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ƒr¼t |t
ƒs¼t	||ƒ||< qZ|||< qZ| ¡ D ]\}}|| j|< qÎ| S )NÚ__prepare_scriptable__rt   )rf   r]   Únnr   Úidr:   r(  r8   rG   Ú!call_prepare_scriptable_func_implrg   )r‹   r  Úobj_idZnew_obj_dictr5   Z
sub_modulerC   rD   r+   r+   r,   r+  å  s,    
 ÿ
r+  c                 C   s   i }t | |ƒS r1   )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 |rÐtƒ at	rÆt
tƒ}t	|ƒ�\ t|tƒr”| ¡ D ]\}}|D ]}||Ž  q‚qvn&t|tƒr²|D ]}	| |	Ž  q¢ntdƒ‚W 5 Q R X n
t d¡ t| tjjƒrüt| ƒ} tjj | tjjj¡S t| dƒ�r|  ¡ n| } t| tƒ�r(t| ƒS t| tƒ�r<t| ƒS t | ¡�rÖt| ƒ}
t | tjjƒ�rpt!d| › d�ƒ‚t | t"j#ƒ�r‚| S t$| ƒ�s”t!dƒ‚t%|  &¡ ƒd	k�r®t!d
ƒ‚|dk�rÆt' (|d ¡}t)| ||
ƒ | S t *| ¡�sît +| ¡�r˜t| ƒ}
t| dƒ�r| j,} t' -| ¡}t| dƒ�r,t!d| j. ƒ‚t/| ƒ t0| ƒ}|�rF|S t1| | j2ƒ}|dk�rft' -| ¡}tj3 4|
||t5| ƒ¡}| j6|_6| |_7t8| |ƒ |S tjj 9| ¡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().__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() insteadzˆError: Unable to infer types. Please format the inputs to type `List[Tuple]` or `Dict[Callable, List[Tuple]]` to be run with MonkeyType.zîWarning: 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Úwarnrf   r¼   rg   ÚScriptFunctionr   r2   r   r   r   rG   r   Ú
ValueErrorr]   r)  r   r-  ru   rv   rw   r   r:   r(  Údictr.  Úlistr/  r  Úisclassr
   Ú
issubclassrN   ÚenumÚEnumr<   rK   Úmror’   r“   r   r  r  Z__original_fnÚ#createResolutionCallbackFromClosurer1  Ú"_check_directly_compile_overloadedr   r   rX   r^   Z_jit_script_compiler   rÒ   Z_torchdynamo_inliner   Zcreate_script_class)r‹   ÚoptimizeZ
_frames_upr•   r0  Zmonkeytype_configra   Zexample_inputÚexampleZexamplesÚqualified_nameZmaybe_already_compiled_fnr–   r”   r+   r+   r,   Úscript,  s®     Nÿ





ÿÿ ÿ
ÿ
ÿÿ



   ÿ
rB  c                 C   s@   |  ¡ D ]2\}}|| ks$| | |krtjj |d|› �¡‚qd S )Nz™Default parameters on overloads do not affect the runtime so they must equal to the default parameter on the implementation function. Found on parameter )rG   r]   ru   ZfrontendZFrontendError)Zimpl_defaultsÚoverload_defaultsÚlocr5   Zoverload_valuer+   r+   r,   Ú_check_overload_defaultsv  s    þrE  c           
      C   sz   t | | jƒ ¡ }tjj | d d t | ¡¡}t ||jƒ}t	| ƒ}t	|ƒ}t
 |¡}t||| ¡ ƒ tj ||||||¡}	|	S r1   )r   rX   Údeclr]   ru   ÚannotationsZget_signaturer  r  r   r’   r=  rE  Úranger^   Z_jit_script_compile_overload)
Úoverload_fnÚ	qual_nameZimpl_fnZoverload_declZoverload_signatureZimpl_astrC  Zimplementation_defaultsr•   r”   r+   r+   r,   Ú_compile_function_with_overload�  s2       ÿ
  ÿúrK  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_overloadsrN   Z,get_overload_no_implementation_error_messageÚappendrK  r   Z_clear_fn_overloads)r‹   Zexisting_compiled_fnsrJ  Zuncompiled_overloadsZcompiled_fnsrI  r+   r+   r,   Ú_get_overloads˜  s&    

ÿ
ÿ

rO  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’   rM  r   rN   )r‹   rJ  r+   r+   r,   r>  ´  s
    
ÿr>  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  r8  rN   r<   r9  r]   r)  r   rK   r<  r
   r’   r“   r   rX   r^   Z_jit_script_interface_compileZ__torch_script_interface__)r‹   Zis_module_interfacerA  rß   r–   Zmangled_classnamer+   r+   r,   Ú	interface¾  s(    
ÿ
   ÿrQ  c                 C   s,   t | ƒ}tj ||¡}t | ¡}t| ||ƒS r1   )r
   r]   r^   Z	CallStackr’   Z'createResolutionCallbackForClassMethodsr   )r‹   rD  Z
_qual_nameZerror_stackrß   r+   r+   r,   Ú_recursive_compile_classÝ  s    
rR  ú ©Ú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+   )rB   r|   ©rX  r+   r,   rE   í  s     zpad.<locals>.<listcomp>)rK   ÚjoinrH  rT  r+   rY  r,   Úpadê  s    r[  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Ú	alignmentrW  c                 C   s   || _ || _|| _i | _d S r1   )r^  r_  rW  Úrows)r?   r^  r_  rW  r+   r+   r,   r@   ñ  s    z_ScriptProfileColumn.__init__)Úlinenor/   c                 C   s   || j |< d S r1   )r`  )r?   ra  r/   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+   )r[  rW  )rB   r	  Úcell©rV  r?   r+   r,   rE     s     z4_ScriptProfileColumn.materialize.<locals>.<listcomp>)
rK   r^  r`  rG   rõ   rN  Úmaxr_  r[  rW  )r?   Ú
max_lengthr`  r	  r/   rc  r+   rd  r,   Úmaterializeú  s    


z _ScriptProfileColumn.materializeN)r]  r   )	rX   rY   rZ   rõ   rã   r@   r   rb  rg  r+   r+   r+   r,   r\  ð  s   r\  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 r1   ri  )r?   rj  rk  r+   r+   r,   r@     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   ú=Ú
)	rj  rg  rN  r6  r[  rK   rk  ÚgetrZ  )
r?   ÚoutputsÚcellsZheader_bufferÚcolr^  r`  ÚlineZ
row_bufferrc  r+   r+   r,   Údump_string  s$    




z_ScriptProfileTable.dump_stringN)rX   rY   rZ   r   r\  rã   r@   rs  r+   r+   r+   r,   rh    s   rh  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 r1   )r	   Z	profilingrt  ÚprofilerH   r+   r+   r,   r@   )  s    z_ScriptProfile.__init__c                 C   s   | j  ¡  d S r1   )ru  ÚenablerH   r+   r+   r,   rv  ,  s    z_ScriptProfile.enablec                 C   s   | j  ¡  d S r1   )ru  ÚdisablerH   r+   r+   r,   rw  /  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š|	 || ¡ ¡ |
 || ¡ ¡ qšt||	|
|gt|ƒƒ}| | ¡ ¡ qd
 |¡S )Nc                 S   s"   g | ]}t |ƒt | d ¡ƒ ‘qS )rS  )rK   Úlstrip©rB   rr  r+   r+   r,   rE   7  s     z._ScriptProfile.dump_string.<locals>.<listcomp>c                    s   g | ]}|ˆ d … ‘qS r1   r+   ry  ©Údedentr+   r,   rE   8  s     zLine #ZHitsz	Time (ns)zLine Contentsr   r$   z

)ru  Z_dump_statsÚsourceÚtextÚ
splitlinesÚminZstarting_linenorK   rH  r\  Zline_maprb  rn  ÚcountZduration_nsrh  r7  rN  rs  rZ  )r?   ro  Úsource_statsZ
source_refZsource_linesÚ
start_lineÚend_linerk  ra  ÚhitsÚtime_nsZline_contentsÚstatsrr  ÚstatÚtabler+   rz  r,   rs  2  s6    


 ÿz_ScriptProfile.dump_stringc                 C   s   t |  ¡ ƒ d S r1   )Úprintrs  rH   r+   r+   r,   ÚdumpP  s    z_ScriptProfile.dumpN)	rX   rY   rZ   r@   rv  rw  rõ   rs  rŠ  r+   r+   r+   r,   rt  (  s
   rt  c                 C   s   | d k	st dƒ‚| S )NzUnwrapping null optional)ÚAssertionErrorr  r+   r+   r,   Ú_unwrap_optionalT  s    rŒ  zaten::_unwrap_optionalzaten::is_scriptingzaten::has_torch_function)N)Nr   NN)r   rS  )|rÒ   Úcollectionsr   r:  r‚   r  r'   r2  Útypingr   r   r   r   r   r   r   r]   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%   r2   r^   rï   rü   r4  r-   Ú
__reduce__Ú
namedtupler.   r3   r6   r<   r=   r[   r0   ri   rˆ   ÚWarningr�   r—   r˜   rõ   r)  r£   Z_magic_methodsr¼   rÌ   rÔ   rQ   rg   ræ   r8   rG   r5   ÚitemÚcallablerf   r¾   Ú
startswithr:   r  Z_compiled_methods_allowlistr%  ÚmethodÚendswithrX   r+  r-  r.  r/  rB  rE  rK  rO  r>  rQ  rR  r    rã   r[  r\  rh  rt  rŒ  Zis_scriptingr+   r+   r+   r,   Ú<module>   s^  $


L#1=	 þä>
R  D	Û(ÿþ$
    ûû  L
	
,


