Unable to use distributed autograd

rahul003: <p>Hi,</p>
<p>I’m trying to use distributed autograd. But I’m running into this error.<br>
I have initialized the process group with dist.init_process_group()</p>
<pre><code class="lang-auto">with dist_autograd.context() as context_id:
  File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/distributed/autograd/__init__.py", line 33, in __enter__
    self.autograd_context = _new_context()
RuntimeError: Need to initialize distributed autograd using torch.distributed.autograd.init()
</code></pre>
<p>But I see no init method in torch.distributed.autograd</p>
<pre><code class="lang-auto">&gt;&gt;&gt; from torch.distributed import autograd
autog&gt;&gt;&gt; autograd
&lt;module 'torch.distributed.autograd' from '/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/distributed/autograd/__init__.py'&gt;
&gt;&gt;&gt; autograd.
autograd.DistAutogradContext(  autograd.division              autograd.sys
autograd.absolute_import       autograd.get_gradients(        autograd.torch
autograd.backward(             autograd.is_available(         autograd.unicode_literals
autograd.context(              autograd.print_function
&gt;&gt;&gt; autograd.
autograd.DistAutogradContext(  autograd.division              autograd.sys
autograd.absolute_import       autograd.get_gradients(        autograd.torch
autograd.backward(             autograd.is_available(         autograd.unicode_literals
</code></pre>

mrshenli: <p>Hey <a class="mention" href="/u/rahul003">@rahul003</a>,</p>
<p>Distributed autograd is using RPC, so you need to call <code>init_rpc</code> instead of <code>init_process_group</code>. See the toy example below:</p>
<pre><code class="lang-python">import torch.multiprocessing as mp
import torch.distributed.rpc as rpc
import torch.distributed.autograd as dist_autograd
import os
import torch
from torch import optim
from torch.distributed.optim import DistributedOptimizer


def train():
    with dist_autograd.context() as context_id:
        t1 = torch.rand((3, 3), requires_grad=True)
        t2 = torch.rand((3, 3), requires_grad=True)
        loss = t1 + t2
        dist_autograd.backward(context_id, [loss.sum()])
        grads = dist_autograd.get_gradients(context_id)
        print(grads[t1])
        print(grads[t2])

def run_worker(rank, world_size):
    os.environ['MASTER_ADDR'] = 'localhost'
    os.environ['MASTER_PORT'] = '29500'
    if rank == 1:
        rpc.init_rpc("worker0", rank=rank, world_size=world_size)
        train()
    else:
        rpc.init_rpc("worker1", rank=rank, world_size=world_size)
        pass

    # block until all rpcs finish
    rpc.shutdown()


if __name__=="__main__":
    world_size = 2
    mp.spawn(run_worker, args=(world_size, ), nprocs=world_size, join=True)
</code></pre>
<p>For more complete tutorials, please see the following links:</p>
<ol>
<li>RL example and RNN example: <a href="https://pytorch.org/tutorials/intermediate/rpc_tutorial.html" rel="nofollow noopener">https://pytorch.org/tutorials/intermediate/rpc_tutorial.html</a>
</li>
<li>Parameter server example: <a href="https://pytorch.org/tutorials/intermediate/rpc_param_server_tutorial.html" rel="nofollow noopener">https://pytorch.org/tutorials/intermediate/rpc_param_server_tutorial.html</a>
</li>
<li>Distributed pipeline example: <a href="https://github.com/pytorch/examples/tree/master/distributed/rpc/pipeline" rel="nofollow noopener">https://github.com/pytorch/examples/tree/master/distributed/rpc/pipeline</a>
</li>
</ol>