{"id":1821,"date":"2025-02-17T16:11:44","date_gmt":"2025-02-17T08:11:44","guid":{"rendered":"https:\/\/www.forillusion.com\/?p=1821"},"modified":"2025-02-17T16:11:45","modified_gmt":"2025-02-17T08:11:45","slug":"4-6-use-gpu","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/4-6-use-gpu\/","title":{"rendered":"4.6 GPU\u8ba1\u7b97"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8ba1\u7b97\u8bbe\u5907<\/h2>\n\n\n\n<p>PyTorch\u53ef\u4ee5\u6307\u5b9a\u7528\u6765\u5b58\u50a8\u548c\u8ba1\u7b97\u7684\u8bbe\u5907\uff0c\u5982\u4f7f\u7528\u5185\u5b58\u7684CPU\u6216\u8005\u4f7f\u7528\u663e\u5b58\u7684GPU\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPyTorch\u4f1a\u5c06\u6570\u636e\u521b\u5efa\u5728\u5185\u5b58\uff0c\u7136\u540e\u5229\u7528CPU\u6765\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u7528<code>torch.cuda.is_available()<\/code>\u67e5\u770bGPU\u662f\u5426\u53ef\u7528:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\nfrom torch import nn\n\ntorch.cuda.is_available() # \u8f93\u51fa True<\/code><\/pre>\n\n\n\n<p>\u67e5\u770bGPU\u6570\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>torch.cuda.device_count() # \u8f93\u51fa 1<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u5f53\u524dGPU\u7d22\u5f15\u53f7\uff0c\u7d22\u5f15\u53f7\u4ece0\u5f00\u59cb\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>torch.cuda.current_device() # \u8f93\u51fa 0<\/code><\/pre>\n\n\n\n<p>\u6839\u636e\u7d22\u5f15\u53f7\u67e5\u770bGPU\u540d\u5b57:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>torch.cuda.get_device_name(0) # \u8f93\u51fa 'GeForce GTX 1050'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><code>Tensor<\/code>\u7684GPU\u8ba1\u7b97<\/h2>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>Tensor<\/code>\u4f1a\u88ab\u5b58\u5728\u5185\u5b58\u4e0a\u3002\u56e0\u6b64\uff0c\u4e4b\u524d\u6bcf\u6b21\u6253\u5370<code>Tensor<\/code>\u7684\u65f6\u5019\u770b\u4e0d\u5230GPU\u76f8\u5173\u6807\u8bc6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = torch.tensor(&#91;1, 2, 3])\nprint(x)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;1, 2, 3])<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<code>.cuda()<\/code>\u53ef\u4ee5\u5c06CPU\u4e0a\u7684<code>Tensor<\/code>\u8f6c\u6362\uff08\u590d\u5236\uff09\u5230GPU\u4e0a\u3002\u5982\u679c\u6709\u591a\u5757GPU\uff0c\u7528<code>.cuda(i)<\/code>\u6765\u8868\u793a\u7b2c $i$ \u5757GPU\u53ca\u76f8\u5e94\u7684\u663e\u5b58\uff08$i$\u4ece0\u5f00\u59cb\uff09\u4e14<code>cuda(0)<\/code>\u548c<code>cuda()<\/code>\u7b49\u4ef7\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = x.cuda(0)\nprint(x)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;1, 2, 3], device='cuda:0')<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7<code>Tensor<\/code>\u7684<code>device<\/code>\u5c5e\u6027\u6765\u67e5\u770b\u8be5<code>Tensor<\/code>\u6240\u5728\u7684\u8bbe\u5907\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(x.device)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>device(type='cuda', index=0)<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u76f4\u63a5\u5728\u521b\u5efa\u7684\u65f6\u5019\u5c31\u6307\u5b9a\u8bbe\u5907\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\nx = torch.tensor(&#91;1, 2, 3], device=device)\n# or\nx = torch.tensor(&#91;1, 2, 3]).to(device)\nprint(x)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;1, 2, 3], device='cuda:0')<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5bf9\u5728GPU\u4e0a\u7684\u6570\u636e\u8fdb\u884c\u8fd0\u7b97\uff0c\u90a3\u4e48\u7ed3\u679c\u8fd8\u662f\u5b58\u653e\u5728GPU\u4e0a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y = x**2\nprint(y)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;1, 4, 9], device='cuda:0')<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b58\u50a8\u5728\u4e0d\u540c\u4f4d\u7f6e\u4e2d\u7684\u6570\u636e\u662f\u4e0d\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8ba1\u7b97\u7684\u3002\u5373\u5b58\u653e\u5728CPU\u4e0a\u7684\u6570\u636e\u4e0d\u53ef\u4ee5\u76f4\u63a5\u4e0e\u5b58\u653e\u5728GPU\u4e0a\u7684\u6570\u636e\u8fdb\u884c\u8fd0\u7b97\uff0c\u4f4d\u4e8e\u4e0d\u540cGPU\u4e0a\u7684\u6570\u636e\u4e5f\u662f\u4e0d\u80fd\u76f4\u63a5\u8fdb\u884c\u8ba1\u7b97\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>z = y + x.cpu()<\/code><\/pre>\n\n\n\n<p>\u4f1a\u62a5\u9519:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.LongTensor for argument #3 'other'<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u578b\u7684GPU\u8ba1\u7b97<\/h2>\n\n\n\n<p>\u540c<code>Tensor<\/code>\u7c7b\u4f3c\uff0cPyTorch\u6a21\u578b\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>.cuda<\/code>\u8f6c\u6362\u5230GPU\u4e0a\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u6a21\u578b\u7684\u53c2\u6570\u7684<code>device<\/code>\u5c5e\u6027\u6765\u67e5\u770b\u5b58\u653e\u6a21\u578b\u7684\u8bbe\u5907\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>net = nn.Linear(3, 1)\nprint(list(net.parameters())&#91;0].device)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>device(type='cpu')<\/code><\/pre>\n\n\n\n<p>\u53ef\u89c1\u6a21\u578b\u5728CPU\u4e0a\uff0c\u5c06\u5176\u8f6c\u6362\u5230GPU\u4e0a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>net.cuda()\nprint(list(net.parameters())&#91;0].device)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>device(type='cuda', index=0)<\/code><\/pre>\n\n\n\n<p>\u540c\u6837\u7684\uff0c\u9700\u8981\u4fdd\u8bc1\u6a21\u578b\u8f93\u5165\u7684<code>Tensor<\/code>\u548c\u6a21\u578b\u90fd\u5728\u540c\u4e00\u8bbe\u5907\u4e0a\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = torch.rand(2,3).cuda()\nnet(x)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;&#91;-0.5800],\n        &#91;-0.2995]], device='cuda:0', grad_fn=&lt;ThAddmmBackward&gt;)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba1\u7b97\u8bbe\u5907 PyTorch\u53ef\u4ee5\u6307\u5b9a\u7528\u6765\u5b58\u50a8\u548c\u8ba1\u7b97\u7684\u8bbe\u5907\uff0c\u5982\u4f7f\u7528\u5185\u5b58\u7684CPU\u6216\u8005\u4f7f\u7528\u663e\u5b58\u7684GPU\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPyTorch\u4f1a\u5c06\u6570\u636e\u521b &#8230;<\/p>","protected":false},"author":1,"featured_media":1822,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,3],"tags":[45,44,12,22],"class_list":["post-1821","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-46","category-3","tag-45","tag-44","tag-12","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1821"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1821\/revisions"}],"predecessor-version":[{"id":1823,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1821\/revisions\/1823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1822"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}