{"id":15768,"date":"2024-07-05T19:18:41","date_gmt":"2024-07-05T11:18:41","guid":{"rendered":"http:\/\/139.9.1.231\/?p=15768"},"modified":"2024-07-17T17:04:48","modified_gmt":"2024-07-17T09:04:48","slug":"torchrun-torch","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2024\/07\/05\/torchrun-torch\/","title":{"rendered":"torchrun-torch\u591a\u673a\u591a\u5361\u5206\u5e03\u5f0f\u8bad\u7ec3\u547d\u4ee4"},"content":{"rendered":"\n<p><a href=\"https:\/\/pytorch.org\/docs\/stable\/elastic\/run.html\">https:\/\/pytorch.org\/docs\/stable\/elastic\/run.html<\/a><\/p>\n\n\n\n<p><code>torchrun<\/code>&nbsp;\u63d0\u4f9b\u4e86\u4f5c\u4e3a&nbsp;<code>torch.distributed.launch<\/code>&nbsp;\u7684\u529f\u80fd\u7684\u8d85\u96c6\uff0c\u5e76\u5177\u6709\u4ee5\u4e0b\u9644\u52a0\u529f\u80fd\uff1a<\/p>\n\n\n\n<ol><li>Worker failures are handled gracefully by restarting all workers.<br>\u901a\u8fc7\u91cd\u65b0\u542f\u52a8\u6240\u6709\u5de5\u4f5c\u8fdb\u7a0b\u6765\u4f18\u96c5\u5730\u5904\u7406\u5de5\u4f5c\u8fdb\u7a0b\u6545\u969c\u3002<\/li><li>Worker&nbsp;<code>RANK<\/code>&nbsp;and&nbsp;<code>WORLD_SIZE<\/code>&nbsp;are assigned automatically.<br>\u5de5\u4f5c\u5668&nbsp;<code>RANK<\/code>&nbsp;\u548c&nbsp;<code>WORLD_SIZE<\/code>&nbsp;\u81ea\u52a8\u5206\u914d\u3002<\/li><li>Number of nodes is allowed to change between minimum and maximum sizes (elasticity).<br>\u5141\u8bb8\u8282\u70b9\u6570\u91cf\u5728\u6700\u5c0f\u548c\u6700\u5927\u5927\u5c0f\u4e4b\u95f4\u53d8\u5316\uff08\u5f39\u6027\uff09\u3002<\/li><\/ol>\n\n\n\n<p><code>torchrun<\/code>\u00a0\u662fsetup.py\u4e2d\u00a0<code>entry_points<\/code>\u00a0\u914d\u7f6e\u4e2d\u58f0\u660e\u7684\u4e3b\u6a21\u5757torch.distributed.run\u7684Python\u63a7\u5236\u53f0\u811a\u672c\u3002\u5b83\u76f8\u5f53\u4e8e\u8c03\u7528\u00a0<code>python\u00a0-m\u00a0torch.distributed.run<\/code>\u00a0\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">torchrun\u542f\u52a8\u5355\u673a\u591a\u5361DDP\u5e76\u884c\u8bad\u7ec3\uff1a<\/p>\n\n\n\n<p>\u542f\u52a8\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul><li>\u4f7f\u7528 torchrun \u547d\u4ee4\u6765\u542f\u52a8\u7a0b\u5e8f<\/li><li>torchrun &#8211;standalone &#8211;nproc_per_node=gpu XXX.py<\/li><\/ul>\n\n\n\n<ol><li><code>--standalone<\/code>&nbsp;\u4ee3\u8868\u5355\u673a\u8fd0\u884c<\/li><li><code>--nproc_per_node=gpu<\/code>&nbsp;\u4ee3\u8868\u4f7f\u7528\u6240\u6709\u53ef\u7528GPU\u3002\u7b49\u4e8e\u53f7\u540e\u4e5f\u53ef\u5199gpu\u6570\u91cfn\uff0c\u8fd9\u6837\u4f1a\u4f7f\u7528\u524dn\u4e2aGPU<\/li><\/ol>\n\n\n\n<p>\u5982\u679c\u60f3\u8981\u8fdb\u4e00\u6b65\u6307\u5b9a\u8981\u8fd0\u884c\u7684 GPU\uff0c\u53ef\u4ee5\u901a\u8fc7 CUDA_VISIBLE_DEVICES \u8bbe\u7f6eGPU\u53ef\u89c1\u6027\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<p><strong>CUDA_VISIBLE_DEVICES=2,3 torchrun &#8211;standalone &#8211;nproc_per_node=gpu multi_gpu_torchrun.py<\/strong><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u591a\u673a\u591a\u5361<\/p>\n\n\n\n<p>torchrun &#8211;nproc_per_node=4 &#8211;nnodes=3 &#8211;node_rank=0 &#8211;master_addr=192.168.0.101 &#8211;master_port=29500 test_mpi.py<\/p>\n\n\n\n<p><strong>1.\u6307\u5b9a\u6bcf\u4e2a\u8282\u70b9\uff08\u673a\u5668\uff09\u4e0a\u7684\u8fdb\u7a0b\u6570\uff0c\u8fd9\u91cc\u662f4\u4e2a\u3002\u610f\u5473\u7740\u6bcf\u4e2a\u673a\u5668\u5c06\u542f\u52a84\u4e2a\u8fdb\u7a0b\u6765\u53c2\u4e0e\u5206\u5e03\u5f0f\u8bad\u7ec3\u3002<\/strong> &#8211;nproc_per_node=4 \u3010\u4e00\u822c\u8bbe\u7f6e\u4e3a\u4e3a\u8282\u70b9GPU\u6570\u91cf\u3011<\/p>\n\n\n\n<p><strong>2.\u6307\u5b9a\u603b\u5171\u7684\u8282\u70b9\u6570\uff0c\u8fd9\u91cc\u662f3\u4e2a\u3002\u610f\u5473\u7740\u603b\u5171\u67093\u4e2a\u673a\u5668\u53c2\u4e0e\u5206\u5e03\u5f0f\u8bad\u7ec3\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--nnodes=3\n<\/code><\/pre>\n\n\n\n<p><strong>3.\u6307\u5b9a\u5f53\u524d\u8282\u70b9\uff08\u673a\u5668\uff09\u7684\u6392\u540d\uff0c\u8fd9\u91cc\u662f0\u3002\u6392\u540d\u4ece0\u5f00\u59cb\uff0c\u7528\u4e8e\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u533a\u5206\u4e0d\u540c\u7684\u8282\u70b9\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--node_rank=0 \u30100\u4ee3\u8868\u4e3b\u8282\u70b9\u3011\n<\/code><\/pre>\n\n\n\n<p><strong>4.\u6307\u5b9a\u4e3b\u8282\u70b9\u7684IP\u5730\u5740\uff0c\u8fd9\u91cc\u662f192.168.0.101\uff08\u66f4\u6839\u636e\u5b9e\u9645\u4fee\u6539\uff09\u3002\u4e3b\u8282\u70b9\u7528\u4e8e\u534f\u8c03\u5206\u5e03\u5f0f\u8bad\u7ec3\u8fc7\u7a0b\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--master_addr=192.168.0.101\n<\/code><\/pre>\n\n\n\n<p><strong>5.\u6307\u5b9a\u4e3b\u8282\u70b9\u7684\u7aef\u53e3\u53f7\uff0c\u8fd9\u91cc\u662f29500\u3002\u4e3b\u8282\u70b9\u4f7f\u7528\u6307\u5b9a\u7684\u7aef\u53e3\u6765\u4e0e\u5176\u4ed6\u8282\u70b9\u8fdb\u884c\u901a\u4fe1\u3002<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u2013master_port=29500\n<\/code><\/pre>\n\n\n\n<p><strong>6.\u5355\u673a\u8fd0\u884c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--standalone<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/pytorch.org\/docs\/stable\/elastic\/run.html torchr &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2024\/07\/05\/torchrun-torch\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">torchrun-torch\u591a\u673a\u591a\u5361\u5206\u5e03\u5f0f\u8bad\u7ec3\u547d\u4ee4<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,39],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/15768"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=15768"}],"version-history":[{"count":15,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/15768\/revisions"}],"predecessor-version":[{"id":16313,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/15768\/revisions\/16313"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=15768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=15768"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=15768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}