指定显卡的三种方式

第一种 os.environ"CUDA_VISIBLE_DEVICES" = "1"

python 复制代码
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import torch

print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))

结果

物理 GPU 1 被映射为逻辑 GPU 0

python 复制代码
1
0
NVIDIA GeForce RTX 4090

第二种 命令行 CUDA_VISIBLE_DEVICES=1 python cuda_test.py

python 复制代码
import torch

print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))

结果

物理 GPU 1 被映射为逻辑 GPU 0

python 复制代码
1
0
NVIDIA GeForce RTX 4090

第三种 torch.cuda.set_device(1)

python 复制代码
import torch
torch.cuda.set_device(1)

print(torch.cuda.device_count())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))
print(torch.cuda.get_device_name(1))

结果

设置任意cuda:1设备为当前cuda设备

python 复制代码
2
1
NVIDIA GeForce RTX 4090
NVIDIA GeForce RTX 4090
相关推荐
火山引擎开发者社区6 小时前
VeOps CLI:你的火山引擎可观测排障助手
人工智能
To_OC8 小时前
调用远程MCP,手搓一个能查酒店、自动打开浏览器的 Agent
人工智能·agent·mcp
启雀AI8 小时前
生物医疗行业如何建设合规、安全、可复用的知识库?
人工智能·安全·软件构建·知识图谱·知识库
x-cmd8 小时前
Mac 涨价后,本地 AI 还能千元入门吗?
linux·人工智能·macos·ai·agent·amd·本地ai入门
To_OC8 小时前
跑通第一个 MCP Server 后,我终于搞懂它到底解决了什么问题
人工智能·agent·mcp
楷哥爱开发8 小时前
如何使用 Claude Fable 5 进行网页抓取?2026最新实战教程
大数据·网络·人工智能
YMWM_8 小时前
lerobot中use_relative_actions=True需要重新计算meta/stats.json等信息
人工智能·深度学习·lerobot
触底反弹8 小时前
🔥 DeepSeek 560 万美金干翻 OpenAI?一文讲透「蒸馏」的来龙去脉
人工智能
私人珍藏库8 小时前
[Android] 会计快题库 -财会职称考试刷题学习
android·人工智能·学习·app·软件·多功能
Sirius Wu9 小时前
OpenClaw Skill:Matplotlib 可视化技能 + 沙箱双层隔离完整详解
服务器·网络·人工智能·安全·ai·架构·aigc