U
    <cw                    @   sJ  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	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlZd dlmZ d dlmZ d dlm  mZ d dlm  mZ d dlmZmZmZ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) d dl*m+Z+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: d dl;m<Z< d dl.m=Z= d dl>m?Z@ dd ZAdddZBdddddddgZCdZDejE) ZFejE) ZGdZHd aIdd  ZJd!d" ZKG d#d$ d$ZLd%d& ZMd'd( ZNd)d* ZOd+d, ZPd-d. ZQe	d/d0gZRG d1d2 d2ZSG d3d4 d4ZTG d5d6 d6ZUd7d8 ZVd9d: ZWd;d< ZXd=d> ZYdd?d@ZZdAdB Z[dCdD Z\dEdF Z]dGdH Z^dIdJ Z_ddLdMZ`dNdO ZadPdQ ZbddSdTZcdUdV ZddWdX ZedYdZ Zfd[d\ Zgd]d^ Zhd_d` Zidadb Zjdcdd Zkdedf Zldgdh Zmdidj Zndkdl Zodmdn Zpdodp Zqdqdr Zrejsjtdsdt Zuejsjtdudv ZvdwZwdxdy Zxejsjteyejzdzd{d|Z{d}Z|d~d Z}da~dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zejjdd Zejjdd Zejjdd ZdddZejjdddZejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd Zejjdd ZG dd dZG dd dZdd ZG dd dejZe0Z0G dd dejjZG dd dZG dd dejZG dd dZG dd de<eZG dd de<ZG dd de<eZG dd de<eZdS )    N)
namedtuple)partial)Event)Lock)mock)RRef_get_debug_info_rref_context_get_debug_info
WorkerInfo)_use_rpc_pickler_thread_local_var	_wait_all)	PythonUDFRPCExecMode_internal_rpc_pickler_build_rpc_profiling_keyFuture)skip_if_lt_x_gpucaptured_outputtp_transports)IS_MACOS
load_testssandcastle_skip_ifget_cycles_per_ms)	dist_initget_function_eventinitialize_pgwait_until_node_failure,wait_until_pending_futures_and_users_flushed#wait_until_owners_and_forks_on_rankworker_name)RpcAgentTestFixture)TemporaryFileName)profilec                   C   s   t t dt dS N   )torchaddones r*   r*   T/tmp/pip-unpacked-wheel-gikjz4vx/torch/testing/_internal/distributed/rpc/rpc_test.pyfoo_add:   s    r,   Fc              
   C   s   | dkrt  n
tj| }|r.tjjdnt  }|T |D tdtd }}t	||}t
||}| }| }W 5 Q R X W 5 Q R X d S )Nr-   ##forward##r&   )
contextlibsuppressr'   cudadeviceautogradprofilerrecord_functionr)   r(   mulZreluZsigmoid)r2   use_record_functionZ
device_ctxZrecord_function_ctxt1t2tr*   r*   r+   udf_with_torch_ops=   s    r;   z
aten::ones	aten::add	aten::mulz
aten::reluzaten::clamp_minzaten::sigmoid#remote_op: ic                   C   s   t d7 a d S r%   _rpc_barrier_countr*   r*   r*   r+   _increment_countb   s    rA   c                   C   s   da d S Nr   r?   r*   r*   r*   r+   _reset_countf   s    rC   c                   @   s   e Zd Zdd Zdd ZdS )StubRpcAgentc                 C   s
   || _ d S N
world_size)selfrG   r*   r*   r+   __init__k   s    zStubRpcAgent.__init__c                 C   s   dd t | jD S )Nc                 S   s   h | ]}t t||d qS ))nameid)r
   r!   .0rankr*   r*   r+   	<setcomp>o   s   z0StubRpcAgent.get_worker_infos.<locals>.<setcomp>)rangerG   rH   r*   r*   r+   get_worker_infosn   s    zStubRpcAgent.get_worker_infosN)__name__
__module____qualname__rI   rR   r*   r*   r*   r+   rD   j   s   rD   c                  K   s   t  S rE   )r   ZMockkwargsr*   r*   r+   +_stub_construct_rpc_backend_options_handleru   s    rX   c                 C   s
   t |dS )NrF   )rD   )storerJ   rN   rG   rpc_backend_optionsr*   r*   r+   _stub_init_rpc_backend_handlery   s    r[   c                 C   s   t |  d S rE   )VALUE_FUTURE
set_resultvaluer*   r*   r+   	set_value}   s    r`   c                   C   s   t  S rE   )r\   resultr*   r*   r*   r+   wait_for_value_future   s    rb   c                 C   s   t |  t S rE   )r\   r]   DONE_FUTUREra   r^   r*   r*   r+   set_and_check_done   s    
rd   TensorClasstensorsc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
MyPickleClassc                 C   s
   d | _ d S rE   r:   rQ   r*   r*   r+   rI      s    zMyPickleClass.__init__c              	   C   s2   t tttddtddfd \}}||fS N   )r   	serializer   my_tensor_functionr'   r)   )rH   Zpickled_python_udfrf   r*   r*   r+   __getstate__   s    zMyPickleClass.__getstate__c                 C   s6   t |d |d }||jd |jd }|| _d S Nr   r&   )r   Zdeserializefuncargsr:   )rH   objZ
python_udfra   r*   r*   r+   __setstate__   s    zMyPickleClass.__setstate__c                 C   s
   || _ d S rE   rh   )rH   valr*   r*   r+   set   s    zMyPickleClass.setN)rS   rT   rU   rI   rm   rr   rt   r*   r*   r*   r+   rg      s   rg   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SlowPickleClassc                 C   s
   || _ d S rE   rh   rH   r:   r*   r*   r+   rI      s    zSlowPickleClass.__init__c                 C   s   t | j | jfS rE   )timesleepr:   rQ   r*   r*   r+   rm      s    zSlowPickleClass.__getstate__c                 C   s   |d | _ t| j  d S rB   )r:   rw   rx   )rH   rq   r*   r*   r+   rr      s    
zSlowPickleClass.__setstate__N)rS   rT   rU   rI   rm   rr   r*   r*   r*   r+   ru      s   ru   c                   @   sN   e Zd ZdddZdd Zedd Zedd	 Zd
d Z	dd Z
dd ZdS )MyClassFc                 C   s   || _ |rtd d S ri   )arw   rx   )rH   rz   delayr*   r*   r+   rI      s    zMyClass.__init__c                 C   s
   | j | S rE   rz   )rH   br*   r*   r+   my_instance_method   s    zMyClass.my_instance_methodc                 C   s   || S rE   r*   )clsder*   r*   r+   my_class_method   s    zMyClass.my_class_methodc                 C   s   | dkS N
   r*   )fr*   r*   r+   my_static_method   s    zMyClass.my_static_methodc                 C   s   |  j |7  _ d S rE   r|   )rH   	incrementr*   r*   r+   increment_value   s    zMyClass.increment_valuec                 C   s   | j S rE   r|   rQ   r*   r*   r+   	get_value   s    zMyClass.get_valuec                 C   s   t d t| j|S N   )rw   rx   r'   r(   rz   )rH   Zmy_tensor_argr*   r*   r+   my_slow_method   s    
zMyClass.my_slow_methodN)F)rS   rT   rU   rI   r~   classmethodr   staticmethodr   r   r   r   r*   r*   r*   r+   ry      s   


ry   c                 O   s   | |  f||S rE   )local_value)methodrrefrp   rW   r*   r*   r+   _call_method_on_rref   s    r   c                 C   s   dd | D S )Nc                 S   s   g | ]}t t|qS r*   )r   ry   )rM   rz   r*   r*   r+   
<listcomp>   s     z!get_rref_list.<locals>.<listcomp>r*   )valuesr*   r*   r+   get_rref_list   s    r   c                 C   s   |   | S rE   to_here)r   r_   r*   r*   r+   add_rref_to_value   s    r   c                 C   s
   | j | S rE   rh   )Zpickle_cls_instancetensorr*   r*   r+   run_nested_pickle   s    r   c                 C   s<   dddgdddgg}dddg}t ||d}| r8| }|S )Nr   r&   rj         r   )rj   r   )r'   Zsparse_coo_tensorcoalesce)r   ivr   r*   r*   r+   build_sparse_tensor   s    
r   c                  C   s:   t dd} | | g}||g}| |g}| |i}| ||||gS Nr   )r'   r)   )rz   r}   cr   r   r*   r*   r+   build_complex_tensors   s    r   c                 C   s8   |   rtd|  s tdt| |s4td| S )Nzt_view is contiguous!zt_cont is not contiguous!zt_view is not equal to t_cont!)is_contiguous	Exceptionr'   equal)t_viewt_contr*   r*   r+   non_cont_test   s    r   c                 C   s   | | | S rE   r*   rz   r}   r   r*   r*   r+   my_function   s    r   c                 C   s   | | S rE   r*   )rz   r}   r*   r*   r+   rl      s    rl   c                 C   s&   | d }| dd  D ]}||7 }q|S rn   r*   )rz   ra   r   r*   r*   r+   my_container_sum   s    
r   r&   c                 C   s"   t |  ttdtdS r%   )rw   rx   r'   r6   r   )secondsr*   r*   r+   my_sleep_func  s    
r   c                 C   sR   | d }| D ]}||7 }q|  D ]\}}||7 }q"|j}||d |d |d fS )Nr   r&   rj   )itemsrf   )Z
list_inputZtensor_class_inputZ
dict_inputresr:   kr   Zcomplex_tensorsr*   r*   r+   my_complex_tensor_function  s    

r   c                 C   s   |   |   S rE   r   )rref_arref_br*   r*   r+   my_rref_function  s    r   皙?c                 C   s   t | | | S rE   rw   rx   )rz   r}   r   r*   r*   r+   delayed_add  s    
r   c                 C   s   | S rE   r*   r|   r*   r*   r+   identity  s    r   c                   C   s   t d d S )Nz
do nothing)printr*   r*   r*   r+   	no_result  s    r   c                 C   s   |   dkrtd| d S )Nrj   Expected errorr&   )Znumel
ValueErrorr^   r*   r*   r+   raise_or_inc   s    r   c                 C   s   t j| tjtdddfdS Nrj   r&   rp   )rpcrpc_syncr'   r(   r)   dstr*   r*   r+   
nested_rpc%  s    r   c                 C   s   t j| tjt t fdS Nr   )r   r   r'   r(   r   r   r*   r*   r+   nested_rpc_sparse)  s
    
r   c                 C   s>   |dkr:t | }| d | }tj|t|||d fd dS d S Nr   r&   r   )r!   r   	rpc_asyncmulti_layer_nested_async_rpc)r   rG   ttlcurrent_dstnext_dstr*   r*   r+   r   1  s    r   c                 C   s<   t j| tjtdddfdt j| tjtdddfdfS r   r   remoter'   r(   r)   r   r*   r*   r+   nested_rref?  s    r   c                 C   s4   t j| tjt t fdt j| tjt t fdfS r   )r   r   r'   r(   r   r   r*   r*   r+   nested_rref_sparseF  s    

r   c                 C   s&   t j| tjtdddfd}| S )Nrj   r   r   )r   r   r'   r(   r)   r   r   r   r*   r*   r+   nested_remoteU  s    r   c                 C   s"   t j| tjt t fd}| S r   )r   r   r'   r(   r   r   r   r*   r*   r+   nested_remote_sparseY  s    r   c                 C   sJ   |dkr>t | }| d | }tj|t||||d fd}|gS | S d S r   )r!   r   r   rref_forward_chainr   )r   rG   r   r   r   r   ret_rrefr*   r*   r+   r   ^  s      r   c                 C   s   t j| tjtdddfdS r   r   r   r*   r*   r+   rpc_return_rrefj  s    r   c                   C   s   dS rB   r*   r*   r*   r*   r+   	light_rpcn  s    r   c                 C   s(   t ddD ]}| |9 } | |d  } q
dS Nr&   d   r   rP   r   r   r*   r*   r+   	heavy_rpcr  s    r   c                 C   s(   t ddD ]}| |9 } | |d  } q
dS r   r   r   r*   r*   r+   heavy_rpc_sparsey  s    r   c                 C   s(   t ddD ]}| |9 } | |d  } q
dS r   r   r   r*   r*   r+   heavy_rpc_torchscript  s    r   c                 C   s   t | | S rE   )r'   r(   r   r*   r*   r+   my_script_func  s    r   r   c                   C   s   t td S rE   )r   expected_errr*   r*   r*   r+   
raise_func  s    r   )r   returnc                 C   s   t | d S rE   )r   )r   r*   r*   r+   raise_func_script  s    r   z>
First line of error 
 next line of error 
 last line of errorc                   C   s   t td S rE   )r   expected_err_escaper*   r*   r*   r+   raise_func_escape  s    r   c                 C   s   | a d S rE   global_rrefr   r*   r*   r+   set_global_rref  s    r   c                   C   s   d a d S rE   r   r*   r*   r*   r+   clear_global_rref  s    r   c                 C   s   |   S rE   )Zconfirmed_by_ownerr   r*   r*   r+   check_rref_confirmed  s    r   c                   C   s   t  S rE   )r	   r*   r*   r*   r+   get_rref_debug_info  s    r   c                    s@   t j   fdd}tj| tj||fd}||   S )Nc                    s     |    d S rE   r]   waitfutoutzr*   r+   callback  s    z#add_use_future_cb.<locals>.callbackr   )	
concurrentfuturesr   r   r   r'   r(   thenra   )toxyr   r   r   r*   r   r+   add_use_future_cb  s
    

r   c                 C   s
   |   jS rE   )r   Zprocess_global_function_events)Zprofile_rrefr*   r*   r+   get_events_from_profile  s    r   c                    s<   t j  tj| t j||fd}| fdd   S )Nr   c                    s     |   S rE   r   r   r   r*   r+   <lambda>      z+add_use_future_set_result.<locals>.<lambda>r'   r   r   r   r   r(   r   r   )r   r   r   r   r   r*   r   r+   add_use_future_set_result  s    
r  c                    sB   t j   fdd}tjt j||fd}||   S )Nc                    s0   t jtj|  fd}| fdd d S )Nr   c                    s     |  S rE   r   )fut2)r   r*   r+   r     r   z<add_use_future_nested_cb.<locals>.callback.<locals>.<lambda>r   r   r'   r(   r   r   )fut1r  r   r   r   r*   r+   r     s    z*add_use_future_nested_cb.<locals>.callbackr   r  )r   r   r   r   r   r  r*   r  r+   add_use_future_nested_cb  s
    

r  c                 C   s   d S rE   r*   r   r*   r*   r+   fail_on_fut  s    r  c                   C   s   t dd S )Nr   RuntimeErrorr*   r*   r*   r+   async_raise_func  s    r  c                   C   s   t ddS ri   )r'   zerosr*   r*   r*   r+   async_wrong_type  s    r  c                 C   s   t j| tj||fdS r   )r   r   r'   r(   )r   r   r   r*   r*   r+   	async_add  s    r  cpuc                 C   s.   t d | |} ||}t| | S r%   )rw   rx   r   r'   r(   r  )r   r   r2   r*   r*   r+   slow_add  s    


r  c                 C   s   t j| t|||fdS r   )r   r   r  )r   r   r   r2   r*   r*   r+   slow_async_add  s    r  c                    s4   t j  tj| t j||fd fdd  S )Nr   c                    s     |   S rE   r   r  r   r   r*   r+   r     r   z,async_add_with_future_ctor.<locals>.<lambda>r'   r   r   r   r   r(   r   r   r   r   r   r*   r  r+   async_add_with_future_ctor  s
    
r  c                    s$   t j| tj||fd fddS )Nr   c                    s   |     S rE   r   r   r   r*   r+   r     r   z#async_add_chained.<locals>.<lambda>r   r   r'   r(   r   r  r*   r  r+   async_add_chained  s    
r  c                    s:   t j| tj|dfd}t|D ]}| fdd}q|S )Nr   r   c                    s   |     S rE   r  r   stepr*   r+   r     r   z)async_add_chained_multi.<locals>.<lambda>)r   r   r'   r(   rP   r   )r   r   numr  r   _r*   r  r+   async_add_chained_multi  s    r  c                    s$   t j| t| ||fd fddS )Nr   c                    s   |     S rE   r  r   r  r*   r+   r   
  r   z"async_add_nested.<locals>.<lambda>)r   r   r  r   r  r*   r  r+   async_add_nested  s    
r   c                    s   g  t |D ]F}|dkr6 tj| tj||fd q tj| tjd|fd qt dt|dtj	  fdd} D ]}|
| qS )Nr   r   )cntretc              	      sV   H d  d7  < d  |   7  < d t krHd  W 5 Q R X d S )Nr!  r&   r"  )r   lenr]   r   futslockZ
ret_futurestater*   r+   inc_and_set  s
    z+async_add_multi_fanout.<locals>.inc_and_set)rP   appendr   r   r'   r(   r   Z
zeros_liker   r   r   )r   r   r  r  r   r(  r   r*   r$  r+   async_add_multi_fanout  s    
r*  c              
   C   s   | j }tj|}tj|}|| tj|H tjtdt	   | 
d t|gd}||  |W  5 Q R  S Q R X d S )N  r&   devices)r2   r'   r1   current_streamStreamwait_streamstream_sleepintr   fill_r   r]   )r:   r2   Zoriginal_streamZ
new_streamr   r*   r*   r+   async_cuda_sleep_and_set_to_one)  s    


r5  c                    s(    fdd}t j| tj||fd|S )Nc                    s"   t jtdt   |    S )Nr+  )r'   r1   r2  r3  r   r_   r   r  r*   r+   cb9  s    z!async_cuda_nested_add.<locals>.cbr   r  )r   r   r   r   r6  r*   r  r+   async_cuda_nested_add7  s    r7  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	TensorWrapper)r   r&  eventthreadc                 C   s6   || _ t | _tjjdd| _t | _	| j	
  d S )NT)Zenable_timing)r   r   r&  r'   r1   r   r9  	threadingThreadr:  startrv   r*   r*   r+   rI   E  s
    
zTensorWrapper.__init__c              	   C   s$   | j  |  j|7  _W 5 Q R X d S rE   )r&  r   )rH   r   r*   r*   r+   increaseM  s    zTensorWrapper.increasec              
   C   s2   | j " | j  | j W  5 Q R  S Q R X d S rE   )r&  r9  recordr   sumrQ   r*   r*   r+   r@  Q  s    
zTensorWrapper.sumN)rS   rT   rU   	__slots__rI   r>  r@  r*   r*   r*   r+   r8  B  s   r8  c                   @   sD   e Zd Zeejjdd Zeejjdd Z	ejjdd Z
dS )AsyncExecutionClassc                    s$   t j| tj||fd fddS )Nr   c                    s   |     S rE   r  r   r  r*   r+   r   ]  r   z6AsyncExecutionClass.static_async_add.<locals>.<lambda>r  r  r*   r  r+   static_async_addY  s    
z$AsyncExecutionClass.static_async_addc                    s4   t j  tj|t j||fd fdd  S )Nr   c                    s     |   S rE   r   r   Zret_futr   r*   r+   r   e  r   z5AsyncExecutionClass.class_async_add.<locals>.<lambda>r  )r   r   r   r   r   r*   rD  r+   class_async_add`  s
    
z#AsyncExecutionClass.class_async_addc                    s$   t j|tj||fd fddS )Nr   c                    s   |     S rE   r  r   r  r*   r+   r   l  r   z5AsyncExecutionClass.bound_async_add.<locals>.<lambda>r  )rH   r   r   r   r   r*   r  r+   bound_async_addi  s    
z#AsyncExecutionClass.bound_async_addN)rS   rT   rU   r   r   	functionsasync_executionrC  r   rE  rF  r*   r*   r*   r+   rB  W  s   rB  c                   C   s
   t j S rE   )r'   r   r   r*   r*   r*   r+   return_futurep  s    rI  c                   @   s   e Zd Zdd ZdS )FooBackendOptionsc                 C   s   t j|  || _d S rE   )r   RpcBackendOptionsrI   init_method)rH   rL  r*   r*   r+   rI   u  s    zFooBackendOptions.__init__N)rS   rT   rU   rI   r*   r*   r*   r+   rJ  t  s   rJ  c                       s$   e Zd Z fddZdd Z  ZS )MyEmbeddingBagModelc                    s"   t    tjjdd|d| _d S )Nr   sparse)superrI   r'   nnZEmbeddingBageb)rH   rO  	__class__r*   r+   rI     s    
zMyEmbeddingBagModel.__init__c                 C   s
   |  |S rE   )rR  rH   r   r*   r*   r+   forward  s    zMyEmbeddingBagModel.forwardrS   rT   rU   rI   rV  __classcell__r*   r*   rS  r+   rM    s   rM  c                   @   s4   e Zd Zdd Zedd Zeejjdd Z	dS )MyParameterServerc                 C   s0   t  | _|| _d| _d| _g | _d | _d | _d S rB   )r   r&  trainers	iterationupdatesr   totalgradient)rH   rZ  r*   r*   r+   rI     s    zMyParameterServer.__init__c                 C   s
   |   jS rE   )r   r^  r   r*   r*   r+   get_gradient  s    zMyParameterServer.get_gradientc              	   C   s   |   }tj }|j ||jkr:||_d|_|j  |j| |j	d krX||_	n| j	|7  _	| jd7  _|j
|jkr|j	t|j
 |_|jD ]}|j	t|j
 }|| qW 5 Q R X |S rn   )r   r'   r   r   r&  r[  r\  clearr)  r]  rZ  floatr^  r]   )r   Z
riterationr   rH   r   ra   r*   r*   r+   average  s$    




zMyParameterServer.averageN)
rS   rT   rU   rI   r   r_  r   rG  rH  rb  r*   r*   r*   r+   rY    s   	
rY  c                       s.   e Zd Z fddZdddZdd Z  ZS )	MyConvNetForMNISTc                    sv   t    ttddddt tddddt tdtdtddt tdd		|| _
|| _d S )	Nr&      r       rj   i      r   )rP  rI   rQ  Z
SequentialZConv2dZReLUZ	MaxPool2dZFlattenLinearr   netr2   )rH   r2   rS  r*   r+   rI     s    



zMyConvNetForMNIST.__init__Fc              
   C   sX   |r|  n|}tjtj| j( tjdt  | |W  5 Q R  S Q R X d S r   )	r   r'   r1   r1  r.  r2   r2  FIFTY_MIL_CYCLESrh  )rH   r   Zis_rrefr*   r*   r+   rV    s    zMyConvNetForMNIST.forwardc                 C   s   i S rE   r*   rQ   r*   r*   r+   rm     s    zMyConvNetForMNIST.__getstate__)F)rS   rT   rU   rI   rV  rm   rX  r*   r*   rS  r+   rc    s   
rc  c                   @   s   e Zd Zd6ddZdd Zdd Zdd	 Zd
d Zdd Zd7ddZ	dd Z
dd Zdd Zd8ddZdd Zdd Zdd  d!d  fd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdS )9RpcTestCommonNc                 C   s`   |t jkrtj||||dS |t jkr<tj||||d S |t jkr\tj||||d	 S d S )Nrp   rW   )
r   SYNCr   r   ASYNCr   r   REMOTEr   r   )rH   r   fnmoderp   rW   r*   r*   r+   _run_func_in_mode  s    


zRpcTestCommon._run_func_in_modec                 C   s2   t j|t|||fd}| | || |  d S r   )r   r   r   assertEqualr   )rH   worker_infor   r   r   r   r*   r*   r+   _self_py_udf_remote  s    z!RpcTestCommon._self_py_udf_remotec           	      C   s   t  }t j|t|||fd}t j|t||fd}t j|t||| fd}| ||| | | |  | | || | |  d S r   )	r   get_worker_infor   r   r   r   r   rr  r   )	rH   r   r   r   r   self_worker_infor   r   r"  r*   r*   r+   _self_remote_rref_as_rpc_arg  s    z*RpcTestCommon._self_remote_rref_as_rpc_argc                 C   sR   t  }t j|t|||fd}t j|t||fd}| | || | |  d S r   )r   ru  r   r   r   rr  r   )rH   r   r   r   r   rv  r   r   r*   r*   r+   _self_remote_rref_as_remote_arg  s     z-RpcTestCommon._self_remote_rref_as_remote_argc                    sp    j dkrltjd jdd jd  fdd} fdd} fd	d
}||| ||| ||| t  d S )Nr   mer&   rJ   backendrN   rG   rZ   c                    s,   | d }t jdt| |fd} || d S Nrj   ry  r   )r   r   rl   rr  r   r   expectra   rQ   r*   r+   	_rpc_sync  s    z0RpcTestCommon._world_size_one.<locals>._rpc_syncc                    s0   | d }t jdt| |fd } || d S r|  )r   r   rl   r   rr  r}  rQ   r*   r+   
_rpc_async  s    
z1RpcTestCommon._world_size_one.<locals>._rpc_asyncc                    s0   | d }t jdt| |fd } || d S r|  )r   r   rl   r   rr  r}  rQ   r*   r+   _remote
  s    
z.RpcTestCommon._world_size_one.<locals>._remote)rN   r   init_rpcrpc_backendrZ   shutdown)rH   rz   r}   r  r  r  r*   rQ   r+   _world_size_one  s    
			


zRpcTestCommon._world_size_onec                 C   s   | j d | j }tdD ]n}|| j  d }|rDt | }t | }ntdd}tdd}tjt|tj	||fd}| 
||d  qd S )Nr&      rj   r   )rN   rG   rP   r   r'   r)   r   r   r!   r(   rr  )rH   rO  dst_rankr   nr   r   r"  r*   r*   r+   
_multi_rpc  s    
zRpcTestCommon._multi_rpc   c           	      C   s   | j dkr| | jdk d}g }t|D ] }tj|||fd}|| q*tj	|
 D ]}| |
 d q\d}g }t|D ] }tj|||fd}|| qtj|D ]}| |d qd S )Nr   r   worker1r   Zworker2)rN   
assertTruerG   rP   r   r   r)  r'   r   Zcollect_allr   rr  wait_all)	rH   r   r   Z
num_repeatr   r%  r  r   rs   r*   r*   r+   _run_uneven_workload*  s     
z"RpcTestCommon._run_uneven_workloadc                 C   sb   t | j| j| j tjd| j | j| j| j| jd | || tj	
  t  tjdd d S Nworker%drz  Fgracefulr   file_init_methodrN   rG   r   r  r  rZ   r  api_wait_all_workersdistbarrierr  rH   r   r   r*   r*   r+   r  F  s    
zRpcTestCommon._wait_all_workersc                 C   sl   t | j| j| j tjd| j | j| j| j| jd | || tj	
  tj	
  t  tjdd d S r  r  r  r*   r*   r+   _wait_all_workers_twice\  s    

z%RpcTestCommon._wait_all_workers_twicec                 C   s@   | j d }|| j }tjt||t| j fd}| || d S Nr&   r   )rN   rG   r   r   r!   rr  )rH   r   expectedr  r  r"  r*   r*   r+   _nested_rpcs  s    


zRpcTestCommon._nested_rpcr+  r*   c                 C   s   | j d }|| j }g }t }t|D ]"}tjt|||d}	||	 q(tj	
|D ]}
| |
d qXt }td| j |||  d S )Nr&   r   r   z0Rank {} finished testing {} times in {} seconds.)rN   rG   rw   rP   r   r   r!   r)  r'   r   r  rr  r   format)rH   r   repeatrp   r  r  r%  tikr  r   rs   tokr*   r*   r+   _stress_test_rpc}  s"    

  zRpcTestCommon._stress_test_rpcc                 C   sB   | j d }|| j }tjt|tj||fd}| | | d S r  )	rN   rG   r   r   r!   r'   r(   rr  r   )rH   r   r   r  r  r  r   r*   r*   r+   _builtin_remote_ret  s    

z!RpcTestCommon._builtin_remote_retc                 C   s0   t jt| jtj||fd}| | | d S r   )r   r   r!   rN   r'   r(   rr  r   )rH   r   r   r  r   r*   r*   r+   _builtin_remote_self  s    z"RpcTestCommon._builtin_remote_selfc                 C   s   dS )Nr*   r*   r   r   r*   r*   r+   r     r   zRpcTestCommon.<lambda>c                 C   s   i S rE   r*   r  r*   r*   r+   r     r   c              
   C   s   d}| j d }|| j }g }g }	t|D ]P}
||
 }|tjt||||||||d |	||||||| q(t|D ]}
| ||
  |	|
  qd S )Nr   r&   rk  )	rN   rG   rP   r)  r   r   r!   rr  r   )rH   ro  rO  args_fn	kwargs_fnmr  r  rrefsr  r   r*   r*   r+   _test_multi_remote_call  s$    

z%RpcTestCommon._test_multi_remote_callc                 C   st   | j d }|| j }tjt|tj||fd}tjt|tj||fd}	tjt|t||	fd}
| |
	 | d S r  )
rN   rG   r   r   r!   r'   r(   r   rr  r   )rH   rz   r}   r   r   r  r  r  r   r   rref_cr*   r*   r+   _py_rref_args  s$    

      zRpcTestCommon._py_rref_argsc                 C   s   | j d }|| j }	|d | j }
tjt|	t|||fd}tjt|	t|||fd}tjt|
t||fd}| | | d S r  )	rN   rG   r   r   r!   r   r   rr  r   )rH   rz   r}   r   r   r   r   r  r  
owner_rankZ	user_rankr   r   r  r*   r*   r+   _py_rref_args_user_share  s&    

      z&RpcTestCommon._py_rref_args_user_sharec                 C   sp   | j d }|| j }	tjt|	t|||fd}
tjt|	t|||fd}tjt|	t|
|fd}| || d S r  )	rN   rG   r   r   r!   r   r   r   rr  )rH   rz   r}   r   r   r   r   r  r  r  r   r   r*   r*   r+   _py_rpc_rref_args  s$    

      zRpcTestCommon._py_rpc_rref_argsc                 C   sP   | j d }|| j }|d | j }tjt||t|fd}| | | d S r  )rN   rG   r   r   r!   rr  r   )rH   r   r  r  	dst_rank1	dst_rank2r   r*   r*   r+   _nested_remote  s    

zRpcTestCommon._nested_remotec           	      C   s   | j d }|| j }|d | j }tjt||t|fd}| }| t|d | |d  | | |d  | d S )Nr&   r   rj   r   )rN   rG   r   r   r!   r   rr  r#  )	rH   r   	expected1	expected2r  r  r  rref_of_rrefsr  r*   r*   r+   _nested_rref  s    

	zRpcTestCommon._nested_rrefc                 C   s   | j d }|| j }|d | j }g }tdD ]$}|tjt||t|fd q.tdD ]L}	||	 }
|
 }| t	|d | |d  | | |d  | q\d S )Nr&   r  r   rj   r   )
rN   rG   rP   r)  r   r   r!   r   rr  r#  )rH   r   r  r  r  r  r  Z	all_rrefsr  r   r  r  r*   r*   r+   _nested_rref_stress  s$    

z!RpcTestCommon._nested_rref_stressc           
      C   s   t |d}t }tdD ]}|tdd }||tdd  t|	 d j
}| |||}| }|jr|  }| |}	|	jr|	  }	| t||	 qd S )NrN  r   r   )rM  rQ  ZMSELossrP   r'   randlongbackwardlist
parametersgradr   rb  r   Z	is_sparseZto_densedoubler   r_  r  r   )
rH   r   rO  r  Zloss_fnr   outputsr^  r   Zps_gradientr*   r*   r+   _trainer_func  s    
zRpcTestCommon._trainer_funcc                 C   sd   t t| jd }g }td| jD ]0}|tjt| j| | j | j	||fd q"t
j| d S r  )r   rY  rG   rP   r)  r   r   r!   rN   r  r'   r   r  )rH   rO  Zps_rrefr   indexr*   r*   r+   _my_parameter_server&  s    
z"RpcTestCommon._my_parameter_serverc           
   
   C   sz  t dgd}tjdV tj }tj }tj| |rht d}t d}||  }	n$tj	ddd}tj
ddd}|| }	tjtdt   ||7 }|r| }||| W 5 Q R X tj| || }|rH| t| |	    | t| |	    | | |	  n| t||	   W 5 Q R X W 5 Q R X d S )Ncuda:0r,  r   r2   r+  )r   r'   r1   r2   r/  r1  r   r   r   r  r)   r2  r3  r   r]   r   r  eqindicesallitemr   rr  size)
rH   wrapper	unwrappersparse_tensorfuturer1  another_streamr   Z
add_tensorexpected_tensorr*   r*   r+   _test_cuda_future_extraction6  s0    

""z*RpcTestCommon._test_cuda_future_extraction)NN)r  )r+  r*   )rS   rT   rU   rq  rt  rw  rx  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r*   r*   r*   r+   rj    s.   
+



rj  c                   @   s  e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd Zedd Zdd Zedd Zedd Zedd Zdd Zedd  Zed!d" Zejejjd#ejejjjd$ed%d&d'd( Zed%d&d)d* Zed%d&d+d, Zed%d&d-d. Z ed%d&d/d0 Z!d1d2 Z"ed%d&d3d4 Z#ed5d6 Z$ed7d8 Z%e&d9d: Z'ed;d< Z(ed=d> Z)ed?d@ Z*edAdB Z+edCdD Z,edEdF Z-edGdH Z.ed%d&dIdJ Z/dKdL Z0dMdN Z1edOdP Z2edQdR Z3ddSdTZ4edUdV Z5edWdX Z6edYdZ Z7ed[d\ Z8ed]d^ Z9ed%d&d_d` Z:edadb Z;edcdd Z<ededf Z=dgdh Z>didj Z?edkdl Z@edmdn ZAedodp ZBedqdr ZCdsdt ZDedudv ZEedwdx ZFdydz ZGdd|d}ZHed~d ZIedd ZJedd ZKdd ZLdd ZMedd ZNedd ZOdddZPdd ZQedd ZRedd ZSdd ZTedd ZUedd ZVdd ZWedd ZXedd ZYdd ZZedd Z[edd Z\dd Z]edd Z^edd Z_dd Z`edd Zaedd Zbdd Zcedd Zdedd Zedd Zfedd Zgedd Zhdd Ziedd ZjeddĄ ZkddƄ ZleddȄ Zmeddʄ Znedd̄ Zoedd΄ ZpeddЄ Zqedd҄ ZreddԄ Zseddք Ztedd؄ Zueddڄ Zvedd܄ Zweddބ Zxedd Zyedd Zzedd Z{edd Z|edd Z}edd Z~edd Zedd Zedd Zedd Zedd Ze&dddZedd Zedd Ze&dddZedd Zedd  Zedd Zedd Zedd Zedd Zed	d
 Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zdd Zdd Zedd  Zd!d" Zd#d$ Zed%d& Zd'd( Zd)d* Ze&d+d, Zed-d. Zed/d0 Zed1d2 Zed3d4 Zed5d&d6d7 Zejejjjd8d9d: Zed%d&d;d< Zed%d&d=d> Zed?d@ ZedAdB ZedCdD ZedEdF Zed%d&dGdH ZedIdJ Zed%d&eedKdLdM ZedNdO Zed%d&dPdQ Zed%d&dRdS ZedTdU ZedVdW ZdXdY ZdZd[ Zed\d] Zed^d_ Zed`da Zedbdc Zeddde ZdZe&dfdg Zedhdi Zedjdk Zedldm Zedndo Zedpdq Zed%d&drds Ze&dtdu Ze&dvdw Ze&dxdy Zǐdzd{ Zed|d} Zed~d Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zېdd Zedd Zedd Zސdd Zedd Zedd Zedd Zdd Zedd Zedd Zedd Zedd ZejfddZedd Zedd Zedd ZeddÄ ZedĐdń ZedƐdǄ ZedȐdɄ Zedʐd˄ Zed̐d̈́ Zedΐdτ ZejfdАdфZedҐdӄ ZedԐdՄ Zed֐dׄ ZejfdؐdلZedڐdۄ Zedܐd݄ Zedސd߄ Zedd Zedd Zedd Zdd Z edd Zedd Zedd Zedd Zed%d&eejdddkddd Zed%d&eejdddkddd Z	edd Z
edd Zed%d&eejdddkddd Zdd  Zdd Zedd Ze&dd Ze&dd Zed	d
 Zed%d&dd Zedd ZdS (  RpcTestc              	   C   sv   | j d }|| j }t }tt|}| |jt| j  | |jt| | td td}W 5 Q R X d S )Nr&   zcould not find destinationZWorkerUnknown)	rN   rG   r   ru  r!   rr  rJ   assertRaisesRegexr
  )rH   r  Z	peer_rankrv  Zpeer_worker_infoZunknown_worker_idr*   r*   r+   test_worker_idV  s    

zRpcTest.test_worker_idc                 C   sh   t j  }dd |D }dd t| jD }| || dd |D }tt| j}| || d S )Nc                 S   s   h | ]
}|j qS r*   rJ   rM   rs  r*   r*   r+   rO   g  s     z0RpcTest.test_get_worker_infos.<locals>.<setcomp>c                 S   s   h | ]}t |qS r*   )r!   rL   r*   r*   r+   rO   h  s    c                 S   s   h | ]
}|j qS r*   )rK   r  r*   r*   r+   rO   m  s     )r   r  _get_current_rpc_agentrR   rP   rG   rr  rt   )rH   Zworker_infosZworker_namesZexpected_worker_namesZ
worker_idsZexpected_worker_idsr*   r*   r+   test_get_worker_infosc  s    zRpcTest.test_get_worker_infosc                 C   s   t  }t| j}t j|tjtdddfd}t j|tjtdddfd}| 	|
 tddd  | 	|tddd  d S r   )r   ru  r!   rN   r   r'   r(   r)   r   rr  r   )rH   rv  self_worker_namer   r"  r*   r*   r+   test_self_addq  s    
zRpcTest.test_self_addc              
   C   s  | j d | j }tjtjtjfD ]<}| j|tj|t	dddfd}| 
|t	ddd  q tjtjtjfD ]@}| t, | j| jd tj|t	dddfd W 5 Q R X qntjtjtjfD ]:}| t& | jdtj|t	dddfd W 5 Q R X qtjtjtjfD ]@}| t* | j|d tj|t	dddfd W 5 Q R X qtjtjtjfD ]@}| t* | j|d tj|t	dddfd W 5 Q R X q^d S )Nr&   rj   r   r-         ?)rN   rG   r   rl  rm  rn  rq  r'   r(   r)   rr  assertRaisesr
  r   )rH   r  	exec_moder"  r*   r*   r+   test_send_to_rankz  s      2,2zRpcTest.test_send_to_rankc                 C   s    |  t tdddd d S Nrj   r&   r   )rt  r   ru  r'   r)   rQ   r*   r*   r+   test_self_py_udf_remote  s    
zRpcTest.test_self_py_udf_remotec                 C   s0   t | jd | j }| |tdddd d S Nr&   rj   r   )r!   rN   rG   rw  r'   r)   rH   r   r*   r*   r+    test_self_remote_rref_as_rpc_arg  s    
z(RpcTest.test_self_remote_rref_as_rpc_argc                 C   s    |  t tdddd d S r  )rw  r   ru  r'   r)   rQ   r*   r*   r+   %test_self_remote_rref_as_self_rpc_arg  s    
z-RpcTest.test_self_remote_rref_as_self_rpc_argc                 C   s0   t | jd | j }| |tdddd d S r  )r!   rN   rG   rx  r'   r)   r  r*   r*   r+   #test_self_remote_rref_as_remote_arg  s    
z+RpcTest.test_self_remote_rref_as_remote_argc                 C   s    |  t tdddd d S r  )rx  r   ru  r'   r)   rQ   r*   r*   r+   (test_self_remote_rref_as_self_remote_arg  s    
z0RpcTest.test_self_remote_rref_as_self_remote_argc              	   C   s   t | jd | j }tj|ttddddfd}d}| t	| |
   W 5 Q R X | t	| |    W 5 Q R X | t	| |   W 5 Q R X d S )Nr&   rj   r   r   zhas no attribute 'non_exist')r!   rN   rG   r   r   r   r'   r)   r  AttributeErrorr   Z	non_existr   r   )rH   r   r   msgr*   r*   r+   test_rref_proxy_non_exist  s    z!RpcTest.test_rref_proxy_non_existc                 C   s   t j|ttddddfd}tddd d }| | |   | |d | 	d
  | |dd| dd  d S )Nrj   r&   r   r   r   )r   r   r   r'   r)   rr  r  r   r   r(   r   viewr   rH   r   r   r  r*   r*   r+   _test_rref_proxy_tensor  s
    zRpcTest._test_rref_proxy_tensorc                 C   s   |  t| jd | j  d S r%   )r  r!   rN   rG   rQ   r*   r*   r+   test_rref_proxy_tensor  s    zRpcTest.test_rref_proxy_tensorc                 C   s   |  t  d S rE   )r  r   ru  rQ   r*   r*   r+   test_rref_proxy_tensor_self  s    z#RpcTest.test_rref_proxy_tensor_selfc                 C   sD  t jt| jd | j ttddddfd}tddd d }| }|	 }| }| 
| |  | 
|d |d | 
|dd|dd | 
| |   | 
|d |d  | 
|dd|dd  | 
| |   | 
|d |d  | 
|d|d  d S )Nr&   rj   r   r   r   r   r-   )r   r   r!   rN   rG   r   r'   r)   r   r   rr  r  r(   r  r   r   )rH   r   r  Zproxy_rpc_syncZproxy_rpc_asyncZproxy_remoter*   r*   r+   test_rref_proxy_reuse  s$     zRpcTest.test_rref_proxy_reusec              
   C   s`  t j|tdd}td}| | |   | | |    | | |    |	d | d | 	d | d | 	d  | d | 	d  | | |   | | |    | | |    | |
d| 
d | |
d| 
d  | |
d| 
d  | |d| d | |d	| d	  | |d
| d
  | |dtdd| dtdd | |dtdd| dtdd  | |dtdd| dtdd  d S )N)   r   r  r   r&   rj   r   	   r      )r   r   ry   rr  r   r   r   r   r   r   r~   r   r   r'   r  r)   r  r*   r*   r+   _test_rref_proxy_class  s`    
zRpcTest._test_rref_proxy_classc                 C   s   |  t| jd | j  d S r%   )r  r!   rN   rG   rQ   r*   r*   r+   test_rref_proxy_class1  s    zRpcTest.test_rref_proxy_classc                 C   s   |  t  d S rE   )r  r   ru  rQ   r*   r*   r+   test_rref_proxy_class_self5  s    z"RpcTest.test_rref_proxy_class_self_initZ_set_and_start_rpc_agentFZ	setup_rpcc              	   C   sZ   d}t j|tt}| td t j|tt}W 5 Q R X t jd|| j| j	| j
d d S )NZstub_backendz$^RPC backend .+: already registered$r  rz  )r   Zbackend_registryZregister_backendrX   r[   r  r
  r  rN   rG   rZ   )rH   Zmock_rpc_agentZmock_dist_autograd_initbackend_namer{  r*   r*   r+   7test_register_rpc_backend_and_set_and_start_rpc_backend9  s,     z?RpcTest.test_register_rpc_backend_and_set_and_start_rpc_backendc              	   C   s\   |  tdF ttjj| j| j| jd\}}}t	j
| j|d| j| j| jd W 5 Q R X d S )Nis not unique)rN   rG   Zduplicate_name)r{  rY   rJ   rN   rG   rZ   )r  r
  nextr'   distributedZ
rendezvousrL  rN   rG   r   Z_init_rpc_backendr  rZ   )rH   rY   r  r*   r*   r+   test_duplicate_nameX  s       
zRpcTest.test_duplicate_namec              	   C   sH   |  td2 tjt| j| jd  | j| j| j| jd W 5 Q R X d S )Nr  r&   rz  )	r  r
  r   r  r!   rN   rG   r  rZ   rQ   r*   r*   r+   test_duplicate_name_2i  s    zRpcTest.test_duplicate_name_2c              	   C   s   t jt| j| j| j| j| jd t| j| j| j t	
  tjdd dkr^| jdkr^d}nd}| t|( t jt| j| j| j| j| jd W 5 Q R X t   d S )Nrz  RPC_INIT_WITH_TCP1r   zAddress already in usezis already initialized)r   r  r!   rN   r  rG   rZ   r   r  r  r  osenvirongetr  r
  r  )rH   Zexpected_reinit_errr*   r*   r+   test_reinitt  s*    zRpcTest.test_reinitc              	   C   s   t jd| j| j| jd G dd dtjj}| }|  tjj	
|}| td& g }| D ]}|t| qbW 5 Q R X d S )Ngloor{  rL  rN   rG   c                       s$   e Zd Z fddZdd Z  ZS )z1RpcTest.test_pg_init_no_rpc_init.<locals>.MyModelc                    s   t    tjdd| _d S )Nr   r   )rP  rI   r'   rQ  rg  linrQ   rS  r*   r+   rI     s    
z:RpcTest.test_pg_init_no_rpc_init.<locals>.MyModel.__init__c                 S   s
   |  |S rE   )r  rU  r*   r*   r+   rV    s    z9RpcTest.test_pg_init_no_rpc_init.<locals>.MyModel.forwardrW  r*   r*   rS  r+   MyModel  s   r  zBCurrent RPC agent is not set! Did you initialize the RPC framework)r  init_process_groupr  rN   rG   r'   rQ  ModuleZtrainparallelZDistributedDataParallelr  r
  r  r)  r   )rH   r  modelparamsparamr*   r*   r+   test_pg_init_no_rpc_init  s    z RpcTest.test_pg_init_no_rpc_initc                 C   s    |  tddtdd d S ri   )r  r'   r)   rQ   r*   r*   r+   test_world_size_one  s    

zRpcTest.test_world_size_onec              	   C   s   d}|  td td|}W 5 Q R X |  td td|}W 5 Q R X |  td td|}W 5 Q R X |  td$ tddd	 td
D |}W 5 Q R X d S )Nr   zWorker name must matchzabc* zmust be non-empty zshorter than 128c                 S   s   g | ]}d qS r|   r*   rM   r   r*   r*   r+   r     s     z.RpcTest.test_invalid_names.<locals>.<listcomp>i  )r  r
  r
   joinrP   )rH   Z	worker_idinfor*   r*   r+   test_invalid_names  s    zRpcTest.test_invalid_namesc                 C   s@   | j d | j }tj }tjt|t|fd}| || d S r  )	rN   rG   r   r  ru  r   r!   r   rr  )rH   r  rs  r"  r*   r*   r+   test_worker_info_pickle  s    
zRpcTest.test_worker_info_picklec                 C   sZ   | j d }|| j }tjt|tjt||t||fd}| |t||d  d S Nr&   r   rj   	rN   rG   r   r   r!   r'   r(   r)   rr  rH   r  r  r"  r*   r*   r+   test_add  s    

zRpcTest.test_addc                   C   s
   t  jS rE   )r   ru  rK   r*   r*   r*   r+   return_callee_id  s    zRpcTest.return_callee_idc                 C   s.   | j d | j }t|tj}| || d S r%   )rN   rG   r   r   r  r  rr  )rH   r  r"  r*   r*   r+   test_int_callee  s    zRpcTest.test_int_calleec                 C   sd   | j d }|| j }tt|}tj|tjt||t||fd}| 	|t||d  d S r  )
rN   rG   r   ru  r!   r   r'   r(   r)   rr  )rH   r  r  Zworkder_infor"  r*   r*   r+   test_add_with_id  s    

  zRpcTest.test_add_with_idc                 C   sR   | j d }|| j }tjt|tjt|||fd}| |t|||  d S r  r  r  r*   r*   r+   test_scalar_add  s    

  zRpcTest.test_scalar_addc                 C   s^   | j d }|| j }tjt|tjt||t||fd}| |	 t||d  d S r  )
rN   rG   r   r   r!   r'   r(   r)   rr  r   )rH   r  r  r   r*   r*   r+   test_async_add  s    

zRpcTest.test_async_addc                 C   s`   | j d }|| j }t| j| j}d|| j  | j < tjt|tj|fd}| ||  d S )Nr&   r   r   )	rN   rG   r'   r)   r   r   r!   Znonzerorr  )rH   r  r  r   r"  r*   r*   r+   test_nonzero  s    

zRpcTest.test_nonzeroc                 C   s   |  d d S NFr  rQ   r*   r*   r+   test_multi_rpc
  s    zRpcTest.test_multi_rpcc              
   C   s   t | jd | j }g }tdD ]}|t|t q | t	d t
j| W 5 Q R X |D ]$}| t	d |  W 5 Q R X q`d S )Nr&   r  r   )r!   rN   rG   rP   r)  r   r   r   r  r   r'   r   r  r   )rH   r   r%  r   r   r*   r*   r+   test_future_wait_twice  s    zRpcTest.test_future_wait_twicec              	   C   s   t | j| j| j tjt| j| j| j| j| jd tj	j
}dd }|tj	_
z*| td tjddd W 5 Q R X W 5 |tj	_
X t  d S )Nrz  c              
   S   sD   zt jjtd| d W n& tk
r> } z|W 5 d }~X Y nX d S )Nr  timeout)r   r  _all_gatherru   r
  )r'  exr*   r*   r+   wait_all_workers_sleep*  s    zERpcTest.test_wait_all_workers_timeout.<locals>.wait_all_workers_sleepr  T{Gz?)r  r'  )r   r  rN   rG   r   r  r!   r  rZ   r  r  r  r
  r  r  r  )rH   og_funcr*  r*   r*   r+   test_wait_all_workers_timeout  s     
z%RpcTest.test_wait_all_workers_timeoutc                 C   s   |  ttdd d S Nr   )r  r   r'   r)   rQ   r*   r*   r+   test_wait_all_workers_dense9  s    z#RpcTest.test_wait_all_workers_densec                 C   s   |  ttdd d S r.  )r  r   r'   r)   rQ   r*   r*   r+   !test_wait_all_workers_twice_dense<  s    z)RpcTest.test_wait_all_workers_twice_densec                 C   sH   t  }t j|j}i }t   D ]}|j||j< q&| || d S rE   )	r   ru  r  r(  rK   r  rR   rJ   rr  )rH   r  resultsr  r*   r*   r+   test_all_gather?  s    zRpcTest.test_all_gatherc              	   C   sr   t d | jdkr>| td t jtd W 5 Q R X n0|  }| t| t jtd W 5 Q R X d S )N皙?r   z,timed out in _all_gather after 0\.10 secondsr  )	r   _set_rpc_timeoutrN   r  r
  r  r(  ru   get_timeout_error_regex)rH   expected_errorr*   r*   r+   test_all_gather_timeoutI  s    

zRpcTest.test_all_gather_timeoutc                 C   s|   t |}|d }t|t |s6|j|kr6| td tj| t|t	 tj| |sx|j|krx| tt
| d S rB   )sortedr   r   rC   rJ   rr  r@   r  Z_barrierrA   r#  )rH   r  namesZmulti_threadedZleaderr*   r*   r+   _test_barrier_helperX  s    zRpcTest._test_barrier_helperc                 C   s2   t  }t   }dd |D }| || d S )Nc                 S   s   g | ]
}|j qS r*   r  rM   workerr*   r*   r+   r   i  s     z0RpcTest.test_rpc_barrier_all.<locals>.<listcomp>)r   ru  r  rR   r:  rH   r  all_worker_infor9  r*   r*   r+   test_rpc_barrier_alld  s    zRpcTest.test_rpc_barrier_allc                 C   sL   t  }t   }|jd r.dd |D }ndd |D }| || d S )Nrj   c                 S   s   g | ]}|j d  r|jqS rj   rK   rJ   r;  r*   r*   r+   r   r  s     
 z3RpcTest.test_rpc_barrier_subset.<locals>.<listcomp>c                 S   s   g | ]}|j d  s|jqS r@  rA  r;  r*   r*   r+   r   t  s     
 r   ru  r  rR   rK   r:  r=  r*   r*   r+   test_rpc_barrier_subsetl  s    
zRpcTest.test_rpc_barrier_subsetc                 C   sL   t  }t   }|jd r.dd |D }nd|j g}| || d S )Nrj   c                 S   s   g | ]}|j d  r|jqS r@  rA  r;  r*   r*   r+   r   }  s     
 z;RpcTest.test_rpc_barrier_partial_subset.<locals>.<listcomp>r<  rB  r=  r*   r*   r+   test_rpc_barrier_partial_subsetw  s    
z'RpcTest.test_rpc_barrier_partial_subsetc                 C   sr   t  }t   }dd |D }g }tdD ],}tj| j||dfd}|| |	  q.|D ]}|
  q`d S )Nc                 S   s   g | ]
}|j qS r*   r  r;  r*   r*   r+   r     s     z:RpcTest.test_rpc_barrier_multithreaded.<locals>.<listcomp>r   T)targetrp   )r   ru  r  rR   rP   r;  r<  r:  r)  r=  r  )rH   r  r>  r9  threadsr  thr*   r*   r+   test_rpc_barrier_multithreaded  s    

z&RpcTest.test_rpc_barrier_multithreadedc                 C   s   |  ttdd dS )zTest graceful termination.r   N)r  r   r'   r)   rQ   r*   r*   r+   +test_graceful_shutdown_with_uneven_workload  s    z3RpcTest.test_graceful_shutdown_with_uneven_workloadc              	   C   s   t jd| j | j| j| j| jd | jd }|| j }t jt|tj	t
||t
||fd}| |t
||d  t   | td0 t jt|tj	t
||t
||fd W 5 Q R X d S )Nr  rz  r&   r   rj   z^RPC has not been initialized)r   r  rN   r  rG   rZ   r   r!   r'   r(   r)   rr  r  r  r
  r  r*   r*   r+   test_shutdown_followed_by_rpc  s,    

z%RpcTest.test_shutdown_followed_by_rpcc                 C   sP   | j d | j }| j d | j }tjt|t| j fd}t }| || d S r  )	rN   rG   r   r   r!   r`   r\   ra   rr  )rH   r  Zexpected_src_rankr"  r_   r*   r*   r+   test_expected_src  s
    zRpcTest.test_expected_srcc                 C   sV   | j d }|| j }tjt|t||d |d fd}| |t||d |d  d S Nr&   rj   r   )rN   rG   r   r   r!   minrr  r  r*   r*   r+   test_py_built_in  s    

"zRpcTest.test_py_built_inc                 C   sX   | j d }|| j }tjt|t||d |d dd}| |t||d |d  d S Nr&   rj   r   rV   )rN   rG   r   r   r!   r   rr  r  r*   r*   r+   test_py_user_defined  s    

zRpcTest.test_py_user_definedc                 C   sZ   t jt jt jfD ]D}t|ddd}| |j| | d| | d| | d| qd S )Nfooworker0r  )r   rl  rm  rn  r   assertInr_   )rH   r  rpc_profiling_keyr*   r*   r+   test_build_rpc_profiling_key  s       z$RpcTest.test_build_rpc_profiling_keyc                 C   s~   |  ||jk |  ||jk t|tjjrH|  tj||jk n|  |j|jk |  |j	|jk | 
|jd d S r%   )r  rJ   
isinstancer'   jitZScriptFunction_jit_internal_qualified_namerS   r_   rr  count)rH   r  dst_worker_namero   	rpc_eventrpc_exec_moder*   r*   r+   check_profiling_info  s    zRpcTest.check_profiling_infoc              	   C   s   | j dkrd S | j d | j }t|}tdtd }}tdd}tj|tj||fd W 5 Q R X |j	}dd |D }dd |D d	 }|j
}	tdd}t|| W 5 Q R X |j	}
d
d |
D d	 }|j
}| |	| d S )Nr&   r   T)Zrecord_shapesr   c                 S   s   g | ]}|j r|qS r*   	is_remoterM   r9  r*   r*   r+   r     s      z;RpcTest.test_profiler_rpc_record_shapes.<locals>.<listcomp>c                 S   s   g | ]}d |j kr|qS r<   r  ra  r*   r*   r+   r     s    
 r   c                 S   s   g | ]}d |j kr|qS rb  r  ra  r*   r*   r+   r     s    
 )rN   rG   r!   r'   r)   _profiler   r   r(   function_eventsZinput_shapesrr  )rH   r   
dst_workerr8   r9   profrd  remote_eventsremote_add_eventZremote_add_input_shapeslocal_function_eventsZlocal_add_eventZlocal_add_input_shapesr*   r*   r+   test_profiler_rpc_record_shapes  s2    
 z'RpcTest.test_profiler_rpc_record_shapesc              	   C   s   | j dkrd S | j d | j }t|}tdd}tj|tdd}| }W 5 Q R X |j}t	dd |D }| 
dh| td	d}tj|tdd}| }W 5 Q R X |j}t	d
d |D }| dh| d S )Nr&   T)Zprofile_memoryr*   r   c                 s   s   | ]}|j V  qd S rE   Zcpu_memory_usagera  r*   r*   r+   	<genexpr>  s     z3RpcTest.test_profiler_rpc_memory.<locals>.<genexpr>r   Fc                 s   s   | ]}|j V  qd S rE   rk  ra  r*   r*   r+   rl    s     )rN   rG   r!   rc  r   r   r;   r   rd  rt   assertNotEqualrr  )rH   r   re  pr   r   rd  Zevent_cpu_mem_usagesr*   r*   r+   test_profiler_rpc_memory  s     
z RpcTest.test_profiler_rpc_memoryc              
      s   | j dkrd S | j d | j }t|}t }tj|tdd}| }W 5 Q R X |j}t	 t}|}|
| t|R}	t|	}
dd |
D }ttjjg D ]$ t fdd|D }| | qW 5 Q R X W 5 Q R X d S )Nr&   r*   r   c                 S   s   g | ]}|d  qS r  r*   ra  r*   r*   r+   r   %  s     z6RpcTest.test_profiler_export_trace.<locals>.<listcomp>c                    s   g | ]} |kqS r*   r*   )rM   
event_nameZexpected_event_namer*   r+   r   '  s     )rN   rG   r!   rc  r   r   r;   r   rd  r#   Zexport_chrome_traceopenjsonloadEXPECTED_REMOTE_EVENTSr   rm  r_   anyr  )rH   r   re  rn  r   r   eventsfnamepathr   traceZevent_namesZevent_existsr*   rq  r+   test_profiler_export_trace  s"    



z"RpcTest.test_profiler_export_tracec              
      s   j dkrd S fddtdjD }fdd|D ]V}t| d}tjj|d2 fd	dt|D }|D ]}|  qvW 5 Q R X q8d S )
Nr&   c                    s   g | ]}| j kr|qS r*   rN   rL   rQ   r*   r+   r   3  s     
 z7RpcTest.test_profiler_rpc_key_names.<locals>.<listcomp>r   c           
   	      s   t  }tj| tdd}|  W 5 Q R X |j}dd |D }ttjtj	t
j| }tt}| D ]f\}}|| |j |jt| jk |t|d    fdd|D }	||	8 }qf|t kd| d d S )	Nr*   r   c                 S   s   i | ]}|j r|j|qS r*   r`  rJ   ra  r*   r*   r+   
<dictcomp>;  s      zSRpcTest.test_profiler_rpc_key_names.<locals>.rpc_with_profiling.<locals>.<dictcomp>c                    s   h | ]}| kr|qS r*   r*   )rM   Zremote_event_nameZoperator_name_substrr*   r+   rO   S  s   zRRpcTest.test_profiler_rpc_key_names.<locals>.rpc_with_profiling.<locals>.<setcomp>z	Expected z* to be included in remote profiler output.)rc  r   r   r;   r   rd  r   r   rm  rU   r!   rN   rt   ru  r   r  
startswithr`  node_idru  rK   r#  )
re  rf  r   rw  remote_event_namesrT  Zremote_event_name_setrJ   r9  Zmatching_eventrQ   r  r+   rpc_with_profiling5  s6    


z?RpcTest.test_profiler_rpc_key_names.<locals>.rpc_with_profilingrj   )max_workersc                    s   g | ]}  qS r*   )ZsubmitrM   r  )re  executorr  r*   r+   r   g  s   )rN   rP   rG   r!   r   r   ZThreadPoolExecutorra   )rH   	dst_ranksr   Znum_parallel_rpcsr%  r   r*   )re  r  r  rH   r+   test_profiler_rpc_key_names*  s     
,z#RpcTest.test_profiler_rpc_key_namesc              
      sD  j dkrd S fddtdjD }|D ]}t|}t }tj|tdd}| }W 5 Q R X |j	}t
|tjj}tj |t|tj dd |D }	ttjtjtj t|tD ]4}
t |
 }||	k |	| }|j| qfd	d
  fdd|D }t|ttdt| dtt  q,d S )Nr&   c                    s   g | ]}| j kr|qS r*   r|  rL   rQ   r*   r+   r   u  s     
 zERpcTest._run_test_profiler_remote_events_profiled.<locals>.<listcomp>r   r*   r   c                 S   s   i | ]}|j r|j|qS r*   r}  ra  r*   r*   r+   r~    s       zERpcTest._run_test_profiler_remote_events_profiled.<locals>.<dictcomp>c                    s"    t  }| | |t| d  S rE   )REMOTE_OP_STRfindr#  rp  Zremote_op_key)rT  r*   r+   convert_remote_to_local  s    zRRpcTest._run_test_profiler_remote_events_profiled.<locals>.convert_remote_to_localc                    s$   g | ]} |j tkr |j qS r*   )rJ   ru  ra  r  r*   r+   r     s   z"Mismatch between profiled events: z and expected events: )rN   rP   rG   r!   rc  r   r   r;   r   rd  r   r   rm  r_   r^  r   rU   ru  r  r  rr  r  rt   )rH   r  r   re  rf  r   r"  rw  r\  rg  Zexpected_remote_event_nameexpected_keyremote_eventZremote_events_listr*   )r  rT  rH   r+   )_run_test_profiler_remote_events_profiledo  sL    


z1RpcTest._run_test_profiler_remote_events_profiledc                 C   s   |    d S rE   r  rQ   r*   r*   r+   $test_profiler_remote_events_profiled  s    z,RpcTest.test_profiler_remote_events_profiledc                 C   s   |    d S rE   r  rQ   r*   r*   r+   4test_profiler_remote_events_profiled_single_threaded  s    z<RpcTest.test_profiler_remote_events_profiled_single_threadedc                 C   s:   t jt|tjtjdddtjdddfd}|  d S )Ng      ?TZrequires_gradr   )r   r   r!   r'   r6   r   r   rH   r   r   r*   r*   r+   run_profiling_workload  s    zRpcTest.run_profiling_workloadr  c              	      sl  | j dkrd S t| j d | j }t| j d | j }td}td}t (}tj|t||||fdd}|	 }W 5 Q R X |j
}	ttjtjt| j |}
ttjtj||}|
t |  dd |	D } fdd|D }| dt| |d }| |j| j d | j   t tjjtj fd	d|D }| dt| |d }| |j| j d | j  d S )
Nr&   rj   r  rp   r'  c                 S   s   g | ]}|j r|qS r*   r_  ra  r*   r*   r+   r     s      z=RpcTest._run_rpc_profiling_async_function.<locals>.<listcomp>c                    s   g | ]}|j  kr|qS r*   r  ra  )r  r*   r+   r     s    
 r   c                    s   g | ]}|j  kr|qS r*   r  ra  )remote_add_keyr*   r+   r     s    
 )rN   r!   rG   r'   r)   rc  r   r   r  r   rd  r   r   rm  rU   r  r  rr  r#  r  rW  Z	_builtinsZ_find_builtinr(   )rH   r2   dst1dst2r   r   rf  r"  r   rd  Z
key_prefixZnested_rpc_key_prefixrg  Zrpc_remote_eventrh  r*   )r  r  r+   !_run_rpc_profiling_async_function  sV    


  
       

z)RpcTest._run_rpc_profiling_async_functionc                 C   s<   t | j| j| j |   tj r8t	  | jdd d S Nr  r  
r   r  rN   rG   r  r'   r1   Zis_availabler  r  rQ   r*   r*   r+   !test_rpc_profiling_async_function  s
    
z)RpcTest.test_rpc_profiling_async_functionc                 C   s<   t | j| j| j |   tj r8t	  | jdd d S r  r  rQ   r*   r*   r+   1test_rpc_profiling_async_function_single_threaded  s
    
z9RpcTest.test_rpc_profiling_async_function_single_threadedc              
      s4  j dkrd S fddtjD }|D ]}t|}t }tj|tdd}|  W 5 Q R X |j	}dd |D }
dt| |d }
|j| fdd	|}t }td
d W 5 Q R X |j	}	dd |	D d }
|
}dd |D }d  fdd}|D ]}||j}||k qq*d S )Nr&   c                    s   g | ]}| j kr|qS r*   r|  r  rQ   r*   r+   r     s     
 zERpcTest.test_rpc_profiling_remote_record_function.<locals>.<listcomp>)r-   Tr   c                 S   s   g | ]}d |j kr|qS r.   r  rM   evtr*   r*   r+   r     s    
 r   c                    s.   | j s
g S | j }| j D ]}| | q|S rE   )cpu_childrenextend)r9  r  r   )get_cpu_childrenr*   r+   r    s    
zKRpcTest.test_rpc_profiling_remote_record_function.<locals>.get_cpu_childrenr-   Tc                 S   s   g | ]}d |j kr|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    r>   c                    s    }| |  |t| d  S rE   )r  r#  r  )r  r*   r+   r  (  s    zRRpcTest.test_rpc_profiling_remote_record_function.<locals>.convert_remote_to_local)rN   rP   rG   r!   rc  r   r   r;   r   rd  rr  r#  r  rJ   r  )rH   r  r  re  rf  r   rd  Zrecord_function_remote_eventZremote_childrenri  Zlocal_record_function_eventZlocal_childrenZlocal_children_namesr  r  Z
local_namer*   )r  r  rH   r+   )test_rpc_profiling_remote_record_function  sD    


z1RpcTest.test_rpc_profiling_remote_record_functionc                    sh   dd  |j } fdd|D }| d|k |d }| |j| | t| jt|tj|t	j
 d S )Nc                 S   s   | |  ttt d  S rE   )r  r  r#  )rp  r*   r*   r+   r  4  s    zDRpcTest.validate_profiling_workload.<locals>.convert_remote_to_localc                    s   i | ]}|j r |j|qS r*   r}  ra  r  r*   r+   r~  8  s    z7RpcTest.validate_profiling_workload.<locals>.<dictcomp>r=   )rd  r  rr  r  r^  r!   rN   r'   r6   r   rm  )rH   r   rf  rw  rg  Zremote_mul_eventr*   r  r+   validate_profiling_workload2  s    
z#RpcTest.validate_profiling_workloadc              
   C   s   | j d | j }| j dkrt "}t }| | W 5 Q R X W 5 Q R X | || t $}t }| | W 5 Q R X W 5 Q R X | || d S r%   )rN   rG   dist_autogradcontextrc  r  r  )rH   r   
context_idrf  r*   r*   r+   (_run_test_profiler_with_autograd_contextH  s    


z0RpcTest._run_test_profiler_with_autograd_contextc                 C   s   |    d S rE   r  rQ   r*   r*   r+   3test_profiler_with_autograd_context_single_threadedZ  s    z;RpcTest.test_profiler_with_autograd_context_single_threadedc                 C   s   |    d S rE   r  rQ   r*   r*   r+   #test_profiler_with_autograd_context^  s    z+RpcTest.test_profiler_with_autograd_contextNc              
      sT   d k	r n| j d | j  |s$tntjj}| j dkrP| }|sLt ntjj	d}	|	}
t
jkrtjt ||d nt
jkrtjt ||d}|rtjt ||d}|  |  n8| t
jk tjt ||d}|  |   W 5 Q R X W 5 Q R X |s |jn| }|rT| t t|j W 5 Q R X d S t|j}| |j| j   fdd|D |h }| t |d |D ]}| |j  q|rt|d}| !|j"j#|j"j# | |j"j$|j"j$ t| j }t }| %|||| |rPt&dd t'|D }t&fddt'|D }| (|| d S )	Nr&   rQ  r   c                    s   h | ]}|j  kr|qS r*   )r  ra  r   r*   r+   rO     s     
 z2RpcTest._profiler_test_with_rpc.<locals>.<setcomp>c                 s   s    | ]\}}d |j kr|V  qdS )rQ  Nr  rM   r   r9  r*   r*   r+   rl    s     
 z2RpcTest._profiler_test_with_rpc.<locals>.<genexpr>c                 3   s"   | ]\}} j |jkr|V  qd S rE   )r_   rJ   r  )r]  r*   r+   rl    s      ))rN   rG   rc  r'   r4   r$   r/   r0   r3   r5   r   rl  r   r   r!   rm  r   r   r  rn  r   r   Z_get_profiling_futurerd  rw  r  
IndexErrorr   r_   rr  r  ZassertGreaterEqualr#  ZassertLessEqual
time_ranger=  endr^  r  	enumerateZ
assertLess)rH   r]  ro   rp   r7   r   kineto_profilern  rf  Zrecord_function_ctx_mgrrfr   r  r   rw  r\  rg  r  Zscope_eventr  r[  Zfoo_event_ixZrpc_event_idxr*   )r   r]  r+   _profiler_test_with_rpcb  sZ    



 

zRpcTest._profiler_test_with_rpcc                 C   s*   | j tjtdd | j tjtddd d S )Nr&   r   Trp   r7   )r  r   rl  r   rQ   r*   r*   r+   $_run_test_profiler_with_sync_rpc_udf  s    z,RpcTest._run_test_profiler_with_sync_rpc_udfc                 C   s   |    d S rE   r  rQ   r*   r*   r+   test_profiler_with_sync_rpc_udf  s    z'RpcTest.test_profiler_with_sync_rpc_udfc                 C   s   |    d S rE   r  rQ   r*   r*   r+   /test_profiler_with_sync_rpc_udf_single_threaded  s    z7RpcTest.test_profiler_with_sync_rpc_udf_single_threadedc                 C   sN   | j tjtjtdtdfd | j tjtjtdtdfdd d S Nr&   r   Tr  )r  r   rl  r'   r6   r)   rQ   r*   r*   r+   (_run_test_profiler_with_sync_rpc_builtin  s        z0RpcTest._run_test_profiler_with_sync_rpc_builtinc                 C   s   |    d S rE   r  rQ   r*   r*   r+   #test_profiler_with_sync_rpc_builtin  s    z+RpcTest.test_profiler_with_sync_rpc_builtinc                 C   s   |    d S rE   r  rQ   r*   r*   r+   3test_profiler_with_sync_rpc_builtin_single_threaded  s    z;RpcTest.test_profiler_with_sync_rpc_builtin_single_threadedc                 C   s>   | j tjtdd | j tjtddd | j tjtddd d S )Nr  r   Tr  )rp   r  )r  r   rm  r   rQ   r*   r*   r+   %_run_test_profiler_with_async_rpc_udf  s       z-RpcTest._run_test_profiler_with_async_rpc_udfc                 C   s   |    d S rE   r  rQ   r*   r*   r+    test_profiler_with_async_rpc_udf  s    z(RpcTest.test_profiler_with_async_rpc_udfc                 C   s   |    d S rE   r  rQ   r*   r*   r+   0test_profiler_with_async_rpc_udf_single_threaded  s    z8RpcTest.test_profiler_with_async_rpc_udf_single_threadedc                 C   sN   | j tjtjtdtdfd | j tjtjtdtdfdd d S r  )r  r   rm  r'   r6   r)   rQ   r*   r*   r+   )_run_test_profiler_with_async_rpc_builtin  s        z1RpcTest._run_test_profiler_with_async_rpc_builtinc                 C   s   |    d S rE   r  rQ   r*   r*   r+   $test_profiler_with_async_rpc_builtin  s    z,RpcTest.test_profiler_with_async_rpc_builtinc                 C   s   |    d S rE   r  rQ   r*   r*   r+   4test_profiler_with_async_rpc_builtin_single_threaded  s    z<RpcTest.test_profiler_with_async_rpc_builtin_single_threadedc                 C   s@   | j tjtdd | j tjtddd | j tjtd| jd d S )Nr  r   Tr  rp   r   )r  r   rn  r   rN   rQ   r*   r*   r+   "_run_test_profiler_with_remote_udf  s          z*RpcTest._run_test_profiler_with_remote_udfc                 C   s   |    d S rE   r  rQ   r*   r*   r+   test_profiler_with_remote_udf  s    z%RpcTest.test_profiler_with_remote_udfc                 C   s   |    d S rE   r  rQ   r*   r*   r+   -test_profiler_with_remote_udf_single_threaded  s    z5RpcTest.test_profiler_with_remote_udf_single_threadedc                 C   sv   | j tjtjtdtdfd | j tjtjtdtdfdd | j tjtjtdtdf| jd d S Nr&   r   Tr  r  )r  r   rn  r'   r6   r)   rN   rQ   r*   r*   r+   &_run_test_profiler_with_remote_builtin	  s"        z.RpcTest._run_test_profiler_with_remote_builtinc                 C   s   |    d S rE   r  rQ   r*   r*   r+   !test_profiler_with_remote_builtin	  s    z)RpcTest.test_profiler_with_remote_builtinc                 C   s   |    d S rE   r  rQ   r*   r*   r+   1test_profiler_with_remote_builtin_single_threaded	  s    z9RpcTest.test_profiler_with_remote_builtin_single_threadedc                 C   s:   | j tjttdfd | j tjttdfdd d S r  )r  r   rm  r   r'   r   rQ   r*   r*   r+   (_run_test_profiler_with_script_async_rpc	  s      

z0RpcTest._run_test_profiler_with_script_async_rpcc                 C   s   |    d S rE   r  rQ   r*   r*   r+   #test_profiler_with_script_async_rpc%	  s    z+RpcTest.test_profiler_with_script_async_rpcc                 C   s   |    d S rE   r  rQ   r*   r*   r+   3test_profiler_with_script_async_rpc_single_threaded)	  s    z;RpcTest.test_profiler_with_script_async_rpc_single_threadedc                 C   s:   | j tjttdfd | j tjttdfdd d S r  )r  r   rl  r   r'   r   rQ   r*   r*   r+   '_run_test_profiler_with_script_sync_rpc-	  s      

z/RpcTest._run_test_profiler_with_script_sync_rpcc                 C   s   |    d S rE   r  rQ   r*   r*   r+   "test_profiler_with_script_sync_rpc8	  s    z*RpcTest.test_profiler_with_script_sync_rpcc                 C   s   |    d S rE   r  rQ   r*   r*   r+   2test_profiler_with_script_sync_rpc_single_threaded<	  s    z:RpcTest.test_profiler_with_script_sync_rpc_single_threadedc                 C   sX   | j tjttdfd | j tjttdfdd | j tjttdf| jd d S r  )r  r   rn  r   r'   r   rN   rQ   r*   r*   r+   )_run_test_profiler_with_script_remote_rpc@	  s"      

  
 z1RpcTest._run_test_profiler_with_script_remote_rpcc                 C   s   |    d S rE   r  rQ   r*   r*   r+   $test_profiler_with_script_remote_rpcO	  s    z,RpcTest.test_profiler_with_script_remote_rpcc                 C   s   |    d S rE   r  rQ   r*   r*   r+   4test_profiler_with_script_remote_rpc_single_threadedS	  s    z<RpcTest.test_profiler_with_script_remote_rpc_single_threadedc           	      C   sp   g }|D ]8}d}|D ]*}|j }|j}|j|kr|| |j}qqt|}t|}| ||d| d|  d S )Nr   zExpected events z
, but got )rJ   r  r=  r)  r  r8  rr  )	rH   Zprocess_global_eventsZexpected_top_level_event_namesZtop_level_event_namesZthread_local_eventsZlast_end_timer9  rp  r  r*   r*   r+   _assert_top_level_eventsW	  s     


z RpcTest._assert_top_level_eventsc                 C   s  | j dkrd S | j d | j }t|}td}td}t|tj}| 	  t|tj
||f t|tj}| 	  t|tj||f | d d d  | d d d  t|t|f}dg}|dg }	| || t|t|f}
| |
|	 |   |   d S )Nr   r&   rj   z	aten::subr<   )rN   rG   r!   r'   r   r   r   Z_server_process_global_profiler   	__enter__r(   sub__exit__r   r  Zkey_averages)rH   r  r[  r   r   Zouter_profile_rrefZinner_profile_rrefZinner_eventsZexpected_inner_eventsZexpected_outer_eventsZouter_eventsr*   r*   r+   #test_server_process_global_profilerj	  s,    



z+RpcTest.test_server_process_global_profilerc                 C   s   d}| j dkrt j}tjjdH}tjtdt	|fd}|
| | td |
| W 5 Q R X W 5 Q R X |  W 5 Q R X d S )Nr&   rQ  r   r   zcan only be called once.)rN   rc  r'   r3   r4   r5   r   r   r!   r   Z_call_end_callbacks_on_futurer  r
  r   )rH   num_sleep_secondspfr  r   r*   r*   r+   /test_async_record_function_double_end_callbacks	  s     
  
 z7RpcTest.test_async_record_function_double_end_callbacksc              
   C   sx   d}| j dkrtt Z}z:tjjdd }tjt	dt
|fd}tjj|| W 5 tjj| X |  W 5 Q R X d S )Nr&   rQ  r   r   )rN   rc  r'   opsr4   Z_record_function_exitZ_record_function_enter_newr   r   r!   r   _call_end_callbacks_on_jit_futr   )rH   r  r  r?  r   r*   r*   r+   >test_async_record_function_double_end_callbacks_new_signatures	  s    
  zFRpcTest.test_async_record_function_double_end_callbacks_new_signaturesc           	   
   C   s   | j dkrt }ttjtjtdd}tj	j
|4}tjtdttdfd}tjj
|j|}W 5 Q R X | }ttdtd}| || W 5 Q R X |j}t|tjt}| tjt|jk d S )Nr&   r  rR  r   r   )rN   rc  r   r   rm  r'   rX  rY  r   r3   r4   r5   r   r   r!   r   r  r  handler   r(   rr  rd  r   r  rJ   )	rH   r  keyr  r   ra   r  rw  r\  r*   r*   r+   'test_async_record_function_cbs_jit_call	  s0    

  
 
z/RpcTest.test_async_record_function_cbs_jit_callc                 C   s<   | j d }|| j }tjt|t|fd}| |j| d S r  )rN   rG   r   r   r!   ry   rr  rz   r  r*   r*   r+   test_py_class_constructor	  s    

z!RpcTest.test_py_class_constructorc                 C   sJ   | j d }|| j }tjt|tdj|fd}| |td| d S rL  )rN   rG   r   r   r!   ry   r~   rr  r  r*   r*   r+   test_py_class_instance_method	  s    

  z%RpcTest.test_py_class_instance_methodc                 C   sN   | j d }|| j }tjt|tj||d fd}| |t||d  d S r  )rN   rG   r   r   r!   ry   r   rr  r  r*   r*   r+   test_py_class_method	  s    

  
zRpcTest.test_py_class_methodc                 C   sJ   | j d }|| j }tjt|tj|d fd}| |t|d  d S )Nr&   r   r   )rN   rG   r   r   r!   ry   r   rr  r  r*   r*   r+   test_py_class_static_method	  s    

  z#RpcTest.test_py_class_static_methodc                 C   s   | j d }|| j }tt|}tj|tj|d fd}tj|t||d |d fd}| 	|
 t|d  | 	|
 t||d |d  d S )Nr&   r   r   rj   )rN   rG   r   ru  r!   r   ry   r   rM  rr  r   )rH   r  r  Zdst_worker_infor  r  r*   r*   r+   test_py_multi_async_call	  s    

z RpcTest.test_py_multi_async_callc                 C   s6   | j d }|| j }tt|t}| |t  d S r%   )rN   rG   r   r   r!   r   rr  r  r*   r*   r+   test_py_no_return_result	  s    

z RpcTest.test_py_no_return_resultc              	   C   sb   | j d }|| j }tjt|tt||t||fd}| |tt||t|| d S r  )	rN   rG   r   r   r!   rl   r'   r)   rr  r  r*   r*   r+   test_py_tensors	  s    

zRpcTest.test_py_tensorsc              
   C   s   g }| j d }|| j }tdD ]6}tjt|tt||t||fd}|	| q d}tj
|D ].}| |tt||t|| |d7 }qhd S )Nr&   r   r   r   )rN   rG   rP   r   r   r!   rl   r'   r)   r)  r   r  rr  )rH   r%  r  r  r   r   jrs   r*   r*   r+    test_py_tensors_multi_async_call
  s"    

 z(RpcTest.test_py_tensors_multi_async_callc                 C   s   | j d }|| j }t||t||g}tt }t||t||d}tjt|t	|||fd}| 
|t	||| d S )Nr&   )rQ  barr   )rN   rG   r'   r)   re   r   r   r   r!   r   rr  )rH   r  r  rz   r}   r   r"  r*   r*   r+   test_py_tensors_in_container
  s    


  z$RpcTest.test_py_tensors_in_containerc              	   C   sz   | j d }|| j }tjt|tt tddfd}t }|	t
tddtdd | |t|tdd d S rL  )rN   rG   r   r   r!   r   rg   r'   r)   rt   rl   rr  )rH   r  r  r"  r  r*   r*   r+   test_py_nested_pickle%
  s    

 zRpcTest.test_py_nested_picklec              	   C   sB   | j d }|| j }| t tjt|tdd}W 5 Q R X d S Nr&   )r   r   )rN   rG   r  	TypeErrorr   r   r!   r   r  r*   r*   r+   test_py_function_exception4
  s    

z"RpcTest.test_py_function_exceptionc              
   C   s   t  p\}}t| j| j| j t  | jd }|| j }tt	|t
}| tt |  W 5 Q R X t  W 5 Q R X | }| t|k d S r%   )r   r   r  rN   rG   r  r  r   r   r!   r   r  r   r   r   getvaluer  )rH   r  errr  r  r   Zstderr_linesr*   r*   r+   test_py_raise_in_user_func;
  s    

z"RpcTest.test_py_raise_in_user_funcc              
   C   s   | j d }|| j }tt|t}z|  W nB tk
rr } z$t|}| 	||
dd W 5 d }~X Y nX | dd d S )Nr&   zutf-8Zunicode_escapeFz/expected raise_func_escape to raise ValueError.)rN   rG   r   r   r!   r   r   r   strrr  encodedecoder  )rH   r  r  r   r   r  r*   r*   r+   &test_py_raise_in_user_func_escaped_strO
  s    

*z.RpcTest.test_py_raise_in_user_func_escaped_strc                 C   s   |  ttddd  d S Nrj   r&   )r  r   r'   r)   rQ   r*   r*   r+   test_nested_rpc]
  s    zRpcTest.test_nested_rpcc                 C   s   |  t d S rE   )r  r   rQ   r*   r*   r+   test_stress_light_rpca
  s    zRpcTest.test_stress_light_rpcc                 C   s   | j tdtddfd d S Nr  r   r  rp   )r  r   r'   r)   rQ   r*   r*   r+   test_stress_heavy_rpce
  s    zRpcTest.test_stress_heavy_rpcc                 C   s   | j tdtddfd d S r  )r  r   r'   r)   rQ   r*   r*   r+   !test_stress_heavy_rpc_torchscripti
  s    z)RpcTest.test_stress_heavy_rpc_torchscriptc                 C   s.   |  tddtddtddd  d S ri   )r  r'   r)   rQ   r*   r*   r+   test_builtin_remote_retm
  s
    

zRpcTest.test_builtin_remote_retc                 C   s.   |  tddtddtddd  d S ri   )r  r'   r)   rQ   r*   r*   r+   test_builtin_remote_selfu
  s
    

z RpcTest.test_builtin_remote_selfc                 C   s,   |rt  t  fS t| | t| | fS d S rE   r   r'   r)   r  rO  r*   r*   r+   _multi_args_fn}
  s    zRpcTest._multi_args_fnc                 C   s   | j tjdtjd d S )NFr  r  r'   r(   r  r  rQ   r*   r*   r+   test_multi_builtin_remote_ret
  s
     z%RpcTest.test_multi_builtin_remote_retc                 C   s\   | j d }|| j }tjt|t||d |d dd}| | t||d |d  d S rO  )rN   rG   r   r   r!   r   rr  r   rH   r  r  r   r*   r*   r+   test_py_udf_remote
  s    

zRpcTest.test_py_udf_remotec                 C   s>   |rt  t  t  dS t| | t| | t| | dS d S )Nr   r  r  r*   r*   r+   _multi_kwargs_fn
  s    zRpcTest._multi_kwargs_fnc                 C   s   | j tdtjd d S )NFr  r  r   r  r  rQ   r*   r*   r+   test_multi_py_udf_remote
  s
    z RpcTest.test_multi_py_udf_remotec              
   C   s6   |  tdddtdddtddd d  d S r  )r  r'   r)   rQ   r*   r*   r+   test_py_rref_args
  s    

zRpcTest.test_py_rref_argsc                 C   s:   |  tddddtddddtddd d  d S Nrj   r&   r   r   r   )r  r'   r)   rQ   r*   r*   r+   test_py_rref_args_user_share
  s    

z$RpcTest.test_py_rref_args_user_sharec                 C   s:   |  tddddtddddtddd d  d S r  )r  r'   r)   rQ   r*   r*   r+   test_py_rpc_rref_args
  s    

zRpcTest.test_py_rpc_rref_argsc                 C   s   |  ttddd  d S )Nrj   r   )r  r   r'   r)   rQ   r*   r*   r+   test_nested_remote
  s    zRpcTest.test_nested_remotec                 C   s*   |  ttddd tddd  d S r  )r  r   r'   r)   rQ   r*   r*   r+   test_nested_rref
  s
    zRpcTest.test_nested_rrefc                 C   s*   |  ttddd tddd  d S r  )r  r   r'   r)   rQ   r*   r*   r+   test_nested_rref_stress
  s
    zRpcTest.test_nested_rref_stressc                 C   s*   d}| j d }|| j }t|| j| d S )Nr  r&   )rN   rG   r   )rH   r   r  r  r*   r*   r+   !test_multi_layer_nested_async_rpc
  s    

z)RpcTest.test_multi_layer_nested_async_rpcc              	   C   sz   | j d }|| j }tt|t}| t |  W 5 Q R X tjt| j t	dd}| t
 |  W 5 Q R X d S r  )rN   rG   r   r   r!   r   r  r   r   r   r  r  r*   r*   r+   test_remote_with_exception
  s    

z"RpcTest.test_remote_with_exceptionc                 C   s\   | j d }|| j }|d | j }tjt|tt|fd}| | t	ddd  d S r  )
rN   rG   r   r   r!   r   rr  r   r'   r)   )rH   r  r  r  r   r*   r*   r+   test_rpc_return_rref
  s    

zRpcTest.test_rpc_return_rrefc              	   C   s   d}| j d }|| j }tjt|tjt||dfd}t|| j||}t	|D ] }| 
t|d |d  }qR|}| 
|tt||d d S )N   r&   r   r   )rN   rG   r   r   r!   r'   r(   r)   r   rP   rr  r#  r   )rH   r   r  r  r   r   r   r"  r*   r*   r+   test_rref_forward_chain  s    

  zRpcTest.test_rref_forward_chainc                 C   s   t d}| | d d S )N#   )r   rr  r   )rH   
local_rrefr*   r*   r+   test_local_rref_no_fork  s    zRpcTest.test_local_rref_no_forkc                 C   s   | j d | j }tjt|tjtdtdfd}| t	d| j  d| j  d| j  dt| j  d| dt| d |
  W 5 Q R X d S )	Nr&   r   z3For UserRRef\(rref_id=GloballyUniqueId\(created_on=z5, local_id=0\), fork_id=GloballyUniqueId\(created_on=zC, local_id=1\)\), can't call localValue\(\) on user WorkerInfo\(id=z, name=z$\). Call it on owner WorkerInfo\(id=z\))rN   rG   r   r   r!   r'   r(   r)   r  r
  r   )rH   	next_rankr   r*   r*   r+   test_local_value_not_on_owner  s      8	z%RpcTest.test_local_value_not_on_ownerc                 C   sz   | j d }|| j }tjt|tdddgfd}|D ] }tj| ttj	|dfd q4dd |D }| 
|dd	d
g d S )Nr&   rj   r   r   r   c                 S   s&   g | ]}t j| ttj|fd qS )r   )r   r   ownerr   ry   r   )rM   r   r*   r*   r+   r   A  s     z3RpcTest.test_return_local_rrefs.<locals>.<listcomp>r        )rN   rG   r   r   r!   r   r*  r   ry   r   rr  )rH   r  r  Z	rref_listr   retsr*   r*   r+   test_return_local_rrefs1  s"    

  

zRpcTest.test_return_local_rrefsc              	      s  dd }t jd j }tj|tjtddfd}t }|j	|d}|sX|
 }W 5 Q R X ||j ttd |  g } fdd}t L}td	D ]<}	|j	|d}|s|| || |
 }|  qW 5 Q R X |s&|d
 }
|dd  D ]}||
k q||j |ttd tj|tdd}|j	|d}|sv|
 }|t d S )Nc                    s&   dt jj d t fdd| D S )NZrpc_z#_rref_typeof_on_ownerc                    s   g | ]}|j  qS r*   )rJ   r  )rM   r   Zexpected_namer*   r+   r   O  s     zARpcTest._test_rref_type.<locals>.launched_rpc.<locals>.<listcomp>)r   rm  r_   rv  )rw  r*   r/  r+   launched_rpcM  s    z-RpcTest._test_rref_type.<locals>.launched_rpcr&   rj   r   blockingc                    s    |    d S rE   )rr  r_   r   Zexpected_typerH   r*   r+   verify_  s    z'RpcTest._test_rref_type.<locals>.verifyr   r   r   )r!   rN   rG   r   r   r'   r(   r)   rc  	_get_typer   r  rd  typerr  rP   r)  add_done_callbackassertFalsery   )rH   r2  r0  r   r   rn  r:   r%  r4  r  Z	first_futr   	rref_typer*   r3  r+   _test_rref_typeJ  s>    

zRpcTest._test_rref_typec                 C   s   | j dd d S NTr1  r;  rQ   r*   r*   r+   test_rref_type_blocking|  s    zRpcTest.test_rref_type_blockingc                 C   s   | j dd d S NFr1  r=  rQ   r*   r*   r+   test_rref_type_non_blocking  s    z#RpcTest.test_rref_type_non_blockingc              	   C   sz   t | jd | j }t|t}|rJ| td |j|d W 5 Q R X n,|j|d}| td |	  W 5 Q R X d S )Nr&   r   r1  )
r!   rN   rG   r   r   r   r  r   r6  r   )rH   r2  r   r   r   r*   r*   r+   _test_rref_type_with_error  s    z"RpcTest._test_rref_type_with_errorc                 C   s   | j dd d S r<  rA  rQ   r*   r*   r+   "test_rref_type_with_error_blocking  s    z*RpcTest.test_rref_type_with_error_blockingc                 C   s   | j dd d S r?  rB  rQ   r*   r*   r+   &test_rref_type_with_error_non_blocking  s    z.RpcTest.test_rref_type_with_error_non_blockingc                 C   st   t tdd }|j|d}|s*| }| |ttd t td}|j|d}|sd| }| |t d S )Nrj   r&   r1  r   )r   r'   r)   r6  r   rr  r7  ry   )rH   r2  r   r:  r*   r*   r+   _test_rref_type_owner  s    zRpcTest._test_rref_type_ownerc                 C   s   | j dd d S r<  rE  rQ   r*   r*   r+   test_rref_type_owner_blocking  s    z%RpcTest.test_rref_type_owner_blockingc                 C   s   | j dd d S r?  rF  rQ   r*   r*   r+   !test_rref_type_owner_non_blocking  s    z)RpcTest.test_rref_type_owner_non_blockingc                 C   s   t d | | S r%   r   r  r*   r*   r+   	_slow_add  s    
zRpcTest._slow_addc                 C   sN   t | jd | j }tj|tjtddfd}| 	|
 ttd d S rL  )r!   rN   rG   r   r   r  rI  r'   r)   rr  r6  r7  )rH   r   r   r*   r*   r+   test_rref_type_slow_init  s    z RpcTest.test_rref_type_slow_initc                 C   s  t d}t d}| jd | j }tjt|tjtddfd}tjt|tjtddfd}|	  |	  | 
| d | | |  | 
| |  | | |  | | |  | | |  | | |  | | t  i }||| < ||| < | ||  | | ||  | | ||  | | ||  | | t|d d S )N(   2   r&   r      rj   )r   rN   rG   r   r   r!   r'   r(   r)   r   rm  r*  rr  ru  r#  )rH   rz   r}   Z
other_rankZother_aZother_br   r*   r*   r+   test_owner_equality  s>        zRpcTest.test_owner_equalityc                 C   s   | j d }|| j }t|}td}| tj|t|dfdd | tj|t|dfd	 d | tj
|t|dfd d d S )Nr&   rK  rL  r   Z   )rN   rG   r!   r   rr  r   r   r   r   r   r   r   )rH   r  r  re  r   r*   r*   r+   test_pass_local_rrefs  s     

   zRpcTest.test_pass_local_rrefsc                 C   s   | j d }|| j }tjt|tjt||dfd}tjt|tjt||dfd}tjt|t||fd}| 	|
 t||d  d S )Nr&   rj   r   r   )rN   rG   r   r   r!   r'   r(   r)   r   rr  r   )rH   r  r  r   r   r  r*   r*   r+   test_remote_same_worker  s$    

      zRpcTest.test_remote_same_workerTc                 C   s   ddddg}| j d | j }t|}tj|t|d fd}tj| ttj	||d fd tj
| ttj	||d fd  tj| ttj	||d fd  tj|ttj|fd}| |t| d	S )
z
        Tests that it is possible to call an instance method on a remote objet
        by using rref.owner() as destination of the call.
        r   rj   r   r  r&   r   r   r   N)rN   rG   r!   r   r   ry   r   r*  r   r   r   r   r   r   rr  r@  )rH   valsr  re  r   ra   r*   r*   r+   test_call_method_on_rref  s2    

  z RpcTest.test_call_method_on_rref)_delete_all_user_and_unforked_owner_rrefsc              	   C   s   t jt| j| j| j| j| jd t| j| j| j t	
  t jt| jd | j tjtdddfd}dd lm  m   m} |rd|_t jdd n*d|_| td	 t jdd W 5 Q R X d S )
Nrz  r&   rj   r   r   Tr  FzLeaking RRef)r   r  r!   rN   r  rG   rZ   r   r  r  r  r   r'   r(   r)   torch.distributed.rpc.apir  r  Z_ignore_rref_leakr  r  r
  )rH   Z._mock_delete_all_user_and_unforked_owner_rrefsignore_leakr   r  r*   r*   r+   _test_rref_leak!  s*    zRpcTest._test_rref_leakc                 C   s   | j dd d S )NFrV  rW  rQ   r*   r*   r+   test_rref_leak?  s    zRpcTest.test_rref_leakc                 C   s   | j dd d S )NTrX  rY  rQ   r*   r*   r+   test_ignore_rref_leakC  s    zRpcTest.test_ignore_rref_leakc                 C   sx   t | j}d}| d|| j|  | jd | j }tjt|t	j
t	dddfd}| | d|| j d S )NZGloballyUniqueIdz(OwnerRRef({}(created_on={}, local_id=0))r&   rj   r   z\UserRRef(RRefId = {0}(created_on={1}, local_id=1), ForkId = {0}(created_on={1}, local_id=2)))r   rN   rr  r  __str__rG   r   r   r!   r'   r(   r)   )rH   rref1Zid_classr  rref2r*   r*   r+   test_rref_strG  s&    
    zRpcTest.test_rref_strc                 C   s   | j dkrtjtdtjdd}|  | }| |tj	j
 tjtdtdd}|  | }| |tj	j
 tjtdttdfd}|  | }| |tj	j
 d S )Nr   r&   )r&   r&   r   r*   )rN   r   r   r!   r'   r(   r   _get_futureassertIsInstance_Cr   r,   r   r   )rH   r   r   r*   r*   r+   test_rref_get_futureZ  s    
zRpcTest.test_rref_get_futurec                 C   s  t | j| j| j t| j}t }| d| | d| | dt|d  | dt|d  t	
  | jd | j }tjt|t|fd t  t	
  t }| d| | dt|d  | dt|d  t	
  tt|t t|d dkrt }td qt	
  tjt|tjtdddfd}tjt|tjtdddfd}|  |  t  t	
  t }| d| | dt|d  | dt|d  t	
  d S )NZnum_owner_rrefsZnum_pending_usersr   r&   r   r3  rj   )r   r  rN   rG   r   r	   rS  rr  r3  r  r  r   r   r!   r   r   r   rw   rx   r   r'   r(   r)   r   )rH   r]  r  r  r^  Zrref3r*   r*   r+   test_rref_context_debug_infor  sR    
    z$RpcTest.test_rref_context_debug_infoc                    s   | j d | j }tjt|tjtdtdfd tj	 
  | t fdd td tjt|tjtdtdfd tj	 
  | d  d S )Nr&   r   c                      s    d S )Nagent.gil_average_wait_time_usr*   r*   r  r*   r+   r     r   z4RpcTest.test_disable_gil_profiling.<locals>.<lambda>Tre  )rN   rG   r   r   r!   r'   r(   r)   r  r  get_debug_infor  KeyErrorZenable_gil_profilingrS  )rH   r  r*   rf  r+   test_disable_gil_profiling  s       
  z"RpcTest.test_disable_gil_profilingc                 C   s2   t jd| j | j| j| j| jd t jdd d S r  )r   r  rN   r  rG   rZ   r  rQ   r*   r*   r+   test_local_shutdown  s    zRpcTest.test_local_shutdownc           	      C   s   dd l m  m} t }t }tj  }| }|	 |	 @ |	 @ }| 
dt| i }|| || || |	 D ]}| ||	  q|	 D ]}| ||	  qd S rB   )torch.distributed.autogradr  r3   r   r	   r   r  r  rg  keysrr  r#  updaterS  )	rH   r  r  Z	rref_infoZ
agent_infoZautograd_infoZcommon_keysr  r  r*   r*   r+   test_debug_info  s    


zRpcTest.test_debug_infozITest is flaky on MacOS since libuv error handling is not as robust as TCPc              	   C   s   t jd| j | j| j| j| jd t d t| j| j| j t	
  | jdkr| jd | j }t|}|  }t|| t j|tjtddfd}| t| |  W 5 Q R X t jdd d S )	Nr  rz  r   r&   r   r   Fr  )r   r  rN   r  rG   rZ   r4  r   r  r  r  r!   Zget_shutdown_error_regexr   r   r'   r(   r)   r  r
  r   r  )rH   r  re  Z	error_strr   r*   r*   r+   test_handle_send_exceptions  s&    


z#RpcTest.test_handle_send_exceptionsc                 C   sv   | j dkrNt| j d | j }td}td}tj|tj||fdd	  t
 }|srt
jd| j| j | jd d S )Nr&   rj      r  r  r  )rN   r!   rG   r'   r)   r   r   r  rI  r   r  Zis_initializedr	  r  )rH   r  r   r   Zdist_initializedr*   r*   r+   test_deadlock  s    


zRpcTest.test_deadlockc                 C   s   t jd| j | j| j| j| jd | jd }|| j }t jt|tj	t
||t
||fd t| j| j| j t  t jdd d S )Nr  rz  r&   r   Fr  )r   r  rN   r  rG   rZ   r   r!   r'   r(   r)   r   r  r  r  r  )rH   r  r  r*   r*   r+   test_local_shutdown_with_rpc*  s"    

z$RpcTest.test_local_shutdown_with_rpcc                 C   sP   d}| j }||_tjt| j| j| j| j|d t }| 	|| t
  d S )Nr  rz  )rZ   rpc_timeoutr   r  r!   rN   r  rG   get_rpc_timeoutrr  r  )rH   r'  rZ   Zset_timeoutr*   r*   r+   $test_set_and_get_default_rpc_timeoutC  s    z,RpcTest.test_set_and_get_default_rpc_timeoutc              
      s   | j d | j  td  fddtdD }|  }|D ]$}| t| |  W 5 Q R X q<td tj	t
 tdd}td tj	t
 tdd}| t| |  W 5 Q R X |  td	 tj	t
 td
d  ttjj dS )z{
        Tests that if no timeout is passed into rpc_async and rpc_sync, then the
        default timeout is used.
        r&   MbP?c                    s    g | ]}t jt td dqS )r*   r   )r   r   r!   r   r  r  r*   r+   r   `  s   z5RpcTest.test_default_timeout_used.<locals>.<listcomp>r      r  r   r   r*   N)rN   rG   r   r4  rP   r5  r  r
  r   r   r!   r   	constantsDEFAULT_RPC_TIMEOUT_SEC)rH   r%  r6  r   r  r  r*   rw  r+   test_default_timeout_usedW  s&    




z!RpcTest.test_default_timeout_usedc              	   C   sh  | j d | j }t|}d}|  }tj|td|d}| t| |	  W 5 Q R X tj|tdd	  | t| tj
|td|d W 5 Q R X tj
|tdd td tj|tdd}| t| |	  W 5 Q R X | t| tj
|tdd W 5 Q R X tj|tddd	  tj
|tddd tj|tddd	  tj
|tddd ttjj d S )	Nr&   r3  r  r  r   rv  r   r   )rN   rG   r!   r5  r   r   r   r  r
  r   r   r4  ry  rz  )rH   r  re  r'  r6  r   r*   r*   r+   test_rpc_timeouts|  s,    
zRpcTest.test_rpc_timeoutsc                 C   sB   t dddd }| || d t dd }| || d d S )NFr  c                 S   s   dS Nexpected resultr*   rQ   r*   r*   r+   	test_func  s    z3RpcTest.test_dist_init_decorator.<locals>.test_funcr~  c                 S   s   dS r}  r*   rQ   r*   r*   r+   r    s    )r   rr  )rH   r  r*   r*   r+   test_dist_init_decorator  s    

z RpcTest.test_dist_init_decoratorc              	   C   sX   G dd d}| }t | | tjjjj|k W 5 Q R X | tjjjjtk d S )Nc                   @   s   e Zd ZdS )z1RpcTest.test_use_rpc_pickler.<locals>.TestPicklerN)rS   rT   rU   r*   r*   r*   r+   TestPickler  s   r  )r   r  r'   r  r   r  Z_default_picklerr   )rH   r  Ztest_picklerr*   r*   r+   test_use_rpc_pickler  s    
 zRpcTest.test_use_rpc_picklerc              	   C   s   t  t | tjg k t| jd | j }t|t	j
t	dddf}| ttjdk | ttjd t	jj W 5 Q R X | |  | | t	ddd  | ttd d S )Nr&   rj   r   future_list)r   r  r   r  r!   rN   rG   r   r   r'   r(   r)   r#  rV  rb  r   donerr  r   r9  hasattrr  r*   r*   r+   test_wait_all  s    $zRpcTest.test_wait_allc              
   C   s   t   | tjg k t| jd | j }tdD ]p}t	|t
jt
||df}t|t
jt
||df}| |t
||d  | | t
||d  q4| ttjdk W 5 Q R X | ttd d S )Nr&   r  r  )r   r  r   r  r!   rN   rG   rP   r   r   r'   r(   r)   r   rr  r   r#  r9  r  )rH   r   r   r   r   r*   r*   r+   test_wait_all_multiple_call  s    z#RpcTest.test_wait_all_multiple_callc              
   C   s   |   }| t|R t @ | tjg k t| jd | j	 }d}t
j|td|d}W 5 Q R X W 5 Q R X | ttd d S )Nr&   r3  r  r  r  )r5  r  r
  r   r  r   r  r!   rN   rG   r   r   r   r9  r  )rH   r6  r   r'  r   r*   r*   r+   test_wait_all_timeout  s    &zRpcTest.test_wait_all_timeoutc              
   C   sl   |  tH t 6 | tjg k t| jd | j }t	
|t}W 5 Q R X W 5 Q R X | ttd d S )Nr&   r  )r  r   r   r  r   r  r!   rN   rG   r   r   r   r9  r  r  r*   r*   r+    test_wait_all_raise_in_user_func  s     z(RpcTest.test_wait_all_raise_in_user_funcc              
   C   sB   |  t t  t  W 5 Q R X W 5 Q R X | ttd d S )Nr  )r  r   r   r   r9  r  r   rQ   r*   r*   r+   test_wait_all_raise_in_body  s    z#RpcTest.test_wait_all_raise_in_bodyc                   C   s   t j  d S rE   )r  timed_out_rpc_eventr   r*   r*   r*   r+   timed_out_rpc  s    zRpcTest.timed_out_rpcc              	   C   s   t  t_t| j| j| j t  t	| jd | j }t
|tj}t
|t}t
|t}| tt tj|||g W 5 Q R X tj  d S r%   )r   r  r  r   r  rN   rG   r  r  r!   r   r   r  r   r  r   r   r'   r   r  rt   rH   r   r  r  fut3r*   r*   r+   test_wait_all_exit_early_python  s    z'RpcTest.test_wait_all_exit_early_pythonc              	   C   s   t  t_t| j| j| j t  t	| jd | j }t
|tj}t
j|tjtdtdfd}t
j|tjtdtdfd}| td tj|||g W 5 Q R X tj  d S )Nr&   r   r   r   zsize of tensor)r   r  r  r   r  rN   rG   r  r  r!   r   r   r  r'   r(   r  r  r
  r   r  rt   r  r*   r*   r+    test_wait_all_exit_early_builtin  s    ""z(RpcTest.test_wait_all_exit_early_builtinc              	   C   s   t  t_t| j| j| j t  t	| jd | j }t
|tj}t
j|ttfd}t
j|ttfd}| tt tj|||g W 5 Q R X tj  d S r  )r   r  r  r   r  rN   rG   r  r  r!   r   r   r  r   r   r  r
  r'   r   r  rt   r  r*   r*   r+   (test_wait_all_exit_early_script_function#  s    z0RpcTest.test_wait_all_exit_early_script_functionc              	   C   s   t jt }d}d}| jdkr:t|d tj|t| jfd | jdkrt  | 	t
|d | td tj|tdd W 5 Q R X d S )	NrR  r  r&   r,   r   r   zRPC pickler does not serializer*   )sysmodulesrS   rN   delattrr   r   r`   rb   r  r  r  r
  r,   )rH   Zthis_moduleZcaller_workerZcallee_workerr*   r*   r+   test_function_not_on_callee9  s    



 z#RpcTest.test_function_not_on_calleec                 C   sZ   t | jd | j }td}td}||_||_| j}tj|tjt	||dfd|_
d S rL  )r!   rN   rG   ry   otherr   r   r'   r(   r)   r   )rH   r[  rz   r}   r  r*   r*   r+   Etest_non_garbage_collected_user_rref_due_to_local_circular_dependencyR  s    zMRpcTest.test_non_garbage_collected_user_rref_due_to_local_circular_dependencyc              	   C   s   t jd| j | j| j| j| jd | jd }|| j }t jt|tj	t
||t
||fd}t jdd | td |  W 5 Q R X | td( d	d lm  m   m} || W 5 Q R X d S )
Nr  rz  r&   r   Tr  z-Cannot call to_here\(\) on it after deletion.z,Cannot call fork an UserRRef after deletion.r   )r   r  rN   r  rG   rZ   r   r!   r'   r(   r)   r  r  r
  r   torch.distributed.rpc.internalr  internalrk   )rH   r  r  r   r  r*   r*   r+   test_use_rref_after_shutdownd  s4    

  z$RpcTest.test_use_rref_after_shutdownc                   C   s   t dddS )Nr   r   r'   r  r1   r*   r*   r*   r+   _return_gpu_tensor  s    zRpcTest._return_gpu_tensorc                   C   s$   t dddt dddgS )Nr   r   r&   r  r*   r*   r*   r+   _return_gpu_tensor_list  s    zRpcTest._return_gpu_tensor_listc                 C   s   t ddS r   )r'   r  )tensor_listr*   r*   r+   _gpu_tensor_list_arg  s    zRpcTest._gpu_tensor_list_argc                 C   s2   | j d | j }tjt|tjtdddfdS r   )rN   rG   r   r   r!   r'   r(   r  )rH   r  r*   r*   r+   _create_rref  s    zRpcTest._create_rrefc                 C   s>   | j d | j }|  }tjt|t|fd}| |d d S Nr&   r   T)rN   rG   r  r   r   r!   r   rr  )rH   r  r   r"  r*   r*   r+   test_user_rrefs_confirmed  s    z!RpcTest.test_user_rrefs_confirmedc                 C   sB   | j d | j }|  }tjt|t|fd}| | d d S r  )	rN   rG   r  r   r   r!   r   rr  r   )rH   r  r   r   r*   r*   r+    test_user_rrefs_confirmed_remote  s    z(RpcTest.test_user_rrefs_confirmed_remotec              
   C   sB   t d}t *}| td t|| W 5 Q R X W 5 Q R X d S )Nr%  z%Can not pickle rref in python pickler)r   r#   r  r
  r'   save)rH   r&  rx  r*   r*   r+   !test_rref_py_pickle_not_supported  s    z)RpcTest.test_rref_py_pickle_not_supportedc              	   C   sL   t jt| jd | j ttdfd}| t	d |
  W 5 Q R X d S )Nr&   rj   r   z.*Expected error.*)r   r   r!   rN   rG   r   r'   r)   r  r   r   )rH   r   r*   r*   r+   test_remote_throw  s    
zRpcTest.test_remote_throwc                 C   s   | j dkrtdd}|ddd}| |  | }| |  | || | j d | j	 }t
jt|t||fd}| || | |  d S )Nr   r   r&   rj   r   )rN   r'   r  Znarrowr9  r   
contiguousr  rr  rG   r   r   r!   r   )rH   r:   r   r   r(  Zt_retr*   r*   r+   test_non_cont_tensors  s    
zRpcTest.test_non_cont_tensorsc                    s   t j jd   fdd}tjt j tj	t
  t
  fd}|| | t
  d   t
  d d  | t
  d  d S )Nr&   c                    s6   |   }|t  d  | d  d S r  )r   rr  r'   r)   r]   clone)r   r"  r  rH   	set_by_cbr*   r+   r     s    z.RpcTest.test_callback_simple.<locals>.callbackr   rj   )r   r   r   rN   r   r   r!   rG   r'   r(   r)   r   rr  r   ra   )rH   r   r   r*   r  r+   test_callback_simple  s    


 zRpcTest.test_callback_simplec              	   C   s   t j }| jd }tjt|| j tj	t
||t
||fd}|t}| | t
||d  | td |  W 5 Q R X d S )Nr&   r   rj   z8my\_function\(\) missing 2 required positional arguments)r   r   r   rN   r   r   r!   rG   r'   r(   r)   r   r   rr  r   r  r
  )rH   r  r  r   Zcb_futr*   r*   r+   test_callback_wrong_arg_num  s    


z#RpcTest.test_callback_wrong_arg_numc              	   C   sd   t | jd | j }tj|tjtdddfd}|dd }| 	t
d |  W 5 Q R X d S )Nr&   rj   r   c                 S   s   | d S r%   r*   r   r*   r*   r+   r     r   z6RpcTest.test_callback_wrong_arg_type.<locals>.<lambda>z$unsupported operand type\(s\) for \+)r!   rN   rG   r   r   r'   r(   r)   r   r  r
  r   )rH   r   fut0r  r*   r*   r+   test_callback_wrong_arg_type  s    z$RpcTest.test_callback_wrong_arg_typec                    s   d}j d   fdd}tjt j tjt  t  fd}g }t|D ]}|	|
t|| qX| t  d  t|D ](}||  t  d |  q| t  d  d S )Nr   r&   c                    s(   |  }|t  d  ||  S ri   )r   rr  r'   r)   )idxr   r"  r  rH   r*   r+   r     s    z-RpcTest.test_callback_multi.<locals>.callbackr   rj   )rN   r   r   r!   rG   r'   r(   r)   rP   r)  r   r   rr  r   )rH   num_cbsr   r   Zcb_futsr  r*   r  r+   test_callback_multi  s$    

zRpcTest.test_callback_multic                 C   s   | j d }t|| j }dd }tjt|| j tjt||dfd}d}t|D ]}|	|}qT| 
| t||d |  d S )Nr&   c                 S   s   |   d S r%   r  r   r*   r*   r+   r   )  s    z-RpcTest.test_callback_chain.<locals>.callbackr   r  )rN   r!   rG   r   r   r'   r(   r)   rP   r   rr  r   )rH   r  r   r   r   r  r  r*   r*   r+   test_callback_chain$  s    
zRpcTest.test_callback_chainc                 C   sh   t | jd | j }t | jd | j }tj|t|tddddfd}| |tddd d  d S rL  )	r!   rN   rG   r   r   r   r'   r)   rr  rH   r  r  r"  r*   r*   r+   test_callback_in_rpc8  s    zRpcTest.test_callback_in_rpcc                    sd   t | jd | j   fdd}tj tjtdddfd|}| 	|
 tddd  d S )Nr&   c                    s,   t j tj|  dfddd }| S )Nr&   r   c                 S   s   |   d S r%   r  r  r*   r*   r+   r   M  r   zBRpcTest.test_callback_with_ret.<locals>.callback.<locals>.<lambda>r  )r  r  r   r*   r+   r   H  s    
z0RpcTest.test_callback_with_ret.<locals>.callbackrj   r   r   )r!   rN   rG   r   r   r'   r(   r)   r   rr  r   )rH   r   r  r*   r   r+   test_callback_with_retD  s    	zRpcTest.test_callback_with_retc              	      sV   t  jd  j } fdd}t|t|} td |	  W 5 Q R X d S )Nr&   c              	      s,     td |   W 5 Q R X tdd S )Nr   Another expected error)r  r   r   r
  )r  rQ   r*   r+   r   ]  s    z2RpcTest.test_callback_with_error.<locals>.callbackr  )
r!   rN   rG   r   r   r   r   r  r
  r   )rH   r   r   r  r*   rQ   r+   test_callback_with_errorY  s
    z RpcTest.test_callback_with_errorc              	   C   sB   t | jd | j }| td t|td  W 5 Q R X d S )Nr&   z incompatible function arguments.)	r!   rN   rG   r  r  r   r   r   r   r  r*   r*   r+   test_callback_nonef  s    zRpcTest.test_callback_nonec                    s   d | j d } fdd}tjt|| j tjt||t||fd}|| |	dd }| 
| t||d  |  |   | 
| t||d  d S )	NFr&   c                    s   |    d d S NTr  r   r  r*   r+   r   t  s    z0RpcTest.test_add_done_callback.<locals>.callbackr   c                 S   s   dS r  r*   )r  r*   r*   r+   r     r   z0RpcTest.test_add_done_callback.<locals>.<lambda>rj   )rN   r   r   r!   rG   r'   r(   r)   r8  r   rr  r   r  )rH   r  r   r   Zfut_thenr*   r  r+   test_add_done_callbacko  s    


zRpcTest.test_add_done_callbackc              	   C   sp   t jt| jd | j tjtdddfd}| |	 tddd  | 
td |d W 5 Q R X d S )Nr&   rj   r   z(Future can only be marked completed once)r   r   r!   rN   rG   r'   r(   r  rr  r   r  r
  r]   rH   r   r*   r*   r+   test_mark_future_twice  s    zRpcTest.test_mark_future_twicec              
   C   s   t j }d}t| jd | j }t 0}| t| t	j
|t|fd W 5 Q R X W 5 Q R X t 0}| t| t	j|t|fd W 5 Q R X W 5 Q R X t 0}| t| t	j|t|fd W 5 Q R X W 5 Q R X d S )N#Can not pickle torch.futures.Futurer&   r   )r'   r   r   r!   rN   rG   r#   r  r
  r   r   r  r   r   )rH   r   errMsgr   rx  r*   r*   r+   test_pickle_future  s    
&&zRpcTest.test_pickle_futurec                 C   sJ   t | jd | j }tj|tjtddfd}|  | 	|
  d S rL  )r!   rN   rG   r   r   r'   r(   r  r   r  r  r  r*   r*   r+   test_future_done  s    zRpcTest.test_future_donec              	   C   sR   t | jd | j }t|t}| td |  W 5 Q R X | 	|
  d S )Nr&   r   )r!   rN   rG   r   r   r   r  r   r   r  r  r  r*   r*   r+   test_future_done_exception  s
    z"RpcTest.test_future_done_exceptionc                 C   sh   t | jd | j }t | jd | j }tj|||tddddfd}| |tddd d  d S rL  )r!   rN   rG   r   r   r'   r)   rr  )rH   ro   r  r  r"  r*   r*   r+   _test_future_cb  s    zRpcTest._test_future_cbc                 C   s   |  t d S rE   )r  r  rQ   r*   r*   r+   test_future_in_rpc  s    zRpcTest.test_future_in_rpcc                 C   s   |  t d S rE   )r  r  rQ   r*   r*   r+   test_future_nested_callback  s    z#RpcTest.test_future_nested_callbackc              	   C   s:   |  td$ | t| jd | j t| W 5 Q R X d S )Nr   r&   )r  r
  rq  r!   rN   rG   r  rH   rp  r*   r*   r+   _test_async_function_raise  s    z"RpcTest._test_async_function_raisec                 C   s   |  tj d S rE   )r  r   rl  rQ   r*   r*   r+   test_async_function_raise  s    z!RpcTest.test_async_function_raisec                 C   s   |  tj d S rE   )r  r   rm  rQ   r*   r*   r+   test_async_function_raise_async  s    z'RpcTest.test_async_function_raise_asyncc                 C   s   |  tj d S rE   )r  r   rn  rQ   r*   r*   r+    test_async_function_raise_remote  s    z(RpcTest.test_async_function_raise_remotec              	   C   s>   d}|  t|$ | t| jd | j t| W 5 Q R X d S )NzZFunctions decorated with @rpc\.async_function must return a torch\.futures\.Future object,r&   )r  r
  rq  r!   rN   rG   r  )rH   rp  r  r*   r*   r+   &_test_async_function_wrong_return_type  s    z.RpcTest._test_async_function_wrong_return_typec                 C   s   |  tj d S rE   )r  r   rl  rQ   r*   r*   r+   %test_async_function_wrong_return_type  s    z-RpcTest.test_async_function_wrong_return_typec                 C   s   |  tj d S rE   )r  r   rm  rQ   r*   r*   r+   +test_async_function_wrong_return_type_async  s    z3RpcTest.test_async_function_wrong_return_type_asyncc                 C   s   |  tj d S rE   )r  r   rn  rQ   r*   r*   r+   ,test_async_function_wrong_return_type_remote  s    z4RpcTest.test_async_function_wrong_return_type_remotec                 C   sb   t | jd | j }t | jd | j }tj|t|tdddfd}| |tddd  d S rL  )	r!   rN   rG   r   r   r  r'   r)   rr  r  r*   r*   r+   test_async_function_simple  s    z"RpcTest.test_async_function_simplec                 C   sj   t | jd | j }t | jd | j }|tddddf}| j||||d}| |tddd  d S )Nr&   rj   r   r   r!   rN   rG   r'   r)   rq  rr  )rH   ro  rp  r  r  rp   r"  r*   r*   r+   _test_async_function   s
    zRpcTest._test_async_functionc                 C   s   |  t d S rE   )r  r  rQ   r*   r*   r+   $test_async_function_with_future_ctor  s    z,RpcTest.test_async_function_with_future_ctorc                 C   s   |  ttj d S rE   )r  r  r   rn  rQ   r*   r*   r+   +test_async_function_with_future_ctor_remote  s    z3RpcTest.test_async_function_with_future_ctor_remotec                 C   s   |  t d S rE   )r  r  rQ   r*   r*   r+   test_async_function_chained  s    z#RpcTest.test_async_function_chainedc                 C   s   |  ttj d S rE   )r  r  r   rn  rQ   r*   r*   r+   "test_async_function_chained_remote  s    z*RpcTest.test_async_function_chained_remotec                 C   s   |  t d S rE   )r  r   rQ   r*   r*   r+   test_async_function_nested  s    z"RpcTest.test_async_function_nestedc                 C   s   |  ttj d S rE   )r  r   r   rn  rQ   r*   r*   r+   !test_async_function_nested_remote  s    z)RpcTest.test_async_function_nested_remotec                 C   s   |  tj d S rE   )r  rB  rC  rQ   r*   r*   r+   test_async_static_method#  s    z RpcTest.test_async_static_methodc                 C   s   |  tjtj d S rE   )r  rB  rC  r   rn  rQ   r*   r*   r+   test_async_static_method_remote'  s    z'RpcTest.test_async_static_method_remotec                 C   s   |  tj d S rE   )r  rB  rE  rQ   r*   r*   r+   test_async_class_method.  s    zRpcTest.test_async_class_methodc                 C   s   |  tjtj d S rE   )r  rB  rE  r   rn  rQ   r*   r*   r+   test_async_class_method_remote2  s    z&RpcTest.test_async_class_method_remotec                 C   sl  t | jd | j }t | jd | j }t|t}tdd}tddd }|tj	kr|
 ||||}||
 ||||7 }||
 ||||7 }n|tjkr| |||| }|| |||| 7 }|| |||| 7 }n\|tjkrX| |||| }|| |||| 7 }|| |||| 7 }| |d|  d S )Nr&   rj   r+  )r!   rN   rG   r   r   rB  r'   r)   r   rl  r   rC  rE  rF  rm  r   r   rn  r   rr  )rH   rp  r  r  r   r   r   r"  r*   r*   r+   !_test_test_async_class_rref_proxy9  s$    

z)RpcTest._test_test_async_class_rref_proxyc                 C   s   |    d S rE   )r  rQ   r*   r*   r+   test_async_class_rref_proxyO  s    z#RpcTest.test_async_class_rref_proxyc                 C   s   | j tjd d S N)rp  )r  r   rm  rQ   r*   r*   r+   !test_async_class_rref_proxy_asyncS  s    z)RpcTest.test_async_class_rref_proxy_asyncc                 C   s   | j tjd d S r  )r  r   rn  rQ   r*   r*   r+   "test_async_class_rref_proxy_remoteW  s    z*RpcTest.test_async_class_rref_proxy_remotec           	      C   sv   t | jd | j }t | jd | j }d}d}|tdd||f}| j||||d}| |tdd||   d S )Nr&   rj   r  r   r   r  )	rH   ro  rp  r  r  r  r  rp   r"  r*   r*   r+   _test_async_function_multi[  s    z"RpcTest._test_async_function_multic                 C   s   |  t d S rE   )r  r  rQ   r*   r*   r+   !test_async_function_multi_chainede  s    z)RpcTest.test_async_function_multi_chainedc                 C   s   |  ttj d S rE   )r  r  r   rm  rQ   r*   r*   r+   'test_async_function_multi_chained_asynci  s    z/RpcTest.test_async_function_multi_chained_asyncc                 C   s   |  ttj d S rE   )r  r  r   rn  rQ   r*   r*   r+   (test_async_function_multi_chained_remotep  s    z0RpcTest.test_async_function_multi_chained_remotec                 C   s   |  t d S rE   )r  r*  rQ   r*   r*   r+    test_async_function_multi_fanoutw  s    z(RpcTest.test_async_function_multi_fanoutc                 C   s   |  ttj d S rE   )r  r*  r   rm  rQ   r*   r*   r+   &test_async_function_multi_fanout_async{  s    z.RpcTest.test_async_function_multi_fanout_asyncc                 C   s   |  ttj d S rE   )r  r*  r   rn  rQ   r*   r*   r+   'test_async_function_multi_fanout_remote  s    z/RpcTest.test_async_function_multi_fanout_remotec              	   C   s:   |  td$ | t| jd | j t| W 5 Q R X d S )Nr  r&   )r  r
  rq  r!   rN   rG   rI  r  r*   r*   r+   _test_return_future  s    zRpcTest._test_return_futurec                 C   s   |  tj d S rE   )r  r   rl  rQ   r*   r*   r+   test_return_future  s    zRpcTest.test_return_futurec                 C   s   |  tj d S rE   )r  r   rm  rQ   r*   r*   r+   test_return_future_async  s    z RpcTest.test_return_future_asyncc                 C   s   |  tj d S rE   )r  r   rn  rQ   r*   r*   r+   test_return_future_remote  s    z!RpcTest.test_return_future_remotec              	   C   s   | j dkrd S | j d | j }d|}tj|tddd}|  }| t| |	 
  W 5 Q R X t  | td |  W 5 Q R X tdddd d S )	Nr   r&   zworker{}r@  r+  r  zRRef creationr|  )rN   rG   r  r   r   r   r5  r  r
  r`  r   r   r   r    )rH   r  re  r   r6  r*   r*   r+   test_rref_timeout  s    

zRpcTest.test_rref_timeoutr  r   zbinit_pg_then_rpc does not work with TCP init, see https://github.com/pytorch/pytorch/issues/41614.c                 C   s   t jd| j| j| jd tjt| j| j| j| j| j	d | jd | j }tj
t|tjtdddfd}| |tddd  t   t  d S )Nr  r  rz  r&   rj   r   )r  r	  rL  rN   rG   r   r  r!   r  rZ   r   r'   r(   r)   rr  r  r  rH   r(  r"  r*   r*   r+   test_init_pg_then_rpc  s$    	"zRpcTest.test_init_pg_then_rpczbinit_rpc_then_pg does not work with TCP init, see https://github.com/pytorch/pytorch/issues/41614.c                 C   s   t jt| j| j| j| j| jd tjd| j	| j| jd | jd | j }t j
t|tjtdddfd}| |tddd  t  t   d S )Nrz  r  r  r&   rj   r   )r   r  r!   rN   r  rG   rZ   r  r	  rL  r   r'   r(   r)   rr  r  r  r  r*   r*   r+   test_init_rpc_then_pg  s$    "zRpcTest.test_init_rpc_then_pgc              	   C   s`   g }t | jd | j }tdD ]}|t|t q | t	d t
j|}W 5 Q R X d S )Nr&   r   r   )r!   rN   rG   rP   r)  r   r   r   r  r   r'   r   r  rH   r%  r   r  r"  r*   r*   r+   test_wait_all_with_exception  s    z$RpcTest.test_wait_all_with_exceptionc              	   C   s   g }t | jd | j }tdD ]&}|tj|tjt	ddfd q |t|t
 | td tj|}W 5 Q R X d S )Nr&   r   rj   r   r   )r!   rN   rG   rP   r)  r   r   r'   r(   r)   r   r  r   r   r  r  r*   r*   r+   $test_wait_all_with_partial_exception  s    $z,RpcTest.test_wait_all_with_partial_exceptionzUTest does not work with TCP init, see https://github.com/pytorch/pytorch/issues/46491c                 C   s   t | j| j| j tjt| j| j| j| j| jd t	  t
  | j}| jd7  _tjt| j| j| j| j|d t| jd | j }tj|tjtdddfd tj|tdd t	  d S )Nrz  Zinit_2r&   rj   r   r*   )r   r  rN   rG   r   r  r!   r  rZ   r  r  r  rL  r   r'   r(   r)   r,   )rH   Znew_backend_optionsr   r*   r*   r+   test_init_rpc_twice
  s.    	zRpcTest.test_init_rpc_twicec              	   C   sv   |  td" tjt| j| j| jdd W 5 Q R X |  td, tjt| j| j| j| jd| jid W 5 Q R X d S )Nz0Argument backend must be a member of BackendType
TENSORPIPE)rJ   rN   rG   r{  zEArgument rpc_backend_options must be an instance of RpcBackendOptionsrL  rJ   rN   rG   r{  rZ   )	r  r  r   r  r!   rN   rG   r  rL  rQ   r*   r*   r+   test_wrong_types2  s*    zRpcTest.test_wrong_typesc              	   C   sB   t | j}| td" tjt| j| j| j|d W 5 Q R X d S )Nz#Could not infer backend for optionsrJ   rN   rG   rZ   )	rJ  rL  r  r  r   r  r!   rN   rG   rH   rZ   r*   r*   r+   &test_cannot_infer_backend_from_optionsJ  s    
z.RpcTest.test_cannot_infer_backend_from_optionsc              	   C   s  t | jd | j }tjdddd}t| |  }|  t	|d }| 
||j t J}tj|tj||fd}t| }|| | 
|t||  W 5 Q R X t \}tj|tj||fd}t| }|j|dd || | 
|d t||  W 5 Q R X | td ttd  W 5 Q R X | td	 ttjddd  W 5 Q R X | td
$ ttjddd d W 5 Q R X | td td  W 5 Q R X d S )Nr&   r   Tr  rj   r   Zretain_graphz9tensors does not require grad and does not have a grad_fnz6grad can be implicitly created only for scalar outputsz,Could not find autograd context with id: 100r   ,RRef should contain a tensor for .backward()rQ  )r!   rN   rG   r'   r  r   r   r@  r  	ones_likerr  r  r  r  r   r(   get_gradientsr  r
  )rH   r   r8   r   Zexpected_gradr  r9   r*   r*   r+   test_owner_rref_backwardY  s2    

 

$"(z RpcTest.test_owner_rref_backwardc                 C   s   |   S rE   )r@  r  r*   r*   r+   _sum}  s    zRpcTest._sumc                 C   s   | S rE   r*   r  r*   r*   r+   	_identity  s    zRpcTest._identityc              
   C   s   t | jd | j }tjddd}t R}tj|t	j
|fd}|j|dd || | t|d t||  W 5 Q R X t Z}tj|t	jdd}| td	 || W 5 Q R X | td
 |  W 5 Q R X W 5 Q R X d S )Nr&   r   Tr  r   r  rj   )rQ  r  z9User RRefs require 'dist_autograd_ctx_id' to be specified)r!   rN   rG   r'   r  r  r  r   r   r  r  r  rr  r  r   r  r  r
  )rH   r   r:   r  r   r*   r*   r+   test_user_rref_backward  s    

*
zRpcTest.test_user_rref_backwardc              	      s   t | j| j| j tjt| j| j| j| j| jd | jdkrtj	j
 tj	j} fdd}dd }z4|tj	_
|tj	_| td t  W 5 Q R X W 5  tj	_
|tj	_X n | td t  W 5 Q R X t  d S )	Nrz  r   c                    s    | | t dd S )NZ
simulationr	  )Zsequence_idZobjects_mapr,  r*   r+   raise_error  s    
z1RpcTest.test_shutdown_errors.<locals>.raise_errorc                   S   s   t dd S )Nsimulation rrefr	  r*   r*   r*   r+   
rref_error  s    z0RpcTest.test_shutdown_errors.<locals>.rref_errorr  ztimed out in _all_gather)r   r  rN   rG   r   r  r!   r  rZ   r  Z_broadcast_to_followersrT  r  r
  r  r  r  )rH   Zog_rref_funcr  r  r*   r  r+   test_shutdown_errors  s.    
zRpcTest.test_shutdown_errorsc                 C   s   |  d d S r"  r  rQ   r*   r*   r+   test_my_parameter_server  s    z RpcTest.test_my_parameter_server)F)r  )FNF)F)F(  rS   rT   rU   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   patchobjectr'   r  r3   r   r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r   r!  r$  r%  r-  r/  r0  r2  r7  r:  r?  rC  rD  rH  rI  rJ  rK  rN  rP  rU  r^  rj  ro  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  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  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@  rA  rC  rD  rE  rG  rH  rI  rJ  rN  rP  rQ  rS  rW  rZ  r[  r_  rc  rd  ri  rj  rn  r   r   ro  rq  rr  ru  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  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   rl  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  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r*   r*   r*   r+   r  U  s  





	

	




7






















	

















D;

,


8

    
M

	




	





















	



















1
)J$)



	##(r  c                   @   s    e Zd Zededd ZdS )CudaRpcTestrj   c              	      s  | j dkrd S | j d | j }| j d | j }t|}t|}tdd6}tj|tdd}tj|tdd}|  |  W 5 Q R X dd	  |j}|D ]}	|	j	r| 
d
|	j | 
g |	j | 
d
|	j q|	jdkrq| |	j||fk  |	tkr| |	jd
 | 
dt|	j |	jd
 }
|	j|kr<| 
|
jd
 |	j|krV| 
|
jd | |	jd
 qdd |D } fdd|D }| 
t|tt d S )Nr&   rj   T)Zuse_cudar5  r   r  c                 S   s   | j | j ttt d  S rE   )rJ   r  r  r#  )r9  r*   r*   r+   get_name  s    z7CudaRpcTest.test_profiler_remote_cuda.<locals>.get_namer   c                 S   s   g | ]}|j r|qS r*   r_  ra  r*   r*   r+   r     s      z9CudaRpcTest.test_profiler_remote_cuda.<locals>.<listcomp>c                    s    g | ]} |t kr |qS r*   )ru  ra  r  r*   r+   r     s      )rN   rG   r!   rc  r   r   r;   r   rd  Zis_asyncrr  Zcuda_time_totalZkernelsZ	cuda_timer  r  ru  ZassertGreaterr#  r2   rt   )rH   Z
dst_cuda_0Z
dst_cuda_1Zdst_worker_cuda_0Zdst_worker_cuda_1rn  r  r  rd  r9  kernelrg  r  r*   r  r+   test_profiler_remote_cuda  sB    


z%CudaRpcTest.test_profiler_remote_cudaN)rS   rT   rU   r   r   r  r*   r*   r*   r+   r    s   r  c                   @   s  e Zd Zdd Zdd Zedddd Zeddd	d
 Zedddd Zedd Z	dd Z
dd Zedd Zdd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zd%d& Zed'd( Zd)d* Zd+d, Zed-d. Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zed9d: Zed;d< Z ed=d> Z!ed?d@ Z"edAdB Z#edCdD Z$edEdF Z%edGdH Z&edddIdJ Z'edddKdL Z(edddMdN Z)e*dOedddPdQ Z+edddRdS Z,edddTdU Z-dVS )WTensorPipeAgentRpcTestc              	   C   sH   t | j}| td( tjt| j| j| jtj	j
|d W 5 Q R X d S )Nz=`rpc_backend_options` must be a `TensorPipeRpcBackendOptions`r  )rJ  rL  r  r  r   r  r!   rN   rG   ZBackendTyper  r  r*   r*   r+    test_mismatched_type_for_options  s    
 z7TensorPipeAgentRpcTest.test_mismatched_type_for_optionsc                 C   sF   t j| jt d}t jt| j| j| j|d | t j	
 t j d S )N)rL  _transportsr  )r   TensorPipeRpcBackendOptionsrL  r   r  r!   rN   rG   ra  r  r  ZTensorPipeAgentr  r*   r*   r+   test_infer_backend_from_options  s    z6TensorPipeAgentRpcTest.test_infer_backend_from_optionsFr  c                 C   sh   d}t j| jj|t d}t jt| j| j| j| j	|d t j
  }| t|d | t   d S )N   )rL  num_worker_threadsr  rz  zagent.thread_pool_size)r   r  rZ   rL  r   r  r!   rN   r  rG   r  r  rg  rr  r3  r  )rH   ZNUM_THREADSrZ   r  r*   r*   r+   #test_set_and_get_num_worker_threads  s     z:TensorPipeAgentRpcTest.test_set_and_get_num_worker_threadsc                 C   s`   d}t j| jj| jj|t d}t jt| j| j	| j| j
|d t  }| || t   d S )Nr   )rL  r  rs  r  rz  )r   r  rZ   rL  r  r   r  r!   rN   r  rG   rt  rr  r  )rH   r'  rZ   Zdefault_timeoutr*   r*   r+   #test_tensorpipe_set_default_timeout.  s"    z:TensorPipeAgentRpcTest.test_tensorpipe_set_default_timeoutc              	   C   sF   ddl m} | }| td tj| jj| jj|d}W 5 Q R X d S )Nr   )	timedeltaz"incompatible constructor arguments)rL  r  rs  )	datetimer  r  r  r   r  rZ   rL  r  )rH   r  r'  rZ   r*   r*   r+   2test_tensorpipe_options_throw_on_timedelta_timeoutF  s    zITensorPipeAgentRpcTest.test_tensorpipe_options_throw_on_timedelta_timeoutc              	   C   s   | j d | j }t|}tj|ttdddfd}d}|  }|rl| 	t
| |j||d W 5 Q R X n.|j||d}| 	t
| |  W 5 Q R X |  d S )Nr&   rj   Tr   r  )r'  r2  )rN   rG   r!   r   r   ry   r'   r)   r5  r  r
  r6  r   r   )rH   r2  r  r   	slow_rrefr'  r   r   r*   r*   r+   _test_rref_get_type_timeoutS  s    z2TensorPipeAgentRpcTest._test_rref_get_type_timeoutc                 C   s   | j dd d S r<  r   rQ   r*   r*   r+   #test_rref_get_type_timeout_blockingj  s    z:TensorPipeAgentRpcTest.test_rref_get_type_timeout_blockingc                 C   s   | j dd d S r?  r!  rQ   r*   r*   r+   'test_rref_get_type_timeout_non_blockingm  s    z>TensorPipeAgentRpcTest.test_rref_get_type_timeout_non_blockingc              	   C   sB   t | jd | j }| td tj|tjdd W 5 Q R X d S )Nr&   zGOverloaded torch operator invoked from Python failed to many any schemar*   r   )	r!   rN   rG   r  r
  r   r   r'   r(   r  r*   r*   r+   test_op_with_invalid_argsp  s     z0TensorPipeAgentRpcTest.test_op_with_invalid_argsc           
   	   C   s:  | j d | j }t|}tj|ttddfd}|  t	||}| 
|d k	d|  |  }d}| t|H ||dtdd}||jkr|  n||jkr|   W 5 Q R X tj|ttdddfd}	d}t	|	|}| t|2 ||dtdd}||	jkr$|  W 5 Q R X |	  d S )Nr&   rj   r   zFailed to get RRef proxy api: r&  Tr+  )rN   rG   r!   r   r   ry   r'   r)   r   getattrr  r5  r  r
  r   r   r   r`  r~   )
rH   Zrref_proxy_apir  r   r   Zrref_apir6  r'  ra   r  r*   r*   r+   _test_rref_proxy_timeoutx  s,    




z/TensorPipeAgentRpcTest._test_rref_proxy_timeoutc                 C   s   dD ]}|  | qd S )N)r   r   r   )r&  )rH   Zrpc_apir*   r*   r+   test_rref_proxy_timeout  s    z.TensorPipeAgentRpcTest.test_rref_proxy_timeoutc                 C   s   | j d | j }tjtjtjfD ]<}t }t }|| }| j|tj	|||fd}| 
|| q tjtjtjfD ]D}tdd}tdd}|| }| j|tj	|||fd}| 
|| qnd S )Nr&   r   T)r   )rN   rG   r   rl  rm  rn  r   rq  r'   r(   rr  )rH   r  r  r   r   r  r"  r*   r*   r+   test_send_to_rank_sparse  s    

z/TensorPipeAgentRpcTest.test_send_to_rank_sparsec                 C   s   |  t t t t  d S rE   )rt  r   ru  r   rQ   r*   r*   r+   test_self_py_udf_remote_sparse  s    z5TensorPipeAgentRpcTest.test_self_py_udf_remote_sparsec                 C   s.   t | jd | j }| |t t t  d S r%   )r!   rN   rG   rw  r   r  r*   r*   r+   'test_self_remote_rref_as_rpc_arg_sparse  s    z>TensorPipeAgentRpcTest.test_self_remote_rref_as_rpc_arg_sparsec                 C   s   |  t t t t  d S rE   )rw  r   ru  r   rQ   r*   r*   r+   ,test_self_remote_rref_as_self_rpc_arg_sparse  s    zCTensorPipeAgentRpcTest.test_self_remote_rref_as_self_rpc_arg_sparsec                 C   s.   t | jd | j }| |t t t  d S r%   )r!   rN   rG   rx  r   r  r*   r*   r+   *test_self_remote_rref_as_remote_arg_sparse  s    zATensorPipeAgentRpcTest.test_self_remote_rref_as_remote_arg_sparsec                 C   s   |  t t t t  d S rE   )rx  r   ru  r   rQ   r*   r*   r+   /test_self_remote_rref_as_self_remote_arg_sparse  s    zFTensorPipeAgentRpcTest.test_self_remote_rref_as_self_remote_arg_sparsec                 C   s   |  t t  d S rE   )r  r   rQ   r*   r*   r+   test_world_size_one_sparse  s    z1TensorPipeAgentRpcTest.test_world_size_one_sparsec                 C   s   |  d d S r  r#  rQ   r*   r*   r+   test_multi_rpc_sparse  s    z,TensorPipeAgentRpcTest.test_multi_rpc_sparsec                 C   s   |  tt  d S rE   )r  r   r   rQ   r*   r*   r+   test_wait_all_workers_sparse  s    z3TensorPipeAgentRpcTest.test_wait_all_workers_sparsec                 C   s   |  tt  d S rE   )r  r   r   rQ   r*   r*   r+   "test_wait_all_workers_twice_sparse  s    z9TensorPipeAgentRpcTest.test_wait_all_workers_twice_sparsec                 C   sJ   | j d }|| j }t t g}tjt|t|fd}| |t| d S r  )rN   rG   r   r   r   r!   r   rr  )rH   r  r  rz   r"  r*   r*   r+   #test_py_sparse_tensors_in_container  s    

  z:TensorPipeAgentRpcTest.test_py_sparse_tensors_in_containerc                 C   s   |  tt d  d S ri   )r  r   r   rQ   r*   r*   r+   test_nested_rpc_sparse  s    z-TensorPipeAgentRpcTest.test_nested_rpc_sparsec                 C   s   | j tdt fd d S )Nr  r  )r  r   r   rQ   r*   r*   r+   test_stress_heavy_rpc_sparse  s    z3TensorPipeAgentRpcTest.test_stress_heavy_rpc_sparsec                 C   s   |  t t t d  d S ri   )r  r   rQ   r*   r*   r+   test_builtin_remote_ret_sparse  s
    z5TensorPipeAgentRpcTest.test_builtin_remote_ret_sparsec                 C   s   |  t t t d  d S ri   )r  r   rQ   r*   r*   r+   test_builtin_remote_self_sparse  s
    z6TensorPipeAgentRpcTest.test_builtin_remote_self_sparsec                 C   s   | j tjdtjd d S )NTr  r  rQ   r*   r*   r+   $test_multi_builtin_remote_ret_sparse  s
     z;TensorPipeAgentRpcTest.test_multi_builtin_remote_ret_sparsec                 C   s   | j tdtjd d S )NTr  r  rQ   r*   r*   r+   test_multi_py_udf_remote_sparse  s
    z6TensorPipeAgentRpcTest.test_multi_py_udf_remote_sparsec                 C   s$   |  t t t t t d  d S )Nr   )r  r   rQ   r*   r*   r+   test_py_rref_args_sparse$  s    z/TensorPipeAgentRpcTest.test_py_rref_args_sparsec              
   C   s,   |  t t t t t t t d  d S N   )r  r   rQ   r*   r*   r+   #test_py_rref_args_user_share_sparse.  s    z:TensorPipeAgentRpcTest.test_py_rref_args_user_share_sparsec              
   C   s,   |  t t t t t t t d  d S r:  )r  r   rQ   r*   r*   r+   test_py_rpc_rref_args_sparse:  s    z3TensorPipeAgentRpcTest.test_py_rpc_rref_args_sparsec                 C   s   |  tt t   d S rE   )r  r   r   rQ   r*   r*   r+   test_nested_remote_sparseF  s    
z0TensorPipeAgentRpcTest.test_nested_remote_sparsec                 C   s   |  tt d t d  d S ri   )r  r   r   rQ   r*   r*   r+   test_nested_rref_sparseM  s
    z.TensorPipeAgentRpcTest.test_nested_rref_sparsec                 C   s   |  tt d t d  d S ri   )r  r   r   rQ   r*   r*   r+   test_nested_rref_stress_sparseU  s
    z5TensorPipeAgentRpcTest.test_nested_rref_stress_sparsec                 C   s   |  d d S r  r
  rQ   r*   r*   r+   test_my_parameter_server_sparse]  s    z6TensorPipeAgentRpcTest.test_my_parameter_server_sparsec                 C   s*   t jt| j| j| j| jd t   d S )NrJ   r{  rN   rZ   )r   r  r!   rN   r  rZ   r  rQ   r*   r*   r+   test_dynamic_rpc_init_rpcb  s    z0TensorPipeAgentRpcTest.test_dynamic_rpc_init_rpcc                 C   s   t | j| j| j | jdkr:tjt| j| j| j| jd t	
  | jdkrtjt| j| j| j| jd tjtdtjtdtdfd}| ttdtd| t	
  t  d S Nr   rB  r&   r   )r   r  rN   rG   r   r  r!   r  rZ   r  r  r   r'   r(   r   rr  r  )rH   ra   r*   r*   r+   =test_dynamic_rpc_new_rank_can_communicated_with_existing_rankm  s(    

& zTTensorPipeAgentRpcTest.test_dynamic_rpc_new_rank_can_communicated_with_existing_rankc              	   C   s   t | j| j| j | jdkr:tjt| j| j| j| jd t	
  | jdkrjtjt| j| j| j| jd t	
  | jdkrtd| jD ]J}tjt|tjtdtdfd}| ttdtd| qt	
  t  d S rD  )r   r  rN   rG   r   r  r!   r  rZ   r  r  rP   r   r'   r(   r   rr  r  )rH   r   ra   r*   r*   r+   <test_dynamic_rpc_existing_rank_can_communicate_with_new_rank  s.    


&"zSTensorPipeAgentRpcTest.test_dynamic_rpc_existing_rank_can_communicate_with_new_rankrj   c                 C   s  t | j| j| j | jdkrx| j}td| jD ],}t|}||ddi ||ddi q.tj	t| j| j
| j|d t  | jdkrtj	t| j| j
| j| jd t  | jdkrttd| jD ]}td}tjt|tj|ddfd}tjt|tj|ddfd}| ttdd| | td|j | ttdd| | td|j qt  t  d S )Nr   r&   rB  rj   r   r  cuda:1)r   r  rN   rG   rZ   rP   r!   set_device_mapr   r  r  r  r  r'   r)   r   r(   r   rr  r2   r  )rH   optionsr   r   r   Zresult_on_device_0Zresult_on_device_1r*   r*   r+   Atest_dynamic_rpc_existing_rank_can_communicate_with_new_rank_cuda  sB    


  zXTensorPipeAgentRpcTest.test_dynamic_rpc_existing_rank_can_communicate_with_new_rank_cudac              	   C   s   |  td  tjt| j| j| jd W 5 Q R X |  td* tjdd}tjt| j| j|d W 5 Q R X |  td* tjdd}tjt| j| j|d W 5 Q R X d S )Nzrank parameter missing)rJ   r{  rZ   z"environment variable RANK expectedzenv://)rL  ztcp://127.0.0.1:23456)	r  r   r   r  r!   rN   r  rZ   r  r  r*   r*   r+   &test_dynamic_rpc_init_rpc_without_rank  s(    z=TensorPipeAgentRpcTest.test_dynamic_rpc_init_rpc_without_rankc              	   C   s   t jd| j| j| jd | jd }| j|k rLtjt| j| j| j|| j	d t 
  | j|kr| td$ tjt| j| j| j| j	d W 5 Q R X d S )Nr  r  r&   rz  zVRPC group mixes statically and dynamically initialized members which is not supported.rB  )r  r	  r  rN   rG   r   r  r!   r  rZ   r  r  r
  )rH   Zworld_size_minus_oner*   r*   r+   )test_dynamic_and_static_init_rpc_together  s0    


z@TensorPipeAgentRpcTest.test_dynamic_and_static_init_rpc_togetherN).rS   rT   rU   r  r  r   r  r  r  r   r"  r#  r$  r&  r'  r(  r)  r*  r+  r,  r-  r.  r/  r0  r1  r2  r3  r4  r5  r6  r7  r8  r9  r<  r=  r>  r?  r@  rA  rC  rE  rF  r   rJ  rK  rL  r*   r*   r*   r+   r    s   




$



	

	


	






	









#,
r  c                   @   s  e Zd Zdd Zeddd Zeddd Zedd	d
 Zeddd Zeddd Z	eddd Z
edd Zeddd Zedd ZdddZdd Zeddd Zeddd Zedd d! Zedd"d# Zedd$d% Zedd&d' Zedd(d) Zedd*d+ Zedd,d- Zedd.d/ Zedd0d1 Zedd2d3 Zedd4d5 Zedd6d7 Zedd8d9 Zedd:d; Z edd<d= Z!edd>d? Z"edd@dA Z#eddBdC Z$eddDdE Z%eddFdG Z&eddHdI Z'eddJdK Z(edLdM Z)dNdO Z*eddPdQ Z+eddRdS Z,edTdU Z-eddVdW Z.dXdY Z/edZd[d\ Z0edZd]d^ Z1ed_d` Z2dadb Z3dcdd Z4ede5dedf Z6eddgdh Z7ede5didj Z8ede5dkdl Z9ede5dmdn Z:ede5dodp Z;ede5dqdr Z<eddsdt Z=edudv Z>dwdx Z?dydz Z@edd{d| ZAd}d~ ZBeddd ZCedd ZDdd ZEeddd ZFdd ZGeddd ZHedd ZIeddd ZJdd ZKeddd ZLeddd ZMeddd ZNeddd ZOdddZPeddd ZQeddd ZReddd ZSeddd ZTeddd ZUedd ZVdd ZWeddd ZXeddd ZYeddd ZZeddd Z[dd Z\eddd Z]eddd Z^eddd Z_eddd Z`edd Zaeddd Zbeddd Zceddd ZdedddĄ ZeedddƄ ZfedddȄ Zgedddʄ Zheddd̄ Zieddd΄ ZjedddЄ Zkeddd҄ ZledddԄ Zmedddք Zneddd؄ Zoedddڄ Zpeddd܄ Zqedddބ Zreddd Zseddd Zteddd ZudS )TensorPipeAgentCudaRpcTestc              	   C   sL   |  t|& tjt| j| j| j| j|d W 5 Q R X | tj	
  d S )Nrz  )r  r   r   r  r!   rN   r  rG   r9  r  Z_is_current_rpc_agent_set)rH   rI  r  r*   r*   r+   _test_device_maps  s    z,TensorPipeAgentCudaRpcTest._test_device_mapsrj   c                 C   s(   | j }|dddi | j|dd d S )NZ
none_existr   r&   z=Node worker0 has invalid target node names in its device mapsr  )rZ   rH  rN  )rH   rI  r*   r*   r+   "test_device_maps_wrong_worker_name)  s    z=TensorPipeAgentCudaRpcTest.test_device_maps_wrong_worker_namer&   c                 C   sB   | j }t| jd | j }||tj di | j|dd d S )Nr&   r   zRNode worker0 has source devices with invalid indices in its device map for worker1rO  	rZ   r!   rN   rG   rH  r'   r1   Zdevice_countrN  rH   rI  r   r*   r*   r+   )test_device_maps_invalid_max_local_device3  s    zDTensorPipeAgentCudaRpcTest.test_device_maps_invalid_max_local_devicec                 C   sB   | j }t| jd | j }||dtj i | j|dd d S )Nr&   r   zRNode worker0 has target devices with invalid indices in its device map for worker1rO  rQ  rR  r*   r*   r+   *test_device_maps_invalid_max_remote_device>  s    zETensorPipeAgentCudaRpcTest.test_device_maps_invalid_max_remote_devicec                 C   sL   | j }t| jd | j }||ddi ||ddi | j|dd d S )Nr&   r   zHNode worker0 has duplicated target devices in its device map for worker1rO  )rZ   r!   rN   rG   rH  rN  rR  r*   r*   r+   test_device_maps_many_to_oneI  s    z7TensorPipeAgentCudaRpcTest.test_device_maps_many_to_onec              	   C   s`   | j dkr\| j}t| j d | j }||ddi | td ||ddi W 5 Q R X d S )Nr   r&   z-`set_device_map` only supports 1-to-1 mapping)rN   rZ   r!   rG   rH  r  r   rR  r*   r*   r+   test_device_maps_one_to_manyU  s    
 z7TensorPipeAgentCudaRpcTest.test_device_maps_one_to_manyc              	   C   sn   | j }t| jd | j }| td ||ddi W 5 Q R X | td ||ddi W 5 Q R X d S )Nr&   z!Device index must not be negativer-   r   )rZ   r!   rN   rG   r  r
  rH  rR  r*   r*   r+   #test_device_maps_invalid_min_device`  s      z>TensorPipeAgentCudaRpcTest.test_device_maps_invalid_min_devicec                 C   s>   t | j| jjdk|j|jjdkgr2| | dS tdd S Nr&   r   Wrong device affinityr  is_cudar2   r  r   r   r  r*   r*   r+   _gpu_addn  s    $z#TensorPipeAgentCudaRpcTest._gpu_addc                 C   s   | j }t| jd | j }||ddd tjt| j| j| j| j|d tj|t	j
tddtddfd}| |jtd | |tdtd d t  d S )Nr&   r   r   r&   rz  rj   r   )rZ   r!   rN   rG   rH  r   r  r  r   rM  r\  r'   r  r   r)   rr  r2   r  )rH   rI  r   r"  r*   r*   r+   test_device_maps_gpuu  s$    "z/TensorPipeAgentCudaRpcTest.test_device_maps_gpuc                 C   s`   | j jdkrdn| j j}|j jdkr(dn|j j}||krT||krT| ||| S tdd S )Nr  rY  )r2   r7  r  r   r   )r   r   x_toy_toz_toZx_deviceZy_devicer*   r*   r+   _gpu_add_given_devices  s
    z1TensorPipeAgentCudaRpcTest._gpu_add_given_devicesNc                    s  |d krt jn|} | } | }| j}	|d krDt| jd | j n|}|	|  tjt| j| j	| j| j|	d t
d|}
t
d|}tj|||
||||fd} fdd D }|| }|jjdkrdn|jj}| || | |t
d| t  d S )Nr&   rz  rj   r   c                    s   i | ]} | |qS r*   r*   )rM   r   
device_mapr*   r+   r~    s      zDTensorPipeAgentCudaRpcTest._test_device_maps_gpu.<locals>.<dictcomp>r  )rM  rb  rZ   r!   rN   rG   rH  r   r  r  r'   r  r   r)   r   r2   r7  r  rr  r  )rH   x_fromy_fromra  rd  r   ro  r_  r`  rI  r   r   r"  Zreverse_device_mapZz_fromZ
ret_devicer*   rc  r+   _test_device_maps_gpu  s,     z0TensorPipeAgentCudaRpcTest._test_device_maps_gpuc                 C   s   | j ddddditjd d S )Nr  re  rf  ra  rd  ro  rg  rM  rb  rQ   r*   r*   r+   test_device_map_cpu  s    z.TensorPipeAgentCudaRpcTest.test_device_map_cpuc                 C   s   | j ddddditjd d S )Nr  r   rh  ri  rQ   r*   r*   r+   "test_device_map_cpu_to_gpu_default  s    z=TensorPipeAgentCudaRpcTest.test_device_map_cpu_to_gpu_defaultc                 C   s   | j ddddditjd d S )Nr  r&   rh  ri  rQ   r*   r*   r+   &test_device_map_cpu_to_gpu_non_default  s    zATensorPipeAgentCudaRpcTest.test_device_map_cpu_to_gpu_non_defaultc                 C   s   | j ddddditjd d S )Nr   r  rh  ri  rQ   r*   r*   r+   "test_device_map_gpu_to_cpu_default  s    z=TensorPipeAgentCudaRpcTest.test_device_map_gpu_to_cpu_defaultc                 C   s   | j ddddditjd d S )Nr&   r  rh  ri  rQ   r*   r*   r+   &test_device_map_gpu_to_cpu_non_default  s    zATensorPipeAgentCudaRpcTest.test_device_map_gpu_to_cpu_non_defaultc                 C   s   | j dddddid d S )Nr   re  rf  ra  rd  rg  rQ   r*   r*   r+   test_device_map_gpu_default  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_defaultc                 C   s   | j dddddid d S )Nr&   ro  rp  rQ   r*   r*   r+   test_device_map_gpu_non_default  s    z:TensorPipeAgentCudaRpcTest.test_device_map_gpu_non_defaultc                 C   s   | j dddddid d S )Nr   r&   ro  rp  rQ   r*   r*   r+   *test_device_map_gpu_default_to_non_default  s    zETensorPipeAgentCudaRpcTest.test_device_map_gpu_default_to_non_defaultc                 C   s   | j dddddid d S )Nr&   r   ro  rp  rQ   r*   r*   r+   *test_device_map_gpu_non_default_to_default   s    zETensorPipeAgentCudaRpcTest.test_device_map_gpu_non_default_to_defaultc                 C   s   | j ddddddd d S Nr   r&   r]  ro  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_1	  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_1c                 C   s   | j ddddddd d S ru  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_2  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_2c                 C   s   | j ddddddd d S Nr&   r   r]  ro  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_3  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_3c                 C   s   | j ddddddd d S rx  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_4$  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_4c                 C   s   | j ddddddd d S ru  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_5-  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_5c                 C   s   | j ddddddd d S ru  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_66  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_6c                 C   s   | j ddddddd d S rx  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_7?  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_7c                 C   s   | j ddddddd d S rx  rp  rQ   r*   r*   r+   test_device_map_gpu_mixed_8H  s    z6TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_8c                 C   s$   | j ddddddt| jd d S Nr   r&   r]  re  rf  ra  rd  r   rg  r!   rN   rQ   r*   r*   r+    test_device_map_gpu_mixed_self_1Q  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_1c                 C   s$   | j ddddddt| jd d S r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_2[  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_2c                 C   s$   | j ddddddt| jd d S Nr&   r   r]  r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_3e  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_3c                 C   s$   | j ddddddt| jd d S r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_4o  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_4c                 C   s$   | j ddddddt| jd d S r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_5y  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_5c                 C   s$   | j ddddddt| jd d S r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_6  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_6c                 C   s$   | j ddddddt| jd d S r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_7  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_7c                 C   s$   | j ddddddt| jd d S r  r  rQ   r*   r*   r+    test_device_map_gpu_mixed_self_8  s    z;TensorPipeAgentCudaRpcTest.test_device_map_gpu_mixed_self_8c                 C   sL   t | j| jjdk|j|jjdkgr@| d| | |d fS tdd S rX  rZ  r  r*   r*   r+   _gpu_add_multi_gpu  s    $z-TensorPipeAgentCudaRpcTest._gpu_add_multi_gpuc                 C   s  | j }||ddi ||ddi tjt| j| j| j| j|d t	d
d}td
d}tj|tj||fd}| |d jtd | |d jtd | |d t	dtd 
d | |d t	dtd 
d t  d S )Nr   r&   rz  rj   r   )rZ   rH  r   r  r!   rN   r  rG   r'   r  r   r)   r   rM  r  rr  r2   r  rH   r   rI  r   r   r-  r*   r*   r+   _test_device_maps_multi_gpu  s,    &&z6TensorPipeAgentCudaRpcTest._test_device_maps_multi_gpuc                 C   s"   t | jd | j }| | d S r%   )r!   rN   rG   r  r  r*   r*   r+   test_device_maps_multi_gpu  s    z5TensorPipeAgentCudaRpcTest.test_device_maps_multi_gpuc                 C   s   t | j}| | d S rE   )r!   rN   r  r  r*   r*   r+   test_device_maps_multi_gpu_self  s    
z:TensorPipeAgentCudaRpcTest.test_device_maps_multi_gpu_selfc                 C   sX   | j jdkrL|j jdkrL| | d| | d| | d| | dfS tdd S )Nr  r   r&   rj   r   rY  )r2   r7  r   r   r  r*   r*   r+   _gpu_add_return_to_gpu  s    4z1TensorPipeAgentCudaRpcTest._gpu_add_return_to_gpuc                 C   s  t | jd | j }| j}tjt | j| j| j| jtj|j|j	|dddit
 dd tj|tjtddtddfd}| |d jtd | |d jtd | |d tdtd d | |d tdtd d t  d S )Nr&   r   r]  )rL  r  device_mapsr  rz  rj   r   )r!   rN   rG   rZ   r   r  r  r  rL  r  r   r   rM  r  r'   r  r   r)   rr  r2   r  )rH   r   rI  r-  r*   r*   r+   test_device_maps_in_options  s0    &&z6TensorPipeAgentCudaRpcTest.test_device_maps_in_optionsc                 C   s^  | j }||ddi ||ddi ||ddi ||ddi tjt| j| j| j| j|d tj|t	j
tdtdfd}tt|D ]$}| || jtd| d  q| |d tdtd d | |d tdtd d | |d tdtd d | |d tdtd d t  d S )Nr   r&   rj   r   rz  r   r   )rZ   rH  r   r  r!   rN   r  rG   r   rM  r  r'   r  r)   rP   r#  rr  r2   r   r  )rH   r   rI  r-  r   r*   r*   r+   _test_device_maps_return_to_gpu  s0    "&&&&z:TensorPipeAgentCudaRpcTest._test_device_maps_return_to_gpur   c                 C   s"   t | jd | j }| | d S r%   )r!   rN   rG   r  r  r*   r*   r+   test_device_maps_return_to_gpu  s    z9TensorPipeAgentCudaRpcTest.test_device_maps_return_to_gpuc                 C   s   t | j}| | d S rE   )r!   rN   r  r  r*   r*   r+   #test_device_maps_return_to_gpu_self  s    
z>TensorPipeAgentCudaRpcTest.test_device_maps_return_to_gpu_selfc                 C   s   | |  dS rB   )r   r  r*   r*   r+   _add_to_gpu  s    z&TensorPipeAgentCudaRpcTest._add_to_gpuc              	   C   s   t | jd | j }d}| t|t |tjkrTtj|t	j
t	dddfd n@|tjkrtj|t	j
t	dddfd  ntd| W 5 Q R X tj|t	j
t	ddfd}| |t	dd  d S )Nr&   znTensorPipe RPC backend only supports CPU tensors by default.*`set_device_map` on `TensorPipeRpcBackendOptions`rj   r   r   unexpected mode )r!   rN   rG   r  r
  r   rl  r   r   r'   r(   r  r   rn  r   r   r   r)   rr  rH   rp  r   r  r"  r*   r*   r+    _test_device_maps_missing_config  s    
$
(z;TensorPipeAgentCudaRpcTest._test_device_maps_missing_configc              	   C   s   t | jd | j }d}| t|h |tjkrNtj|t	j
tddfd n:|tjkrztj|t	j
tddfd  ntd| W 5 Q R X tj|tjtddfd}| |tdd  d S )Nr&   z(Response device mapping is not availablerj   r   r  )r!   rN   rG   r  r
  r   rl  r   r   rM  r  r'   r  rn  r   r   r   r(   r)   rr  r  r*   r*   r+   )_test_device_maps_missing_config_response/  s$    

zDTensorPipeAgentCudaRpcTest._test_device_maps_missing_config_responsec                 C   s   |  tj d S rE   )r  r   rl  rQ   r*   r*   r+   test_device_maps_missing_configG  s    z:TensorPipeAgentCudaRpcTest.test_device_maps_missing_configc                 C   s~   t | jd | j }| j}tjt | j| j| j| j| jd t }t }| 	t
j t  t }| || |k  d S Nr&   rz  )r!   rN   rG   rZ   r   r  r  rt  rw   r  r   rl  r  r  )rH   r   rI  r'  r  r  r*   r*   r+   +test_device_maps_missing_config_not_timeoutL  s    zFTensorPipeAgentCudaRpcTest.test_device_maps_missing_config_not_timeoutc                 C   s&   t | jjd D ]}| tj qd S r   )rP   rZ   r  r  r   rl  rH   r  r*   r*   r+   $test_device_maps_missing_config_loopb  s    z?TensorPipeAgentCudaRpcTest.test_device_maps_missing_config_loopc                 C   s   |  tj d S rE   )r  r   rl  rQ   r*   r*   r+   (test_device_maps_missing_config_responseh  s    zCTensorPipeAgentCudaRpcTest.test_device_maps_missing_config_responsec                 C   s&   t | jjd D ]}| tj qd S r   )rP   rZ   r  r  r   rl  r  r*   r*   r+   -test_device_maps_missing_config_response_loopm  s    zHTensorPipeAgentCudaRpcTest.test_device_maps_missing_config_response_loopc                 C   s   |  tj d S rE   )r  r   rn  rQ   r*   r*   r+   &test_device_maps_missing_config_remotes  s    zATensorPipeAgentCudaRpcTest.test_device_maps_missing_config_remotec                 C   s   |  tj d S rE   )r  r   rn  rQ   r*   r*   r+   /test_device_maps_missing_config_remote_responsex  s    zJTensorPipeAgentCudaRpcTest.test_device_maps_missing_config_remote_responsec                 C   s   | j }t| jd | j }||ddi tjt| j| j| j| j|d tj|t	j
tddfd}| | jjd | | tdd t  d S )Nr&   r   rz  rj   r   )rZ   r!   rN   rG   rH  r   r  r  r   rM  r  r'   r  rr  r   r2   r  r)   r   r  )rH   rI  r   r   r*   r*   r+   test_device_maps_remote}  s$    z2TensorPipeAgentCudaRpcTest.test_device_maps_remotec              	   C   s   t j| j}t jj| jd}|| | | || t j| t jdt	  | | }W 5 Q R X || || |S )Nr  r   )
r'   r1   r.  r2   r/  r0  Zrecord_streamr1  r2  ri  )r   r   s0s1r   r*   r*   r+   _slow_add_on_user_stream  s    




z3TensorPipeAgentCudaRpcTest._slow_add_on_user_streamc                 C   sZ   | j }t| jd | j }||| tjt| j| j| j| j|d || t  d S r  )	rZ   r!   rN   rG   rH  r   r  r  r  )rH   ro  rd  rI  r   r*   r*   r+   _test_custom_stream  s    z.TensorPipeAgentCudaRpcTest._test_custom_streamc                 C   s<   t ddd}tj|tj||fd}| |d|  d S )Nrj   r   r   )r'   r)   r   r   r   rM  r  rr  )rH   r   r   r"  r*   r*   r+   _test_stream_sync  s    z,TensorPipeAgentCudaRpcTest._test_stream_syncc                 C   s   |  | jddi d S Nr  rG  )r  r  rQ   r*   r*   r+   test_custom_stream  s    z-TensorPipeAgentCudaRpcTest.test_custom_streamc              	   C   s   g }t dD ]6}tddd| }|tj|tj||fd qt dD ].}| 	|| 
 dtddd |  qLd S )Nr  rj   r   r   )rP   r'   r)   r   r)  r   r   rM  r  rr  r   )rH   r   r%  r   r   r*   r*   r+   _test_stream_multi_async  s    z3TensorPipeAgentCudaRpcTest._test_stream_multi_asyncc                 C   s   |  | jddi d S r  )r  r  rQ   r*   r*   r+   test_custom_stream_multi  s    z3TensorPipeAgentCudaRpcTest.test_custom_stream_multic                 C   s"   t j| tj||fd}t||S r   )r   r   rM  r  )r   r   r   r   r"  r*   r*   r+   _nested_slow_add_on_user_stream  s    z:TensorPipeAgentCudaRpcTest._nested_slow_add_on_user_streamc                 C   s   t ddd}t dddd }t dddd }t| jd | j }tj|tj	||||fd}| 
|d|  d S )Nrj   r   r   r   r;  )r'   r)   r   r!   rN   rG   r   r   rM  r  rr  )rH   r   r   r   r   
nested_dstr"  r*   r*   r+   _test_stream_nested_sync  s    
z3TensorPipeAgentCudaRpcTest._test_stream_nested_syncc                 C   s   |  | jddd d S NrG  r  )r  rG  )r  r  rQ   r*   r*   r+   test_custom_stream_nested  s    z4TensorPipeAgentCudaRpcTest.test_custom_stream_nestedc              
   C   s  | j dkrg }d}g g g   }}}t|D ]}tddd|d  }tddd| }	tddd|d  }
|| ||	 ||
 t| j d | j }|tj	|t
j|||	|
fd q,t|D ],}| ||  || ||  ||   qd S )Nr   r   rj   r&   r   )rN   rP   r'   r)   r   r)  r!   rG   r   r   rM  r  rr  r   )rH   r   r%  r  xsZysZzsr   r   r   r   r  r*   r*   r+   _test_stream_nested_multi_async  s*    



z:TensorPipeAgentCudaRpcTest._test_stream_nested_multi_asyncc                 C   s   |  | jddd d S r  )r  r  rQ   r*   r*   r+   test_custom_stream_nested_multi  s    z:TensorPipeAgentCudaRpcTest.test_custom_stream_nested_multic                 C   s(   | j r|j r|  |  S tdd S )NrY  )r[  r  r1   r   r  r*   r*   r+   _gpu_add_wrong_gpus  s    z.TensorPipeAgentCudaRpcTest._gpu_add_wrong_gpusc              	   C   s   t | jd | j }| j}||ddi tjt | j| j| j| j|d t	d
d}td
d}| td tj|tj||fd}W 5 Q R X t  d S )Nr&   r   rz  rj   zMExpected all tensors to be on the same device, but found at least two devicesr   )r!   rN   rG   rZ   rH  r   r  r  r'   r  r   r)   r  r
  r   rM  r  r  r  r*   r*   r+   test_device_mismatch  s,    z/TensorPipeAgentCudaRpcTest.test_device_mismatchc           
      C   s   t | jd | j }| j}||||i tjt | j| j| j| j|d | jdkrtj|t	|fd}t
dD ]F}tdddd|}| | }| |}	| ||	 qnt  d S )Nr&   rz  r   r   rx     )r!   rN   rG   rZ   rH  r   r  r  r   rc  rP   r'   randnr   rV  r   r   rr  r  )
rH   local_deviceremote_devicer   rI  r   r  r   actualr  r*   r*   r+   _test_rref_synchronization8  s$    
z5TensorPipeAgentCudaRpcTest._test_rref_synchronizationc                 C   s   |  dd d S Nr  r  rQ   r*   r*   r+   "test_rref_to_here_synchronization1T  s    z=TensorPipeAgentCudaRpcTest.test_rref_to_here_synchronization1c                 C   s   |  dd d S NrG  r  r  rQ   r*   r*   r+   "test_rref_to_here_synchronization2X  s    z=TensorPipeAgentCudaRpcTest.test_rref_to_here_synchronization2c                 C   s   |  dd d S NrG  r  rQ   r*   r*   r+   "test_rref_to_here_synchronization3\  s    z=TensorPipeAgentCudaRpcTest.test_rref_to_here_synchronization3c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   "test_rref_to_here_synchronization4`  s    z=TensorPipeAgentCudaRpcTest.test_rref_to_here_synchronization4c                 C   s  t | jd | j }| j}||||i t | jd | j | j }||||i |d k	rl||| j  tjt | j| j| j| j|d | jdkrtj	|t
|fd}tdD ]N}ttdddd|}	|	 |	d }
| |	d}| |
| qt  d S )Nr&   rz  r   r   rx  r  T)r!   rN   rG   rZ   rH  set_devicesr   r  r  r   rc  rP   r   r'   r  r   rV  r   r   rr  r  )rH   r  r  ZdevicesOptionsr   rI  	input_srcr   r  Zrref_xr  r  r*   r*   r+   !_test_rref_as_arg_synchronizationd  s,    z<TensorPipeAgentCudaRpcTest._test_rref_as_arg_synchronizationc                 C   s   |  dd d S r  r  rQ   r*   r*   r+   !test_rref_as_arg_synchronization1  s    z<TensorPipeAgentCudaRpcTest.test_rref_as_arg_synchronization1c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   !test_rref_as_arg_synchronization2  s    z<TensorPipeAgentCudaRpcTest.test_rref_as_arg_synchronization2c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   !test_rref_as_arg_synchronization3  s    z<TensorPipeAgentCudaRpcTest.test_rref_as_arg_synchronization3c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   !test_rref_as_arg_synchronization4  s    z<TensorPipeAgentCudaRpcTest.test_rref_as_arg_synchronization4c                 C   s    |  dddd tdD  d S )Nr  c                 S   s   g | ]
}d gqS )r  r*   r  r*   r*   r+   r     s     zPTensorPipeAgentCudaRpcTest.test_rref_as_arg_synchronization5.<locals>.<listcomp>r   )r  rP   rQ   r*   r*   r+   !test_rref_as_arg_synchronization5  s
    z<TensorPipeAgentCudaRpcTest.test_rref_as_arg_synchronization5c                 C   s   |   S rE   r   r   r*   r*   r+   _rref_relay  s    z&TensorPipeAgentCudaRpcTest._rref_relayc                 C   s6  | j }td}td}td}| jdkrJ||||i ||||i n6| jdkrf||||i n| jdkr||||i tjt| j| j| j| j|d | jdkr*tj|t	|fd}t
dD ]b}ttdddd|}	| |	d	}
tj|tj|
fd }| |	d	}| || qt  d S )
Nr   r&   rj   rz  r   r   rx  r  T)rZ   r!   rN   rH  r   r  r  rG   r   rc  rP   r   r'   r  r   rV  rM  r  r   r   rr  r  )rH   r  r  rI  r  Z	model_dstZ	out_relayr   r  Z
rref_inputZrref_outr   r  r*   r*   r+   "_test_rref_forward_synchronization  s>    



z=TensorPipeAgentCudaRpcTest._test_rref_forward_synchronizationc                 C   s   |  dd d S r  r  rQ   r*   r*   r+   "test_rref_forward_synchronization1  s    z=TensorPipeAgentCudaRpcTest.test_rref_forward_synchronization1c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   "test_rref_forward_synchronization2  s    z=TensorPipeAgentCudaRpcTest.test_rref_forward_synchronization2c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   "test_rref_forward_synchronization3  s    z=TensorPipeAgentCudaRpcTest.test_rref_forward_synchronization3c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   "test_rref_forward_synchronization4  s    z=TensorPipeAgentCudaRpcTest.test_rref_forward_synchronization4c           
      C   s   | j dkr| j}|d||i tjddd|d tdtjjd 	|}t
dD ]V}tdd	|}| |}t|    }|  }	| ||	 qVt  d S )Nr   Zw0r&   )rN   rG   rZ   )   i N  r  r  )rN   rZ   rH  r   r  r   r'   rQ  rg  r   rP   r  r   rV  r   r@  r   r  rr  r  )
rH   r  r  rI  r  r  dataoutputZv0Zv1r*   r*   r+   (_test_owner_rref_forward_synchronization  s.    
  zCTensorPipeAgentCudaRpcTest._test_owner_rref_forward_synchronizationc                 C   s   |  dd d S r  r  rQ   r*   r*   r+   (test_owner_rref_forward_synchronization1  s    zCTensorPipeAgentCudaRpcTest.test_owner_rref_forward_synchronization1c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   (test_owner_rref_forward_synchronization2  s    zCTensorPipeAgentCudaRpcTest.test_owner_rref_forward_synchronization2c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   (test_owner_rref_forward_synchronization3  s    zCTensorPipeAgentCudaRpcTest.test_owner_rref_forward_synchronization3c                 C   s   |  dd d S r  r  rQ   r*   r*   r+   (test_owner_rref_forward_synchronization4  s    zCTensorPipeAgentCudaRpcTest.test_owner_rref_forward_synchronization4c                 C   s4   t ddd|  }t jdt  |dd S )Nr+  rx  r   r   r   )r'   r)   r1   r2  ri  split)r   r   r*   r*   r+   _return_tensor_view  s    z.TensorPipeAgentCudaRpcTest._return_tensor_viewc                 C   s   t | jd | j }| j}||ddi tjt | j| j| j| j|d g }tdD ]}|	tj
|tj|fd qVtdD ]$}| tdd| ||   q~t  d S )Nr&   r   rz  r   r   r   rx  )r!   rN   rG   rZ   rH  r   r  r  rP   r)  r   rM  r  rr  r'   r)   r   r  )rH   r   rI  r%  r   r*   r*   r+    test_tensor_view_as_return_value  s(    
"z;TensorPipeAgentCudaRpcTest.test_tensor_view_as_return_valuec              	   C   sz   |  tdd t| jd | j }| j}||ddi |dg tj	t| j| j
| j| j|d t  W 5 Q R X d S )NzHNode worker0 has unexpected source devices in its device map for worker1r&   r   rz  )r  r   r!   rN   rG   rZ   rH  r  r   r  r  r  rH   r   rI  r*   r*   r+   test_devices_option_mismatch7  s     z7TensorPipeAgentCudaRpcTest.test_devices_option_mismatchc              	   C   s|   |  tdf t| jd | j }tj| jj| jj	|ddiidgd}tj
t| j| j| j| j|d t  W 5 Q R X d S )NzHNode worker0 has unexpected target devices in its device map for worker1r&   r   )rL  r  r  r-  rz  )r  r   r!   rN   rG   r   r  rZ   rL  r  r  r  r  r  r*   r*   r+   $test_devices_option_mismatch_reverseL  s&    
z?TensorPipeAgentCudaRpcTest.test_devices_option_mismatch_reversec                 C   s   t dgd}d S )Nr   r,  r   r  r*   r*   r+   test_cuda_future_device_as_inte  s    z9TensorPipeAgentCudaRpcTest.test_cuda_future_device_as_intc                 C   s   t dgd}d S )Nr  r,  r   r  r*   r*   r+   test_cuda_future_device_as_stri  s    z9TensorPipeAgentCudaRpcTest.test_cuda_future_device_as_strc                 C   s   t tddgd}d S )Nr1   r   r,  )r   r'   r2   r  r*   r*   r+   !test_cuda_future_device_as_devicem  s    z<TensorPipeAgentCudaRpcTest.test_cuda_future_device_as_devicec              	   C   s(   |  td tdgd}W 5 Q R X d S )Nz)Expected devices to have indices, got cpur  r,  )r  r   r   r  r*   r*   r+    test_cuda_future_device_not_cudaq  s
     z;TensorPipeAgentCudaRpcTest.test_cuda_future_device_not_cudac                 C   s   | j dd dd dd d S )Nc                 S   s   | S rE   r*   rh   r*   r*   r+   r   {  r   zUTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_cuda_tensor.<locals>.<lambda>c                 S   s   | S rE   r*   r   r*   r*   r+   r   {  r   Fr  r  r  r  rQ   r*   r*   r+   (test_cuda_future_can_extract_cuda_tensorx  s
      zCTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_cuda_tensorc                 C   s   | j dd dd dd d S )Nc                 S   s   | gS rE   r*   rh   r*   r*   r+   r     r   z_TensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_list_with_cuda_tensor.<locals>.<lambda>c                 S   s   | d S rB   r*   r  r*   r*   r+   r     r   Fr  r  rQ   r*   r*   r+   2test_cuda_future_can_extract_list_with_cuda_tensor~  s
      zMTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_list_with_cuda_tensorc                 C   s   | j dd dd dd d S )Nc                 S   s   t | S rE   r8  rh   r*   r*   r+   r     r   zgTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_custom_class_with_cuda_tensor.<locals>.<lambda>c                 S   s   | j S rE   r   r  r*   r*   r+   r     r   Fr  r  rQ   r*   r*   r+   :test_cuda_future_can_extract_custom_class_with_cuda_tensor  s
      zUTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_custom_class_with_cuda_tensorc              
      s   t jddd}t jddd tddgd} fdd}||}t jdR t j }t j|0 t jt	dt
   |d	 || W 5 Q R X W 5 Q R X t jdF t j }t j|$ | t | d	   W 5 Q R X W 5 Q R X d S )
Nr  r  r  rG  r,  c                    s   |   } j|dd  S )NT)Znon_blocking)r_   Zcopy_)r   t0Ztensor1r*   r+   r6    s    zPTensorPipeAgentCudaRpcTest.test_cuda_future_callback_changes_devices.<locals>.cbr+  r&   )r'   r  r   r   r1   r2   r/  r1  r2  r3  r   r4  r]   r  r  r   r  r  rH   Ztensor0Zparent_futurer6  Zchild_futurer1  r  r*   r  r+   )test_cuda_future_callback_changes_devices  s    



zDTensorPipeAgentCudaRpcTest.test_cuda_future_callback_changes_devicesc              
      s  t jddd}t jddd tdgd} fdd}||}t jdR t j }t j|0 t jt	dt
   |d	 || W 5 Q R X W 5 Q R X | td
 |  W 5 Q R X t jdF t j }t j|$ | t | d	   W 5 Q R X W 5 Q R X d S )Nr  r  r  rG  r,  c              
      sH   t jd2 t jtdt    d  W  5 Q R  S Q R X d S )NrG  r+  r&   )r'   r1   r2   r2  r3  r   r4  r   r  r*   r+   r6    s    
zKTensorPipeAgentCudaRpcTest.test_cuda_future_value_on_bad_device.<locals>.cbr+  r&   zoThe result contained tensors residing on device\(s\) cuda:0 which are not among the expected device\(s\) cuda:1)r'   r  r   r   r1   r2   r/  r1  r2  r3  r   r4  r]   r  r   r   r  r  r  r  r  r*   r  r+   $test_cuda_future_value_on_bad_device  s(    



z?TensorPipeAgentCudaRpcTest.test_cuda_future_value_on_bad_devicec              	   C   s   t | jd | j }| j}||ddi tjt | j| j| j| j|d tj	ddd}tj
|t|fd}tjd}tj|$ | t| d   W 5 Q R X t  d S )Nr&   r  rz  r  r  r   )r!   rN   rG   rZ   rH  r   r  r  r'   r  r   r5  r1   r/  r1  r  r  r   r  r  r  )rH   r   rI  r:   r   r  r*   r*   r+   %test_async_execution_with_cuda_future  s     (z@TensorPipeAgentCudaRpcTest.test_async_execution_with_cuda_futurec           	   	   C   s   t | jd | j }t | jd | j }| j}||ddi tjt | j| j| j| j|d tj	ddd}tj	ddd}tj	ddd}tj
|t||||fd}tjd}tj|$ | t| d   W 5 Q R X t  d S )	Nr&   rj   r  rz  r  r  r   r   )r!   rN   rG   rZ   rH  r   r  r  r'   r)   r   r7  r1   r/  r1  r  r  r   r  r  r  )	rH   r   r  rI  rz   r}   r   r   r  r*   r*   r+   ,test_async_execution_nested_with_cuda_future  s&    (zGTensorPipeAgentCudaRpcTest.test_async_execution_nested_with_cuda_futurec                 C   s:   t jddd}tdgd}|| |d |  d S )Nr  r  r  r,  r&   )r'   r  r   r]   r4  r   )rH   r   r  r*   r*   r+   &test_cuda_future_modify_tensor_inplace  s
    

zATensorPipeAgentCudaRpcTest.test_cuda_future_modify_tensor_inplacec                 C   sD   t jdddg}tdgd}|| t jddd|d< |  d S )Nr  r  r  r,  r   )r'   r  r   r]   r)   r   )rH   r  r  r*   r*   r+   test_cuda_future_replace_tensor  s
    

z:TensorPipeAgentCudaRpcTest.test_cuda_future_replace_tensorc                 C   s   t | jd | j }| j}||ddi tjt | j| j| j| j|d tj|t	t
jdddfd}| d |  }| |d t  d S )Nr&   r  rz  *   r  r   )r!   rN   rG   rZ   rH  r   r  r  r   r8  r'   r  r   r>  r@  rr  r  )rH   r   rI  r   r"  r*   r*   r+   &test_rref_with_unpickleable_attributes  s    zATensorPipeAgentCudaRpcTest.test_rref_with_unpickleable_attributesc                 C   s   | j dd dd dd d S )Nc                 S   s   | S rE   r*   rh   r*   r*   r+   r   *  r   z\TensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_cuda_sparse_tensor.<locals>.<lambda>c                 S   s   | S rE   r*   r  r*   r*   r+   r   *  r   Tr  r  rQ   r*   r*   r+   /test_cuda_future_can_extract_cuda_sparse_tensor'  s
      zJTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_cuda_sparse_tensorc                 C   s   | j dd dd dd d S )Nc                 S   s   | gS rE   r*   rh   r*   r*   r+   r   0  r   zfTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_list_with_cuda_sparse_tensor.<locals>.<lambda>c                 S   s   | d S rB   r*   r  r*   r*   r+   r   0  r   Tr  r  rQ   r*   r*   r+   9test_cuda_future_can_extract_list_with_cuda_sparse_tensor-  s
      zTTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_list_with_cuda_sparse_tensorc                 C   s   | j dd dd dd d S )Nc                 S   s   t | S rE   r  rh   r*   r*   r+   r   6  r   znTensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_custom_class_with_cuda_sparse_tensor.<locals>.<lambda>c                 S   s   | j S rE   r   r  r*   r*   r+   r   6  r   Tr  r  rQ   r*   r*   r+   Atest_cuda_future_can_extract_custom_class_with_cuda_sparse_tensor3  s
      z\TensorPipeAgentCudaRpcTest.test_cuda_future_can_extract_custom_class_with_cuda_sparse_tensor)NN)N)vrS   rT   rU   rN  r   rP  rS  rT  rU  rV  rW  r   r\  r^  rb  rg  rj  rk  rl  rm  rn  rq  rr  rs  rt  rv  rw  ry  rz  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  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  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  r  r  r*   r*   r*   r+   rM    s  
	











	
	
	
	
	












	
	
	
	
	
	
	
	










	


	







 
'





4




















 






rM  )r-   F)F)r&   )r   )r  )r  )concurrent.futuresr   r/   rs  r  r  r;  rw   collectionsr   	functoolsr   r   r   Zunittestr   r'   Ztorch.nnrQ  Ztorch.distributedr  r  Ztorch.distributed.rpcr   rk  r3   r  r   r   r	   r
   rU  r   r   r   r  r   r   r   r   Ztorch.futuresr   Z*torch.testing._internal.common_distributedr   r   r   Z$torch.testing._internal.common_utilsr   r   r   r   Z"torch.testing._internal.dist_utilsr   r   r   r   r   r    r!   Z>torch.testing._internal.distributed.rpc.rpc_agent_test_fixturer"   r#   Ztorch.autograd.profiler_legacyr$   rc  r,   r;   ru  r  r   r\   rc   ri  r@   rA   rC   rD   rX   r[   r`   rb   rd   re   rg   ru   ry   r   r   r   r   r   r   r   r   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW  scriptr   r   r   r   r   ZTensorr   r   r   r   r   r   r   r   r   r   r  r  r  rG  rH  r  r  r  r  r  r  r  r  r   r*  r5  r7  r8  rB  rI  rK  rJ  r
  rM  rY  rc  rj  r  r  r  rM  r*   r*   r*   r+   <module>   sX  $	



	

















&                              1    )