3
I4W^~V  ã               @   sÆ  d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 ej
ƒ Zd\dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZdd„ Zdd„ Zd]dd„Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zi ad$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Z i a!i Z"d,d-„ Z#d.d/„ Z$yDddl%Z%dd0l%m&Z&m'Z'm(Z(m)Z) d1d2„ Z*d3d4„ Z+d5d6„ Z,d7d8„ Z-W nÎ e.k
�r   G d9d:„ d:eƒZ/G d;d<„ d<eƒZ0G d=d>„ d>eƒZ1G d?d@„ d@eƒZ2G dAdB„ dBeƒZ3G dCdD„ dDeƒZ4G dEdF„ dFeƒZ5G dGdH„ dHeƒZ6e/ƒ Z&e2ƒ Z'e4ƒ Z(e4ƒ Z)dId2„ Z*dJd4„ Z+dKd6„ Z,dLd8„ Z-Y nX y ddl7Z7ddMl7m8Z8 dNdO„ Z9W nD e.k
�r|   G dPdQ„ dQeƒZ:G dRdS„ dSeƒZ;e;ƒ Z8dTdO„ Z9Y nX G dUdV„ dVeƒZ<e<ƒ Z=x$e>dWdXƒD ]Z?e=e@ƒ dYjAe?ƒ< �q W dZd[„ ZBdS )^zº
The weak_script annotation needs to be here instead of inside torch/jit/ so it
can be used in other places in torch/ (namely torch.nn) without running into
circular dependency problems
é    N)Úbuiltins)Úget_source_lines_and_filec                sJ   t jƒ }d}x|| d k r*|j}|d7 }qW |j‰|j‰ ‡ ‡fdd„}|S )a6  
    Creates a function which, given a string variable name,
    returns the value of the variable in the scope of the caller of
    the function which called createResolutionCallback (by default).

    This is used to enable access in-scope Python variables inside
    TorchScript fragments.

    frames_up is number of additional frames to go up on the stack.
    The default value is 0, which correspond to the frame of the caller
    of createResolutionCallback. Also for example, if frames_up is set
    to 1, then the frame of the caller's caller of createResolutionCallback
    will be taken.

    For example, the following program prints 2::

        def bar():
            cb = createResolutionCallback(1)
            print(cb("foo"))

        def baz():
            foo = 2
            bar()

        baz()
    r   é   c                s8   | ˆkrˆ|  S | ˆ kr ˆ |  S t t| ƒr4tt| ƒS d S )N)Úhasattrr   Úgetattr)Úkey)Ú	f_globalsÚf_locals© ú4/tmp/pip-build-e66j97b2/torch/torch/_jit_internal.pyÚenv7   s    
z%createResolutionCallback.<locals>.env)ÚinspectÚcurrentframeÚf_backr	   r   )Z	frames_upÚframeÚir   r
   )r   r	   r   ÚcreateResolutionCallback   s    r   c             C   s>   i }|j | jƒ x(t| jjƒD ]\}}| j| j||< qW |S )zC
    Get a dictionary of closed over variables from a function
    )ÚupdateÚ__globals__Ú	enumerateÚ__code__Úco_freevarsÚ__closure__Úcell_contents)ÚfnÚcapturesÚindexZcaptured_namer
   r
   r   Úget_closureB   s
    r   c                s   t | ƒ‰ ‡ fdd„}|S )z
    Create a resolutionCallback by introspecting the function instead of
    looking up the stack for the enclosing scope
    c                s(   | ˆ krˆ |  S t t| ƒr$tt| ƒS d S )N)r   r   r   )r   )Úclosurer
   r   r   V   s
    

z0createResolutionCallbackFromClosure.<locals>.env)r   )r   r   r
   )r   r   Ú#createResolutionCallbackFromClosureO   s    r   c                s6   t ˆ ƒrdS ‡ fdd„ˆ jD ƒ}dd„ |D ƒ}t|ƒS )NFc                s&   g | ]}t jtˆ |ƒƒrtˆ |ƒ‘qS r
   )r   Ú	isroutiner   )Ú.0Úname)Úclsr
   r   ú
<listcomp>e   s    z%can_compile_class.<locals>.<listcomp>c             S   s   g | ]}t |d ƒ‘qS )r   )r   )r!   r   r
   r
   r   r$   f   s    )Úis_ignored_fnÚ__dict__Úall)r#   ÚfnsZhas_coder
   )r#   r   Úcan_compile_class`   s
    r)   c                s@   ‡fdd„ˆj D ƒ}i ‰ x|D ]}ˆ jt|ƒƒ qW ‡ fdd„S )zš
    This looks at all the methods defined in a class and pulls their closed-over
    variables into a dictionary and uses that to resolve variables.
    c                s&   g | ]}t jtˆ |ƒƒrtˆ |ƒ‘qS r
   )r   r    r   )r!   r"   )r#   r
   r   r$   q   s    z;createResolutionCallbackForClassMethods.<locals>.<listcomp>c                s   ˆ j | d ƒS )N)Úget)r   )r   r
   r   Ú<lambda>w   s    z9createResolutionCallbackForClassMethods.<locals>.<lambda>)r&   r   r   )r#   r(   r   r
   )r   r#   r   Ú'createResolutionCallbackForClassMethodsj   s
    
r,   c       	         s²   ‡ ‡‡‡fdd„}ˆj dkr4ˆj dk	r4ˆj }|ˆ_ nDˆj dkrVˆj dk	rVˆj }|ˆ_ n"ˆj dkrpˆj dkrpd}ntdƒ‚||_ |dk	rŒ||_|dk	rš||_ˆˆˆ |ˆdœt|< |S )zÖ
    Dispatches to either of 2 script functions based on a boolean argument.
    In TorchScript, the boolean argument must be constant so that the correct
    function to use can be determined at compile time.
    c                 sF   d}ˆ|kr|ˆ }nˆ t | ƒk r*| ˆ  }|r8ˆ| |ŽS ˆ| |ŽS d S )NF)Úlen)ÚargsÚkwargsZdispatch_flag)Ú	arg_indexÚarg_nameÚif_falseÚif_truer
   r   r   €   s    

zboolean_dispatch.<locals>.fnNz&only one function can have a docstring)r3   r2   r   Údefaultr1   )Ú__doc__ÚRuntimeErrorÚ
__module__Ú__name__Úboolean_dispatched)	r1   r0   r4   r3   r2   Úmodule_nameÚ	func_namer   Údocr
   )r0   r1   r2   r3   r   Úboolean_dispatchz   s*    r=   c               @   s    e Zd ZdZdZdZdZdZdS )ÚFunctionModifierszn
    Used to denote the behavior of a function in TorchScript. See export() and
    ignore() for details.
    z:unused (ignored and replaced with raising of an exception)z>ignore (leave as a call to Python, cannot be torch.jit.save'd)z7export (compile this function even if nothing calls it)z>default (compile if called from a exported function / forward)N)r8   r7   Ú__qualname__r5   ÚUNUSEDÚIGNOREÚEXPORTÚDEFAULTr
   r
   r
   r   r>   ¨   s
   r>   c             C   s   t j| _| S )a¥  
    This decorator indicates that a method is used as an entry point into a
    ``ScriptModule`` and should be compiled. ``forward`` implicitly is assumbed to be an
    entry point, so it does not need this decorator. Functions and methods
    called from ``forward`` are compiled as they are seen, so they do not need
    this decorator either.

    Example (using ``@torch.jit.export`` on a method):

    .. testcode::

        import torch
        import torch.nn as nn

        class MyModule(nn.Module):
            def implicitly_compiled_method(self, x):
                return x + 99

            # `forward` is implicitly decorated with `@torch.jit.export`,
            # so adding it here would have no effect
            def forward(self, x):
                return x + 10

            @torch.jit.export
            def another_forward(self, x):
                # When the compiler sees this call, it will compile
                # `implicitly_compiled_method`
                return self.implicitly_compiled_method(x)

            def unused_method(self, x):
                return x - 20

        # `m` will contain compiled methods:
        #     `forward`
        #     `another_forward`
        #     `implicitly_compiled_method`
        # `unused_method` will not be compiled since it was not called from
        # any compiled methods and wasn't decorated with `@torch.jit.export`
        m = torch.jit.script(MyModule())
    )r>   rB   Ú_torchscript_modifier)r   r
   r
   r   Úexport³   s    )rE   c             C   s   t j| _| S )a  
    This decorator indicates to the compiler that a function or method should
    be ignored and replaced with the raising of an exception. This allows you
    to leave code in your model that is not yet TorchScript compatible and still
    export your model.

        Example (using ``@torch.jit.unused`` on a method)::

            import torch
            import torch.nn as nn

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

                @torch.jit.unused
                def memory_efficient(self, x):
                    import pdb
                    pdb.set_trace()
                    return x + 10

                def forward(self, x):
                    # Use not-yet-scriptable memory efficient mode
                    if self.use_memory_efficient:
                        return self.memory_efficient(x)
                    else:
                        return x + 10

            m = torch.jit.script(MyModule(use_memory_efficent=False))
            m.save("m.pt")

            m = torch.jit.script(MyModule(use_memory_efficient=True))
            # exception raised
            m(torch.rand(100))
    )r>   r@   rD   )r   r
   r
   r   Úunusedà   s    %rF   Fc                sv   t ˆ ƒrˆ }tj|_|S tˆ tƒs0tdjˆ ƒƒ‚|jddƒ}|rTt	j
dtd� |‰ nˆ rft	j
dtd� ‡ fdd„}|S )	a£  
    This decorator indicates to the compiler that a function or method should
    be ignored and left as a Python function. This allows you to leave code in
    your model that is not yet TorchScript compatible. Models with ignored
    functions cannot be exported; use torch.jit.unused instead.

    Example (using ``@torch.jit.ignore`` on a method)::

        import torch
        import torch.nn as nn

        class MyModule(nn.Module):
            @torch.jit.ignore
            def debugger(self, x):
                import pdb
                pdb.set_trace()

            def forward(self, x):
                x += 10
                # The compiler would normally try to compile `debugger`,
                # but since it is `@ignore`d, it will be left as a call
                # to Python
                self.debugger(x)
                return x

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

        # Error! The call `debugger` cannot be saved since it calls into Python
        m.save("m.pt")

    Example (using ``@torch.jit.ignore(drop=True)`` on a method):

    .. testcode::

        import torch
        import torch.nn as nn

        class MyModule(nn.Module):
            @torch.jit.ignore(drop=True)
            def training_method(self, x):
                import pdb
                pdb.set_trace()

            def forward(self, x):
                if self.training:
                    self.training_method(x)
                return x

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

        # This is OK since `training_method` is not saved, the call is replaced
        # with a `raise`.
        m.save("m.pt")

    .. testcleanup::

        import os
        os.remove('m.pt')
    zEArgument to @torch.jit.ignore must be a bool or a function but got {}Údrop_on_exportNz‰ignore(drop_on_export=True) has been deprecated. TorchScript will now drop the function call on compilation. Use torch.jit.unused now. {})Úcategoryzzignore(True) has been deprecated. TorchScript will now drop the function call on compilation. Use torch.jit.unused now. {}c                s   ˆ rt j| _nt j| _| S )N)r>   r@   rD   rA   )r   )Údropr
   r   Ú	decorator\  s    
zignore.<locals>.decorator)Úcallabler>   rA   rD   Ú
isinstanceÚboolr6   ÚformatÚpopÚwarningsÚwarnÚDeprecationWarning)rI   r/   r   rG   rJ   r
   )rI   r   Úignore  s"    =
rS   c             C   s:   x4t | ƒD ](}t| |ƒ}t|ƒr
t|ƒtjkr
dS q
W dS )NTF)Údirr   rK   Úget_torchscript_modifierr>   rB   )Úmodr"   Úitemr
   r
   r   Úmodule_has_exportse  s    
rX   c             C   s   t | ƒ}|d krdS |tjkS )NF)rU   r>   r@   )r   Úattrr
   r
   r   Úshould_dropm  s    rZ   c             C   s   t | ƒ}|tjkp|tjkS )N)rU   r>   r@   rA   )r   rV   r
   r
   r   r%   t  s    r%   c             C   s*   t | ƒsd S t| dƒr| j} t| dtjƒS )NÚ__func__rD   )rK   r   r[   r   r>   rC   )r   r
   r
   r   rU   y  s
    
rU   c                s   ‡ fdd„}|S )zb
    Decorator to denote that a function returns a list of all the parameters
    in a module
    c                s
   ˆ | _ | S )N)Z_parameter_names_fn)r   )Úparameter_names_fnr
   r   rJ   †  s    z"_parameter_list.<locals>.decoratorr
   )r\   rJ   r
   )r\   r   Ú_parameter_list�  s    r]   c             C   s4   t | ƒ}tj|ƒ}|d kr&g }|t|< |j| ƒ | S )N)Ú_qualified_nameÚ_overloaded_fnsr*   Úappend)ÚfuncÚ	qual_nameZfn_overload_listr
   r
   r   Ú	_overload”  s    

rc   c             C   s
   t j| ƒS )N)r_   r*   )rb   r
   r
   r   Ú_get_fn_overloadsž  s    rd   c             C   s
   t | = d S )N)r_   )rb   r
   r
   r   Ú_clear_fn_overloads¡  s    re   c             C   s8   t jƒ }xtdƒD ]
}|j}qW |jj}|jj}||fS )Né   )r   r   Úranger   Úf_codeÚco_nameÚco_firstlineno)ÚmethodÚcurrent_framer   Ú
class_nameÚline_nor
   r
   r   Úget_class_name_lineno¤  s    
ro   c             C   sŒ   t | ƒ}tj|d ƒ}|d kr(i }|t|< t| ƒ\}}|j|d ƒ}|d krbg }|||< |t||f< nt||f }||kr~tdƒ‚|j| ƒ | S )NzmCannot currently overload the same method name in two different classes with the same name in the same module)r^   Ú_overloaded_methodsr*   ro   Ú_overloaded_method_class_filenor6   r`   )ra   rb   Úclass_name_maprm   rn   Zmethod_overloadsZexisting_linenor
   r
   r   Ú_overload_method¾  s     
rs   c             C   s˜   t | dƒsd S t| ƒ}tj|d ƒ}|d kr.d S |j|jd ƒ}|d krHd S t| ƒd }t|ƒd }|tt|ƒd ƒ }||ko‚||ks”tdt| ƒ ƒ‚|S )Nr8   r   r   zLOverloads are not useable when a module is redeclared within the same file: )	r   r^   rp   r*   r8   r   r-   Ú	ExceptionÚstr)rk   Z	mod_classrb   rr   Z	overloadsZmethod_line_noZmod_class_filenoZmod_end_filenor
   r
   r   Ú_get_overloaded_methodsÕ  s    
rv   )ÚTupleÚListÚDictÚOptionalc             C   s,   | j dko*t| dd ƒtjkp*t| dd ƒtkS )NÚtypingÚ
__origin__)r7   r   r{   rw   Útuple)Úannr
   r
   r   Úis_tupleì  s    
r   c             C   s,   | j dko*t| dd ƒtjkp*t| dd ƒtkS )Nr{   r|   )r7   r   r{   rx   Úlist)r~   r
   r
   r   Úis_listò  s    
r�   c             C   s,   | j dko*t| dd ƒtjkp*t| dd ƒtkS )Nr{   r|   )r7   r   r{   ry   Údict)r~   r
   r
   r   Úis_dict÷  s    
rƒ   c             C   s°   dd„ }d}| j dkrŒt| dd ƒtjkrŒt| df ƒ}t|ƒdkrŒ||d td ƒƒrf||d	 td ƒƒ pŠ||d	 td ƒƒoŠ||d td ƒƒ }| j dko¦t| dd ƒtjk}|p®|S )
Nc             S   s   t j| ƒsdS t| |ƒS )NF)r   ÚisclassÚ
issubclass)Zthe_typeZ
super_typer
   r
   r   Úsafe_is_subclassþ  s    
z%is_optional.<locals>.safe_is_subclassFr{   r|   Ú__args__rf   r   r   )r7   r   r{   ÚUnionr-   Útyperz   )r~   r†   Zunion_optionalr.   Úoptionalr
   r
   r   Úis_optionalü  s    
&&
r‹   c               @   s   e Zd Zdd„ ZdS )ÚTupleClsc             C   s   t |ƒS )N)ÚTupleInstance)ÚselfÚtypesr
   r
   r   Ú__getitem__  s    zTupleCls.__getitem__N)r8   r7   r?   r�   r
   r
   r
   r   rŒ     s   rŒ   c               @   s   e Zd ZdgZdd„ ZdS )r�   r‡   c             C   s
   || _ d S )N)r‡   )rŽ   r�   r
   r
   r   Ú__init__  s    zTupleInstance.__init__N)r8   r7   r?   Ú	__slots__r‘   r
   r
   r
   r   r�     s   r�   c               @   s   e Zd ZdgZdd„ ZdS )ÚListInstancer‡   c             C   s
   || _ d S )N)r‡   )rŽ   r�   r
   r
   r   r‘   %  s    zListInstance.__init__N)r8   r7   r?   r’   r‘   r
   r
   r
   r   r“   "  s   r“   c               @   s   e Zd Zdd„ ZdS )ÚListClsc             C   s   t |ƒS )N)r�   )rŽ   r�   r
   r
   r   r�   )  s    zListCls.__getitem__N)r8   r7   r?   r�   r
   r
   r
   r   r”   (  s   r”   c               @   s   e Zd ZdgZdd„ ZdS )ÚDictInstancer‡   c             C   s
   || _ d S )N)r‡   )rŽ   r�   r
   r
   r   r‘   /  s    zDictInstance.__init__N)r8   r7   r?   r’   r‘   r
   r
   r
   r   r•   ,  s   r•   c               @   s   e Zd Zdd„ ZdS )ÚDictClsc             C   s   t |ƒS )N)r•   )rŽ   r�   r
   r
   r   r�   3  s    zDictCls.__getitem__N)r8   r7   r?   r�   r
   r
   r
   r   r–   2  s   r–   c               @   s   e Zd ZdgZdd„ ZdS )ÚOptionalInstancer‡   c             C   s
   || _ d S )N)r‡   )rŽ   r�   r
   r
   r   r‘   9  s    zOptionalInstance.__init__N)r8   r7   r?   r’   r‘   r
   r
   r
   r   r—   6  s   r—   c               @   s   e Zd Zdd„ ZdS )ÚOptionalClsc             C   s   t |ƒS )N)r—   )rŽ   r�   r
   r
   r   r�   =  s    zOptionalCls.__getitem__N)r8   r7   r?   r�   r
   r
   r
   r   r˜   <  s   r˜   c             C   s
   t | tƒS )N)rL   r�   )r~   r
   r
   r   r   E  s    c             C   s
   t | tƒS )N)rL   r“   )r~   r
   r
   r   r�   H  s    c             C   s
   t | tƒS )N)rL   r•   )r~   r
   r
   r   rƒ   K  s    c             C   s
   t | tƒS )N)rL   r—   )r~   r
   r
   r   r‹   N  s    )ÚFinalc             C   s   | j dkot| dd ƒtjkS )NÚtyping_extensionsr|   )r7   r   rš   r™   )r~   r
   r
   r   Úis_finalV  s    
r›   c               @   s   e Zd ZdgZdd„ ZdS )ÚFinalInstancer‡   c             C   s
   || _ d S )N)r‡   )rŽ   r�   r
   r
   r   r‘   ^  s    zFinalInstance.__init__N)r8   r7   r?   r’   r‘   r
   r
   r
   r   rœ   [  s   rœ   c               @   s   e Zd Zdd„ ZdS )ÚFinalClsc             C   s   t |ƒS )N)rœ   )rŽ   r�   r
   r
   r   r�   b  s    zFinalCls.__getitem__N)r8   r7   r?   r�   r
   r
   r
   r   r�   a  s   r�   c             C   s
   t | tƒS )N)rL   rœ   )r~   r
   r
   r   r›   g  s    c               @   s   e Zd Zdd„ ZdS )ÚBroadcastingListClsc             C   s   d S )Nr
   )rŽ   r�   r
   r
   r   r�   m  s    zBroadcastingListCls.__getitem__N)r8   r7   r?   r�   r
   r
   r
   r   rž   l  s   rž   rf   é   zBroadcastingList{}c             C   sŠ   t | tjjƒr| jS | j}|dkr&d}| j}|dkr:| jS |d krPtdj|ƒƒ‚|dkr^d}nd| }d|kr~td	j||ƒƒ‚|d | S )
Nz<lambda>Z_lambdaztorch._classeszFCould not get qualified name for class '{}': __module__ can't be None.Ú__main__Z	__torch__z
__torch__.Ú.zKCould not get qualified name for class '{}': '{}' is not a valid identifier)	rL   ÚtorchZ_CZFunctionZqualified_namer8   r7   r6   rN   )Úobjr"   r:   r
   r
   r   r^   w  s$    
r^   )r   )F)Cr5   r   ÚweakrefrP   Ztorch._Cr¢   Z
torch._sixr   Ztorch._utils_internalr   ÚWeakKeyDictionaryr9   r   r   r   r)   r,   r=   Úobjectr>   rE   rF   rS   rX   rZ   r%   rU   r]   r_   rc   rd   re   ro   rp   rq   rs   rv   r{   rw   rx   ry   rz   r   r�   rƒ   r‹   ÚImportErrorrŒ   r�   r“   r”   r•   r–   r—   r˜   rš   r™   r›   rœ   r�   rž   ZBroadcastingList1rg   r   ÚglobalsrN   r^   r
   r
   r
   r   Ú<module>   sŠ   
/
.-(
]
