U
    JcO	                     @   s6   d dl Z d dlmZ d dlmZ d	ddZdd ZdS )
    N)tree_map)Optional c                    s   ddl m ddlm  ddlm t|dkrV|dksDt|dkrVtd| j dd fd	d
}t	|| t	|| st
d| j d| d| dd fdd}t	|| t	|| dS )z6
    Common validation across all ops go in here.
    r   )_PartialTensor)ReplicatedTensor)ShardedTensorNz No input for 'z'!Fc                    s&   t |  st | st | r"dd S )NT)
isinstancee)r   r   r   has_distributed_tensorr   L/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/_shard/common_op_utils.pyis_distributed_tensor   s    z0_basic_validation.<locals>.is_distributed_tensorztorch function 'z', with args: z and kwargs: z+ are called without any distributed tensor!c                    sB   t |  st | st | r>d k	r8| jk	r8td| jd S )NzSAll distributed tensors should use the same ProcessGroup if used together in an op.)r   Z_process_groupRuntimeErrorr	   )r   r   r   cur_pgr   r   validate_pg$   s    z&_basic_validation.<locals>.validate_pg)Z'torch.distributed._shard.partial_tensorr   Z*torch.distributed._shard.replicated_tensorr   Z'torch.distributed._shard.sharded_tensorr   len
ValueError__name__r   	TypeError)opargskwargsr   r   r   )r   r   r   r   r   r   _basic_validation   s"     



r   c                    s   | d fdd	}d S )Nr   c              
      s8   |dkri }t j   ||W  5 Q R  S Q R X dS )a[  
        Handles ``__torch_function__`` dispatch for the default tensor ops that
        behave the same as ``torch.Tensor`` such as ``torch.Tensor.shape`` or
        ``torch.Tensor.dtype``. We simply lower to the real op call with
        DisableTorchFunction context like ``torch.Tensor.__torch_function__``
        to avoid recursions.
        N)torchZ_CZDisableTorchFunction)typesr   r   Zpgr   r   r   tensor_default_op2   s    	z/_register_default_op.<locals>.tensor_default_op)r   NNr   )r   	decoratorr   r   r   r   _register_default_op1   s    r   )r   N)r   Ztorch.utils._pytreer   typingr   r   r   r   r   r   r   <module>   s   
,