{"id":16314,"date":"2024-07-18T10:29:42","date_gmt":"2024-07-18T02:29:42","guid":{"rendered":"http:\/\/139.9.1.231\/?p=16314"},"modified":"2024-08-06T15:16:33","modified_gmt":"2024-08-06T07:16:33","slug":"pytorchtrain-param","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2024\/07\/18\/pytorchtrain-param\/","title":{"rendered":"pytorch\u5206\u5e03\u5f0f \u8bad\u7ec3\u53c2\u6570\u8bbe\u7f6e"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code><strong># \u81ea\u5df1\u7684\u6570\u636e\u83b7\u53d6\ndataset = MyDataset(input_size, data_size)\n \n# \u4f7f\u7528 DistributedSampler\ntrain_sampler = torch.utils.data.distributed.DistributedSampler(dataset)\n \ntrainloader = DataLoader(dataset=dataset,\n                         pin_memory=true,\n                         shuffle=(train_sampler is None),   # \u4f7f\u7528\u5206\u5e03\u5f0f\u8bad\u7ec3 shuffle \u5e94\u8be5\u8bbe\u7f6e\u4e3a False\n                         batch_size=args.batch_size,\n                         num_workers=args.workers,\n                         sampler=train_sampler)<\/strong><\/code><\/pre>\n\n\n\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u51e0\u4e2a\u53c2\u6570\uff1abatch_size\u3001num_workers\u3001shuffle\u3001pin_memory\u5728\u8fdb\u884c\u591a\u673a\u591a\u5361\u4ee5\u53ca\u5355\u673a\u591a\u5361\u7684\u8bbe\u7f6e\u3002<\/strong><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>1\u3001 Batch_size\u8bbe\u7f6e:<\/strong><\/p>\n\n\n\n<p><strong><code>Dataparallel<\/code>&nbsp;\uff1a \u8bbe\u7f6e batch_size  \u662f\u6307\u603b\u591a\u5361\u7684Batch size\uff0c\u6570\u636e\u88ab\u76f4\u63a5\u5212\u5206\u5230\u591a\u4e2a&nbsp;<code>GPU<\/code>&nbsp;\u4e0a<\/strong><\/p>\n\n\n\n<p><strong><strong><code>DistributedDataParallel<\/code>&nbsp;<\/strong><\/strong>\uff1a<strong><strong><code>batch size<\/code>&nbsp;\u8bbe\u7f6e\u6210\u5355\u5361\u4e00\u6837\u5373\u53ef\uff0c\u56e0\u4e3a\u5404\u4e2aGPU\u5bf9\u5e94\u7684\u8fdb\u7a0b\u72ec\u7acb\u4ece\u78c1\u76d8\u4e2d\u52a0\u8f7d\u6570\u636e<\/strong><\/strong>\uff0c<strong>\u8fd9\u91cc\u7684 Batch_size\u6307\u7684\u662f\u5355\u5361\u7684\u3002<\/strong><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">2\u3001shuffle\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<p>shuffle\uff1a<\/p>\n\n\n\n<p> <code>Dataparallel<\/code>&nbsp; \uff1a\u8bbe\u7f6e &#8216;shuffle&#8217;: True<\/p>\n\n\n\n<p> <strong><strong><code>DistributedDataParallel<\/code>&nbsp;<\/strong><\/strong> \uff1a\u4e3a\u4e86\u80fd\u591f\u6309\u987a\u5e8f\u5212\u5206\u6570\u636e\u5b50\u96c6\uff0c\u62ff\u5230\u4e0d\u540c\u90e8\u5206\u6570\u636e\uff0c\u6240\u4ee5\u6570\u636e\u96c6\u4e0d\u80fd\u591f\u8fdb\u884c\u968f\u673a\u6253\u6563\uff0c\u6240\u4ee5\u7528\u4e86\u53c2\u6570 &#8216;shuffle&#8217;: False<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">3\u3001 pin_memory \u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<p>\u662f\u5426\u63d0\u524d\u7533\u8bf7CUDA\u5185\u5b58\uff08\u9ed8\u8ba4\u4e3aFalse\uff0c\u4f46\u6709\u8bf4\u6cd5\u9664\u975e\u6570\u636e\u96c6\u5f88\u5c0f\uff0c\u5426\u5219\u5728N\u5361\u4e0a\u63a8\u8350\u603b\u662f\u6253\u5f00\uff09\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u5f00\u4e86pin memory:<br>\u6bcf\u4e2aworker\u90fd\u9700\u8981\u7f13\u5b58\u4e00\u4e2abatch\u7684\u6570\u636e.<br><strong>batch size\u548cnum_workers\u90fd\u5927, \u663e\u5b58\u4f1a\u70b8<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48 \u8bbe\u7f6e\u00a0<\/strong>pip_memory=true\uff0c \u770b\u89e3\u91ca\uff1a<br>\u591aGPU\u8bad\u7ec3\u7684\u65f6\u5019\u6ce8\u610f\u673a\u5668\u7684\u5185\u5b58\u662f\u5426\u8db3\u591f(\u4e00\u822c\u5185\u5b58\u4e3a\u663e\u5361\u663e\u5b58x2)\uff0c\u5982\u679c\u4e0d\u591f\uff0c\u5efa\u8bae\u5173\u95edpin_memory(\u9501\u9875\u5185\u5b58)\u9009\u9879\u3002<br>\u91c7\u7528<strong>DistributedDataParallel<\/strong>\u591aGPUs\u8bad\u7ec3\u7684\u65b9\u5f0f\u6bd4<strong>DataParallel<\/strong>\u66f4\u5feb\u4e00\u4e9b\uff0c\u5982\u679c\u4f60\u7684Pytorch\u7f16\u8bd1\u65f6\u6709nccl\u7684\u652f\u6301\uff0c\u90a3\u4e48\u6700\u597d\u4f7f\u7528DistributedDataParallel\u65b9\u5f0f\u3002<br>\u5173\u4e8e\u4ec0\u4e48\u662f\u9501\u9875\u5185\u5b58\uff1a<br>pin_memory\u5c31\u662f\u9501\u9875\u5185\u5b58\uff0c\u521b\u5efaDataLoader\u65f6\uff0c\u8bbe\u7f6epin_memory=True\uff0c\u5219\u610f\u5473\u7740\u751f\u6210\u7684Tensor\u6570\u636e\u6700\u5f00\u59cb\u662f\u5c5e\u4e8e\u5185\u5b58\u4e2d\u7684\u9501\u9875\u5185\u5b58\uff0c\u8fd9\u6837\u5c06\u5185\u5b58\u7684Tensor\u8f6c\u4e49\u5230GPU\u7684\u663e\u5b58\u5c31\u4f1a\u66f4\u5feb\u4e00\u4e9b\u3002<br>\u4e3b\u673a\u4e2d\u7684\u5185\u5b58\uff0c\u6709\u4e24\u79cd\u5b58\u5728\u65b9\u5f0f\uff0c\u4e00\u662f\u9501\u9875\uff0c\u4e8c\u662f\u4e0d\u9501\u9875\uff0c\u9501\u9875\u5185\u5b58\u5b58\u653e\u7684\u5185\u5bb9\u5728\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4e0d\u4f1a\u4e0e\u4e3b\u673a\u7684\u865a\u62df\u5185\u5b58\u8fdb\u884c\u4ea4\u6362\uff08\u6ce8\uff1a\u865a\u62df\u5185\u5b58\u5c31\u662f\u786c\u76d8\uff09\uff0c\u800c\u4e0d\u9501\u9875\u5185\u5b58\u5728\u4e3b\u673a\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u6570\u636e\u4f1a\u5b58\u653e\u5728\u865a\u62df\u5185\u5b58\u4e2d\u3002\u663e\u5361\u4e2d\u7684\u663e\u5b58\u5168\u90e8\u662f\u9501\u9875\u5185\u5b58,\u5f53\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u5145\u8db3\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8bbe\u7f6epin_memory=True\u3002\u5f53\u7cfb\u7edf\u5361\u4f4f\uff0c\u6216\u8005\u4ea4\u6362\u5185\u5b58\u4f7f\u7528\u8fc7\u591a\u7684\u65f6\u5019\uff0c\u8bbe\u7f6epin_memory=False\u3002\u56e0\u4e3apin_memory\u4e0e\u7535\u8111\u786c\u4ef6\u6027\u80fd\u6709\u5173\uff0cpytorch\u5f00\u53d1\u8005\u4e0d\u80fd\u786e\u4fdd\u6bcf\u4e00\u4e2a\u70bc\u4e39\u73a9\u5bb6\u90fd\u6709\u9ad8\u7aef\u8bbe\u5907\uff0c\u56e0\u6b64pin_memory\u9ed8\u8ba4\u4e3aFalse\u3002<\/p>\n\n\n\n<p>\u5f53\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u5145\u8db3\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8bbe\u7f6epin_memory=True\u3002\u5f53\u7cfb\u7edf\u5361\u4f4f\uff0c\u6216\u8005\u4ea4\u6362\u5185\u5b58\u4f7f\u7528\u8fc7\u591a\u7684\u65f6\u5019\uff0c\u8bbe\u7f6epin_memory=False\u3002pin_memory\u9ed8\u8ba4\u4e3aFalse\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">4\u3001 num_workers \u8bbe\u7f6e\uff1a<strong>num_worker\u7684\u8bbe\u7f6e\u503c\u4e00\u822c\u662f\u6240\u8fd0\u884c\u673a\u5b50\u4e0a\u7684CPU\u6838\u5fc3\u6570<\/strong><\/p>\n\n\n\n<p>\u53ef\u4ee5\u8bbe\u7f6eset num_workers =4 x number of available GPUs&nbsp;<\/p>\n\n\n\n<p>um_worker\u5927: \u4e0b\u4e00\u8f6e\u8fed\u4ee3\u7684batch\u53ef\u80fd\u5728\u4e0a\u4e00\u8f6e\/\u4e0a\u4e0a\u4e00\u8f6e&#8230;\u8fed\u4ee3\u65f6\u5df2\u7ecf\u52a0\u8f7d\u597d\u4e86\u3002 \u574f\u5904\u662fGPU memory\u5f00\u9500\u5927 (\u8fd9\u662f\u5f00\u4e86pin memory\u7684\u60c5\u51b5\u5427) \uff0c\u4e5f\u52a0\u91cd\u4e86CPU\u8d1f\u62c5\u3002<\/p>\n\n\n\n<p>CPU\u7684\u7269\u7406\u4e2a\u6570\uff1agrep &#8216;physical id&#8217; \/proc\/cpuinfo | sort | uniq | wc -l \u7ed3\u679c\u4e3a2\uff0c\u8bf4\u660eCPU\u6709\u4e24\u4e2a\u3002 \u6bcf\u4e2aCPU\u7684\u6838\u6570\uff1acat \/proc\/cpuinfo |grep &#8220;cores&#8221;|uniq 10\uff0c\u8bf4\u660e\u6bcf\u4e2a10\u6838\u3002 cpu\u6838\u6570 = 2&#215;10<\/p>\n\n\n\n<p>1\u3001cpu\u4e2a\u6570<\/p>\n\n\n\n<p>grep &#8216;physical id&#8217; \/proc\/cpuinfo | sort -u<\/p>\n\n\n\n<p><strong>2\u3001\u6838\u5fc3\u6570\u3010\u5f53\u6570\u636e\u96c6\u8f83\u5927\u65f6\u5efa\u8bae\u91c7\u7528\uff0cnum_works\u4e00\u822c\u8bbe\u7f6e\u4e3a\uff08CPU \u6838\u5fc3\u6570 +-1\uff09\u4e3a\u6700\u4f73\u3011<\/strong><\/p>\n\n\n\n<p><strong>grep &#8216;core id&#8217; \/proc\/cpuinfo | sort -u | wc -l<\/strong><\/p>\n\n\n\n<p><strong>3\u3001\u7ebf\u7a0b\u6570<\/strong><\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-background\"><strong>grep &#8216;processor&#8217; \/proc\/cpuinfo | sort -u | wc -l<\/strong><\/p>\n\n\n\n<p><strong>\u4e00\u822c\u5efa\u8bae <code>num_workers<\/code> \u7684\u503c\u63a5\u8fd1 CPU \u6838\u5fc3\u6570\uff0c\u4f46\u4e0d<\/strong>\u8981\u8d85\u8fc7\uff0c\u4ee5\u514d\u5bfc\u81f4\u8fc7\u591a\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u3002<\/p>\n\n\n\n<p><strong>\u5982\u679c\u6570\u636e\u96c6\u8f83\u5927\u4e14\u9884\u5904\u7406\u590d\u6742\uff0c\u8f83\u9ad8\u7684 <code>num_workers<\/code> \u503c\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u3002\u53cd\u4e4b\uff0c\u5982\u679c\u6570\u636e\u96c6\u8f83\u5c0f\u6216\u8005\u9884\u5904\u7406\u7b80\u5355\uff0c\u5219\u53ef\u80fd\u4e0d\u9700\u8981\u592a\u591a\u7684\u5de5\u4f5c\u7ebf\u7a0b\u3002<\/strong><\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\"><strong>Num workers\uff1a\u53ea\u8981\u4f60\u7684 GPU \u8ba1\u7b97\u5360\u7528\u6ca1\u6709\u7528\u6ee1\uff0c\u8bf4\u660e GPU \u8981\u7b49\u6570\u636e\u51c6\u5907\u3002\u53ef\u4ee5\u8bd5\u7740\u589e\u52a0\u8fdb\u7a0b\u6570\u76ee\uff0c\u540c\u65f6\u89c2\u5bdf\u662f\u5426\u662f\u786c\u76d8 IO \u74f6\u9888\uff0c\u5982\u679c\u662f\u591a\u673a\u8bad\u7ec3\uff0c\u8fd8\u8981\u6ce8\u610f\u7f51\u7edc\u74f6\u9888\u3002\u4e0d\u8fc7\uff0c\u6700\u5927\u4e5f\u4e0d\u80fd\u8d85\u8fc7\u6838\u5fc3\u6570\uff0c\u4e00\u822c\u8fd8\u8981\u51cf\u4e00\u70b9\uff0c\u56e0\u4e3a\u4e3b\u8fdb\u7a0b\uff0c\u591a\u5361\u591a\u8fdb\u7a0b\u8bad\u7ec3\uff0c\u90fd\u4f1a\u5360\u7528\u6838\u5fc3\u3002<\/strong><\/p>\n\n\n\n<p><strong>num_worker\u901a\u8fc7\u5f71\u54cd\u6570\u636e\u52a0\u8f7d\u901f\u5ea6\uff0c\u4ece\u800c\u5f71\u54cd\u8bad\u7ec3\u901f\u5ea6\u3002<\/strong>&nbsp;\u6bcf\u8f6edataloader\u52a0\u8f7d\u6570\u636e\u65f6\uff1adataloader\u4e00\u6b21\u6027\u521b\u5efanum_worker\u4e2aworker\uff0cworker\u5c31\u662f\u666e\u901a\u7684\u5de5\u4f5c\u8fdb\u7a0b\u3002\u5e76\u7528batch_sampler\u5c06\u6307\u5b9abatch\u5206\u914d\u7ed9\u6307\u5b9a\u7684worker\uff0cworker\u5c06\u5b83\u8d1f\u8d23\u7684batch\u52a0\u8f7d\u8fdbRAM\u3002\u7136\u540e\uff0cdataloader\u4eceRAM\u4e2d\u627e\u672c\u8f6e\u8fed\u4ee3\u8981\u7528\u7684batch\uff0c\u5982\u679c\u627e\u5230\u4e86\uff0c\u5c31\u4f7f\u7528\uff1b\u5982\u679c\u6ca1\u627e\u5230\uff0c\u5c31\u7528num_worker\u4e2aworker\u7ee7\u7eed\u52a0\u8f7dbatch\u5230RAM\uff0c\u76f4\u5230dataloader\u5728RAM\u4e2d\u627e\u5230\u76ee\u6807batch\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2024\/07\/image-98.png\" alt=\"\" class=\"wp-image-16358\" width=\"447\" height=\"265\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2024\/07\/image-98.png 603w, http:\/\/139.9.1.231\/wp-content\/uploads\/2024\/07\/image-98-300x178.png 300w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981\u6ce8\u610f\u7684\u51e0\u4e2a\u53c2\u6570\uff1abatch_size\u3001num_workers\u3001shuffle\u3001pin_memory\u5728\u8fdb\u884c\u591a &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2024\/07\/18\/pytorchtrain-param\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">pytorch\u5206\u5e03\u5f0f \u8bad\u7ec3\u53c2\u6570\u8bbe\u7f6e<\/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,4,39],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/16314"}],"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=16314"}],"version-history":[{"count":48,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/16314\/revisions"}],"predecessor-version":[{"id":16665,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/16314\/revisions\/16665"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=16314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=16314"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=16314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}