{"id":4565,"date":"2024-08-23T14:06:08","date_gmt":"2024-08-23T06:06:08","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=4565"},"modified":"2024-08-23T14:06:08","modified_gmt":"2024-08-23T06:06:08","slug":"%e5%9c%a8-wsl2%e5%a6%82%e4%bd%95%e6%a3%80%e9%aa%8c%e6%89%80%e6%9c%89%e6%98%be%e5%8d%a1%e9%83%bd%e5%8f%af%e4%bb%a5%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=4565","title":{"rendered":"\u5728 WSL2\u5982\u4f55\u68c0\u9a8c\u6240\u6709\u663e\u5361\u90fd\u53ef\u4ee5\u4f7f\u7528"},"content":{"rendered":"\n<p>\u5728 WSL2 \u4e2d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u68c0\u9a8c\u6240\u6709\u663e\u5361\u662f\u5426\u90fd\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>nvidia-smi<\/code> \u547d\u4ee4<\/h3>\n\n\n\n<p>\u5728 WSL2 \u73af\u5883\u4e2d\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >nvidia-smi\n<\/pre><\/div>\n\n\n\n<p>\u6b64\u547d\u4ee4\u4f1a\u663e\u793a\u6240\u6709\u53ef\u7528\u7684 GPU \u4ee5\u53ca\u5b83\u4eec\u7684\u72b6\u6001\u3002\u5982\u679c\u4f60\u770b\u5230\u6240\u6709 8 \u5f20 RTX 4090 \u90fd\u5217\u51fa\u6765\u4e86\uff0c\u5e76\u4e14\u663e\u793a\u6b63\u5e38\u72b6\u6001\uff08\u4f8b\u5982\uff1a\u7a7a\u95f2\u6216\u6b63\u5728\u4f7f\u7528\uff09\uff0c\u90a3\u4e48\u8bf4\u660e\u8fd9\u4e9b\u663e\u5361\u90fd\u53ef\u4ee5\u88ab WSL2 \u8bc6\u522b\u548c\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u8f93\u51fa\u5e94\u8be5\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >+-----------------------------------------------------------------------------+\n| NVIDIA-SMI 510.39.01    Driver Version: 510.39.01    CUDA Version: 11.6     |\n|-------------------------------+----------------------+----------------------+\n| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n| Fan  Temp  Perf  Pwr:Usage\/Cap|         Memory-Usage | GPU-Util  Compute M. |\n|                               |                      |             MIG M.  |\n|===============================+======================+======================|\n|   0  RTX 4090          On   | 00000000:01:00.0 Off |                  N\/A |\n| 30%   54C    P2   300W \/ 450W |    1100MiB \/ 24576MiB |     23%      Default |\n+-------------------------------+----------------------+----------------------+\n|   1  RTX 4090          On   | 00000000:02:00.0 Off |                  N\/A |\n| 30%   54C    P2   300W \/ 450W |    1100MiB \/ 24576MiB |     23%      Default |\n+-------------------------------+----------------------+----------------------+\n...\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>CUDA<\/code> \u8fdb\u884c\u68c0\u67e5<\/h3>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684 CUDA \u7a0b\u5e8f\u6765\u786e\u8ba4\u662f\u5426\u80fd\u591f\u4f7f\u7528\u6240\u6709 GPU\u3002\u9996\u5148\u786e\u4fdd CUDA \u5de5\u5177\u5305\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u53ef\u7528\u7684 GPU \u6570\u91cf\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >python -c \"import torch; print(torch.cuda.device_count())\"\n<\/pre><\/div>\n\n\n\n<p>\u5982\u679c\u8f93\u51fa\u663e\u793a\u7684\u6570\u503c\u548c\u4f60\u7684\u663e\u5361\u6570\u91cf\u4e00\u81f4\uff0c\u8bf4\u660e\u6240\u6709\u663e\u5361\u90fd\u53ef\u4ee5\u88ab CUDA \u8bc6\u522b\u548c\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u4f60\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u68c0\u67e5\u6bcf\u5f20\u663e\u5361\u662f\u5426\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >python -c \"import torch; [print(torch.cuda.get_device_name(i)) for i in range(torch.cuda.device_count())]\"\n<\/pre><\/div>\n\n\n\n<p>\u6b64\u547d\u4ee4\u4f1a\u5217\u51fa\u6bcf\u5f20 GPU \u7684\u540d\u79f0\uff0c\u786e\u4fdd\u5b83\u4eec\u663e\u5361\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u8fd0\u884c\u8d1f\u8f7d\u6d4b\u8bd5<\/h3>\n\n\n\n<p>\u8981\u786e\u8ba4\u6240\u6709\u663e\u5361\u5728\u8ba1\u7b97\u4efb\u52a1\u4e0b\u90fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ef\u4ee5\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u811a\u672c\uff0c\u5e76\u76d1\u63a7\u663e\u5361\u7684\u5229\u7528\u7387\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528 PyTorch \u8fdb\u884c\u6d4b\u8bd5\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >import torch\nfrom torch import nn, optim\n\n# \u6d4b\u8bd5\u662f\u5426\u80fd\u591f\u5728\u6bcf\u4e2a GPU \u4e0a\u8fd0\u884c\u7b80\u5355\u7684\u8ba1\u7b97\nfor i in range(torch.cuda.device_count()):\n    device = torch.device(f'cuda:{i}')\n    print(f'Testing GPU {i} ({torch.cuda.get_device_name(i)})')\n    x = torch.rand((1000, 1000), device=device)\n    y = torch.mm(x, x)\n    print(f'GPU {i} computation success.')\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u68c0\u67e5\u591a GPU \u914d\u7f6e<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff08\u5982 TensorFlow \u6216 PyTorch\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u68c0\u67e5\u591a GPU \u4f7f\u7528\u60c5\u51b5\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >python -c \"import torch; print(torch.cuda.device_count()); print(torch.cuda.is_available())\"\n<\/pre><\/div>\n\n\n\n<p>\u786e\u8ba4 <code>device_count()<\/code> \u8fd4\u56de\u7684\u6570\u91cf\u4e0e\u5b9e\u9645 GPU \u6570\u91cf\u4e00\u81f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5168\u9762\u5730\u9a8c\u8bc1\u6240\u6709 GPU \u662f\u5426\u5728 WSL2 \u4e2d\u6b63\u5e38\u5de5\u4f5c\u3002\u6700\u91cd\u8981\u7684\u662f\u4f7f\u7528 <code>nvidia-smi<\/code> \u548c CUDA \u76f8\u5173\u547d\u4ee4\u6765\u67e5\u770b\u663e\u5361\u7684\u72b6\u6001\u548c\u53ef\u7528\u6027\u3002\u5982\u679c\u67d0\u4e9b\u663e\u5361\u672a\u663e\u793a\u6216\u6709\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6392\u67e5\u9a71\u52a8\u6216\u914d\u7f6e\u95ee\u9898\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 WSL2 \u4e2d\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u68c0\u9a8c\u6240\u6709\u663e\u5361\u662f\u5426\u90fd\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\uff1a 1. \u4f7f\u7528 nvidia-smi \u547d\u4ee4 \u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[43],"tags":[],"class_list":["post-4565","post","type-post","status-publish","format-standard","hentry","category-infoarticle"],"views":3147,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4565"}],"version-history":[{"count":1,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4565\/revisions"}],"predecessor-version":[{"id":4566,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4565\/revisions\/4566"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}