U
    yhS                     @   s   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 d dlZd dlm	Z	 e
eZdZe ZejZede eje dd Zdd	 Zd
d ZdddZdd ZdS )    N)Optional)get_remote_module_templateZ_remote_module_z#Created a temporary directory at %sc                 C   s   t | ddstdtj| }tjjj}||}d|	 ksRtd|	  |
d}g }g }|d k	spt|jD ]J}||j | rd|j }nd}|j d|j | }	||	 qv|d	d  }d
|}
|d	d  }d
|}t|jd	kst|jd }t|j}|
||fS )N__torch_script_interface__FzGExpect a TorchScript class interface decorated by @torch.jit.interface.forwardz2Expect forward in interface methods, while it has z =  z:    z, r   )getattrAssertionErrortorch_jit_internal_qualified_nameZjit_stateZ
_python_cuZget_interfaceZgetMethodNamesZ	getMethod	argumentsappendnameZhas_default_valuedefault_valuetypejoinlenZreturnsstr)Zmodule_interfaceZqualified_namecuZmodule_interface_cZmethod_schemaZarg_str_listZarg_type_str_listargumentZdefault_value_strZarg_type_strargs_strarg_types_strreturn_type_str r   W/var/www/html/venv/lib/python3.8/site-packages/torch/distributed/nn/jit/instantiator.py#get_arg_return_types_from_interface   sB      








r   c              	   C   s   z t | }| }W 5 Q R X W n tk
r8   d }Y nX ||krpt | d}td|  || W 5 Q R X ntd|  d S )Nwz
Writing %szSkipped writing %s)openreadOSErrorloggerinfowrite)out_pathtextfZold_textr   r   r   _write@   s    

r(   c                 C   sF   t |jf |}tjt|  d}t|| t  t	|  }|S )Nz.py)
r   formatospathr   INSTANTIATED_TEMPLATE_DIR_PATHr(   	importlibinvalidate_cachesimport_module)generated_module_namestr_dict!enable_moving_cpu_tensors_to_cudaZgenerated_code_textr%   Zgenerated_moduler   r   r   &_do_instantiate_remote_module_templateO   s     
r3   Tc              	   C   s   t | ddstd|  dtj| dd}t | }d| j d| j d	}t	| \}}}d
}d| }	d| d}
t
|||	|
||dd}t|||S )Nr   Fzmodule_interface_cls z8 must be a type object decorated by @torch.jit.interface._zfrom z import z as module_interface_clsr   z -> z -> Future[]z@torch.jit.script)assign_module_interface_cls	arg_typesarrow_and_return_typearrow_and_future_return_typeargskwargsjit_script_decorator)r   
ValueErrorr
   r   r   replace_FILE_PREFIX
__module____name__r   dictr3   )Zmodule_interface_clsr2   Zmodule_interface_cls_namer0   Zassign_module_interface_cls_strr   r   r   Z
kwargs_strZarrow_and_return_type_strZ arrow_and_future_return_type_strr1   r   r   r   -instantiate_scriptable_remote_module_templated   sB    
 

	  rD   c               	   C   s,   t  d} tdddddddd}t| |dS )	NZnon_scriptablezmodule_interface_cls = Nonez*argsz**kwargsz*args, **kwargsr   )r7   r;   r<   r8   r9   r:   r=   T)r@   rC   r3   )r0   r1   r   r   r   1instantiate_non_scriptable_remote_module_template   s    
rE   )T)r-   loggingr*   systempfiletypingr   r
   Z9torch.distributed.nn.jit.templates.remote_module_templater   	getLoggerrB   r"   r@   TemporaryDirectoryZ	_TEMP_DIRr   r,   r#   r+   r   r   r(   r3   rD   rE   r   r   r   r   <module>   s&   
& 
)