{"id":9335,"date":"2024-06-12T20:06:29","date_gmt":"2024-06-12T12:06:29","guid":{"rendered":"\/?p=9335"},"modified":"2024-06-12T20:07:31","modified_gmt":"2024-06-12T12:07:31","slug":"%e3%80%90pytorch%e3%80%91%e5%9c%a8%e5%a4%9a%e8%bf%9b%e7%a8%8b%e4%b8%ad%e4%bd%bf%e7%94%a8-cuda","status":"publish","type":"post","link":"\/?p=9335","title":{"rendered":"\u3010Pytorch\u3011\u5728\u591a\u8fdb\u7a0b\u4e2d\u4f7f\u7528 CUDA"},"content":{"rendered":"<p>Python \u539f\u751f\u81ea\u5e26\u7684\u591a\u8fdb\u7a0b\u5e93\u4e0d\u652f\u6301\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528 CUDA \u8fdb\u884c\u52a0\u901f\u8fd0\u7b97\u3002\u56e0\u6b64\uff0c\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528 Pytorch \u4e2d\u7684 multiprocessing \u5e93\u5b9e\u73b0\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528 CUDA \u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u4f7f\u7528 Python \u539f\u751f\u81ea\u5e26\u7684 <code>multiprocessing<\/code> \u5e93\u4e2d\uff0c\u82e5\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528 CUDA\uff0c\u5219\u4f1a\u62a5\u9519\uff1a<\/p>\n<blockquote>\n<p>RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the \u2018spawn\u2019 start method<\/p>\n<\/blockquote>\n<p>\u5982\u679c\u6839\u636e\u63d0\u793a\u4fe1\u606f\uff0c\u76f4\u63a5\u4f7f\u7528<\/p>\n<pre><code>torch.multiprocessing.set_start_method(&quot;spawn&quot;)<\/code><\/pre>\n<p>\u5219\u4f1a\u62a5\u9519\uff1a<\/p>\n<blockquote>\n<p>RuntimeError(\u2018context has already been set\u2019)<\/p>\n<\/blockquote>\n<p>\u8fd9\u662f\u56e0\u4e3a\u60f3\u8981\u5b9e\u73b0\u5728\u591a\u8fdb\u7a0b\u4e2d\u8c03\u7528 CUDA\uff0c\u9700\u8981\u5148\u65b0\u5efa\u4e00\u4e2a <code>context<\/code> \u5bf9\u8c61\uff0c\u5e76\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u7528\u8fd9\u4e2a <code>context<\/code> \u5bf9\u8c61\u66ff\u6362 <code>torch.multiprocessing<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>context = torch.multiprocessing.get_context(&quot;spawn&quot;)\n...\nprocess = context.Process(target=..., args=...)<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u5728\u5b50\u8fdb\u7a0b\u4e2d\u5c31\u53ef\u4ee5\u653e\u5fc3\u5730\u8c03\u7528 CUDA \u4e86.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u539f\u751f\u81ea\u5e26\u7684\u591a\u8fdb\u7a0b\u5e93\u4e0d\u652f\u6301\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528 CUDA \u8fdb\u884c\u52a0\u901f\u8fd0\u7b97\u3002\u56e0\u6b64\uff0c\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528 Pytorch \u4e2d\u7684 multipr   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[274],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9335"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9335"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9335\/revisions"}],"predecessor-version":[{"id":9337,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9335\/revisions\/9337"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9335"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}