U
    Jc=%                     @   s  U d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlZd dlmZ eeZd ae Zdd Ze rej sede rd dlmZ d d	lmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d
dl=m>Z>m?Z?m@Z@ d
dl>T d dlAZAd dlBm  mCZD d
dl?mEZE d
dlFmGZG d
dlHmIZI eeeeJeJf ddf eKd< dddZLdd ZMeEjNdddddfddZOe>jPdd ZQdS )    )	timedeltaN)	GeneratorTuple)urlparsec                   C   s   t tjdS )N	_rpc_init)hasattrtorch_C r
   r
   B/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/rpc/__init__.pyis_available   s    r   z*Failed to initialize torch.distributed.rpc)Store)!_disable_jit_rref_pickle_enable_jit_rref_pickle'_disable_server_process_global_profiler&_enable_server_process_global_profiler_set_and_start_rpc_agent_reset_current_rpc_agent)_delete_all_user_and_unforked_owner_rrefs_destroy_rref_context_set_profiler_node_id_is_current_rpc_agent_set_rref_context_get_debug_info_cleanup_python_rpc_handler_invoke_rpc_builtin_invoke_rpc_python_udf_invoke_rpc_torchscript_invoke_remote_builtin_invoke_remote_python_udf_invoke_remote_torchscript_set_rpc_timeout_get_current_rpc_agentget_rpc_timeoutenable_gil_profilingRpcBackendOptions _TensorPipeRpcBackendOptionsBaseRpcAgentPyRRefTensorPipeAgentRemoteProfilerManager
WorkerInfo_DEFAULT_INIT_METHOD_DEFAULT_NUM_WORKER_THREADS_UNSET_RPC_TIMEOUT_DEFAULT_RPC_TIMEOUT_SEC   )apibackend_registry	functions)*)BackendType)TensorPipeRpcBackendOptions)_server_process_global_profilerendezvous_iteratorc              	   C   sj  t jd |dk	r(t|tjs(td|dk	rBt|tsBtd|dkr|dk	rtD ] }t|tt	|rV|} qqVtd| |tj
krtd| dtj
 d| d	 |dkrtj
}|dkrt	|}|st||}n tj|j||d
att\}}}|t|jd t$ ttdt|}td7 aW 5 Q R X t| t| t||| ||| dS )ak  
        Initializes RPC primitives such as the local RPC agent
        and distributed autograd, which immediately makes the current
        process ready to send and receive RPCs.

        Args:
            name (str): a globally unique name of this node. (e.g.,
                ``Trainer3``, ``ParameterServer2``, ``Master``, ``Worker1``)
                Name can only contain number, alphabet, underscore, colon,
                and/or dash, and must be shorter than 128 characters.
            backend (BackendType, optional): The type of RPC backend
                implementation. Supported values is
                ``BackendType.TENSORPIPE`` (the default).
                See :ref:`rpc-backends` for more information.
            rank (int): a globally unique id/rank of this node.
            world_size (int): The number of workers in the group.
            rpc_backend_options (RpcBackendOptions, optional): The options
                passed to the RpcAgent constructor. It must be an agent-specific
                subclass of :class:`~torch.distributed.rpc.RpcBackendOptions`
                and contains agent-specific initialization configurations. By
                default, for all agents, it sets the default timeout to 60
                seconds and performs the rendezvous with an underlying process
                group initialized using ``init_method = "env://"``,
                meaning that environment variables ``MASTER_ADDR`` and
                ``MASTER_PORT`` need to be set properly. See
                :ref:`rpc-backends` for more information and find which options
                are available.
        ztorch.distributed.init_rpcNz0Argument backend must be a member of BackendTypezEArgument rpc_backend_options must be an instance of RpcBackendOptionsz$Could not infer backend for options zORPC was initialized with no explicit backend but with options corresponding to z9, hence that backend will be used instead of the default z(. To silence this warning pass `backend=z` explicitly.)rank
world_size)secondszrpc_prefix_{}r/   )r   r	   Z_log_api_usage_once
isinstancer1   r4   	TypeErrorr$   typeZconstruct_rpc_backend_options
TENSORPIPEloggerwarningdistZ_create_store_from_optionsZ
rendezvousZinit_methodr7   nextZset_timeoutr   Zrpc_timeout_init_counter_lockZPrefixStorestrformat_init_counterdist_autograd_initr   _init_rpc_backend)namebackendr9   r:   rpc_backend_optionsZcandidate_backendstore_r
   r
   r   init_rpcP   sj    #
 
 
  
rP   c           	      C   sb   | t j|tj|t|tj|tjtd f|ti}|	 D ](\}}t
||s4td||t|q4d S )Nz.Argument {} must be of type {} but got type {})r1   r4   rB   r   rE   numbersIntegralr>   r$   itemsr<   RuntimeErrorrF   )	rL   rN   rK   r9   r:   rM   Ztype_mappingargZarg_typer
   r
   r   _validate_rpc_args   s,          	
  rV   c                 C   sD   t | ||||| t r tdtj| |||||d}t| d S )NzRPC is already initialized)rN   rK   r9   r:   rM   )rV   r   rT   r1   Zinit_backendr0   Z_init_rpc_states)rL   rN   rK   r9   r:   rM   Z	rpc_agentr
   r
   r   rJ      s    		rJ   c                  C   s*   t  } | t   | t  | S )N)r   updater0   r!   Zget_debug_inforH   _get_debug_info)infor
   r
   r   rX      s    rX   )Nr8   NN)Rdatetimer   loggingos	threadingwarningstypingr   r   urllib.parser   r   Ztorch.distributedZdistributedrB   	getLogger__name__r@   rG   LockrD   r   r	   r   rT   Ztorch._C._distributed_c10dr   Ztorch._C._distributed_rpcr   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.    r0   r1   r2   rQ   Ztorch.distributed.autogradZautogradrH   r4   optionsr5   Zserver_process_global_profilerr6   int__annotations__rP   rV   r?   rJ   Z_require_initializedrX   r
   r
   r
   r   <module>   sR    
$    
v
