o
    Hh                     @   s   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 d dlZd dl	m
Z
 eeZdZe ZejZe ej 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_template_remote_module_z#Created a temporary directory at %sc                 C   s  t | dds
J dtj| }tjjj}||}d| v s)J d|  |	d}g }g }|d us8J |j
D ]%}||j | rNd|j }nd}|j d|j | }	||	 q;|d	d  }d
|}
|d	d  }d
|}t|jd	ksJ |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torch_jit_internal_qualified_namejit_state
_python_cuget_interfacegetMethodNames	getMethod	argumentsappendnamehas_default_valuedefault_valuetypejoinlenreturnsstr)module_interfacequalified_namecumodule_interface_cmethod_schemaarg_str_listarg_type_str_listargumentdefault_value_strarg_type_strargs_strarg_types_strreturn_type_str r*   Y/var/www/vscode/kcb/lib/python3.10/site-packages/torch/distributed/nn/jit/instantiator.py#get_arg_return_types_from_interface   s8   








r,   c                 C   s   zt | }| }W d    n1 sw   Y  W n ty%   d }Y nw ||krMt | d}td|  || W d    d S 1 sFw   Y  d S td|  d S )Nwz
Writing %szSkipped writing %s)openreadOSErrorloggerinfowrite)out_pathtextfold_textr*   r*   r+   _writeB   s   

"r8   c                 C   sJ   t |jdi |}tjt|  d}t|| t  t	|  }|S )Nz.pyr*   )
r   formatospathr   INSTANTIATED_TEMPLATE_DIR_PATHr8   	importlibinvalidate_cachesimport_module)generated_module_namestr_dict!enable_moving_cpu_tensors_to_cudagenerated_code_textr4   generated_moduler*   r*   r+   &_do_instantiate_remote_module_templateQ   s   

rE   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,   dictrE   )module_interface_clsrB   module_interface_cls_namer@   assign_module_interface_cls_strr'   r(   r)   
kwargs_strarrow_and_return_type_str arrow_and_future_return_type_strrA   r*   r*   r+   -instantiate_scriptable_remote_module_templatef   s@   



	r\   c               	   C   s,   t  d} tdddddddd}t| |dS )	Nnon_scriptablezmodule_interface_cls = Nonez*argsz**kwargsz*args, **kwargsr   )rI   rM   rN   rJ   rK   rL   rO   T)rR   rU   rE   )r@   rA   r*   r*   r+   1instantiate_non_scriptable_remote_module_template   s   
r^   )T)atexitr=   loggingr:   systempfiletypingr   r
   9torch.distributed.nn.jit.templates.remote_module_templater   	getLoggerrT   r1   rR   TemporaryDirectory	_TEMP_DIRr   r<   registercleanupr2   r;   r   r,   r8   rE   r\   r^   r*   r*   r*   r+   <module>   s,   
&
)