Why does my pytorch rpc workers deadlock, is it because I am using main as my master?

Brando_Miranda: <p>I wanted to run the simplest example I could for computing a distributed autograd and print the gradients in my main script. For that I do the following:</p>
<ol>
<li>start the main process as the master/worker0</li>
<li>start a helper as worker1</li>
<li>send from worker1 to do my computation through an rpc</li>
<li>get gradients from remote and print them</li>
</ol>
<p>This doesn’t work for some reason. It seems my workers deadlock each other. I am unsure why. At first I was getting an error complaining that it seemed the workers were trying to use the same port (thats what I assume because it got fixed once the workers/processes used a different port).</p>
<p>Now that I fixed that I get that they deadlock each other and they don’t do my distributed autograd computation. I am usure why. I assume it has something to do with <a href="https://pytorch.org/docs/stable/rpc.html#torch.distributed.rpc.init_rpc" rel="noopener nofollow ugc">init_rpc</a> since they stop right there it seems. The docs the the following for init_rpc:</p>
<blockquote>
<p>torch.distributed.rpc.init_rpc(name, backend=None, rank=-1, world_size=None, rpc_backend_options=None) Initializes RPC primitives such as the local RPC agent and distributed autograd, which immediately makes the current process ready to send and receive RPCs.</p>
</blockquote>
<p>or it might be due to <a href="https://pytorch.org/docs/stable/rpc.html#torch.distributed.rpc.shutdown" rel="noopener nofollow ugc">shutdown</a></p>
<blockquote>
<p>torch.distributed.rpc.shutdown(graceful=True) Perform a shutdown of the RPC agent, and then destroy the RPC agent. This stops the local agent from accepting outstanding requests, and shuts down the RPC framework by terminating all RPC threads. If graceful=True, this will block until all local and remote RPC processes reach this method and wait for all outstanding work to complete. Otherwise, if graceful=False, this is a local shutdown, and it does not wait for other RPC processes to reach this method.</p>
</blockquote>
<p>I tried removing/adding the shutdowns from my code in different combinations but it still got deadlocked…which is why I ended up making a question instead since I am unsure why the issue might arise.</p>
<p>My code is super simple, I just multiple two tensors remotely and then try to get the gradients. Nothing more. It is insipired from this <a href="https://pytorch.org/docs/stable/rpc/distributed_autograd.html#simple-end-to-end-example" rel="noopener nofollow ugc">a simple end to end example</a>.</p>
<p>I ran out of ideas of what to do. Anyone know what’s going on?</p>
<pre><code class="lang-auto">import os

import torch
from torch.distributed import rpc
import torch.multiprocessing as mp
from torch.multiprocessing import Process


def start_worker(rank, world_size):
    # initialize worker proc
    os.environ['MASTER_ADDR'] = '127.0.0.1'
    os.environ['MASTER_PORT'] = f'2950{rank}'
    print(f'initializing worker{rank}')
    rpc.init_rpc(f'worker{rank}', rank=rank, world_size=world_size)
    print(f'finished initializing worker{rank}')
    # block until all rpcs finish, and shutdown the RPC instance
    # rpc.shutdown()

def test1_rpc():
    print('running test1_rpc')
    world_size = 2

    # initialize worker
    worker_rank = 1
    worker_proc = Process(target=start_worker, args=(worker_rank, world_size))
    worker_proc.start()

    # initialize main as master
    rank = 0  # master rank
    os.environ['MASTER_ADDR'] = '127.0.0.1'
    os.environ['MASTER_PORT'] = f'2950{rank}'
    # Initializes RPC primitives such as the local RPC agent and distributed autograd, which immediately makes the current process ready to send and receive RPCs.
    print(f'initializing worker{rank}')
    rpc.init_rpc(f'worker{rank}', rank=rank, world_size=world_size)
    print(f'finished initializing worker{rank}')

    # # initialize worker
    # worker_rank = 1
    # worker_proc = Process(target=start_worker, args=(worker_rank, world_size))
    # worker_proc.start()
    # print('initialized and started worker proc')

    # do distributed autograd backward pass
    def loss(a, b):
        return a.mm(b).sum()

    with torch.distributed.autograd.context() as context_id:
        print('in distributed autograd')
        a = torch.ones([2, 5], requires_grad=True)
        b = torch.ones([5, 3], requires_grad=True)
        loss = rpc.rpc_sync(to='worker1', func=loss, args=(a, b))

        torch.distributed.autograd(context_id, loss)
        print(a.grad)
        print(b.grad)

    rpc.shutdown()

def test1():
    a = torch.ones([2, 5], requires_grad=True)
    b = torch.ones([5, 3], requires_grad=True)
    loss = a.mm(b).sum()
    loss.backward()
    print(a.grad)
    print(b.grad)
    print()


if __name__ == '__main__':
    test1()
    test1_rpc()
</code></pre>
<p>ouput:</p>
<pre><code class="lang-auto">tensor([[3., 3., 3., 3., 3.],
        [3., 3., 3., 3., 3.]])
tensor([[2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.]])
running test1_rpc
initializing worker0
initializing worker1
</code></pre>
<hr>
<p>crossposted: <a href="https://stackoverflow.com/questions/66323651/why-does-my-pytorch-rpc-workers-deadlock-is-it-because-i-am-using-main-as-my-ma" rel="noopener nofollow ugc">https://stackoverflow.com/questions/66323651/why-does-my-pytorch-rpc-workers-deadlock-is-it-because-i-am-using-main-as-my-ma</a></p>