U
    §Ê¹jM¦  ã                   @   sB  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 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mZ d dlmZmZmZmZ d dlmZ e   d	d
¡Z!e   dd¡Z"ddddddddddddddddddgZ#dd „ Z$d!d"„ Z%d#d$„ Z&d%d&„ Z'd'd(„ Z(e)e*e+e,e-dƒej.ej/ej0fZ1d)d*„ Z2G d+d,„ d,ej3j4j5ƒZ6d-d.„ Z7dad0d1„Z8G d2d3„ d3ƒZ9e9ƒ Z:d4d5„ Z;d6d7„ Z<dbd8d9„Z=d:d;„ Z>dcd=d>„Z?d?d@„ Z@dAdB„ ZAdCdD„ ZBdEdF„ ZCdGdH„ ZDdIdJ„ ZEdKdL„ ZFdMdN„ ZGdOdP„ ZHdQdR„ ZIdSdT„ ZJdUdV„ ZKdWdX„ ZLdYdZ„ ZMd[d\„ ZNd]d^„ ZOd_d`„ ZPdS )dé    N)ÚDictÚListÚSetÚType)Ú
fake_range)Ú_find_builtin)ÚAttributeTypeIsSupportedChecker)Ú_add_script_classÚ_get_script_classÚ
_python_cu)Úget_class_propertiesÚget_default_argsÚget_jit_class_defÚget_jit_def)ÚModuleÚScriptMethodStub)Úresolution_callbackÚdef_Úoriginal_methodÚPropertyStub)r   r   Ú_versionÚ_parametersÚ_buffersZ_non_persistent_buffers_setZ_backward_hooksZ_backward_pre_hooksÚ_forward_hooksZ_forward_hooks_with_kwargsÚ_forward_pre_hooksZ_forward_pre_hooks_with_kwargsZ_forward_hooks_always_calledZ_state_dict_hooksZ_state_dict_pre_hooksZ_load_state_dict_pre_hooksZ_load_state_dict_post_hooksÚ_modulesÚ_initializingZdump_patchesc                 C   sF   t | ƒ}|sBt| | jƒ}tjj | ¡}tj ||||¡}t	| |ƒ |S ©N)
r
   r   Ú__name__ÚtorchÚjitZfrontendZget_default_args_for_classÚ_CZ_jit_script_class_compiler	   )ÚobjÚrcbZqualified_nameZscript_classÚastÚdefaults© r&   úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/jit/_recursive.pyÚ_compile_and_register_class7   s       ÿ
r(   c                 C   s$   t  | ¡}t| |dd�}t||| ƒS ©NÚRecursiveScriptModule©Ú	self_name)Ú_jit_internalÚ#createResolutionCallbackFromClosurer   r   )ÚfuncÚnamer#   r$   r&   r&   r'   Ú	make_stubE   s    
r1   c                 C   s"   t | |ƒ}t|tƒr|S t||ƒS r   )ÚgetattrÚ
isinstancer   r1   )Ú	nn_moduleÚmethod_namer/   r&   r&   r'   Úmake_stub_from_methodK   s    

	r6   c                 C   sD   g }t | ƒD ]2}t| |d ƒ}t |¡tjjkr| t| |ƒ¡ q|S r   )Údirr2   r-   Úget_torchscript_modifierÚFunctionModifiersÚEXPORTÚappendr6   )ÚmodÚstubsr0   Úitemr&   r&   r'   Ú make_stubs_from_exported_methodsZ   s    ÿÿr?   c                 C   sH   t | dtƒ ƒ}dd„ }|t| ƒƒ}tƒ }|D ]}||kr,| |¡ q,|S )NÚ__jit_ignored_attributes__c                 S   s   dd„ t | ƒ ¡ D ƒS )Nc                 S   s   h | ]\}}t |tƒr|’qS r&   )r3   Úproperty)Ú.0ÚkÚvr&   r&   r'   Ú	<setcomp>m   s     
 zGjit_ignored_properties.<locals>.get_properties_names.<locals>.<setcomp>)ÚvarsÚitems)Úmoduler&   r&   r'   Úget_properties_namesl   s    z4jit_ignored_properties.<locals>.get_properties_names)r2   ÚlistÚtypeÚsetÚadd)rH   Ú!user_annotated_ignored_attributesrI   Ú
propertiesZuser_annoted_ignored_propertiesZignored_attrr&   r&   r'   Újit_ignored_propertiesg   s      ÿrP   c                    s~   t |tƒr|S t |ttfƒr4t‡ ‡fdd„|D ƒƒS d dd„ tD ƒ¡}tt dt 	t
|ƒ¡› dˆ› dˆ › d|› d	�	¡ƒ‚d S )
Nc                 3   s   | ]}t ˆ |ˆƒV  qd S r   )Ú_get_valid_constant)rB   Úx©ÚattrÚ
owner_typer&   r'   Ú	<genexpr>Œ   s     z&_get_valid_constant.<locals>.<genexpr>z, c                 s   s   | ]}t  |¡V  qd S r   )r   Útypename)rB   Útypr&   r&   r'   rV   �   s     z

        'z' object in attribute 'Ú.zo' is not a valid constant.
        Valid constants are:
        1. a nn.ModuleList
        2. a value of type {z,}
        3. a list or tuple of (2)
        )r3   Ú_constant_typesÚtuplerJ   ÚjoinÚ	TypeErrorÚtextwrapÚdedentr   rW   rK   )rT   rD   rU   Ú	constantsr&   rS   r'   rQ   ˆ   s$    
ÿÿÿüÿÿrQ   c                       s   e Zd Z‡ fdd„Z‡  ZS )ÚSourceContextc                    s   t ƒ  ||||¡ d S r   )ÚsuperÚ__init__)ÚselfÚsourceÚfilenameZfile_linenoZleading_whitespace_len©Ú	__class__r&   r'   rc   œ   s    zSourceContext.__init__)r   Ú
__module__Ú__qualname__rc   Ú__classcell__r&   r&   rg   r'   ra   ›   s   ra   c                    sR   t jdk rt| di ƒS t | ¡}|r(|S ‡ fdd„‰ t| tƒrB| nt| ƒ}ˆ |ƒS )N)é   é
   Ú__annotations__c                    s6   t  | ¡}|r|S | jD ]}ˆ |ƒ}|r|  S qi S r   )ÚinspectÚget_annotationsÚ	__bases__)ÚclsÚcls_annotationsÚbase©Úget_cls_annotationsr&   r'   rv   «   s    


z,get_annotations.<locals>.get_cls_annotations)ÚsysÚversion_infor2   ro   rp   r3   rK   )r"   Úannotationsrr   r&   ru   r'   rp       s    


rp   Tc                    sH  t j t| ƒ¡}t| t jjƒr&| ¡  t| t jjt jj	fƒrD| 
¡  t| t jjƒrZ| ¡  t| t jjƒrp| ¡  t| ƒ‰ t| t jjjƒrŒi ‰ t| dtƒ ƒ}| |¡ t| ƒ}‡ fdd„}tƒ }| j ¡ D ]V\}}||krÚqÈ|dksòt|t jƒsòt‚|||ƒ\}	}
| ||	 ¡ dd¡ | |¡ qÈ| j ¡ D ]`\}}||k�r@�q*|dk�s\t|t jƒ�s\t‚|||ƒ\}	}
| ||	 ¡ dd¡ | |¡ �q*| j ¡ D ]˜\}}||k�r¬�q–|||ƒ\}	}
|dk�rÜ| ||	 ¡ dd¡ �q–|	  ¡ �r|	 ¡  !¡ �søt‚t jj" #|	 ¡ ¡}n
t$||ƒ}| %||¡ | |¡ �q–tt| ddƒƒ}ˆ  ¡ D ]"\}}t j& '|¡�rH| |¡ �qH|D ]º}||k�rØ|| jk�r�d	}n,|| jk�r¢d
}n|| jk�r´d}ntdƒ‚t( )d|› d|› d�¡ �qpt*| |ƒ�sút( )d|› d�¡ �qpt| |ƒ}| +|t,||t| ƒj-ƒ¡ | |¡ �qpt| di ƒ}| .t/t0| |ƒƒ¡ | ¡ D ]\}}| 1||¡ �qT| j2 ¡ D �]’\}}|t3k�sv| 4d¡�rš�qv||k�r¨�qv||k�r¶�qvt|t j5j6ƒ}|�rÐ|j7}t8 9|¡�rFz*t j: ;|¡}| <|t j =|¡ ¡ |¡ W n: t>k
�r@ } zd|› �}| ?||¡ W 5 d}~X Y nX �qvt@|ƒ}|�rd| A||¡ �qvt|t j:jBƒ�r’| <|t j =|¡ ¡ |¡ �qv|||ƒ\}	}|	  ¡ �rÀ| ||	 ¡ dd¡ nH|�rÊdnd}|	 C¡ › d|› �}dt  Dt|ƒ¡› d|› d�}| ?||¡ �qv| jE F¡ D ]}| G|¡ �q| jH F¡ D ]}| I|¡ �q2|S )z²
    Build a ConcreteModuleTypeBuilder from an nn.Module. This
    ConcreteModuleType doesn't have a JIT type associated with it yet, it
    must be filled in by the caller.
    r@   c              
      sÒ   d}z†| ˆ krHˆ |  t jjjd krHt jj ˆ |  tƒ ¡}t j 	|¡}n@t
|t jjƒrxt jj |jtƒ ¡}t j 	|¡}nt j |¡}d}W n> tk
rÈ } z td| › d|› d|› �ƒ|‚W 5 d }~X Y nX ||fS )NFÚforwardTzError inferring type for z: )r   Únnr   rn   r    ry   Úann_to_typer   r!   ZInferredTyper3   Ú	AttributerK   Ú_jit_try_infer_typeÚRuntimeError)r0   r>   Úinferredr|   Ú	attr_typeÚre©Zclass_annotationsr&   r'   Ú
infer_typeÕ   s*    ÿÿþ ÿ.z/infer_concrete_type_builder.<locals>.infer_typeNTFZ__constants__r&   Ú	submoduleÚbufferÚ	parameterz3added_names must be submodule, parameter, or bufferú'zA' was found in ScriptModule constants,  but it is a non-constant z. Consider removing it.zb' was found in ScriptModule constants, but was not actually set in __init__. Consider removing it.Ú__overloads__Ú__z˜(This function exists as an attribute on the Python module, but we failed to compile it to a TorchScript function. 
The error stack is reproduced here:
zFIts type was inferred; try adding a type annotation for the attribute.Ú z. zT(This attribute exists on the Python module, but we failed to convert Python type: 'z' to a TorchScript type. ú))Jr   r!   ZConcreteModuleTypeBuilderrK   r3   r{   Ú
ModuleDictZset_module_dictÚ
ModuleListÚ
SequentialZset_module_listZParameterListZset_parameter_listZParameterDictZset_parameter_dictrp   ZaoZquantizationZQuantWrapperr2   rJ   Zadd_ignored_attributesrP   rL   r   rG   ZTensorÚAssertionErrorZadd_attributerM   r   r   ÚsuccessZis_interface_typeÚConcreteModuleTypeÚfrom_jit_typeÚget_module_concrete_typeZ
add_moduler-   Zis_finalÚwarningsÚwarnÚhasattrZadd_constantrQ   r   ÚupdateÚget_overload_name_mappingÚget_overload_annotationsZadd_overloadÚ__dict__Úignored_attributesÚ
startswithZ_opsZOpOverloadPacketÚopro   Ú
isfunctionr    ÚscriptZadd_function_attributer~   Ú	ExceptionZadd_failed_attributer   Zadd_builtin_functionZScriptFunctionÚreasonrW   r   ÚvaluesZadd_forward_hookr   Zadd_forward_pre_hook)r4   Úshare_typesÚconcrete_type_builderrN   Úignored_propertiesr„   Zadded_namesr0   r>   r�   Ú_Úsub_concrete_typeZconstants_setÚannÚhintÚvalueÚ	overloadsZoverloaded_namesZisoverloadpacketZscripted_fnÚeZbuiltin_symbol_namer€   Zinferred_msgZadditional_infoÚhookÚpre_hookr&   rƒ   r'   Úinfer_concrete_type_builder¹   s     ÿ




ÿ

ÿÿ
ÿ
 ÿÿÿ

  ÿÿ  ÿ
ÿýÿr°   c                   @   sJ   e Zd ZU eee eejj	 f e
d< eejj	 e
d< dd„ Zdd„ ZdS )ÚConcreteTypeStoreÚ
type_storeÚmethods_compiledc                 C   s   i | _ tƒ | _d S r   )r²   rL   r³   )rd   r&   r&   r'   rc   «  s    zConcreteTypeStore.__init__c                 C   sf   t |ƒ}t|ƒ}|| jkr$g | j|< | j| }|D ]}| |¡r2|  S q2| ¡ }| j|  |¡ |S )z|
        Infer a ConcreteType from this `nn.Module` instance. Underlying JIT
        types are re-used if possible.
        )r°   rK   r²   ÚequalsÚbuildr;   )rd   r4   r¥   Znn_module_typeZknown_typesZ
known_typeÚconcrete_typer&   r&   r'   Úget_or_create_concrete_type±  s    




z-ConcreteTypeStore.get_or_create_concrete_typeN)r   ri   rj   r   r   r   r   r   r!   r’   rn   r   rc   r·   r&   r&   r&   r'   r±   §  s   
r±   c                 C   s\   dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}|   |||||¡ d S )Nc                 S   s   g | ]
}|j ‘qS r&   ©r   ©rB   Úmr&   r&   r'   Ú
<listcomp>Î  s     z<create_methods_and_properties_from_stubs.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r&   ©r   r¹   r&   r&   r'   r»   Ï  s     c                 S   s   g | ]}t |jƒ‘qS r&   )r   r   r¹   r&   r&   r'   r»   Ð  s     c                 S   s   g | ]
}|j ‘qS r&   r¸   ©rB   Úpr&   r&   r'   r»   Ò  s     c                 S   s   g | ]
}|j ‘qS r&   r¼   r½   r&   r&   r'   r»   Ó  s     )Z_create_methods_and_properties)r¶   Úmethod_stubsÚproperty_stubsZmethod_defsZmethod_rcbsZmethod_defaultsZproperty_defsZproperty_rcbsr&   r&   r'   Ú(create_methods_and_properties_from_stubsË  s        ÿrÁ   c                 C   sL   dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}dd„ |D ƒ}|   ||||¡ d S )Nc                 S   s   g | ]
}|j ‘qS r&   r¸   ©rB   Úhr&   r&   r'   r»   Û  s     z+create_hooks_from_stubs.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r&   r¼   rÂ   r&   r&   r'   r»   Ü  s     c                 S   s   g | ]
}|j ‘qS r&   r¸   rÂ   r&   r&   r'   r»   Þ  s     c                 S   s   g | ]
}|j ‘qS r&   r¼   rÂ   r&   r&   r'   r»   ß  s     )Z_create_hooks)r¶   Ú
hook_stubsÚpre_hook_stubsZ	hook_defsZ	hook_rcbsZpre_hook_defsZpre_hook_rcbsr&   r&   r'   Úcreate_hooks_from_stubsÚ  s
    rÆ   c                 C   sZ   t | tƒst‚t | tjjƒr,t| dƒr,| jS |r<t 	| ¡}nt
| |ƒ}| ¡  | ¡ }|S )a×  
    Gets a concrete type for nn_modules. If share_types is True, the concrete
    type is fetched from concrete_type_store. If it is False, a new concrete type
    is created without first searching concrete_type_store.

    Args:
        nn_module:  The original Python nn.Module that we are creating a ScriptModule for.
        share_types = Whether to share underlying JIT types between modules (if possible).

    Returns:
        A concrete type for nn_module.
    Ú_concrete_type)r3   r   r�   r   r    ÚScriptModuler—   rÇ   Úconcrete_type_storer·   r°   Zset_poisonedrµ   )r4   r¤   r¶   r¥   r&   r&   r'   r”   ä  s     ÿ
r”   c                 C   sj   t  t| ƒ¡}t  t| ƒ¡}tt| ƒ||ƒ t |¡}tj 	|¡}| j
 ¡ D ]\}}| ||¡ qLt|ƒS )z
    Create and return a RecursiveScriptClass instance from a Python object.

    Arguments:
        obj: A Python object.
    )r-   Ú_qualified_namerK   Z'createResolutionCallbackForClassMethodsr(   r   Z	get_classr   r!   Z_create_object_with_typer›   rG   ÚsetattrÚwrap_cpp_class)r"   Zqualified_class_namer#   Zclass_tyZ
cpp_objectr0   r«   r&   r&   r'   Úcreate_script_class  s    
rÍ   Fc                 C   s@   t | tjjƒrt‚t| ƒ t| |ƒ}|s4tƒ  | ¡ t	| ||ƒS )aÜ  
    Creates a new ScriptModule from an nn.Module

    Args:
        nn_module:  The original Python nn.Module that we are creating a ScriptModule for.
        stubs_fn:  Lambda that takes an nn.Module and generates a list of ScriptMethodStubs to compile.
        share_types:  Whether to share underlying JIT types between modules (if possible).
            NOTE: Only set to False this when we cannot guarantee type sharing will work
                correctly. This only happens today for traced modules, where the same
                module can produce different traced methods depending on the inputs.
        is_tracing: Whether this function is called during tracing or scripting. If tracing,
                we don't need to do AttributeTypeIsSupportedChecker because all the unsupported
                attributes will be baked as constant in the tracing graph. In addition,
                this check significantly slows down the traced modules when the module size is big.
    )
r3   r   r    r*   r�   Úcheck_module_initializedr”   r   ÚcheckÚcreate_script_module_impl)r4   Ústubs_fnr¤   Ú
is_tracingr¶   r&   r&   r'   Úcreate_script_module  s    
rÓ   c                    s†  t j ˆ j¡‰ˆˆƒ}tˆƒ}tˆƒ\}}tˆdtƒ ƒ}tˆƒ‰‡ ‡‡‡‡fdd„}t j	j
 ˆ|¡}	ˆ tjkržtˆ ||ƒ tˆ ||ƒ t j ˆ¡ tj ˆ ¡ t|	j ¡ ƒD ]\}
}||	j|
< q¬t|	j ¡ ƒD ]\}
}||	j|
< qÎtˆt jjt jjt jjfƒ�r$dˆ ¡ k�r$|	 dtˆƒ› d�¡ tˆt jjƒ�r~dˆ ¡ k�r~tˆ  ¡ ƒ�rtt!tˆ  ¡ ƒƒ}|	 d|› d�¡ n
|	 d	¡ |D ]X}|j"d
k�r–�q‚|j"j#}||j$ %¡ j%k�r´�q‚ˆ &|¡}t' (|j"¡|ƒ}||	j)|< �q‚|D ]V}|j$ %¡ j%}ˆ &|j$ *¡ j%¡}|j$ +¡ }|�rˆ &|j%¡nd
}t,|||ƒ|	j)|< �qàt-ˆƒD ]@}|ˆk�rR�q@tˆ|d
ƒ}t. /|¡t.j0j1k�r@t2|	ˆ|ƒ �q@|	S )aR  
    Convert an nn.Module to a RecursiveScriptModule.

    Args:
        nn_module:  The original Python nn.Module that we are creating a ScriptModule for.
        concrete_type:  The fully initialized ConcreteType of the module.
        stubs_fn:  Lambda that takes an nn.Module and generates a list of ScriptMethodStubs to compile.
    r@   c           	         sN  ˆ   ¡  ¡ D ]2}tˆ|ƒ}t|tjjƒr.|jn|}ˆ ||¡ qˆ  	¡ D ]„\}}tˆ|ƒ}t|t
ƒsvtdt|ƒ› �ƒ‚|j}t|tjjƒr–t||ƒ}n t|tjjƒrª|}nt||ˆƒ}ˆ ||¡ || j|< qHtˆƒD ]l}|ˆkräqÖtˆ|d ƒ}t |¡�r,t |¡�r,tˆ|ƒj}| | ¡}t| ||ƒ qÖˆ  |¡rÖt| ||ƒ qÖˆ | _d S )NzExpected Module but got )Zget_attributesÚkeysr2   r3   r   r    r}   r«   rË   Zget_modulesr   r�   rK   Újit_typer!   ZInterfaceTypeÚinterface_scriptrÈ   rÐ   r   r7   ro   Úismethodr-   Úis_ignored_fnÚ__func__Ú__get__Zis_ignored_attributerÇ   )	Úscript_moduler0   Z
orig_valuer¨   Úmodule_typeZscriptedr>   Zunbound_functionZbound_method©r¶   Ú
cpp_moduler¦   r4   rÑ   r&   r'   Úinit_fnE  sL    
ÿý
 ÿþ  ÿ

z*create_script_module_impl.<locals>.init_fnÚ__len__zdef __len__(self):
   return Ú
Ú__contains__z3def __contains__(self, key: str):
   return key in z2def __contains__(self, key: str):
   return False
N)3r   r!   Z_create_module_with_typerÕ   Úget_property_stubsÚget_hook_stubsr2   rJ   rP   r    r*   Ú
_constructrÉ   r³   rÁ   rÆ   Z_run_emit_module_hookrM   Ú	enumerateÚ_cÚ_get_forward_pre_hooksr   Ú_get_forward_hooksr   r3   r{   rŽ   r�   r�   Z_method_namesZdefineÚlenrÔ   Úreprr   r   r   r0   Z_get_methodÚ	functoolsÚwrapsr›   Zgetter_nameÚsetter_namerA   r7   r-   r8   r9   ZCOPY_TO_SCRIPT_WRAPPERÚ!add_python_attr_to_scripted_model)r4   r¶   rÑ   r¿   rÀ   rÄ   rÅ   rN   rß   rÛ   ÚidxÚfnrÔ   Zmethod_stubr0   Zscript_methodZwrapped_script_methodZproperty_stubZproperty_nameÚfgetrî   Úfsetr>   r&   rÝ   r'   rÐ   2  sŒ    	  ÿ3
  ÿ ÿÿ
üÿ
þ
ÿ


ÿ

ÿÿrÐ   c                 C   s<   t | |d ƒ}|d krdS t tjj|d ƒ}|d kr4dS ||kS )NF)r2   r   r    r*   )Úscript_modelrT   Zscript_attrZdefault_attrr&   r&   r'   Úscript_model_defines_attrÕ  s    rõ   c                 C   s*   t ||ƒr&t| |ƒr&t| |t||ƒƒ d S r   )r—   rõ   rË   r2   )rô   ÚorigrT   r&   r&   r'   rï   ß  s    rï   c                    s®   i }t t| ƒƒD ]˜‰ ˆ |krqt| ˆ d ƒ}t|ƒs4qt|dƒr|jd k	rt || j¡}|d kr`q|j	|kr|t
t d|j	¡ƒ‚‡ fdd„tt|ƒƒD ƒ}tt||ƒƒ||< q|S )Nri   Úmethodc                    s   g | ]}ˆ d  t |ƒ ‘qS )rŠ   )Ústr)rB   Úi©r0   r&   r'   r»   þ  s     z,get_overload_annotations.<locals>.<listcomp>)r7   rK   r2   Úcallabler—   ri   r-   Z_get_overloaded_methodsrh   rÙ   r   Z,get_overload_no_implementation_error_messageÚrangerê   rJ   Úzip)r<   rP   r¬   r>   Zmethod_overloadsÚnamesr&   rú   r'   rš   ä  s0     ÿ
 ÿÿrš   c                 C   sL   i }|   ¡ D ]:\}}|j}||kr*g ||< |D ]\}}||  |¡ q.q|S r   )rG   r   r;   )Úoverload_infoÚoverload_name_mappingsÚorig_fnr¬   Úoriginal_nameÚoverload_namer§   r&   r&   r'   r™     s    r™   c                 C   s@   t jj | d tƒ t | ¡¡}|d kr<t | ¡}t	d|› �ƒ‚d S )Nz>Must explicitly add type annotations to overloaded functions: )
r   r    ry   Zget_signaturer   ro   r×   r-   rÊ   r   )r/   Ú	signatureZ	qual_namer&   r&   r'   Ú_check_no_signature  s       ÿ
ÿr  c           
      C   s€   g }|   ¡ D ]n\}}t||jdd�}|D ]P\}}t|ƒ t||jdd�}tj | ¡ ||¡}t 	|¡}	| 
t|	||ƒ¡ q(q|S r)   )rG   r   r   r  r   r!   Z_replace_overloaded_method_declÚdeclr-   r.   r;   r   )
rÿ   Úoverload_stubsr  r¬   Zorig_astr  Zoverload_fnZover_astZnew_astÚ_rcbr&   r&   r'   Úmake_stubs_for_overloads  s,      ÿ  ÿ  ÿ
r	  c                 C   sÌ   t | tjjƒst‚t| dƒs6tdt t| ƒ¡› d�ƒ‚t| dƒsÈ| j	 
¡ D ]8\}}|d k	rJtjj |¡rJtd t t| ƒ¡|¡ƒ‚qJ| j 
¡ D ]8\}}|d k	rŽtjj |¡rŽtd t t| ƒ¡|¡ƒ‚qŽd S )Nr   rˆ   z=' has not been initialized, did you forget to call 'super()'?Zremote_parameterszK'{}' has uninitialized parameters {}. Did you forget to run a forward pass?zH'{}' has uninitialized buffers {}. Did you forget to run a forward pass?)r3   r   r{   r   r�   r—   r   rW   rK   r   rG   r‡   Zis_lazyÚformatr   )r<   r0   ÚparamÚbufr&   r&   r'   rÎ   0  s,    
ÿ
 ÿÿ ÿÿrÎ   c                    s\  t | ƒ t| dtƒ ƒ}t| ƒ}g }t| dƒrdt | j¡sdt| jddƒ}ttj	j
ddƒ}||krddg}g }t| ƒD ]6}||kr~qpt| |dƒ}t |¡tjjkrp| |¡ qp|| }tt| di ƒƒ‰ t| |ƒ}	ˆ  t|	ƒ¡ t|	ƒ}
ˆ | _‡ fdd„}t||ƒ}tƒ }g }|D ](}||k�r�q
| |¡ | |¡ �q
g }|D ]}| t| |ƒ¡ �q<|
| S )zš
    Implements the default rules for which methods should act as starting
    points for compilation (TODO add a link when the rules are published).
    r@   rz   rÙ   Nr‰   c                    s   | ˆ kS r   r&   )r5   ©r   r&   r'   Úignore_overloadedr  s    z3infer_methods_to_compile.<locals>.ignore_overloaded)rÎ   r2   rJ   rP   r—   r-   rØ   rz   r   r{   r   r7   r8   r9   r:   r;   Údictrš   r˜   r™   r	  r‰   ÚfilterrL   rM   r6   )r4   rN   r¦   ÚmethodsZforward_funcZmodule_forwardZexportedr0   r>   rÿ   r  r  Zfiltered_methodsZuniquerZuniqued_methodsr=   r÷   r&   r  r'   Úinfer_methods_to_compileI  sX      ÿÿÿÿ



r  c                 C   sô   t | ƒ i }g }| j ¡ D ]`}|j|kr^t|ƒt||j ƒkrhtd|j› dt| ƒj› d�ƒ‚n
|||j< | t||jƒ¡ qg }| j	 ¡ D ]`}|j|krÎt|ƒt||j ƒkrØtd|j› dt| ƒj› d�ƒ‚n
|||j< | t||jƒ¡ qŠ||fS )z=
    Returns forward hook and pre_hook ScriptModuleStubs
    zHook 'z' on zV has at least two different python definitions. Please use unique names for all hooks.z
Pre-hook ')
rÎ   r   r£   r   Úidr   rK   r;   r1   r   )r4   Zhook_maprÄ   r®   rÅ   r¯   r&   r&   r'   rä   ‡  s*    
ÿ

ÿ
rä   c                    s€   t | ƒ}t|dd�}i ‰ t|ƒD ]H}t||dƒ}t|tƒr |jsXtd|› d| j› d�ƒ‚t	 
|j¡ˆ |< q ‡ fdd„|D ƒ}|S )	zx
    Create property stubs for the properties of the module by creating method
    stubs for the getter and setter.
    r*   r+   Nz	Property z of z must have a getterc                    s    g | ]}t ˆ | ¡ j |ƒ‘qS r&   )r   r0   )rB   r$   ©Zrcbsr&   r'   r»   ¾  s     z&get_property_stubs.<locals>.<listcomp>)rK   r   r7   r2   r3   rA   rò   r   r   r-   r.   )r4   Z	module_tyZproperties_astsr0   r>   r=   r&   r  r'   rã   «  s    
ÿrã   c                    s0   t |tjjƒr|S t|ƒ ‡ fdd„}t||ƒS )a%  
    Makes a ScriptModule from an nn.Module, using the interface methods rule for
    determining which methods to compile.

    Args:
        mod_interface: the interface type that the module have
        nn_module:  The original Python nn.Module that we are creating a ScriptModule for.
    c                    s&   g }ˆ   ¡ D ]}| t| |ƒ¡ q|S )z‘
        Rule to infer the methods from the interface type to know which
        methods need to act as starting points for compilation.
        )ZgetMethodNamesr;   r6   )r4   r=   r÷   ©Úmod_interfacer&   r'   Ú"infer_interface_methods_to_compileÐ  s    z<interface_script.<locals>.infer_interface_methods_to_compile)r3   r   r    rÈ   rÎ   rÓ   )r  r4   r  r&   r  r'   rÖ   Â  s
    	
rÖ   c                 C   sd   t  | ¡rd S t| tjjƒr d S t | ¡sJt | ¡sJt	d| › d| › d�ƒ‚t  
| ¡}tjj| |d�S )Nú`z|` is not a function. Recursive scripting only supports Python functions or methods currently.
Consider manually annotating `z` with @torch.jit.script.)r  )r-   rØ   r3   r   r{   r   ro   rŸ   r×   r   r.   r    r    )rñ   Úlocr#   r&   r&   r'   Útry_compile_fnÝ  s    
ÿ	
r  c                 C   s   t j | ¡S )zE
    Wrap this torch._C.Object in a Python RecursiveScriptClass.
    )r   r    ZRecursiveScriptClass)Z	cpp_classr&   r&   r'   rÌ   õ  s    rÌ   c                 C   s   dd„ }t jj | |¡S )zb
    Wrap this torch._C.ScriptModule in a Python ScriptModule, recursively for all submodules
    c                 S   sŠ   t j | j¡ ¡ D ]\}}t| |t|ƒƒ qt jj | j 	¡ ¡| _
t| j ¡ ƒD ]\}}|| j|< qPt| j ¡ ƒD ]\}}|| j|< qrd S r   )r   r!   r�   rç   rG   rË   Úwrap_cpp_moduler’   r“   Ú_typerÇ   ræ   rè   r   ré   r   )rÛ   r0   rÞ   rð   rñ   r&   r&   r'   rß     s    ÿz wrap_cpp_module.<locals>.init_fn)r   r    r*   rå   )rÞ   rß   r&   r&   r'   r  ü  s    r  c              	   C   sB   t  |¡rd S t||jƒ}tj  ¡ � t| |fdƒ W 5 Q R X |S )Nr&   )r-   rØ   r1   r   r   Z_disable_emit_hooksrÁ   )r¶   rñ   Zstubr&   r&   r'   Úcompile_unbound_method  s    
r  c                    s.   ‡ ‡fdd„}ˆ|_ ˆj|_tj ˆ|¡ |S )zé
    Returns a function that lazily binds `unbound_method` to a provided
    Module IValue, then invokes the method. We do this so that any Python
    shenanigans that will poison type sharing are impossible at compile
    time.
    c                    s0   ‡ fdd„}t jj | |¡}t ˆ|¡}||Ž S )Nc                    s\   ˆ j }t|ƒD ]&}t||d ƒ}t |¡rt| ||ƒ qˆ  ¡  ¡ D ]\}}t| ||ƒ qBd S r   )Zpy_classr7   r2   r-   rØ   rË   Zget_constantsrG   )rÛ   Z
orig_classr0   r>   r«   )r¶   r&   r'   rß   $  s    
z7lazy_bind.<locals>.lazy_binding_method.<locals>.init_fn)r   r    r*   rå   ÚtypesÚ
MethodType)rÞ   Úargsrß   rÛ   r÷   ©r¶   Úunbound_methodr&   r'   Úlazy_binding_method#  s    z&lazy_bind.<locals>.lazy_binding_method)Zoriginal_fnr   r   r-   Zcopy_torchscript_modifier)r¶   r"  r#  r&   r!  r'   Ú	lazy_bind  s
    r$  )T)T)TF)QÚcollectionsrì   ro   rw   r^   r  r•   Útypingr   r   r   r   r   Ztorch._jit_internalr-   Ztorch._sourcesr   Ztorch.jit._builtinsr   Ztorch.jit._checkr   Ztorch.jit._stater	   r
   r   Ztorch.jit.frontendr   r   r   r   Ztorch.nnr   Ú
namedtupler   r   rœ   r(   r1   r6   r?   rP   ÚboolÚfloatÚintrø   rK   ZdeviceZlayoutZdtyperZ   rQ   r!   Z_jit_tree_viewsZSourceRangeFactoryra   rp   r°   r±   rÉ   rÁ   rÆ   r”   rÍ   rÓ   rÐ   rõ   rï   rš   r™   r  r	  rÎ   r  rä   rã   rÖ   r  rÌ   r  r  r$  r&   r&   r&   r'   Ú<module>   s¦    ÿîø
 o!

 
 $
 >$