{"id":16666,"date":"2024-08-06T15:26:35","date_gmt":"2024-08-06T07:26:35","guid":{"rendered":"http:\/\/139.9.1.231\/?p=16666"},"modified":"2024-08-06T15:26:37","modified_gmt":"2024-08-06T07:26:37","slug":"pytorch-fast","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2024\/08\/06\/pytorch-fast\/","title":{"rendered":"PyTorch \u63d0\u901f"},"content":{"rendered":"\n<p>\u6458\u81ea\uff1a<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file<\/a><\/p>\n\n\n\n<p>Note<\/p>\n\n\n\n<p>\u539f\u59cb\u6587\u6863:<a href=\"https:\/\/www.yuque.com\/lart\/ugkv9f\/ugysgn\">https:\/\/www.yuque.com\/lart\/ugkv9f\/ugysgn<\/a><\/p>\n\n\n\n<p>\u58f0\u660e: \u5927\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u77e5\u4e4e\u548c\u5176\u4ed6\u535a\u5ba2\u7684\u5206\u4eab, \u8fd9\u91cc\u53ea\u4f5c\u4e3a\u4e00\u4e2a\u6536\u96c6\u7f57\u5217. \u6b22\u8fce\u7ed9\u51fa\u66f4\u591a\u5efa\u8bae.<\/p>\n\n\n\n<p>\u77e5\u4e4e\u56de\u7b54 (\u6b22\u8fce\u70b9\u8d5e\u54e6):<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.zhihu.com\/question\/307282137\/answer\/907835663\">pytorch dataloader \u6570\u636e\u52a0\u8f7d\u5360\u7528\u4e86\u5927\u90e8\u5206\u65f6\u95f4, \u5404\u4f4d\u5927\u4f6c\u90fd\u662f\u600e\u4e48\u89e3\u51b3\u7684? &#8211; \u4eba\u6c11\u827a\u672f\u5bb6\u7684\u56de\u7b54 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/www.zhihu.com\/question\/356829360\/answer\/907832358\">\u4f7f\u7528 pytorch \u65f6, \u8bad\u7ec3\u96c6\u6570\u636e\u592a\u591a\u8fbe\u5230\u4e0a\u5343\u4e07\u5f20, Dataloader \u52a0\u8f7d\u5f88\u6162\u600e\u4e48\u529e? &#8211; \u4eba\u6c11\u827a\u672f\u5bb6\u7684\u56de\u7b54 &#8211; \u77e5\u4e4e<\/a><\/li><\/ul>\n\n\n\n<h3>\u9884\u5904\u7406\u63d0\u901f<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E9%A2%84%E5%A4%84%E7%90%86%E6%8F%90%E9%80%9F\"><\/a><\/h3>\n\n\n\n<ul><li>\u5c3d\u91cf\u51cf\u5c11\u6bcf\u6b21\u8bfb\u53d6\u6570\u636e\u65f6\u7684\u9884\u5904\u7406\u64cd\u4f5c, \u53ef\u4ee5\u8003\u8651\u628a\u4e00\u4e9b\u56fa\u5b9a\u7684\u64cd\u4f5c, \u4f8b\u5982&nbsp;<code>resize<\/code>&nbsp;, \u4e8b\u5148\u5904\u7406\u597d\u4fdd\u5b58\u4e0b\u6765, \u8bad\u7ec3\u7684\u65f6\u5019\u76f4\u63a5\u62ff\u6765\u7528\u3002<\/li><li>\u5c06\u9884\u5904\u7406\u642c\u5230 GPU \u4e0a\u52a0\u901f\u3002<ul><li>Linux \u53ef\u4ee5\u4f7f\u7528&nbsp;<a href=\"https:\/\/github.com\/NVIDIA\/DALI\"><code>NVIDIA\/DALI<\/code><\/a>\u3002<\/li><li>\u4f7f\u7528\u57fa\u4e8e Tensor \u7684\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\u3002<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>IO \u63d0\u901f<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#io-%E6%8F%90%E9%80%9F\"><\/a><\/h3>\n\n\n\n<ul><li>mmcv \u5bf9\u6570\u636e\u7684\u8bfb\u53d6\u63d0\u4f9b\u4e86\u6bd4\u8f83\u9ad8\u6548\u4e14\u5168\u9762\u7684\u652f\u6301\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/339190576\">OpenMMLab\uff1aMMCV \u6838\u5fc3\u7ec4\u4ef6\u5206\u6790(\u4e09): FileClient<\/a><\/li><\/ul>\n\n\n\n<h4>\u4f7f\u7528\u66f4\u5feb\u7684\u56fe\u7247\u5904\u7406<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E4%BD%BF%E7%94%A8%E6%9B%B4%E5%BF%AB%E7%9A%84%E5%9B%BE%E7%89%87%E5%A4%84%E7%90%86\"><\/a><\/h4>\n\n\n\n<ul><li><code>opencv<\/code>&nbsp;\u4e00\u822c\u8981\u6bd4&nbsp;<code>PIL<\/code>&nbsp;\u8981\u5feb \u3002<ul><li>\u8bf7\u6ce8\u610f\uff0c<code>PIL<\/code>&nbsp;\u7684\u60f0\u6027\u52a0\u8f7d\u7684\u7b56\u7565\u4f7f\u5f97\u5176\u770b\u4e0a\u53bb&nbsp;<code>open<\/code>&nbsp;\u8981\u6bd4&nbsp;<code>opencv<\/code>&nbsp;\u7684&nbsp;<code>imread<\/code>&nbsp;\u8981\u5feb\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u90a3\u5e76\u6ca1\u6709\u5b8c\u5168\u52a0\u8f7d\u6570\u636e\u3002\u53ef\u4ee5\u5bf9&nbsp;<code>open<\/code>&nbsp;\u8fd4\u56de\u7684\u5bf9\u8c61\u8c03\u7528\u5176&nbsp;<code>load()<\/code>&nbsp;\u65b9\u6cd5\uff0c\u4ece\u800c\u624b\u52a8\u52a0\u8f7d\u6570\u636e\uff0c\u8fd9\u65f6\u7684\u901f\u5ea6\u624d\u662f\u5408\u7406\u7684\u3002<\/li><\/ul><\/li><li>\u5bf9\u4e8e&nbsp;<code>jpeg<\/code>&nbsp;\u8bfb\u53d6, \u53ef\u4ee5\u5c1d\u8bd5&nbsp;<code>jpeg4py<\/code>\u3002<\/li><li>\u5b58&nbsp;<code>bmp<\/code>&nbsp;\u56fe (\u964d\u4f4e\u89e3\u7801\u65f6\u95f4)\u3002<\/li><li>\u5173\u4e8e\u4e0d\u540c\u56fe\u50cf\u5904\u7406\u5e93\u901f\u5ea6\u7684\u8ba8\u8bba\uff1a<a href=\"https:\/\/www.zhihu.com\/question\/48762352\">Python \u7684\u5404\u79cd imread \u51fd\u6570\u5728\u5b9e\u73b0\u65b9\u5f0f\u548c\u8bfb\u53d6\u901f\u5ea6\u4e0a\u6709\u4f55\u533a\u522b\uff1f &#8211; \u77e5\u4e4e<\/a><\/li><\/ul>\n\n\n\n<h4>\u6574\u5408\u6570\u636e\u4e3a\u5355\u4e2a\u8fde\u7eed\u6587\u4ef6 (\u964d\u4f4e\u8bfb\u53d6\u6b21\u6570)<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%95%B4%E5%90%88%E6%95%B0%E6%8D%AE%E4%B8%BA%E5%8D%95%E4%B8%AA%E8%BF%9E%E7%BB%AD%E6%96%87%E4%BB%B6-%E9%99%8D%E4%BD%8E%E8%AF%BB%E5%8F%96%E6%AC%A1%E6%95%B0\"><\/a><\/h4>\n\n\n\n<p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u5c0f\u6587\u4ef6\u8bfb\u53d6\uff0c\u53ef\u4ee5\u4fdd\u5b58\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u8fde\u7eed\u8bfb\u53d6\u7684\u8fde\u7eed\u6587\u4ef6\u683c\u5f0f\u3002<a href=\"https:\/\/github.com\/Lyken17\/Efficient-PyTorch#data-loader\">\u53ef\u4ee5\u9009\u62e9\u8003\u8651&nbsp;<code>TFRecord (Tensorflow)<\/code>&nbsp;,&nbsp;<code>recordIO<\/code>,&nbsp;<code>hdf5<\/code>,&nbsp;<code>pth<\/code>,&nbsp;<code>n5<\/code>,&nbsp;<code>lmdb<\/code>\u7b49<\/a>\u3002<\/p>\n\n\n\n<ul><li><code>TFRecord<\/code>&nbsp;\uff1a<a href=\"https:\/\/github.com\/vahidk\/tfrecord\">https:\/\/github.com\/vahidk\/tfrecord<\/a><\/li><li><code>lmdb<\/code>&nbsp;\u6570\u636e\u5e93\uff1a<ul><li><a href=\"https:\/\/github.com\/Fangyh09\/Image2LMDB\">https:\/\/github.com\/Fangyh09\/Image2LMDB<\/a><\/li><li><a href=\"https:\/\/blog.csdn.net\/P_LarT\/article\/details\/103208405\">https:\/\/blog.csdn.net\/P_LarT\/article\/details\/103208405<\/a><\/li><li><a href=\"https:\/\/github.com\/lartpang\/PySODToolBox\/blob\/master\/ForBigDataset\/ImageFolder2LMDB.py\">https:\/\/github.com\/lartpang\/PySODToolBox\/blob\/master\/ForBigDataset\/ImageFolder2LMDB.py<\/a><\/li><\/ul><\/li><li>\u57fa\u4e8e&nbsp;<a href=\"https:\/\/github.com\/vahidk\/EffectivePyTorch?tab=readme-ov-file#building-efficient-custom-data-loaders\"><code>Tar<\/code>\u6587\u4ef6\u548c<code>IterableDataset<\/code>\u7684\u5b9e\u73b0<\/a><\/li><\/ul>\n\n\n\n<h4>\u9884\u8bfb\u53d6\u6570\u636e<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E9%A2%84%E8%AF%BB%E5%8F%96%E6%95%B0%E6%8D%AE\"><\/a><\/h4>\n\n\n\n<p>\u9884\u8bfb\u53d6\u4e0b\u4e00\u6b21\u8fed\u4ee3\u9700\u8981\u7684\u6570\u636e\u3002\u4f7f\u7528\u6848\u4f8b\uff1a<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/66145913\">\u5982\u4f55\u7ed9\u4f60 PyTorch \u91cc\u7684 Dataloader \u6253\u9e21\u8840 &#8211; MKFMIKU \u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/72956595\">\u7ed9 pytorch \u8bfb\u53d6\u6570\u636e\u52a0\u901f &#8211; \u4f53 hi \u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><\/ul>\n\n\n\n<h4>\u501f\u52a9\u5185\u5b58<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%80%9F%E5%8A%A9%E5%86%85%E5%AD%98\"><\/a><\/h4>\n\n\n\n<ul><li>\u76f4\u63a5\u8f7d\u5230\u5185\u5b58\u91cc\u9762\u3002<ul><li>\u5c06\u56fe\u7247\u8bfb\u53d6\u540e\u5b58\u5230\u4e00\u4e2a\u56fa\u5b9a\u7684\u5bb9\u5668\u5bf9\u8c61\u4e2d\u3002<ul><li>YoloV5 \u4e2d\u7684&nbsp;<a href=\"https:\/\/github.com\/ultralytics\/yolov5\/blob\/19f33cbae29ac2127dd877b52e228c178dda6086\/utils\/dataloaders.py#L521-L534\"><code>--cache<\/code><\/a>\u3002<\/li><\/ul><\/li><\/ul><\/li><li>\u628a\u5185\u5b58\u6620\u5c04\u6210\u78c1\u76d8\u3002<\/li><\/ul>\n\n\n\n<h4>\u501f\u52a9\u56fa\u6001<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%80%9F%E5%8A%A9%E5%9B%BA%E6%80%81\"><\/a><\/h4>\n\n\n\n<p>\u673a\u68b0\u786c\u76d8\u6362\u6210 NVME \u56fa\u6001\u3002\u53c2\u8003\u81ea&nbsp;<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/66145913\">\u5982\u4f55\u7ed9\u4f60 PyTorch \u91cc\u7684 Dataloader \u6253\u9e21\u8840 &#8211; MKFMIKU \u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/p>\n\n\n\n<h3>\u8bad\u7ec3\u7b56\u7565<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E8%AE%AD%E7%BB%83%E7%AD%96%E7%95%A5\"><\/a><\/h3>\n\n\n\n<h4>\u4f4e\u7cbe\u5ea6\u8bad\u7ec3<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E4%BD%8E%E7%B2%BE%E5%BA%A6%E8%AE%AD%E7%BB%83\"><\/a><\/h4>\n\n\n\n<p>\u5728\u8bad\u7ec3\u4e2d\u4f7f\u7528\u4f4e\u7cbe\u5ea6 (&nbsp;<code>FP16<\/code>&nbsp;\u751a\u81f3&nbsp;<code>INT8<\/code>&nbsp;\u3001\u4e8c\u503c\u7f51\u7edc\u3001\u4e09\u503c\u7f51\u7edc) \u8868\u793a\u53d6\u4ee3\u539f\u6709\u7cbe\u5ea6 (&nbsp;<code>FP32<\/code>&nbsp;) \u8868\u793a\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u8282\u7ea6\u4e00\u5b9a\u7684\u663e\u5b58\u5e76\u63d0\u901f, \u4f46\u662f\u8981\u5c0f\u5fc3\u4e00\u4e9b\u4e0d\u5b89\u5168\u7684\u64cd\u4f5c\u5982 mean \u548c sum\u3002<\/p>\n\n\n\n<ul><li>\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3\u7684\u4ecb\u7ecd\u6587\u7ae0\uff1a<ul><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/6FCumAWa8fZ1r7xwIRC9ow\">\u7531\u6d45\u5165\u6df1\u7684\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3\u6559\u7a0b<\/a><\/li><\/ul><\/li><li><a href=\"https:\/\/github.com\/nvidia\/apex\"><code>NVIDIA\/Apex<\/code><\/a>&nbsp;\u63d0\u4f9b\u7684\u6df7\u5408\u7cbe\u5ea6\u652f\u6301\u3002<ul><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/HQnI8rzPvZN6Q_5c8d1nVQ\">PyTorch \u5fc5\u5907\u795e\u5668 | \u552f\u5feb\u4e0d\u7834\uff1a\u57fa\u4e8e Apex \u7684\u6df7\u5408\u7cbe\u5ea6\u52a0\u901f<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/80386137\">Pytorch \u5b89\u88c5 APEX \u7591\u96be\u6742\u75c7\u89e3\u51b3\u65b9\u6848 &#8211; \u9648\u701a\u53ef\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><\/ul><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/amp_examples.html\">PyTorch1.6 \u5f00\u59cb\u63d0\u4f9b\u7684<code>torch.cuda.amp<\/code><\/a>&nbsp;\u4ee5\u652f\u6301\u6df7\u5408\u7cbe\u5ea6\u3002<\/li><\/ul>\n\n\n\n<h4>\u66f4\u5927\u7684 batch<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%9B%B4%E5%A4%A7%E7%9A%84-batch\"><\/a><\/h4>\n\n\n\n<p>\u66f4\u5927\u7684 batch \u5728\u56fa\u5b9a\u7684 epoch \u7684\u60c5\u51b5\u4e0b\u5f80\u5f80\u4f1a\u5e26\u6765\u66f4\u77ed\u7684\u8bad\u7ec3\u65f6\u95f4\u3002\u4f46\u662f\u5927\u7684 batch \u9762\u4e34\u7740\u8d85\u53c2\u6570\u7684\u8bbe\u7f6e\u3001\u663e\u5b58\u5360\u7528\u95ee\u9898\u7b49\u8bf8\u591a\u8003\u91cf\uff0c\u8fd9\u53c8\u662f\u53e6\u4e00\u4e2a\u5907\u53d7\u5173\u6ce8\u7684\u9886\u57df\u4e86\u3002<\/p>\n\n\n\n<ul><li>\u8d85\u53c2\u6570\u8bbe\u7f6e<ul><li>Accurate, large minibatch SGD: training imagenet in 1 hour\uff0c<a href=\"https:\/\/arxiv.org\/abs\/1706.02677\">\u8bba\u6587<\/a><\/li><\/ul><\/li><li>\u4f18\u5316\u663e\u5b58\u5360\u7528<ul><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/amp_examples.html#gradient-accumulation\">Gradient Accumulation<\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/1.11\/checkpoint.html#torch-utils-checkpoint\">Gradient Checkpointing<\/a><ul><li>Training deep nets with sublinear memory cost\uff0c<a href=\"https:\/\/arxiv.org\/abs\/1604.06174\">\u8bba\u6587<\/a><\/li><\/ul><\/li><li>In-Place Operation<ul><li>In-Place Activated BatchNorm for Memory-Optimized Training of DNNs\uff0c<a href=\"https:\/\/arxiv.org\/abs\/1712.02616\">\u8bba\u6587<\/a>\uff0c<a href=\"https:\/\/github.com\/mapillary\/inplace_abn\">\u4ee3\u7801<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>\u4ee3\u7801\u5c42\u9762<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E4%BB%A3%E7%A0%81%E5%B1%82%E9%9D%A2\"><\/a><\/h3>\n\n\n\n<h4>\u5e93\u8bbe\u7f6e<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%BA%93%E8%AE%BE%E7%BD%AE\"><\/a><\/h4>\n\n\n\n<ul><li>\u5728\u8bad\u7ec3\u5faa\u73af\u4e4b\u524d\u8bbe\u7f6e&nbsp;<code>torch.backends.cudnn.benchmark = True<\/code>&nbsp;\u53ef\u4ee5\u52a0\u901f\u8ba1\u7b97\u3002\u7531\u4e8e\u8ba1\u7b97\u4e0d\u540c\u5185\u6838\u5927\u5c0f\u5377\u79ef\u7684 cuDNN \u7b97\u6cd5\u7684\u6027\u80fd\u4e0d\u540c\uff0c\u81ea\u52a8\u8c03\u4f18\u5668\u53ef\u4ee5\u8fd0\u884c\u4e00\u4e2a\u57fa\u51c6\u6765\u627e\u5230\u6700\u4f73\u7b97\u6cd5\u3002\u5f53\u4f60\u7684\u8f93\u5165\u5927\u5c0f\u4e0d\u7ecf\u5e38\u6539\u53d8\u65f6\uff0c\u5efa\u8bae\u5f00\u542f\u8fd9\u4e2a\u8bbe\u7f6e\u3002\u5982\u679c\u8f93\u5165\u5927\u5c0f\u7ecf\u5e38\u6539\u53d8\uff0c\u90a3\u4e48\u81ea\u52a8\u8c03\u4f18\u5668\u5c31\u9700\u8981\u592a\u9891\u7e41\u5730\u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\uff0c\u8fd9\u53ef\u80fd\u4f1a\u635f\u5bb3\u6027\u80fd\u3002\u5b83\u53ef\u4ee5\u5c06\u5411\u524d\u548c\u5411\u540e\u4f20\u64ad\u901f\u5ea6\u63d0\u9ad8 1.27x \u5230 1.70x\u3002<\/li><li>\u4f7f\u7528\u9875\u9762\u9501\u5b9a\u5185\u5b58\uff0c\u5373\u5728 DataLoader \u4e2d\u8bbe\u5b9a&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/data.html#memory-pinning\"><code>pin_memory=True<\/code><\/a>\u3002<\/li><li>\u5408\u9002\u7684&nbsp;<code>num_worker<\/code>\uff0c\u7ec6\u8282\u8ba8\u8bba\u53ef\u89c1&nbsp;<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/39752167\">Pytorch \u63d0\u901f\u6307\u5357 &#8211; \u4e91\u68a6\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a>\u3002<\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.optim.Optimizer.zero_grad.html#torch-optim-optimizer-zero-grad\">optimizer.zero_grad(set_to_none=False<\/a>&nbsp;\u8fd9\u91cc\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e&nbsp;<code>set_to_none=True<\/code>&nbsp;\u6765\u964d\u4f4e\u7684\u5185\u5b58\u5360\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u9002\u5ea6\u63d0\u9ad8\u6027\u80fd\u3002\u4f46\u662f\u8fd9\u4e5f\u4f1a\u6539\u53d8\u67d0\u4e9b\u884c\u4e3a\uff0c\u5177\u4f53\u53ef\u89c1\u6587\u6863\u3002\u901a\u8fc7&nbsp;<code>model.zero_grad()<\/code>&nbsp;\u6216&nbsp;<code>optimizer.zero_grad()<\/code>&nbsp;\u5c06\u5bf9\u6240\u6709\u53c2\u6570\u6267\u884c&nbsp;<code>memset<\/code>\uff0c\u5e76\u901a\u8fc7\u8bfb\u5199\u64cd\u4f5c\u66f4\u65b0\u68af\u5ea6\u3002\u4f46\u662f\uff0c\u5c06\u68af\u5ea6\u8bbe\u7f6e\u4e3a&nbsp;<code>None<\/code>&nbsp;\u5c06\u4e0d\u4f1a\u6267\u884c&nbsp;<code>memset<\/code>\uff0c\u5e76\u4e14\u5c06\u4f7f\u7528\u201c\u53ea\u5199\u201d\u64cd\u4f5c\u66f4\u65b0\u68af\u5ea6\u3002\u56e0\u6b64\uff0c\u8bbe\u7f6e\u68af\u5ea6\u4e3a&nbsp;<code>None<\/code>&nbsp;\u66f4\u5feb\u3002<\/li><li>\u53cd\u5411\u4f20\u64ad\u671f\u95f4\u8bbe\u5b9a\u4f7f\u7528&nbsp;<code>eval<\/code>&nbsp;\u6a21\u5f0f\u5e76\u4f7f\u7528&nbsp;<code>torch.no_grad<\/code>&nbsp;\u5173\u95ed\u68af\u5ea6\u8ba1\u7b97\u3002<\/li><li>\u53ef\u4ee5\u8003\u8651\u4f7f\u7528&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/tensor_attributes.html#torch-memory-format\">channels_last<\/a>&nbsp;\u7684\u5185\u5b58\u683c\u5f0f\u3002<\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/cuda.html#use-nn-parallel-distributeddataparallel-instead-of-multiprocessing-or-nn-dataparallel\">\u7528<code>DistributedDataParallel<\/code>\u4ee3\u66ff<code>DataParallel<\/code><\/a>\u3002\u5bf9\u4e8e\u591a GPU \u6765\u8bf4\uff0c\u5373\u4f7f\u53ea\u6709\u5355\u4e2a\u8282\u70b9\uff0c\u4e5f\u603b\u662f\u4f18\u5148\u4f7f\u7528&nbsp;<code>DistributedDataParallel<\/code>&nbsp;\u800c\u4e0d\u662f&nbsp;<code>DataParallel<\/code>&nbsp;\uff0c\u56e0\u4e3a&nbsp;<code>DistributedDataParallel<\/code>&nbsp;\u5e94\u7528\u4e8e\u591a\u8fdb\u7a0b\uff0c\u5e76\u4e3a\u6bcf\u4e2a GPU \u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u4ece\u800c\u7ed5\u8fc7 Python \u5168\u5c40\u89e3\u91ca\u5668\u9501 (GIL) \u5e76\u63d0\u9ad8\u901f\u5ea6\u3002<\/li><\/ul>\n\n\n\n<h4>\u6a21\u578b<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%A8%A1%E5%9E%8B\"><\/a><\/h4>\n\n\n\n<ul><li>\u4e0d\u8981\u521d\u59cb\u5316\u4efb\u4f55\u7528\u4e0d\u5230\u7684\u53d8\u91cf\uff0c\u56e0\u4e3a PyTorch \u7684\u521d\u59cb\u5316\u548c\u00a0<code>forward<\/code>\u00a0\u662f\u5206\u5f00\u7684\uff0c\u4ed6\u4e0d\u4f1a\u56e0\u4e3a\u4f60\u4e0d\u53bb\u4f7f\u7528\uff0c\u800c\u4e0d\u53bb\u521d\u59cb\u5316\u3002<\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.jit.script.html#torch.jit.script\"><code>@torch.jit.script<\/code><\/a>\uff0c\u4f7f\u7528 PyTroch JIT \u5c06\u9010\u70b9\u8fd0\u7b97\u878d\u5408\u5230\u5355\u4e2a CUDA kernel \u4e0a\u3002PyTorch \u4f18\u5316\u4e86\u7ef4\u5ea6\u5f88\u5927\u7684\u5f20\u91cf\u7684\u8fd0\u7b97\u64cd\u4f5c\u3002\u5728 PyTorch \u4e2d\u5bf9\u5c0f\u5f20\u91cf\u8fdb\u884c\u592a\u591a\u7684\u8fd0\u7b97\u64cd\u4f5c\u662f\u975e\u5e38\u4f4e\u6548\u7684\u3002\u6240\u4ee5\u6709\u53ef\u80fd\u7684\u8bdd\uff0c\u5c06\u8ba1\u7b97\u64cd\u4f5c\u90fd\u91cd\u5199\u4e3a\u6279\u6b21\uff08batch\uff09\u7684\u5f62\u5f0f\uff0c\u53ef\u4ee5\u51cf\u5c11\u6d88\u8017\u548c\u63d0\u9ad8\u6027\u80fd\u3002\u800c\u5982\u679c\u6ca1\u529e\u6cd5\u81ea\u5df1\u624b\u52a8\u5b9e\u73b0\u6279\u6b21\u7684\u8fd0\u7b97\u64cd\u4f5c\uff0c\u90a3\u4e48\u53ef\u4ee5\u91c7\u7528 TorchScript \u6765\u63d0\u5347\u4ee3\u7801\u7684\u6027\u80fd\u3002TorchScript \u662f\u4e00\u4e2a Python \u51fd\u6570\u7684\u5b50\u96c6\uff0c\u4f46\u7ecf\u8fc7\u4e86 PyTorch \u7684\u9a8c\u8bc1\uff0cPyTorch \u53ef\u4ee5\u901a\u8fc7\u5176 just in time(jtt) \u7f16\u8bd1\u5668\u6765\u81ea\u52a8\u4f18\u5316 TorchScript \u4ee3\u7801\uff0c\u63d0\u9ad8\u6027\u80fd\u3002\u4f46\u66f4\u597d\u7684\u505a\u6cd5\u8fd8\u662f\u624b\u52a8\u5b9e\u73b0\u6279\u6b21\u7684\u8fd0\u7b97\u64cd\u4f5c\u3002<\/li><li>\u5728\u4f7f\u7528\u6df7\u5408\u7cbe\u5ea6\u7684 FP16 \u65f6\uff0c\u5bf9\u4e8e\u6240\u6709\u4e0d\u540c\u67b6\u6784\u8bbe\u8ba1\uff0c\u8bbe\u7f6e\u5c3a\u5bf8\u4e3a 8 \u7684\u500d\u6570\u3002<\/li><li>BN \u4e4b\u524d\u7684\u5377\u79ef\u5c42\u53ef\u4ee5\u53bb\u6389 bias\u3002\u56e0\u4e3a\u5728\u6570\u5b66\u4e0a\uff0cbias \u53ef\u4ee5\u901a\u8fc7 BN \u7684\u5747\u503c\u51cf\u6cd5\u6765\u62b5\u6d88\u3002\u6211\u4eec\u53ef\u4ee5\u8282\u7701\u6a21\u578b\u53c2\u6570\u3001\u8fd0\u884c\u65f6\u7684\u5185\u5b58<\/li><\/ul>\n\n\n\n<h4>\u6570\u636e<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%95%B0%E6%8D%AE\"><\/a><\/h4>\n\n\n\n<ul><li>\u5c06 batch size \u8bbe\u7f6e\u4e3a 8 \u7684\u500d\u6570\uff0c\u6700\u5927\u5316 GPU \u5185\u5b58\u7684\u4f7f\u7528\u3002<\/li><li>GPU \u4e0a\u5c3d\u53ef\u80fd\u6267\u884c NumPy \u98ce\u683c\u7684\u64cd\u4f5c\u3002<\/li><li>\u4f7f\u7528&nbsp;<code>del<\/code>&nbsp;\u91ca\u653e\u5185\u5b58\u5360\u7528\u3002<\/li><li>\u907f\u514d\u4e0d\u540c\u8bbe\u5907\u4e4b\u95f4\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u4f20\u8f93\u3002<\/li><li>\u521b\u5efa\u5f20\u91cf\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u6307\u5b9a\u8bbe\u5907\uff0c\u800c\u4e0d\u8981\u521b\u5efa\u540e\u518d\u4f20\u8f93\u5230\u76ee\u6807\u8bbe\u5907\u4e0a\u3002<\/li><li>\u4f7f\u7528&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.from_numpy.html#torch-from-numpy\"><code>torch.from_numpy(ndarray)<\/code><\/a>&nbsp;\u6216\u8005&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.as_tensor.html#torch-as-tensor\"><code>torch.as_tensor(data, dtype=None, device=None)<\/code><\/a>\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u800c\u907f\u514d\u91cd\u65b0\u7533\u8bf7\u7a7a\u95f4\uff0c\u5177\u4f53\u4f7f\u7528\u7ec6\u8282\u548c\u6ce8\u610f\u4e8b\u9879\u53ef\u53c2\u8003\u5bf9\u5e94\u6587\u6863\u3002\u5982\u679c\u6e90\u8bbe\u5907\u548c\u76ee\u6807\u8bbe\u5907\u90fd\u662f CPU\uff0c<code>torch.from_numpy<\/code>&nbsp;\u548c&nbsp;<code>torch.as_tensor<\/code>&nbsp;\u4e0d\u4f1a\u62f7\u8d1d\u6570\u636e\u3002\u5982\u679c\u6e90\u6570\u636e\u662f NumPy \u6570\u7ec4\uff0c\u4f7f\u7528&nbsp;<code>torch.from_numpy<\/code>&nbsp;\u66f4\u5feb\u3002\u5982\u679c\u6e90\u6570\u636e\u662f\u4e00\u4e2a\u5177\u6709\u76f8\u540c\u6570\u636e\u7c7b\u578b\u548c\u8bbe\u5907\u7c7b\u578b\u7684\u5f20\u91cf\uff0c\u90a3\u4e48&nbsp;<code>torch.as_tensor<\/code>&nbsp;\u53ef\u4ee5\u907f\u514d\u62f7\u8d1d\u6570\u636e\uff0c\u8fd9\u91cc\u7684\u6570\u636e\u53ef\u4ee5\u662f Python \u7684 list\uff0c tuple\uff0c\u6216\u8005\u5f20\u91cf\u3002<\/li><li>\u4f7f\u7528\u975e\u963b\u585e\u4f20\u8f93\uff0c\u5373\u8bbe\u5b9a&nbsp;<code>non_blocking=True<\/code>\u3002\u8fd9\u4f1a\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\u5c1d\u8bd5\u5f02\u6b65\u8f6c\u6362\uff0c\u4f8b\u5982\uff0c\u5c06\u9875\u9762\u9501\u5b9a\u5185\u5b58\u4e2d\u7684 CPU \u5f20\u91cf\u8f6c\u6362\u4e3a CUDA \u5f20\u91cf\u3002<\/li><\/ul>\n\n\n\n<h3>\u5bf9\u4f18\u5316\u5668\u7684\u4f18\u5316<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%AF%B9%E4%BC%98%E5%8C%96%E5%99%A8%E7%9A%84%E4%BC%98%E5%8C%96\"><\/a><\/h3>\n\n\n\n<ul><li>\u5c06\u6a21\u578b\u53c2\u6570\u5b58\u653e\u5230\u4e00\u5757\u8fde\u7eed\u7684\u5185\u5b58\u4e2d\uff0c\u4ece\u800c\u51cf\u5c11&nbsp;<code>optimizer.step()<\/code>&nbsp;\u7684\u65f6\u95f4\u3002<ul><li><a href=\"https:\/\/github.com\/PhilJd\/contiguous_pytorch_params\"><code>contiguous_pytorch_params<\/code><\/a><\/li><\/ul><\/li><li>\u4f7f\u7528 APEX \u4e2d\u7684&nbsp;<a href=\"https:\/\/nvidia.github.io\/apex\/optimizers.html\">fused building blocks<\/a><\/li><\/ul>\n\n\n\n<h3>\u6a21\u578b\u8bbe\u8ba1<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%A8%A1%E5%9E%8B%E8%AE%BE%E8%AE%A1\"><\/a><\/h3>\n\n\n\n<h4>CNN<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#cnn\"><\/a><\/h4>\n\n\n\n<ul><li>ShuffleNetV2\uff0c<a href=\"https:\/\/arxiv.org\/pdf\/1807.11164.pdf\">\u8bba\u6587<\/a>\u3002<ul><li>\u5377\u79ef\u5c42\u8f93\u5165\u8f93\u51fa\u901a\u9053\u4e00\u81f4: \u5377\u79ef\u5c42\u7684\u8f93\u5165\u548c\u8f93\u51fa\u7279\u5f81\u901a\u9053\u6570\u76f8\u7b49\u65f6 MAC\uff08\u5185\u5b58\u8bbf\u95ee\u6d88\u8017\u65f6\u95f4,&nbsp;<code>memory access cost<\/code>&nbsp;\u7f29\u5199\u4e3a&nbsp;<code>MAC<\/code>&nbsp;\uff09 \u6700\u5c0f, \u6b64\u65f6\u6a21\u578b\u901f\u5ea6\u6700\u5feb<\/li><li>\u51cf\u5c11\u5377\u79ef\u5206\u7ec4: \u8fc7\u591a\u7684 group \u64cd\u4f5c\u4f1a\u589e\u5927 MAC, \u4ece\u800c\u4f7f\u6a21\u578b\u901f\u5ea6\u53d8\u6162<\/li><li>\u51cf\u5c11\u6a21\u578b\u5206\u652f: \u6a21\u578b\u4e2d\u7684\u5206\u652f\u6570\u91cf\u8d8a\u5c11, \u6a21\u578b\u901f\u5ea6\u8d8a\u5feb<\/li><li>\u51cf\u5c11&nbsp;<code>element-wise<\/code>&nbsp;\u64cd\u4f5c:&nbsp;<code>element-wise<\/code>&nbsp;\u64cd\u4f5c\u6240\u5e26\u6765\u7684\u65f6\u95f4\u6d88\u8017\u8fdc\u6bd4\u5728 FLOPs \u4e0a\u7684\u4f53\u73b0\u7684\u6570\u503c\u8981\u591a, \u56e0\u6b64\u8981\u5c3d\u53ef\u80fd\u51cf\u5c11&nbsp;<code>element-wise<\/code>&nbsp;\u64cd\u4f5c\u3002&nbsp;<code>depthwise convolution<\/code>&nbsp;\u4e5f\u5177\u6709\u4f4e FLOPs \u3001\u9ad8 MAC \u7684\u7279\u70b9\u3002<\/li><\/ul><\/li><\/ul>\n\n\n\n<h4>Vision Transformer<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#vision-transformer\"><\/a><\/h4>\n\n\n\n<ul><li>TRT-ViT: TensorRT-oriented Vision Transformer\uff0c<a href=\"https:\/\/arxiv.org\/abs\/2205.09579\">\u8bba\u6587<\/a>\uff0c<a href=\"https:\/\/www.yuque.com\/lart\/papers\/pghqxg\">\u89e3\u8bfb<\/a>\u3002<ul><li>stage-level\uff1aTransformer block \u9002\u5408\u653e\u7f6e\u5230\u6a21\u578b\u7684\u540e\u671f\uff0c\u8fd9\u53ef\u4ee5\u6700\u5927\u5316\u6548\u7387\u548c\u6027\u80fd\u7684\u6743\u8861\u3002<\/li><li>stage-level\uff1a\u5148\u6d45\u540e\u6df1\u7684 stage \u8bbe\u8ba1\u6a21\u5f0f\u53ef\u4ee5\u63d0\u5347\u6027\u80fd\u3002<\/li><li>block-level\uff1aTransformer \u548c BottleNeck \u7684\u6df7\u5408 block \u8981\u6bd4\u5355\u72ec\u7684 Transformer \u66f4\u6709\u6548\u3002<\/li><li>block-level\uff1a\u5148\u5168\u5c40\u518d\u5c40\u90e8\u7684 block \u8bbe\u8ba1\u6a21\u5f0f\u6709\u52a9\u4e8e\u5f25\u8865\u6027\u80fd\u95ee\u9898\u3002<\/li><\/ul><\/li><\/ul>\n\n\n\n<h4>\u901a\u7528\u601d\u8def<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E9%80%9A%E7%94%A8%E6%80%9D%E8%B7%AF\"><\/a><\/h4>\n\n\n\n<ul><li>\u964d\u4f4e\u590d\u6742\u5ea6: \u4f8b\u5982\u6a21\u578b\u88c1\u526a\u548c\u526a\u679d, \u51cf\u5c11\u6a21\u578b\u5c42\u6570\u548c\u53c2\u6570\u89c4\u6a21<\/li><li>\u6539\u6a21\u578b\u7ed3\u6784: \u4f8b\u5982\u6a21\u578b\u84b8\u998f, \u901a\u8fc7\u77e5\u8bc6\u84b8\u998f\u65b9\u6cd5\u6765\u83b7\u53d6\u5c0f\u6a21\u578b<\/li><\/ul>\n\n\n\n<h3>\u63a8\u7406\u52a0\u901f<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%8E%A8%E7%90%86%E5%8A%A0%E9%80%9F\"><\/a><\/h3>\n\n\n\n<h4>\u534a\u7cbe\u5ea6\u4e0e\u6743\u91cd\u91cf\u5316<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%8D%8A%E7%B2%BE%E5%BA%A6%E4%B8%8E%E6%9D%83%E9%87%8D%E9%87%8F%E5%8C%96\"><\/a><\/h4>\n\n\n\n<p>\u5728\u63a8\u7406\u4e2d\u4f7f\u7528\u4f4e\u7cbe\u5ea6 (&nbsp;<code>FP16<\/code>&nbsp;\u751a\u81f3&nbsp;<code>INT8<\/code>&nbsp;\u3001\u4e8c\u503c\u7f51\u7edc\u3001\u4e09\u503c\u7f51\u7edc) \u8868\u793a\u53d6\u4ee3\u539f\u6709\u7cbe\u5ea6 (&nbsp;<code>FP32<\/code>&nbsp;) \u8868\u793a\u3002<\/p>\n\n\n\n<ul><li><code>TensorRT<\/code>&nbsp;\u662f NVIDIA \u63d0\u51fa\u7684\u795e\u7ecf\u7f51\u7edc\u63a8\u7406 (Inference) \u5f15\u64ce, \u652f\u6301\u8bad\u7ec3\u540e 8BIT \u91cf\u5316, \u5b83\u4f7f\u7528\u57fa\u4e8e\u4ea4\u53c9\u71b5\u7684\u6a21\u578b\u91cf\u5316\u7b97\u6cd5, \u901a\u8fc7\u6700\u5c0f\u5316\u4e24\u4e2a\u5206\u5e03\u7684\u5dee\u5f02\u7a0b\u5ea6\u6765\u5b9e\u73b0<\/li><li>Pytorch1.3 \u5f00\u59cb\u5df2\u7ecf\u652f\u6301\u91cf\u5316\u529f\u80fd, \u57fa\u4e8e QNNPACK \u5b9e\u73b0, \u652f\u6301\u8bad\u7ec3\u540e\u91cf\u5316, \u52a8\u6001\u91cf\u5316\u548c\u91cf\u5316\u611f\u77e5\u8bad\u7ec3\u7b49\u6280\u672f<\/li><li>\u53e6\u5916&nbsp;<code>Distiller<\/code>&nbsp;\u662f Intel \u57fa\u4e8e Pytorch \u5f00\u6e90\u7684\u6a21\u578b\u4f18\u5316\u5de5\u5177, \u81ea\u7136\u4e5f\u652f\u6301 Pytorch \u4e2d\u7684\u91cf\u5316\u6280\u672f<\/li><li>\u5fae\u8f6f\u7684&nbsp;<code>NNI<\/code>&nbsp;\u96c6\u6210\u4e86\u591a\u79cd\u91cf\u5316\u611f\u77e5\u7684\u8bad\u7ec3\u7b97\u6cd5, \u5e76\u652f\u6301&nbsp;<code>PyTorch\/TensorFlow\/MXNet\/Caffe2<\/code>&nbsp;\u7b49\u591a\u4e2a\u5f00\u6e90\u6846\u67b6<\/li><\/ul>\n\n\n\n<p>\u66f4\u591a\u7ec6\u8282\u53ef\u53c2\u8003&nbsp;<a href=\"https:\/\/mp.weixin.qq.com\/s\/3uUwf9vQmQ4jkGjLxzb9aQ\">\u6709\u4e09 AI:\u3010\u6742\u8c08\u3011\u5f53\u524d\u6a21\u578b\u91cf\u5316\u6709\u54ea\u4e9b\u53ef\u7528\u7684\u5f00\u6e90\u5de5\u5177?<\/a>\u3002<\/p>\n\n\n\n<h4>\u64cd\u4f5c\u878d\u5408<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%93%8D%E4%BD%9C%E8%9E%8D%E5%90%88\"><\/a><\/h4>\n\n\n\n<ul><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/110552861\">\u6a21\u578b\u63a8\u7406\u52a0\u901f\u6280\u5de7\uff1a\u878d\u5408 BN \u548c Conv \u5c42 &#8211; \u5c0f\u5c0f\u5c06\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/48005099\">\u7f51\u7edc inference \u9636\u6bb5 conv \u5c42\u548c BN \u5c42\u7684\u878d\u5408 &#8211; autocyz \u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/1.3.0\/quantization.html#torch.quantization.fuse_modules\">PyTorch \u672c\u8eab\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u529f\u80fd<\/a><\/li><\/ul>\n\n\n\n<h4>\u91cd\u53c2\u6570\u5316\uff08Re-Parameterization\uff09<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E9%87%8D%E5%8F%82%E6%95%B0%E5%8C%96re-parameterization\"><\/a><\/h4>\n\n\n\n<ul><li>RepVGG<ul><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/M4Kspm6hO3W8fXT_JqoEhA\">RepVGG|\u8ba9\u4f60\u7684 ConVNet \u4e00\u5377\u5230\u5e95\uff0cplain \u7f51\u7edc\u9996\u6b21\u8d85\u8fc7 80%top1 \u7cbe\u5ea6<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>\u65f6\u95f4\u5206\u6790<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%97%B6%E9%97%B4%E5%88%86%E6%9E%90\"><\/a><\/h3>\n\n\n\n<ul><li>Python \u81ea\u5e26\u4e86\u51e0\u4e2a\u6027\u80fd\u5206\u6790\u7684\u6a21\u5757&nbsp;<code>profile<\/code>&nbsp;,&nbsp;<code>cProfile<\/code>&nbsp;\u548c&nbsp;<code>hotshot<\/code>&nbsp;, \u4f7f\u7528\u65b9\u6cd5\u57fa\u672c\u90fd\u5dee\u4e0d\u591a, \u65e0\u975e\u6a21\u5757\u662f\u7eaf Python \u8fd8\u662f\u7528 C \u5199\u7684\u3002<\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/profiler.html?highlight=profile#module-torch.profiler\">PyTorch Profiler<\/a>&nbsp;\u662f\u4e00\u79cd\u5de5\u5177\uff0c\u53ef\u5728\u8bad\u7ec3\u548c\u63a8\u7406\u8fc7\u7a0b\u4e2d\u6536\u96c6\u6027\u80fd\u6307\u6807\u3002Profiler \u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668 API \u53ef\u7528\u4e8e\u66f4\u597d\u5730\u4e86\u89e3\u54ea\u79cd\u6a21\u578b\u7b97\u5b50\u6210\u672c\u6700\u9ad8\uff0c\u68c0\u67e5\u5176\u8f93\u5165\u5f62\u72b6\u548c\u5806\u6808\u8bb0\u5f55\uff0c\u7814\u7a76\u8bbe\u5907\u5185\u6838\u6d3b\u52a8\u5e76\u53ef\u89c6\u5316\u6267\u884c\u8bb0\u5f55\u3002<\/li><\/ul>\n\n\n\n<h3>\u9879\u76ee\u63a8\u8350<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E9%A1%B9%E7%9B%AE%E6%8E%A8%E8%8D%90\"><\/a><\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/github.com\/666DZY666\/model-compression\">\u57fa\u4e8e Pytorch \u5b9e\u73b0\u6a21\u578b\u538b\u7f29<\/a>:<ul><li>\u91cf\u5316:8\/4\/2 bits(dorefa)\u3001\u4e09\u503c\/\u4e8c\u503c (twn\/bnn\/xnor-net)\u3002<\/li><li>\u526a\u679d: \u6b63\u5e38\u3001\u89c4\u6574\u3001\u9488\u5bf9\u5206\u7ec4\u5377\u79ef\u7ed3\u6784\u7684\u901a\u9053\u526a\u679d\u3002<\/li><li>\u5206\u7ec4\u5377\u79ef\u7ed3\u6784\u3002<\/li><li>\u9488\u5bf9\u7279\u5f81\u4e8c\u503c\u91cf\u5316\u7684 BN \u878d\u5408\u3002<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>\u6269\u5c55\u9605\u8bfb<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%89%A9%E5%B1%95%E9%98%85%E8%AF%BB\"><\/a><\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/www.zhihu.com\/question\/307282137\">pytorch dataloader \u6570\u636e\u52a0\u8f7d\u5360\u7528\u4e86\u5927\u90e8\u5206\u65f6\u95f4, \u5404\u4f4d\u5927\u4f6c\u90fd\u662f\u600e\u4e48\u89e3\u51b3\u7684? &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/www.zhihu.com\/question\/356829360\">\u4f7f\u7528 pytorch \u65f6, \u8bad\u7ec3\u96c6\u6570\u636e\u592a\u591a\u8fbe\u5230\u4e0a\u5343\u4e07\u5f20, Dataloader \u52a0\u8f7d\u5f88\u6162\u600e\u4e48\u529e? &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/www.zhihu.com\/question\/67209417\">PyTorch \u6709\u54ea\u4e9b\u5751\/bug? &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/sagivtech.com\/2017\/09\/19\/optimizing-pytorch-training-code\/\">Optimizing PyTorch training code<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/79020733\">26 \u79d2\u5355 GPU \u8bad\u7ec3 CIFAR10, Jeff Dean \u4e5f\u70b9\u8d5e\u7684\u6df1\u5ea6\u5b66\u4e60\u4f18\u5316\u6280\u5de7 &#8211; \u673a\u5668\u4e4b\u5fc3\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/www.zhihu.com\/question\/354086469\/answer\/894235805\">\u7ebf\u4e0a\u6a21\u578b\u52a0\u5165\u51e0\u4e2a\u65b0\u7279\u5f81\u8bad\u7ec3\u540e\u4e0a\u7ebf, tensorflow serving \u9884\u6d4b\u65f6\u95f4\u4e3a\u4ec0\u4e48\u6bd4\u539f\u6765\u6162 20 \u591a\u500d? &#8211; TzeSing \u7684\u56de\u7b54 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/www.yuque.com\/lart\/gw5mta\">\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u538b\u7f29<\/a><\/li><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/_ATSwwVqigvqmDB0Y9lOAQ\">\u4eca\u5929, \u4f60\u7684\u6a21\u578b\u52a0\u901f\u4e86\u5417? \u8fd9\u91cc\u6709 5 \u4e2a\u65b9\u6cd5\u4f9b\u4f60\u53c2\u8003(\u9644\u4ee3\u7801\u89e3\u6790)<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/77952356\">pytorch \u5e38\u89c1\u7684\u5751\u6c47\u603b &#8211; \u90c1\u632f\u6ce2\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/39752167\">Pytorch \u63d0\u901f\u6307\u5357 &#8211; \u4e91\u68a6\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/ShgNdizIPzeXOREoz8rgJA\">\u4f18\u5316 PyTorch \u7684\u901f\u5ea6\u548c\u5185\u5b58\u6548\u7387\uff082022\uff09<\/a><\/li><\/ul>\n\n\n\n<h2>PyTorch \u8282\u7701\u663e\u5b58<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#pytorch-%E8%8A%82%E7%9C%81%E6%98%BE%E5%AD%98\"><\/a><\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u539f\u59cb\u6587\u6863:<a href=\"https:\/\/www.yuque.com\/lart\/ugkv9f\/nvffyf\">https:\/\/www.yuque.com\/lart\/ugkv9f\/nvffyf<\/a><\/p><p>\u6574\u7406\u81ea: Pytorch \u6709\u4ec0\u4e48\u8282\u7701\u5185\u5b58 (\u663e\u5b58) \u7684\u5c0f\u6280\u5de7? &#8211; \u77e5\u4e4e&nbsp;<a href=\"https:\/\/www.zhihu.com\/question\/274635237\">https:\/\/www.zhihu.com\/question\/274635237<\/a><\/p><\/blockquote>\n\n\n\n<h3>\u4f7f\u7528 In-Place \u64cd\u4f5c<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E4%BD%BF%E7%94%A8-in-place-%E6%93%8D%E4%BD%9C\"><\/a><\/h3>\n\n\n\n<ul><li>\u5bf9\u4e8e\u9ed8\u8ba4\u652f\u6301&nbsp;<code>inplace<\/code>&nbsp;\u7684\u64cd\u4f5c\u5c3d\u91cf\u542f\u7528\u3002\u6bd4\u5982&nbsp;<code>relu<\/code>&nbsp;\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>inplace=True<\/code>&nbsp;\u3002<\/li><li>\u53ef\u4ee5\u5c06&nbsp;<code>batchnorm<\/code>&nbsp;\u548c\u4e00\u4e9b\u7279\u5b9a\u7684\u6fc0\u6d3b\u51fd\u6570\u6253\u5305\u6210&nbsp;<a href=\"https:\/\/github.com\/mapillary\/inplace_abn\"><code>inplace_abn<\/code><\/a>\u3002<\/li><\/ul>\n\n\n\n<h3>\u635f\u5931\u51fd\u6570<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%8D%9F%E5%A4%B1%E5%87%BD%E6%95%B0\"><\/a><\/h3>\n\n\n\n<p>\u6bcf\u6b21\u5faa\u73af\u7ed3\u675f\u65f6\u5220\u9664 loss, \u53ef\u4ee5\u8282\u7ea6\u5f88\u5c11\u663e\u5b58, \u4f46\u804a\u80dc\u4e8e\u65e0\u3002\u53ef\u89c1&nbsp;<a href=\"https:\/\/discuss.pytorch.org\/t\/tensor-to-variable-and-memory-freeing-best-practices\/6000\/2\">Tensor to Variable and memory freeing best practices<\/a><\/p>\n\n\n\n<h3>\u6df7\u5408\u7cbe\u5ea6<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%B7%B7%E5%90%88%E7%B2%BE%E5%BA%A6\"><\/a><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u8282\u7ea6\u4e00\u5b9a\u7684\u663e\u5b58\u5e76\u63d0\u901f, \u4f46\u662f\u8981\u5c0f\u5fc3\u4e00\u4e9b\u4e0d\u5b89\u5168\u7684\u64cd\u4f5c\u5982 mean \u548c sum\u3002<\/p>\n\n\n\n<ul><li>\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3\u7684\u4ecb\u7ecd\u6587\u7ae0\uff1a<ul><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/6FCumAWa8fZ1r7xwIRC9ow\">\u7531\u6d45\u5165\u6df1\u7684\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3\u6559\u7a0b<\/a><\/li><\/ul><\/li><li><a href=\"https:\/\/github.com\/nvidia\/apex\"><code>NVIDIA\/Apex<\/code><\/a>&nbsp;\u63d0\u4f9b\u7684\u6df7\u5408\u7cbe\u5ea6\u652f\u6301\u3002<ul><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/HQnI8rzPvZN6Q_5c8d1nVQ\">PyTorch \u5fc5\u5907\u795e\u5668 | \u552f\u5feb\u4e0d\u7834\uff1a\u57fa\u4e8e Apex \u7684\u6df7\u5408\u7cbe\u5ea6\u52a0\u901f<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/80386137\">Pytorch \u5b89\u88c5 APEX \u7591\u96be\u6742\u75c7\u89e3\u51b3\u65b9\u6848 &#8211; \u9648\u701a\u53ef\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><\/ul><\/li><li><a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/amp_examples.html\">PyTorch1.6 \u5f00\u59cb\u63d0\u4f9b\u7684<code>torch.cuda.amp<\/code><\/a>&nbsp;\u4ee5\u652f\u6301\u6df7\u5408\u7cbe\u5ea6\u3002<\/li><\/ul>\n\n\n\n<h3>\u7ba1\u7406\u4e0d\u9700\u8981\u53cd\u5411\u4f20\u64ad\u7684\u64cd\u4f5c<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E7%AE%A1%E7%90%86%E4%B8%8D%E9%9C%80%E8%A6%81%E5%8F%8D%E5%90%91%E4%BC%A0%E6%92%AD%E7%9A%84%E6%93%8D%E4%BD%9C\"><\/a><\/h3>\n\n\n\n<ul><li>\u5bf9\u4e8e\u4e0d\u9700\u8981\u53cd\u5411\u4f20\u64ad\u7684\u524d\u5411\u9636\u6bb5\uff0c\u5982\u9a8c\u8bc1\u548c\u63a8\u7406\u671f\u95f4\uff0c\u4f7f\u7528&nbsp;<code>torch.no_grad<\/code>&nbsp;\u6765\u5305\u88f9\u4ee3\u7801\u3002<ul><li>\u6ce8\u610f&nbsp;<code>model.eval()<\/code>&nbsp;\u4e0d\u7b49\u4e8e&nbsp;<code>torch.no_grad()<\/code>&nbsp;, \u8bf7\u770b\u5982\u4e0b\u8ba8\u8bba:&nbsp;<a href=\"https:\/\/discuss.pytorch.org\/t\/model-eval-vs-with-torch-no-grad\/19615\">&#8216;model.eval()&#8217; vs &#8216;with torch.no_grad()&#8217;<\/a><\/li><\/ul><\/li><li>\u5c06\u4e0d\u9700\u8981\u8ba1\u7b97\u68af\u5ea6\u7684\u53d8\u91cf\u7684&nbsp;<code>requires_grad<\/code>&nbsp;\u8bbe\u4e3a&nbsp;<code>False<\/code>, \u8ba9\u53d8\u91cf\u4e0d\u53c2\u4e0e\u68af\u5ea6\u7684\u540e\u5411\u4f20\u64ad\uff0c\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u68af\u5ea6\u7684\u663e\u5b58\u5360\u7528\u3002<\/li><li>\u79fb\u9664\u4e0d\u9700\u8981\u8ba1\u7b97\u7684\u68af\u5ea6\u8def\u5f84\uff1a<ul><li><a href=\"https:\/\/arxiv.org\/abs\/2203.16755\">Stochastic Backpropagation: A Memory Efficient Strategy for Training Video Models<\/a>\uff0c\u89e3\u8bfb\u53ef\u89c1\uff1a<ul><li><a href=\"https:\/\/www.yuque.com\/lart\/papers\/xu5t00\">https:\/\/www.yuque.com\/lart\/papers\/xu5t00<\/a><\/li><li><a href=\"https:\/\/blog.csdn.net\/P_LarT\/article\/details\/124978961\">https:\/\/blog.csdn.net\/P_LarT\/article\/details\/124978961<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>\u663e\u5b58\u6e05\u7406<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%98%BE%E5%AD%98%E6%B8%85%E7%90%86\"><\/a><\/h3>\n\n\n\n<ul><li><code>torch.cuda.empty_cache()<\/code>&nbsp;\u8fd9\u662f&nbsp;<code>del<\/code>&nbsp;\u7684\u8fdb\u9636\u7248, \u4f7f\u7528&nbsp;<code>nvidia-smi<\/code>&nbsp;\u4f1a\u53d1\u73b0\u663e\u5b58\u6709\u660e\u663e\u7684\u53d8\u5316. \u4f46\u662f\u8bad\u7ec3\u65f6\u6700\u5927\u7684\u663e\u5b58\u5360\u7528\u4f3c\u4e4e\u6ca1\u53d8. \u5927\u5bb6\u53ef\u4ee5\u8bd5\u8bd5:&nbsp;<a href=\"https:\/\/discuss.pytorch.org\/t\/how-can-we-release-gpu-memory-cache\/14530\">How can we release GPU memory cache?<\/a><\/li><li>\u53ef\u4ee5\u4f7f\u7528&nbsp;<code>del<\/code>&nbsp;\u5220\u9664\u4e0d\u5fc5\u8981\u7684\u4e2d\u95f4\u53d8\u91cf, \u6216\u8005\u4f7f\u7528&nbsp;<code>replacing variables<\/code>&nbsp;\u7684\u5f62\u5f0f\u6765\u51cf\u5c11\u5360\u7528.<\/li><\/ul>\n\n\n\n<h3>\u68af\u5ea6\u7d2f\u52a0\uff08Gradient Accumulation\uff09<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%A2%AF%E5%BA%A6%E7%B4%AF%E5%8A%A0gradient-accumulation\"><\/a><\/h3>\n\n\n\n<p>\u628a\u4e00\u4e2a&nbsp;<code>batchsize=64<\/code>&nbsp;\u5206\u4e3a\u4e24\u4e2a 32 \u7684 batch\uff0c\u4e24\u6b21 forward \u4ee5\u540e\uff0cbackward \u4e00\u6b21\u3002\u4f46\u4f1a\u5f71\u54cd&nbsp;<code>batchnorm<\/code>&nbsp;\u7b49\u548c&nbsp;<code>batchsize<\/code>&nbsp;\u76f8\u5173\u7684\u5c42\u3002<\/p>\n\n\n\n<p>\u5728&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/amp_examples.html#gradient-accumulation\">PyTorch \u7684\u6587\u6863<\/a>&nbsp;\u4e2d\u63d0\u5230\u4e86\u68af\u5ea6\u7d2f\u52a0\u4e0e\u6df7\u5408\u7cbe\u5ea6\u5e76\u7528\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528\u68af\u5ea6\u7d2f\u52a0\u6280\u672f\u53ef\u4ee5\u5bf9\u5206\u5e03\u5f0f\u8bad\u7ec3\u52a0\u901f\uff0c\u8fd9\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/250471767\">[\u539f\u521b][\u6df1\u5ea6][PyTorch] DDP \u7cfb\u5217\u7b2c\u4e09\u7bc7\uff1a\u5b9e\u6218\u4e0e\u6280\u5de7 &#8211; 996 \u9ec4\u91d1\u4e00\u4ee3\u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/p>\n\n\n\n<h3>\u68af\u5ea6\u68c0\u67e5\u70b9\uff08Gradient Checkpointing\uff09<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E6%A2%AF%E5%BA%A6%E6%A3%80%E6%9F%A5%E7%82%B9gradient-checkpointing\"><\/a><\/h3>\n\n\n\n<p>PyTorch \u4e2d\u63d0\u4f9b\u4e86&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/1.11\/checkpoint.html#torch-utils-checkpoint\"><code>torch.utils.checkpoint<\/code><\/a>\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u53cd\u5411\u4f20\u64ad\u671f\u95f4\uff0c\u5728\u6bcf\u4e2a\u68c0\u67e5\u70b9\u4f4d\u7f6e\u91cd\u65b0\u6267\u884c\u4e00\u6b21\u524d\u5411\u4f20\u64ad\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>\u8bba\u6587&nbsp;<a href=\"https:\/\/arxiv.org\/abs\/1604.06174\">Training Deep Nets with Sublinear Memory Cost<\/a>&nbsp;\u57fa\u4e8e\u68af\u5ea6\u68c0\u67e5\u70b9\u6280\u672f\uff0c\u5c06\u663e\u5b58\u4ece O(N) \u964d\u5230\u4e86 O(sqrt(N))\u3002\u5bf9\u4e8e\u8d8a\u6df1\u7684\u6a21\u578b, \u8fd9\u4e2a\u65b9\u6cd5\u7701\u7684\u663e\u5b58\u5c31\u8d8a\u591a, \u4e14\u901f\u5ea6\u4e0d\u4f1a\u660e\u663e\u53d8\u6162\u3002<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.yuque.com\/lart\/ugkv9f\/azvnyg\">PyTorch \u4e4b Checkpoint \u673a\u5236\u89e3\u6790<\/a><\/li><li><a href=\"https:\/\/blog.csdn.net\/one_six_mix\/article\/details\/93937091\">torch.utils.checkpoint \u7b80\u4ecb \u548c \u7b80\u6613\u4f7f\u7528<\/a><\/li><li><a href=\"https:\/\/github.com\/Lyken17\/pytorch-memonger\">Sublinear Memory Cost \u7684\u4e00\u4efd PyTorch \u5b9e\u73b0<\/a>\uff0c\u53c2\u8003\u81ea\uff1a<a href=\"https:\/\/www.zhihu.com\/question\/274635237\/answer\/755102181\">Pytorch \u6709\u4ec0\u4e48\u8282\u7701\u5185\u5b58(\u663e\u5b58)\u7684\u5c0f\u6280\u5de7? &#8211; Lyken \u7684\u56de\u7b54 &#8211; \u77e5\u4e4e<\/a><\/li><\/ul>\n\n\n\n<h3>\u76f8\u5173\u5de5\u5177<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E7%9B%B8%E5%85%B3%E5%B7%A5%E5%85%B7\"><\/a><\/h3>\n\n\n\n<ul><li>These codes can help you to detect your GPU memory during training with Pytorch.&nbsp;<a href=\"https:\/\/github.com\/Oldpan\/Pytorch-Memory-Utils\">https:\/\/github.com\/Oldpan\/Pytorch-Memory-Utils<\/a><\/li><li>Just less than nvidia-smi?&nbsp;<a href=\"https:\/\/github.com\/wookayin\/gpustat\">https:\/\/github.com\/wookayin\/gpustat<\/a><\/li><\/ul>\n\n\n\n<h3>\u53c2\u8003\u8d44\u6599<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\"><\/a><\/h3>\n\n\n\n<ul><li><a href=\"https:\/\/www.zhihu.com\/question\/274635237\/answer\/573633662\">Pytorch \u6709\u4ec0\u4e48\u8282\u7701\u5185\u5b58(\u663e\u5b58)\u7684\u5c0f\u6280\u5de7? &#8211; \u90d1\u54f2\u4e1c\u7684\u56de\u7b54 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/oldpan.me\/archives\/how-to-calculate-gpu-memory\">\u6d45\u8c08\u6df1\u5ea6\u5b66\u4e60: \u5982\u4f55\u8ba1\u7b97\u6a21\u578b\u4ee5\u53ca\u4e2d\u95f4\u53d8\u91cf\u7684\u663e\u5b58\u5360\u7528\u5927\u5c0f<\/a><\/li><li><a href=\"https:\/\/oldpan.me\/archives\/how-to-use-memory-pytorch\">\u5982\u4f55\u5728 Pytorch \u4e2d\u7cbe\u7ec6\u5316\u5229\u7528\u663e\u5b58<\/a><\/li><li><a href=\"https:\/\/www.zhihu.com\/question\/274635237\/answer\/756144739\">Pytorch \u6709\u4ec0\u4e48\u8282\u7701\u663e\u5b58\u7684\u5c0f\u6280\u5de7? &#8211; \u9648\u701a\u53ef\u7684\u56de\u7b54 &#8211; \u77e5\u4e4e<\/a><\/li><li><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/424512257\">PyTorch \u663e\u5b58\u673a\u5236\u5206\u6790 &#8211; Connolly \u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/li><\/ul>\n\n\n\n<h2>\u5176\u4ed6\u6280\u5de7<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%85%B6%E4%BB%96%E6%8A%80%E5%B7%A7\"><\/a><\/h2>\n\n\n\n<h3>\u91cd\u73b0<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E9%87%8D%E7%8E%B0\"><\/a><\/h3>\n\n\n\n<p>\u53ef\u5173\u6ce8\u6587\u6863\u4e2d&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/randomness.html#reproducibility\">\u76f8\u5173\u7ae0\u8282<\/a>\u3002<\/p>\n\n\n\n<h4>\u5f3a\u5236\u786e\u5b9a\u6027\u64cd\u4f5c<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E5%BC%BA%E5%88%B6%E7%A1%AE%E5%AE%9A%E6%80%A7%E6%93%8D%E4%BD%9C\"><\/a><\/h4>\n\n\n\n<p><a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/randomness.html#avoiding-nondeterministic-algorithms\">\u907f\u514d\u4f7f\u7528\u975e\u786e\u5b9a\u6027\u7b97\u6cd5<\/a>\u3002<\/p>\n\n\n\n<p>PyTorch \u4e2d\uff0c<a href=\"https:\/\/pytorch.org\/docs\/stable\/generated\/torch.use_deterministic_algorithms.html#torch.use_deterministic_algorithms\"><code>torch.use_deterministic_algorithms()<\/code><\/a>&nbsp;\u53ef\u4ee5\u5f3a\u5236\u4f7f\u7528\u786e\u5b9a\u6027\u7b97\u6cd5\u800c\u4e0d\u662f\u975e\u786e\u5b9a\u6027\u7b97\u6cd5\uff0c\u5e76\u4e14\u5982\u679c\u5df2\u77e5\u64cd\u4f5c\u662f\u975e\u786e\u5b9a\u6027\u7684\uff08\u5e76\u4e14\u6ca1\u6709\u786e\u5b9a\u6027\u7684\u66ff\u4ee3\u65b9\u6848\uff09\uff0c\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n\n\n\n<h4>\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#%E8%AE%BE%E7%BD%AE%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%A7%8D%E5%AD%90\"><\/a><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">def seed_torch(seed=1029):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.\n    torch.backends.cudnn.benchmark = False\n    torch.backends.cudnn.deterministic = True\n\nseed_torch()<\/pre>\n\n\n\n<p>\u53c2\u8003\u81ea<a href=\"https:\/\/www.zdaiot.com\/MLFrameworks\/Pytorch\/Pytorch%E9%9A%8F%E6%9C%BA%E7%A7%8D%E5%AD%90\/\">https:\/\/www.zdaiot.com\/MLFrameworks\/Pytorch\/Pytorch%E9%9A%8F%E6%9C%BA%E7%A7%8D%E5%AD%90\/<\/a><\/p>\n\n\n\n<h4>PyTorch 1.9 \u7248\u672c\u524d DataLoader \u4e2d\u7684\u9690\u85cf BUG<a href=\"https:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme-ov-file#pytorch-19-%E7%89%88%E6%9C%AC%E5%89%8D-dataloader-%E4%B8%AD%E7%9A%84%E9%9A%90%E8%97%8F-bug\"><\/a><\/h4>\n\n\n\n<p>\u5177\u4f53\u7ec6\u8282\u53ef\u89c1&nbsp;<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/523239005\">\u53ef\u80fd 95%\u7684\u4eba\u8fd8\u5728\u72af\u7684 PyTorch \u9519\u8bef &#8211; serendipity \u7684\u6587\u7ae0 &#8211; \u77e5\u4e4e<\/a><\/p>\n\n\n\n<p>\u89e3\u51b3\u65b9\u6cd5\u53ef\u53c2\u8003&nbsp;<a href=\"https:\/\/pytorch.org\/docs\/stable\/notes\/randomness.html#dataloader\">\u6587\u6863<\/a>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def seed_worker(worker_id):\n    worker_seed = torch.initial_seed() % 2**32\n    numpy.random.seed(worker_seed)\n    random.seed(worker_seed)\n\nDataLoader(..., worker_init_fn=seed_worker)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u81ea\uff1ahttps:\/\/github.com\/lartpang\/PyTorchTricks?tab=readme &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2024\/08\/06\/pytorch-fast\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">PyTorch \u63d0\u901f<\/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\/16666"}],"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=16666"}],"version-history":[{"count":4,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/16666\/revisions"}],"predecessor-version":[{"id":16670,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/16666\/revisions\/16670"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=16666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=16666"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=16666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}