U
    Jºc]  ã                   @   s~   d dl Z d dlZdd„ Zeƒ r0ej ¡ s0edƒ‚eƒ rjd dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZ G dd„ deƒZdS )é    Nc                   C   s   t tjdƒS )NÚ_dist_autograd_init)ÚhasattrÚtorchÚ_C© r   r   úG/tmp/pip-unpacked-wheel-gikjz4vx/torch/distributed/autograd/__init__.pyÚis_available   s    r   z/Failed to initialize torch.distributed.autograd)Úget_gradientsÚbackwardÚ_initÚ_new_contextÚ_release_contextÚ_get_max_idÚ_is_valid_contextÚ_retrieve_contextÚ_current_contextÚ_get_debug_infoÚDistAutogradContextc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Úcontexta  
    Context object to wrap forward and backward passes when using
    distributed autograd. The ``context_id`` generated in the ``with``
    statement  is required to uniquely identify a distributed backward pass
    on all workers. Each worker stores metadata associated with this
    ``context_id``, which is required to correctly execute a distributed
    autograd pass.

    Example::
        >>> import torch.distributed.autograd as dist_autograd
        >>> # xdoctest: +SKIP
        >>> with dist_autograd.context() as context_id:
        >>>   t1 = torch.rand((3, 3), requires_grad=True)
        >>>   t2 = torch.rand((3, 3), requires_grad=True)
        >>>   loss = rpc.rpc_sync("worker1", torch.add, args=(t1, t2)).sum()
        >>>   dist_autograd.backward(context_id, [loss])
    c                 C   s   t ƒ | _| j ¡ S ©N)r   Úautograd_contextÚ_context_id)Úselfr   r   r   Ú	__enter__.   s    zcontext.__enter__c                 C   s   t | j ¡ ƒ d S r   )r   r   r   )r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__2   s    zcontext.__exit__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   r   )Úsysr   r   r   r   ÚRuntimeErrorZtorch._C._distributed_autogradr	   r
   r   r   r   r   r   r   r   r   r   Úobjectr   r   r   r   r   Ú<module>   s   4