API alternative for torch.distributed.launch?

HuangLED: <p>For some reason it is hard (and ugly) to start my own module using torch.distributed.launch. It is inside an application therefore I would need to concatenate an execution cmd line string to do it.</p>
<p>Is there a way that I can call torch.distributed.launch using an API way? (i.e. I then expose my module as a simple function and feed my function as a callback to torch’s distributed module)</p>
<p>Thanks!</p>

H-Huang: <p>As part of torch 1.9.0 we are introducing <code>torch.distributed.run</code> to replace <code>torch.distributed.launch</code> definition is here (<a href="https://github.com/pytorch/pytorch/blob/master/torch/distributed/run.py" class="inline-onebox" rel="noopener nofollow ugc">pytorch/run.py at master · pytorch/pytorch · GitHub</a>). This eventually calls into a function called elastic_launch (<a href="https://github.com/pytorch/pytorch/blob/master/torch/distributed/launcher/api.py#L80" class="inline-onebox" rel="noopener nofollow ugc">pytorch/api.py at master · pytorch/pytorch · GitHub</a>) which seems to be what you are looking for. For example, you can import it and use it like <code>elastic_launch(LaunchConfig, your_module_function)(your_module_args)</code>. Does this satisfy your use case?</p>

HuangLED: <p>Thanks! Let me try that out!</p>

HuangLED: <p>Thanks Howard.</p>
<p>Compiled and installed from source, and trying out the elastic_launch API.</p>
<p>One question is, is there a definitive list of every thing needed in environments for this elastic_launch?  when using the script, there are bunch of things such as RANK, master_addr to make everything ready. However the elastic_launch’s config only has a subset of these variables.</p>
<p>Haven’t fully traced the code yet, just would like to double check first instead of trail and error finding them.</p>
<p>right now, I am able to get things kicked off but apparently some configurations is missing.</p>
<pre><code class="lang-auto">(pid=601762)     "message": "EtcdException: Could not get the list of servers, maybe you provided the wrong host(s) to connect to?",
(pid=601762)     "extraInfo": {
(pid=601762)       "py_callstack": "Traceback (most recent call last):\n  File \"/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/connection.py\", line 170, in _new_conn\n    (self._dns_host, self.port), self.timeout, **extra_kw\n  File \"/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/util/connection.py\", line 96, in create_connection\n
</code></pre>
<p>and</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "train_ray_local.py", line 169, in &lt;module&gt;
    ray.get([client.train.remote(), client2.train.remote()])
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/ray/_private/client_mode_hook.py", line 47, in wrapper
    return func(*args, **kwargs)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/ray/worker.py", line 1481, in get
    raise value.as_instanceof_cause()
ray.exceptions.RayTaskError(EtcdException): ray::Network.train() (pid=601762, ip=10.231.13.71)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

ray::Network.train() (pid=601762, ip=10.231.13.71)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/connection.py", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/http/client.py", line 1277, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/http/client.py", line 1323, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/http/client.py", line 1272, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/http/client.py", line 1032, in _send_output
    self.send(msg)
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/http/client.py", line 972, in send
    self.connect()
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/connection.py", line 200, in connect
    conn = self._new_conn()
  File "/home/centos/anaconda3/envs/dev/lib/python3.7/site-packages/urllib3/connection.py", line 182, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: &lt;urllib3.connection.HTTPConnection object at 0x7f6a609edb50&gt;: Failed to establish a new connection: [Errno 111] Connection refused
</code></pre>

H-Huang: <p>Hi, I should have provided more resources in my previous response, my apologies. Here is the reading about <code>torch.distributed.run</code> and the arguments it supports (<a href="https://pytorch.org/docs/master/elastic/run.html" class="inline-onebox" rel="noopener nofollow ugc">Elastic Launch — PyTorch master documentation</a>). It is a superset of the arguments of <code>torch.distributed.launch</code> but also includes fault tolerance provided by TorchElastic <a href="https://pytorch.org/docs/master/distributed.elastic.html" class="inline-onebox" rel="noopener nofollow ugc">Torch Distributed Elastic — PyTorch master documentation</a>.</p>
<p>Regarding LaunchConfig and elastic_launch, they are not yet public APIs and are subject to change. By default it uses an etcd backend which is a third-party distributed key-value store which is why I believe you are hitting your errors. However, if you don’t want third party dependencies, you can use an internally built backend for rendezvous. Here is an example for you:</p>
<p><code>test.py</code></p>
<pre><code class="lang-python">from torch.distributed.launcher.api import LaunchConfig, elastic_launch
import os

def my_process(args):
    env_variables = [
        "RANK",
        "WORLD_SIZE",
        "MASTER_ADDR",
        "MASTER_PORT",
        "TORCHELASTIC_MAX_RESTARTS",
        # etc...
    ]
    for env_var in env_variables:
        print(f"{env_var}: {os.environ.get(env_var)}")
    print(f"here are my own args: {args}")

if __name__ == "__main__":
    config = LaunchConfig(min_nodes=1, max_nodes=1, nproc_per_node=2, rdzv_endpoint="localhost:0", rdzv_backend="c10d")
    outputs = elastic_launch(config, my_process)("my args")
</code></pre>
<p>Then just run <code>python test.py</code></p>
<p>The output looks something like (along with some torchelastic logging and warnings):</p>
<pre><code class="lang-auto">RANK: 1
WORLD_SIZE: 2
MASTER_ADDR: &lt;my machine&gt;
MASTER_PORT: 51217
TORCHELASTIC_MAX_RESTARTS: 3
here are my own args: my args
RANK: 0
WORLD_SIZE: 2
MASTER_ADDR: &lt;my machine&gt;
MASTER_PORT: 51217
TORCHELASTIC_MAX_RESTARTS: 3
here are my own args: my args
</code></pre>

HuangLED: <p>Awesome example! clear and simple.  Thanks for it Howard! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>One follow up question, what is the best way to double check the distributed training actually happened.  Just want to make sure  I am not cheated by some lucky succeeded run of mis-configuration  <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:">    I read this page (<a href="https://pytorch.org/docs/stable/distributed.html" class="inline-onebox" rel="noopener nofollow ugc">Distributed communication package - torch.distributed — PyTorch 1.8.1 documentation</a>) but not sure what would be the definitive way to tell.</p>

mrshenli: <p>One way might be printing out the forward output on all rank every N iterations, and see if the loss is different and resides on different GPUs. You can also check <code>nvidia-smi</code> to see if they indeed keep multiple GPUs busy.</p>

HuangLED: <p>I tried this example, it works greatly even on multiple machines. But I am trying to understand how these two parameters work, and what the mechanism is:</p>
<pre><code class="lang-auto">rdzv_endpoint="localhost:0", rdzv_backend="c10d"
</code></pre>
<p>Let us say I have two machines, now I used above two parameters to both machines. How does that work? localhost:0 seems really non-typical. Under the hood, how is etcd involved in this process?</p>
<p>Thanks a lot!</p>

HuangLED: <p>would you please point me to the exact place where all-reduce happens?  maybe I can just print out message to confirm that all-reduce happened?</p>

cbalioglu: <p>For a distributed setting you should provide the hostname of one of your machines:</p>
<pre><code class="lang-auto">rdzv_endpoint="node0.example.com" rdzv_backend="c10d"
</code></pre>
<p>The example that Howard gave was meant to be run on a single machine with two worker processes. <code>localhost:0</code> simply means use a random port on the local machine for the coordination of the nodes. Since you only have one node (that runs two workers) specifying localhost as an endpoint worked in that example. However if you want to run your training on more than one machine (a.k.a. node) the endpoint should be a hostname/FQDN that is reachable by all machines.</p>

HuangLED: <p>Thanks Can!</p>
<p>OK. looks like it works now. To run on multiple machines, i assume we dont’ need to start an etcd instance first. Just simply set rdzv_endpoint=“IP_ADDRESS” rdzv_backend=“c10d”. And it works. Looks like c10d backend takes care of everything for us.</p>
<p>Looking at the output, I am not sure if parallel part actually happened</p>
<pre><code class="lang-auto">(pid=15988, ip=10.231.21.63) {"name": "torchelastic.worker.status.SUCCEEDED", "source": "WORKER", "timestamp": 0, "metadata": {"run_id": "95509630884468593764425877023036505025", **"global_rank": 0**, "group_rank": 0, "worker_id": "16109", "role": "default_role", "hostname": "n231-021-063.novalocal", "state": "SUCCEEDED", "total_run_time": 75, "rdzv_backend": "c10d", "raw_error": null, "metadata": "{\"group_world_size\": 1, \"entry_point\": \"sage_main_routine\", \"local_rank\": [0], \"role_rank\": [0], \"role_world_size\": [1]}", "agent_restarts": 0}}
(pid=952664) {"name": "torchelastic.worker.status.SUCCEEDED", "source": "WORKER", "timestamp": 0, "metadata": {"run_id": "176607332379713185119918774283739049986", **"global_rank": 0**, "group_rank": 0, "worker_id": "952994", "role": "default_role", "hostname": "n231-013-071.novalocal", "state": "SUCCEEDED", "total_run_time": 76, "rdzv_backend": "c10d", "raw_error": null, "metadata": "{\"group_world_size\": 1, \"entry_point\": \"sage_main_routine\", \"local_rank\": [0], \"role_rank\": [0], \"role_world_size\": [1]}", "agent_restarts": 0}}
</code></pre>
<p>After it finishes, I got the output above. Both says it used group_rank as 0, my setup for the run is like this, each node has 0 or 1 as rank respectively:</p>
<pre><code class="lang-auto">    os.environ["MASTER_ADDR"] = "10.231.13.71"
    os.environ["MASTER_PORT"] = "2345"
    os.environ["WORLD_SIZE"] = "2"
    os.environ["RANK"] = str(rank)
</code></pre>
<p>Maybe I still missed something in the os environment var setting?  Are these four enough?</p>

cbalioglu: <blockquote>
<p>Thanks Can!</p>
</blockquote>
<p>You are welcome!</p>
<blockquote>
<p>OK. looks like it works now. To run on multiple machines, i assume we dont’ need to start an etcd instance first. Just simply set rdzv_endpoint=“IP_ADDRESS” rdzv_backend=“c10d”. And it works. Looks like c10d backend takes care of everything for us.</p>
</blockquote>
<p>That is right. The new <code>c10d</code> rendezvous backend does not depend on any 3rd party software. You don’t need to start (or even install) etcd.</p>
<p>From the output it looks like your machines run in silo. Each has a world size of 1 and rank 0.</p>
<p>Since you are using our new launcher API you should not set any environment variables in your training script. Our launcher sets all four variables you listed before executing it.</p>
<p>I suggest checking out our “Distributed Elastic” docs for v1.9 RC <a href="https://pytorch.org/docs/1.9.0/elastic/run.html" rel="noopener nofollow ugc">here</a> which also lists all the environment variables set for you by the launcher API.</p>
<p>Let me know if you still have trouble after reading the docs.</p>
<p>Cheers.</p>

HuangLED: <p>Finished reading the doc.  Now I am a bit confused.</p>
<p>This “Distributed Elastic” page describes how to use a <strong>script</strong> to start the training. But for my case I was seeking for an <strong>API</strong> version of this functionality.  Based on the earlier response from Howard, it is ready, just apply function elastic_launch() on my entry point function.   Are you suggesting this function elastic_launch() is actually not ready for use?</p>
<p>Based on what I tried so far, the function way works pretty well in bringing up the processes on multiple machine (thank you guys!) But somehow I need a good example to learn how to set things up properly.</p>
<p>my current configuration:</p>
<pre><code class="lang-auto">sage_main_routine():
    os.environ["MASTER_ADDR"] = "10.231.131.7"
    os.environ["MASTER_PORT"] = "2346"
    os.environ["WORLD_SIZE"] = "2"
    os.environ["RANK"] = str(rank)
    os.environ["GROUP_RANK"] = str(rank)

In the main script: 
        config = LaunchConfig(
            min_nodes=1,
            max_nodes=2,
            nproc_per_node=1,
            rdzv_endpoint="10.231.131.7",
            rdzv_backend="c10d",
        )

        outputs = elastic_launch(config, sage.sage_main_routine)(
            Path(FOLDER), args, self.rank
        )

</code></pre>
<p>UPDATE: have the 2-node being silo is my accidentally turn off Parallel module. Now I am having errors related to tcp connection timeouts. Either way I think bringing up the processes works great, it is just need some help  on  how to set the correct set of variables.</p>

cbalioglu: <p>As Howard mentioned the <code>elastic_launch</code> function is not part of our public API (yet); therefore, we do not have docs for it, but reading and understanding how the script works and what environment variables it sets, should be helpful since it is a thin wrapper on top of <code>elastic_launch</code>.</p>
<p>Your <code>sage_main_routine</code> should be actually “reversed” like this:</p>
<pre><code class="lang-python">sage_main_routine(foo):
    # These environment variables are set by `elastic_launch`.
    master_addr = os.environ["MASTER_ADDR"]
    master_port = os.environ["MASTER_PORT"]
    world_size = os.environ["WORLD_SIZE"]
    rank = os.environ["RANK"]

    store = TCPStore(master_addr, master_port, world_size, is_master=rank== 0)

    # Rest of the script...

In the main script: 
        config = LaunchConfig(
            min_nodes=1,
            max_nodes=2,
            nproc_per_node=1,
            rdzv_endpoint="10.231.131.7",
            rdzv_backend="c10d",
        )

        foo = "my dummy value"

        outputs = elastic_launch(config, sage.sage_main_routine)(
           foo, # Your custom script arguments
        )
</code></pre>

HuangLED: <p>Gotcha! Thanks for the tips!</p>
<p>After reading more into how things are implemented, I think now I know what is going on.</p>
<p>We are integrating pytorch into a system where we spawn multiple processes, and the training happens within <strong>one</strong> of the sub-processes on one single machine.  pytorch package imports get leaked into multiple processes including the parent, thus for each time “Using backend: pytorch” is shown, there is one c10d/rendezvous setup/registration happening automatically. Therefore the distributed modules got confused and in consequence connection issues.</p>
<p>What is the easy way to tell a process when we spawning it, saying “ok, you are (or not) the one to initialize distributed modules”?   I guess I can hack into pytorch for now, but I am not sure about side-effects doing so.  If there is already a mechanism, that would be awesome.</p>
<p>Now I guess I get the reason why this module was originally provided only as a script.</p>
<p>Thank you so much everyone!</p>

cbalioglu: <p>Spawning/forking without an <code>exec</code> in general is always tricky and can introduce subtle bugs (like you had in this case) We do not have a mechanism to detect double initializations and it is unlikely that we can come up with a solution that works for everyone considering the size of our user base.</p>
<p>My only suggestion would be to rethink how you import and initialize your Python modules. Ideally only a single subprocess should import the relevant packages from PyTorch while the rest performs other auxiliary tasks.</p>
<blockquote>
<p>Now I guess I get the reason why this module was originally provided only as a script.</p>
</blockquote>
<p>That is correct. The internals of how the launcher script works is still under development. So I strongly suggest using the script if possible. We do not guarantee any backwards compatibility for our API since it is not public yet.</p>
<blockquote>
<p>Thank you so much everyone!</p>
</blockquote>
<p>You are welcome! Good luck with your experiments and let us know if you need any other help!</p>

HuangLED: <p>Thanks Can!  Really appreciate all the information!</p>
<p>My last question, I want to make sure the “parallel training” is indeed happening, would you please share a few code pointers where processes do the parameters sharing with others. I’d like to print out the parameter (maybe before and after as well) to make sure I have things rolling correctly.</p>

gcramer23: <blockquote>
<p>My last question, I want to make sure the “parallel training” is indeed happening, would you please share a few code pointers where processes do the parameters sharing with others.</p>
</blockquote>
<p>For clarification, the architecture is multiple nodes and multiple processes within a node, with one process doing the training? Also, are you using a DDP model?</p>

HuangLED: <p>I am using two machines, each machine calls elastic_launch() routine once.</p>
<p>And yes, inside my model definition, I am wrapping my model into a DDP.  Two machines runs the same exact training routine.</p>

gcramer23: <blockquote>
<p>My last question, I want to make sure the “parallel training” is indeed happening, would you please share a few code pointers where processes do the parameters sharing with others.</p>
</blockquote>
<p>DDP tutorial <a href="https://pytorch.org/tutorials/intermediate/ddp_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Getting Started with Distributed Data Parallel — PyTorch Tutorials 2.1.1+cu121 documentation</a>.</p>
<p>" DDP uses collective communications in the <a href="https://pytorch.org/tutorials/intermediate/dist_tuto.html" rel="noopener nofollow ugc">torch.distributed</a> package to synchronize gradients and buffers. More specifically, DDP registers an autograd hook for each parameter given by <code>model.parameters()</code> and the hook will fire when the corresponding gradient is computed in the backward pass. Then DDP uses that signal to trigger gradient synchronization across processes. Please refer to <a href="https://pytorch.org/docs/master/notes/ddp.html" rel="noopener nofollow ugc">DDP design note</a> for more details."</p>
<p>You can check before and after .backward().</p>