{"id":28488,"date":"2025-03-13T12:36:56","date_gmt":"2025-03-13T04:36:56","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28488"},"modified":"2025-03-13T12:36:56","modified_gmt":"2025-03-13T04:36:56","slug":"ollama-zidingyizai","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/en\/ollama-zidingyizai\/","title":{"rendered":"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c"},"content":{"rendered":"<h2>Windows<\/h2>\n<p>\u4ee5\u4e0b\u4ee5 Windows \u7cfb\u7edf\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c Ollama\u3002<\/p>\n<p><a href=\"https:\/\/www.kdjingpai.com\/de\/ollama\/\">Ollama<\/a> \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f7f\u7528 CPU \u8fdb\u884c\u63a8\u7406\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u5feb\u7684\u63a8\u7406\u901f\u5ea6\uff0c\u53ef\u4ee5\u914d\u7f6e Ollama \u4f7f\u7528\u7684 GPU\u3002\u672c\u6559\u7a0b\u5c06\u6307\u5bfc\u5982\u4f55\u5728 Windows \u7cfb\u7edf\u4e0a\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u542f\u7528 GPU \u52a0\u901f\u3002<\/p>\n<h3>\u524d\u63d0\u6761\u4ef6<\/h3>\n<ul>\n<li>\u7535\u8111\u6709 NVIDIA \u663e\u5361\u3002<\/li>\n<li>\u5df2\u5b89\u88c5 NVIDIA \u663e\u5361\u9a71\u52a8\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u00a0<code>nvidia-smi<\/code>\u00a0\u6765\u68c0\u67e5\u9a71\u52a8\u7a0b\u5e8f\u662f\u5426\u5b89\u88c5\u3002<\/li>\n<li>\u5df2\u5b89\u88c5 CUDA \u5de5\u5177\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u00a0<code>nvcc --version<\/code>\u00a0\u6765\u68c0\u67e5 CUDA \u662f\u5426\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p>Tip<\/p>\n<p>\u5173\u4e8e NVIDIA \u663e\u5361\u9a71\u52a8\u7a0b\u5e8f\u548c CUDA \u5de5\u5177\u5305\u7684\u5b89\u88c5\uff0c\u53ef\u4ee5\u81ea\u884c\u641c\u7d22\u76f8\u5173\u6559\u7a0b\uff0c\u672c\u6587\u4e0d\u518d\u8d58\u8ff0\u3002 \u5982\u679c\u4f60\u7684\u7535\u8111\u6ee1\u8db3\u4e0a\u8ff0\u524d\u63d0\u6761\u4ef6\uff0c\u5728\u4f7f\u7528 Ollama \u65f6\uff0c\u662f\u9ed8\u8ba4\u4f7f\u7528 GPU \u52a0\u901f\u7684\u3002\u5982\u679c\u4f60\u60f3\u6307\u5b9a\u4f7f\u7528\u7279\u5b9a\u7684 GPU\uff0c\u53ef\u4ee5\u6309\u7167\u4e0b\u9762\u7684\u6b65\u9aa4\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<h3>\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<ol>\n<li><strong>\u6253\u5f00\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e<\/strong>\n<ul>\n<li>\u5728 Windows \u641c\u7d22\u680f\u4e2d\u8f93\u5165 &#8220;\u73af\u5883\u53d8\u91cf&#8221;\uff0c\u7136\u540e\u9009\u62e9 &#8220;\u7f16\u8f91\u7cfb\u7edf\u73af\u5883\u53d8\u91cf&#8221;\u3002<\/li>\n<li>\u5728\u5f39\u51fa\u7684 &#8220;\u7cfb\u7edf\u5c5e\u6027&#8221; \u7a97\u53e3\u4e2d\uff0c\u70b9\u51fb &#8220;\u9ad8\u7ea7&#8221; \u9009\u9879\u5361\uff0c\u7136\u540e\u70b9\u51fb &#8220;\u73af\u5883\u53d8\u91cf&#8221; \u6309\u94ae\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u521b\u5efa OLLAMA_GPU_LAYER \u53d8\u91cf<\/strong>\n<ul>\n<li>\u5728 &#8220;\u7cfb\u7edf\u53d8\u91cf&#8221; \u533a\u57df\uff0c\u70b9\u51fb &#8220;\u65b0\u5efa&#8221; \u6309\u94ae\u3002<\/li>\n<li>\u5728 &#8220;\u65b0\u5efa\u7cfb\u7edf\u53d8\u91cf&#8221; \u5bf9\u8bdd\u6846\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u4fe1\u606f\uff1a\n<ul>\n<li><strong>\u53d8\u91cf\u540d\uff1a<\/strong>\u00a0<code>OLLAMA_GPU_LAYER<\/code><\/li>\n<li><strong>\u53d8\u91cf\u503c\uff1a<\/strong>\u00a0<code>cuda<\/code>\u00a0(\u8fd9\u5c06\u544a\u8bc9 Ollama \u4f7f\u7528 CUDA \u8fdb\u884c GPU \u52a0\u901f)<\/li>\n<\/ul>\n<\/li>\n<li>\u70b9\u51fb &#8220;\u786e\u5b9a&#8221; \u4fdd\u5b58\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28491\" title=\"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/069a085229373e8.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c-1\" width=\"2113\" height=\"1078\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/069a085229373e8.png 2113w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/069a085229373e8-768x392.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/069a085229373e8-1536x784.png 1536w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/069a085229373e8-2048x1045.png 2048w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/069a085229373e8-18x9.png 18w\" sizes=\"auto, (max-width: 2113px) 100vw, 2113px\" \/><\/li>\n<li><strong>(\u53ef\u9009) \u6307\u5b9a\u4f7f\u7528\u7684 GPU<\/strong>\n<ul>\n<li>\u5982\u679c\u4f60\u7684\u7cfb\u7edf\u6709\u591a\u4e2a GPU\uff0c\u5e76\u4e14\u4f60\u60f3\u6307\u5b9a Ollama \u4f7f\u7528\u7279\u5b9a\u7684 GPU\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u00a0<code>CUDA_VISIBLE_DEVICES<\/code>\u00a0\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<li><strong>\u67e5\u627e GPU \u7684 UUID\uff1a<\/strong>\u00a0\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528 UUID \u800c\u4e0d\u662f\u7f16\u53f7\uff0c\u56e0\u4e3a\u7f16\u53f7\u53ef\u80fd\u4f1a\u56e0\u4e3a\u9a71\u52a8\u66f4\u65b0\u6216\u7cfb\u7edf\u91cd\u542f\u800c\u53d1\u751f\u53d8\u5316\u3002\n<ul>\n<li>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216 PowerShell\u3002<\/li>\n<li>\u8fd0\u884c\u547d\u4ee4\uff1a<code>nvidia-smi -L<\/code><\/li>\n<li>\u5728\u8f93\u51fa\u4e2d\uff0c\u627e\u5230\u60f3\u8981\u4f7f\u7528\u7684 GPU \u7684 &#8220;UUID&#8221; \u503c\u3002 \u4f8b\u5982\uff1a<code>GPU 00000000:01:00.0<\/code>\u00a0\u4e0b\u9762\u7684\u00a0<code>UUID : GPU-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/code>\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28489\" title=\"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c-2\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2606eb43896bdff.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c-2\" width=\"1468\" height=\"81\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2606eb43896bdff.png 1468w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2606eb43896bdff-768x42.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/2606eb43896bdff-18x1.png 18w\" sizes=\"auto, (max-width: 1468px) 100vw, 1468px\" \/><\/li>\n<li><strong>\u521b\u5efa CUDA_VISIBLE_DEVICES \u53d8\u91cf\uff1a<\/strong>\n<ul>\n<li>\u5728 &#8220;\u7cfb\u7edf\u53d8\u91cf&#8221; \u533a\u57df\uff0c\u70b9\u51fb &#8220;\u65b0\u5efa&#8221; \u6309\u94ae\u3002<\/li>\n<li>\u5728 &#8220;\u65b0\u5efa\u7cfb\u7edf\u53d8\u91cf&#8221; \u5bf9\u8bdd\u6846\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u4fe1\u606f\uff1a\n<ul>\n<li><strong>\u53d8\u91cf\u540d\uff1a<\/strong>\u00a0<code>CUDA_VISIBLE_DEVICES<\/code><\/li>\n<li><strong>\u53d8\u91cf\u503c\uff1a<\/strong>\u00a0\u627e\u5230\u7684 GPU \u7684 UUID\u3002 \u4f8b\u5982\uff1a<code>GPU-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u70b9\u51fb &#8220;\u786e\u5b9a&#8221; \u4fdd\u5b58\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u91cd\u8981\uff1a<\/strong>\u00a0\u4e3a\u4e86\u4f7f\u73af\u5883\u53d8\u91cf\u751f\u6548\uff0c\u00a0<strong>\u91cd\u65b0\u542f\u52a8 Ollama \u8fd0\u884c\u7684\u7ec8\u7aef\u6216\u5e94\u7528\u7a0b\u5e8f<\/strong>\u00a0\u3002<\/p>\n<p><strong>\u9a8c\u8bc1 GPU \u52a0\u901f\u662f\u5426\u751f\u6548\uff1a<\/strong><\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u3002<\/li>\n<li>\u8fd0\u884c Ollama\u3002 \u4f8b\u5982\uff1a\u00a0<code>ollama run <a href=\"https:\/\/www.kdjingpai.com\/de\/deepseek-r1nenglixiang\/\">deepseek-r1<\/a>:1.5b<\/code><\/li>\n<li>\u65b0\u5f00\u4e00\u4e2a\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\uff0c\u4f7f\u7528\u00a0<code>ollama ps<\/code>\u00a0\u547d\u4ee4\u67e5\u770b Ollama \u8fd0\u884c\u7684\u8fdb\u7a0b\u3002<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28490\" title=\"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c-3\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/142c6422dd0c1b5.png\" alt=\"Ollama \u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c-3\" width=\"1272\" height=\"115\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/142c6422dd0c1b5.png 1272w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/142c6422dd0c1b5-768x69.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/142c6422dd0c1b5-18x2.png 18w\" sizes=\"auto, (max-width: 1272px) 100vw, 1272px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>Linux<\/h2>\n<p>\u4ee5\u4e0b\u4ee5 Linux \u7cfb\u7edf\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c Ollama\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u00a0<code>ollama_gpu_selector.sh<\/code>\u00a0\u811a\u672c\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre><code>#!\/bin\/bash\r\n\r\n# Validate input\r\nvalidate_input(){\r\nif[[! $1 =~^[0-4](,[0-4])*$ ]];then\r\necho \"Error: Invalid input. Please enter numbers between 0 and 4, separated by commas.\"\r\nexit1\r\nfi\r\n}\r\n\r\n# Update the service file with CUDA_VISIBLE_DEVICES values\r\nupdate_service(){\r\n# Check if CUDA_VISIBLE_DEVICES environment variable exists in the service file\r\nif grep -q '^Environment=\"CUDA_VISIBLE_DEVICES='\/etc\/systemd\/system\/ollama.service;then\r\n# Update the existing CUDA_VISIBLE_DEVICES values\r\nsudo sed -i 's\/^Environment=\"CUDA_VISIBLE_DEVICES=.*\/Environment=\"CUDA_VISIBLE_DEVICES='\"$1\"'\"\/'\/etc\/systemd\/system\/ollama.service\r\nelse\r\n# Add a new CUDA_VISIBLE_DEVICES environment variable\r\nsudo sed -i '\/\\[Service\\]\/a Environment=\"CUDA_VISIBLE_DEVICES='\"$1\"'\"'\/etc\/systemd\/system\/ollama.service\r\nfi\r\n\r\n# Reload and restart the systemd service\r\nsudo systemctl daemon-reload\r\nsudo systemctl restart ollama.service\r\necho \"Service updated and restarted with CUDA_VISIBLE_DEVICES=$1\"\r\n}\r\n\r\n# Check if arguments are passed\r\nif[\"$#\"-eq 0];then\r\n# Prompt user for CUDA_VISIBLE_DEVICES values if no arguments are passed\r\nread -p \"Enter CUDA_VISIBLE_DEVICES values (0-4, comma-separated): \" cuda_values\r\nvalidate_input \"$cuda_values\"\r\nupdate_service \"$cuda_values\"\r\nelse\r\n# Use arguments as CUDA_VISIBLE_DEVICES values\r\ncuda_values=\"$1\"\r\nvalidate_input \"$cuda_values\"\r\nupdate_service \"$cuda_values\"\r\nfi\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u4e3a\u811a\u672c\u6587\u4ef6\u6dfb\u52a0\u6267\u884c\u6743\u9650<\/li>\n<\/ol>\n<pre><code>chmod +x ollama_gpu_selector.sh\r\nsudo .\/ollama_gpu_selector.sh\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u6309\u7167\u63d0\u793a\u8f93\u5165 GPU \u7f16\u53f7\uff0c\u5373\u53ef\u6307\u5b9a Ollama \u4f7f\u7528\u7684 GPU\u3002\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u591a\u4e2a GPU \u7f16\u53f7\uff0c\u4f8b\u5982\uff1a<code>0,1,2<\/code>\u3002<\/p>\n<ol start=\"3\">\n<li>\u91cd\u542f Ollama \u670d\u52a1<\/li>\n<\/ol>\n<pre><code>cat \/etc\/systemd\/system\/ollama.service\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u6307\u4ee4\u540e\uff0c\u67e5\u770b Ollama \u670d\u52a1\u6587\u4ef6\uff0c\u786e\u8ba4\u00a0<code>CUDA_VISIBLE_DEVICES<\/code>\u00a0\u73af\u5883\u53d8\u91cf\u5df2\u7ecf\u66f4\u65b0\u3002<\/p>\n<p>\u5982\u679c\u5df2\u7ecf\u66f4\u65b0\uff0c\u5219\u4f1a\u65b0\u589e\u4f8b\u5982\u00a0<code>Environment=\"CUDA_VISIBLE_DEVICES=0,1,2\"<\/code>\u00a0\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Windows \u4ee5\u4e0b\u4ee5 Windows \u7cfb\u7edf\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u81ea\u5b9a\u4e49\u5728 GPU \u4e2d\u8fd0\u884c Ollama\u3002 Ollama \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f7f\u7528 CPU \u8fdb\u884c\u63a8\u7406\u3002\u4e3a\u4e86\u83b7\u5f97\u66f4\u5feb\u7684\u63a8\u7406\u901f\u5ea6\uff0c\u53ef\u4ee5\u914d\u7f6e Ollama \u4f7f\u7528\u7684 GPU\u3002\u672c\u6559\u7a0b\u5c06\u6307\u5bfc\u5982\u4f55\u5728 Wind&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182],"tags":[],"class_list":["post-28488","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/28488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/comments?post=28488"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/posts\/28488\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/media?parent=28488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/categories?post=28488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/en\/wp-json\/wp\/v2\/tags?post=28488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}