U
    Jc                     @   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 c                 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 ]H}||j | rd
|j}nd}dj
|j|j|d	}	||	 qv|d
d  }d|}
|d
d  }d|}t|jd
kst|jd }t|j}|
||fS )N__torch_script_interface__FzGExpect a TorchScript class interface decorated by @torch.jit.interface.Zforwardz4Expect forward in interface methods, while it has {}z = {} z{name}: {type}{default_value})nametypedefault_value   z, r   )getattrAssertionErrortorch_jit_internal_qualified_nameZjit_stateZ
_python_cuZget_interfaceZgetMethodNamesformatZ	getMethod	argumentsappendr   Zhas_default_valuer   r   joinlenZreturnsstr)Zmodule_interfaceZqualified_nameZ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   I/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/nn/jit/instantiator.py#get_arg_return_types_from_interface   sN      




  



r   c              	   C   s   z"t | d}| }W 5 Q R X W n tk
r:   d }Y nX ||krvt | d }td|  || W 5 Q R X ntd|  d S )Nrwz
Writing {}zSkipped writing {})openreadIOErrorloggerinfor   write)out_pathtextfZold_textr   r   r   _writeC   s    
r(   c                 C   sF   t |jf |}tjt|  d}t|| t  t	|  }|S )Nz.py)
r   r   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_templateR   s     
r2   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   dictr2   )Zmodule_interface_clsr1   Zmodule_interface_cls_namer/   Zassign_module_interface_cls_strr   r   r   Z
kwargs_strZarrow_and_return_type_strZ arrow_and_future_return_type_strr0   r   r   r   -instantiate_scriptable_remote_module_templateg   sB    
 

	  rC   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   )r6   r:   r;   r7   r8   r9   r<   T)r?   rB   r2   )r/   r0   r   r   r   1instantiate_non_scriptable_remote_module_template   s    
rD   )T)r,   loggingr)   systempfiletypingr   r   Z9torch.distributed.nn.jit.templates.remote_module_templater   	getLoggerrA   r"   r?   TemporaryDirectoryZ	_TEMP_DIRr   r+   r#   r*   r   r   r(   r2   rC   rD   r   r   r   r   <module>   s&   
* 
)