{"id":948564,"date":"2024-12-27T00:04:45","date_gmt":"2024-12-26T16:04:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948564.html"},"modified":"2024-12-27T00:04:47","modified_gmt":"2024-12-26T16:04:47","slug":"python%e5%a6%82%e4%bd%95%e9%aa%8c%e8%af%81gpu","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/948564.html","title":{"rendered":"python\u5982\u4f55\u9a8c\u8bc1gpu"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083858\/b4cebc09-3b09-4be7-8a07-4d9e09cff950.webp\" alt=\"python\u5982\u4f55\u9a8c\u8bc1gpu\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u9a8c\u8bc1GPU\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5e93\u5982TensorFlow\u3001PyTorch\u3001CUDA\u7b49\u5de5\u5177\uff0c\u68c0\u67e5GPU\u662f\u5426\u88ab\u68c0\u6d4b\u5230\u3001\u67e5\u770bGPU\u7684\u8be6\u7ec6\u4fe1\u606f\u3001\u6d4b\u8bd5GPU\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/strong>\u5176\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7TensorFlow\u6216PyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u6765\u9a8c\u8bc1GPU\u7684\u53ef\u7528\u6027\u548c\u6027\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528TENSORFLOW\u9a8c\u8bc1GPU<\/p>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u548c\u9a8c\u8bc1GPU\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5GPU\u662f\u5426\u53ef\u7528<\/li>\n<\/ol>\n<p><p>\u5728TensorFlow\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>tf.config.list_physical_devices(&#39;GPU&#39;)<\/code> \u6765\u68c0\u67e5\u662f\u5426\u68c0\u6d4b\u5230GPU\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>gpus = tf.config.list_physical_devices(&#39;GPU&#39;)<\/p>\n<p>if gpus:<\/p>\n<p>    print(&quot;GPU is av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lable&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;GPU is not available&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u51fa\u663e\u793aGPU\u53ef\u7528\uff0c\u90a3\u4e48\u60a8\u7684TensorFlow\u5df2\u7ecf\u6210\u529f\u68c0\u6d4b\u5230\u60a8\u7684GPU\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u67e5\u770bGPU\u8be6\u7ec6\u4fe1\u606f<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7TensorFlow\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u66f4\u591a\u5173\u4e8eGPU\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u8bbe\u5907\u540d\u79f0\u548c\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for gpu in gpus:<\/p>\n<p>    print(&quot;Name:&quot;, gpu.name, &quot;  Type:&quot;, gpu.device_type)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u6240\u6709\u53ef\u7528GPU\u7684\u540d\u79f0\u548c\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PYTORCH\u9a8c\u8bc1GPU<\/p>\n<\/p>\n<p><p>PyTorch\u662f\u53e6\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u4e5f\u63d0\u4f9b\u4e86\u68c0\u67e5\u548c\u4f7f\u7528GPU\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68c0\u67e5GPU\u662f\u5426\u53ef\u7528<\/li>\n<\/ol>\n<p><p>\u5728PyTorch\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>torch.cuda.is_available()<\/code> \u6765\u9a8c\u8bc1GPU\u7684\u53ef\u7528\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>if torch.cuda.is_available():<\/p>\n<p>    print(&quot;GPU is available&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;GPU is not available&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51faGPU\u7684\u53ef\u7528\u72b6\u6001\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6GPU\u7684\u6570\u91cf\u548c\u540d\u79f0<\/li>\n<\/ol>\n<p><p>PyTorch\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u6709\u5173\u53ef\u7528GPU\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if torch.cuda.is_available():<\/p>\n<p>    print(f&quot;Number of GPUs available: {torch.cuda.device_count()}&quot;)<\/p>\n<p>    for i in range(torch.cuda.device_count()):<\/p>\n<p>        print(f&quot;Device {i}: {torch.cuda.get_device_name(i)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5217\u51fa\u6bcf\u4e2a\u53ef\u7528GPU\u7684\u6570\u91cf\u548c\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528CUDA\u9a8c\u8bc1GPU<\/p>\n<\/p>\n<p><p>CUDA\u662fNVIDIA\u63d0\u4f9b\u7684\u5e76\u884c\u8ba1\u7b97\u5e73\u53f0\u548c\u7f16\u7a0b\u6a21\u578b\uff0c\u5e38\u7528\u4e8eGPU\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5CUDA\u5de5\u5177\u5305<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5CUDA\u5de5\u5177\u5305\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7NVIDIA\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528nvidia-smi\u547d\u4ee4\u884c\u5de5\u5177<\/li>\n<\/ol>\n<p><p>\u5728\u7ec8\u7aef\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>nvidia-smi<\/code> \u547d\u4ee4\u6765\u83b7\u53d6\u5173\u4e8eGPU\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u4f9bGPU\u7684\u4f7f\u7528\u60c5\u51b5\u3001\u6e29\u5ea6\u3001\u98ce\u6247\u901f\u5ea6\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">nvidia-smi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u663e\u793a\u6709\u5173\u6240\u6709\u53ef\u7528GPU\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6d4b\u8bd5GPU\u7684\u8ba1\u7b97\u80fd\u529b<\/p>\n<\/p>\n<p><p>\u9a8c\u8bc1GPU\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u662f\u6d4b\u8bd5\u5176\u8ba1\u7b97\u80fd\u529b\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u7b80\u5355\u7684\u8ba1\u7b97\u4efb\u52a1\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528TensorFlow\u6d4b\u8bd5<\/li>\n<\/ol>\n<p><p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u7b80\u5355\u7684\u77e9\u9635\u4e58\u6cd5\u6765\u6d4b\u8bd5TensorFlow\u4e2d\u7684GPU\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_size = 10000<\/p>\n<p>a = tf.random.normal([matrix_size, matrix_size])<\/p>\n<p>b = tf.random.normal([matrix_size, matrix_size])<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u4f7f\u7528GPU\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>c = tf.matmul(a, b)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;Time taken on GPU: &quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528PyTorch\u6d4b\u8bd5<\/li>\n<\/ol>\n<p><p>\u540c\u6837\u5730\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528PyTorch\u8fdb\u884c\u7c7b\u4f3c\u7684\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_size = 10000<\/p>\n<p>a = torch.randn(matrix_size, matrix_size).cuda()<\/p>\n<p>b = torch.randn(matrix_size, matrix_size).cuda()<\/p>\n<h2><strong>\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u4f7f\u7528GPU\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>c = torch.mm(a, b)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<p>print(&quot;Time taken on GPU: &quot;, end_time - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6d4b\u8bd5\uff0c\u60a8\u53ef\u4ee5\u9a8c\u8bc1GPU\u7684\u8ba1\u7b97\u80fd\u529b\uff0c\u5e76\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u80fd\u591f\u5728GPU\u4e0a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528GPU\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<ol>\n<li>GPU\u672a\u88ab\u68c0\u6d4b\u5230<\/li>\n<\/ol>\n<p><p>\u5982\u679cTensorFlow\u6216PyTorch\u672a\u68c0\u6d4b\u5230GPU\uff0c\u786e\u4fdd\u60a8\u5df2\u6b63\u786e\u5b89\u88c5CUDA\u548ccuDNN\uff0c\u5e76\u4e14\u60a8\u7684\u73af\u5883\u53d8\u91cf\u5df2\u6b63\u786e\u914d\u7f6e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u9a71\u52a8\u7a0b\u5e8f\u95ee\u9898<\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u60a8\u7684NVIDIA\u9a71\u52a8\u7a0b\u5e8f\u662f\u6700\u65b0\u7684\uff0c\u56e0\u4e3a\u65e7\u7248\u672c\u53ef\u80fd\u4e0e\u65b0\u7248\u672c\u7684CUDA\u6216\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e0d\u517c\u5bb9\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5185\u5b58\u4e0d\u8db3<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u60a8\u7684GPU\u5185\u5b58\u4e0d\u8db3\uff0c\u5c1d\u8bd5\u51cf\u5c11\u6a21\u578b\u7684\u6279\u91cf\u5927\u5c0f\u6216\u4f7f\u7528\u6a21\u578b\u538b\u7f29\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u9a8c\u8bc1\u548c\u4f7f\u7528GPU\u6765\u52a0\u901fPython\u4e2d\u7684\u8ba1\u7b97\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u901a\u8fc7TensorFlow\u3001PyTorch\u8fd8\u662fCUDA\u5de5\u5177\u5305\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u90fd\u80fd\u5e2e\u52a9\u60a8\u786e\u4fddGPU\u7684\u6b63\u786e\u914d\u7f6e\u548c\u5229\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u6d4b\u6211\u7684Python\u73af\u5883\u662f\u5426\u652f\u6301GPU\uff1f<\/strong><br \/>\u8981\u786e\u8ba4\u60a8\u7684Python\u73af\u5883\u662f\u5426\u652f\u6301GPU\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5CUDA\u548ccuDNN\u6765\u68c0\u67e5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528TensorFlow\u6216PyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u8fd0\u884c\u7b80\u5355\u7684\u4ee3\u7801\u6765\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528TensorFlow\u65f6\uff0c\u53ef\u4ee5\u8fd0\u884c<code>tf.test.is_gpu_available()<\/code>\u6765\u67e5\u770b\u662f\u5426\u6210\u529f\u8bc6\u522bGPU\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528GPU\u65f6\u6211\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528GPU\u8fdb\u884c\u8ba1\u7b97\u65f6\uff0c\u786e\u4fdd\u60a8\u7684\u4ee3\u7801\u4f18\u5316\uff0c\u4ee5\u5145\u5206\u5229\u7528GPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002\u9700\u8981\u6ce8\u610f\u5185\u5b58\u7ba1\u7406\uff0c\u786e\u4fdd\u6570\u636e\u80fd\u591f\u6709\u6548\u5730\u5728CPU\u548cGPU\u4e4b\u95f4\u4f20\u8f93\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u4e0d\u540cGPU\u7684\u8ba1\u7b97\u80fd\u529b\u548c\u5185\u5b58\u9650\u5236\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u907f\u514d\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u7684GPU\u65e0\u6cd5\u88abPython\u8bc6\u522b\uff0c\u8be5\u600e\u4e48\u529e\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u7684GPU\u65e0\u6cd5\u88abPython\u8bc6\u522b\uff0c\u9996\u5148\u68c0\u67e5\u9a71\u52a8\u7a0b\u5e8f\u662f\u5426\u5df2\u6b63\u786e\u5b89\u88c5\u5e76\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u786e\u4fdd\u60a8\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e0eGPU\u7684\u517c\u5bb9\u6027\u3002\u6b64\u5916\uff0c\u68c0\u67e5CUDA\u548ccuDNN\u7684\u7248\u672c\u662f\u5426\u4e0e\u6846\u67b6\u5339\u914d\u3002\u82e5\u95ee\u9898\u4ecd\u7136\u5b58\u5728\uff0c\u8003\u8651\u67e5\u9605\u76f8\u5173\u8bba\u575b\u6216\u6587\u6863\u83b7\u53d6\u7279\u5b9a\u9519\u8bef\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u9a8c\u8bc1GPU\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5e93\u5982TensorFlow\u3001PyTorch\u3001CUDA\u7b49\u5de5\u5177\uff0c\u68c0\u67e5 [&hellip;]","protected":false},"author":3,"featured_media":948570,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948564"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=948564"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948564\/revisions"}],"predecessor-version":[{"id":948571,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948564\/revisions\/948571"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948570"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}