指定显卡的三种方式

第一种 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
相关推荐
人工智能AI技术几秒前
游戏智能体基础:NPC、策略、对话、剧情生成
人工智能
MoonBit月兔2 分钟前
MoonBit 作为重大成果亮相广东省人工智能应用对接大会,展示 AI 原生编程语言最新进展
开发语言·人工智能·moonbit
FrontAI3 分钟前
深入浅出 LangGraph —— 第9章:流式输出:实时响应用户
人工智能·langchain·ai agent·langgraph
从零开始学习人工智能4 分钟前
量化评估RAG效果:LLM答案自动评估脚本全解析
人工智能·多模态·rag
白熊1888 分钟前
【大模型Agent】基于LangGraph搭建 多轮对话客户支持机器人 项目示例
人工智能·大模型·llm·agent·langgraph
love在水一方13 分钟前
【Voxel-SLAM】Data Structures / 数据结构文档(二)
数据结构·人工智能·机器学习
ConardLi15 分钟前
开源我的 GPT-Image2 生图 Skill,附大量玩法指南
前端·人工智能·后端
QYR_1117 分钟前
2026卷绕式扣式电池产业洞察:智能制造如何重塑微型储能格局?
人工智能·市场调研
白熊18817 分钟前
【大模型Agent】LangGraph 深度科普:为智能体而生的“有状态”编排框架
人工智能·langchain·agent·langgraph
数智工坊20 分钟前
【SIoU Loss论文阅读】:引入角度感知的框回归损失,让检测收敛更快更准
论文阅读·人工智能·深度学习·机器学习·数据挖掘·回归·cnn