3
I4W^d(  ã               @   s‚   d dl Z d dlZd dlZd dljZd dlmZmZmZm	Z	m
Z
 d dlmZ dd„ Zf fdd„Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )é    N)ÚModuleÚ
ModuleListÚ	ParameterÚ
SequentialÚ
ModuleDict)Úget_function_from_typec             C   s$  t jjt| ƒƒ}t jj|d�}tt| dg ƒƒ}i |jd< xht| ƒD ]\}t| |ƒ}|dkrr|| j	krrt
j|||ƒ qB||kr~qBqBt|ttt jjfƒrBt|||ƒ qBW x>| jD ]4}| j| dkrÌt
j||dƒ qª|j|| j| ƒ qªW x2t| di ƒjƒ D ]\}}t jj|ƒrô|j|ƒ qôW ||jd< xd|jd D ]V}t| |ƒ�r,|| j	k�sT|| jk�rb|dk	�rb�q,t||ƒ�s,t||t| |ƒƒ �q,W t| di ƒ}xøt| ƒD ]ì}t||ƒ�rì|jj|ƒ�rœ||k�rœt| |ƒ}t|||ƒ |jj||ƒ �qœt| |ƒ}||k�rt jjj|| ƒ}	nt jj|ƒ}	|	dk	�rœy|jj||	|ƒ W nF t k
�r„ }
 z(dj!t| ƒ||	|t"|
ƒƒ}t |ƒ‚W Y dd}
~
X nX �qœW t#t| di ƒƒ|jd< xDt| ƒD ]8}t| |ƒ}t||ƒ�rÊ�q¬t$j%|ƒ�r¬t|||ƒ �q¬W t jj&||ƒ x(|jj'ƒ D ]}t|||jj(|ƒƒ �qW |S )	zp
    Copies the parameters, buffers, constants, attributes, and submodules
    of an nn.Module into itself.
    )Ú_qualified_nameZ__constants__Z_constants_setNÚ__annotations__z^When compiling {}, could not register attribute {} of type {} with value {}
Original error: {}Ú__overloads__Z
_overloads))ÚtorchÚjitr   ÚtypeÚScriptModuleÚsetÚgetattrÚ__dict__ÚdirÚ_parametersÚobjectÚ__setattr__Ú
isinstancer   r   Ú	AttributeÚsetattrZ_buffersZregister_bufferÚitemsÚ_jit_internalÚis_finalÚaddÚhasattrZ_cZ_has_attributeZ_set_attributeÚannotationsZann_to_typeÚ_CZ_jit_try_infer_typeZ_register_attributeÚRuntimeErrorÚformatÚstrÚdictÚinspectÚismethodÚ_create_methods_from_stubsZ_method_namesZ_get_method)ÚoriginalÚstubsZqualified_nameZscript_moduleZconstants_setÚnameÚitemÚannZclass_annotationsZthe_typeÚeÚmsgÚmethod_name© r/   ú5/tmp/pip-build-e66j97b2/torch/torch/jit/_recursive.pyÚcopy_to_script_module
   sp    


"



 
r1   c                sF  t ˆtjjƒrˆS t ˆtjjtjjtjjfƒr6tˆƒS t	ˆdƒsTt
djtˆƒjƒƒ‚f }t	ˆdƒr’ˆjjtjjjkr‚t
djˆƒƒ‚tjˆjƒs’d}g }g }xvtˆƒD ]j}tˆ|ƒ}t|ƒr¤tj|ƒtjjkrÖ|j|ƒ t	|dƒoè|jdk	r¤tj|ˆjƒ}|dk	r¤|j||fƒ q¤W |t|ƒ }t‡ fdd„|D ƒƒ}ttˆd	i ƒƒ‰g }x¸|D ]°\}	}
tjj|	d
d�‰t t!‡fdd„t"t#|
ƒƒƒƒ}|ˆˆj$ƒ j$< xht%|
|ƒD ]Z\}}tjj&|ƒ tjj|d
d�}tj'j(|j)ƒ ˆ|ƒ}tj*|	ƒ}|jtjj+|||ƒƒ �qžW �qNW ˆˆ_,‡fdd„}‡fdd„}t-||ƒ}t t!||ƒƒ}t.ˆ|| ƒS )zÝ
    Makes a ScriptModule from an nn.Module. If `_methods` is provided,
    these methods are treated as @script_methods. If not, it defaults to
    `('forward',)`. Methods accessed in forward are scripted on demand.
    r   z@'{}' has not been initialized, did you forget to call 'super()'?Úforwardz#No forward method was defined on {}Ú
__module__Nc             3   s   | ]}|ˆ kr|V  qd S )Nr/   )Ú.0r)   )Úexclude_methodsr/   r0   ú	<genexpr>‘   s    z#recursive_script.<locals>.<genexpr>r
   r   )Z	self_namec                s   ˆ j ƒ j d t| ƒ S )NÚ__)r)   r"   )Úi)Úorig_astr/   r0   Ú<lambda>˜   s    z"recursive_script.<locals>.<lambda>c                s   | ˆ kS )Nr/   )r.   )Úoverload_name_mappingsr/   r0   Úignore_overloaded¤   s    z+recursive_script.<locals>.ignore_overloadedc                s"   t tˆ ƒ| ƒ}tjj|tj|ƒƒS )N)r   r   r   r   Úscript_methodr   Ú#createResolutionCallbackFromClosure)ÚmethodÚfunc)Úmodr/   r0   Ú	make_stub§   s    z#recursive_script.<locals>.make_stub)r2   )/r   r   r   r   Únnr   r   r   Úcreate_constant_iterable_moduler   r    r!   r   Ú__name__r2   Ú__func__r   r   Úis_ignored_fnr   r   ÚcallableZget_torchscript_modifierZFunctionModifiersZEXPORTÚappendr3   Z_get_overloaded_methodsÚ	__class__Útupler#   Zget_jit_defÚlistÚmapÚrangeÚlenr)   ÚzipZ_check_no_signaturer   Z_replace_overloaded_method_declÚdeclr>   ZScriptMethodStubr
   Úfilterr1   )rA   r5   ÚmethodsZexportedZ	overloadsr)   r*   Zmethod_overloadsZoverload_stubsZorig_fnZoverload_fnsÚnamesZoverload_fnZover_astZnew_astÚ_rcbr<   rB   Zfiltered_methodsr(   r/   )r5   rA   r9   r;   r0   Úrecursive_scripti   sX    




"
rV   c          
   C   sZ   t j|ƒrd S tj|ƒsd S tjj|t j|ƒƒ}tjjƒ � tjj	| |fƒ W d Q R X |S )N)
r   rG   r$   r%   r   r   r=   r>   Z_disable_emit_hooksr&   )ÚmoduleÚfnZstubr/   r/   r0   Úcreate_method_from_fn°   s    

rY   c             C   s&   | |j krd S t|ƒ}||j j| < |S )N)Ú_modulesrV   Z_python_modules)ÚfieldrW   ÚparentZnew_strong_submoduler/   r/   r0   Úmake_strong_submodule½   s
    
r]   c             C   sb   t j| ƒrd S t| tjjƒr d S tj| ƒ rHtj| ƒ rHt	dj
| | ƒƒ‚t j| ƒ}tjj| |d�S )Nzš`{}` is not a function. Recursive scripting only supports Python functions or methods currently.
Consider manually annotating `{}` with @torch.jit.script.)rU   )r   rG   r   r   rC   r   r$   Ú
isfunctionr%   r    r!   r>   r   Úscript)rX   ÚlocZrcbr/   r/   r0   Útry_compile_fnË   s    


ra   c             C   s¢   t jƒ }x@| jjƒ D ]2\}}t|tttfƒr:t|ƒ||< qt	|ƒ||< qW t| tƒrdt
jjt|ƒƒS t| tƒrzt
jj|ƒS t| tƒr�t
jj|ƒS tdj| ƒƒ‚d S )Nz`Only nn.ModuleList, nn.Sequential, and nn.ModuleDict can be made into constant modules, found {})ÚcollectionsÚOrderedDictrZ   r   r   r   r   r   rD   rV   r   r   Z_ConstSequentialZ_ConstModuleListZ_ConstModuleDictr    r!   )rW   ÚmodulesÚkeyÚ	submoduler/   r/   r0   rD   á   s    


rD   )r$   r   rb   Ztorch._jit_internalr   Ztorch.nnr   r   r   r   r   Z
torch._sixr   r1   rV   rY   r]   ra   rD   r/   r/   r/   r0   Ú<module>   s   
_G