【Python】pytorch,CUDA是否可用,查看显卡显存剩余容量

CUDA可用,共有 1 个GPU设备可用。

当前使用的GPU设备索引:0

当前使用的GPU设备名称:NVIDIA T1000

GPU显存总量:4.00 GB

已使用的GPU显存:0.00 GB

剩余GPU显存:4.00 GB

PyTorch版本:1.10.1+cu102

python 复制代码
import torch

# 检查CUDA是否可用
cuda_available = torch.cuda.is_available()

if cuda_available:
    # 获取GPU设备数量
    num_gpu = torch.cuda.device_count()

    # 获取当前使用的GPU索引
    current_gpu_index = torch.cuda.current_device()

    # 获取当前GPU的名称
    current_gpu_name = torch.cuda.get_device_name(current_gpu_index)

    # 获取GPU显存的总量和已使用量
    total_memory = torch.cuda.get_device_properties(current_gpu_index).total_memory / (1024 ** 3)  # 显存总量(GB)
    used_memory = torch.cuda.memory_allocated(current_gpu_index) / (1024 ** 3)  # 已使用显存(GB)
    free_memory = total_memory - used_memory  # 剩余显存(GB)

    print(f"CUDA可用,共有 {num_gpu} 个GPU设备可用。")
    print(f"当前使用的GPU设备索引:{current_gpu_index}")
    print(f"当前使用的GPU设备名称:{current_gpu_name}")
    print(f"GPU显存总量:{total_memory:.2f} GB")
    print(f"已使用的GPU显存:{used_memory:.2f} GB")
    print(f"剩余GPU显存:{free_memory:.2f} GB")
else:
    print("CUDA不可用。")

# 检查PyTorch版本
print(f"PyTorch版本:{torch.__version__}")

windows先装显卡驱动,再装CUDA10.2,最后装了pytorch。

pip install torch1.10.1+cu102 torchvision0.13.1+cu102 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu102

相关推荐
跟橙姐学代码10 分钟前
Python 类的正确打开方式:从新手到进阶的第一步
前端·python·ipython
c8i10 分钟前
关于python中的变量中使用的下划线_总结
python
站大爷IP18 分钟前
用Flet打造跨平台文本编辑器:从零到一的Python实战指南
python
MC皮蛋侠客31 分钟前
使用python test测试http接口
开发语言·python·http
胡耀超1 小时前
5、Python-NumPy科学计算基础
开发语言·人工智能·python·深度学习·numpy
BIGSHU09232 小时前
java多线程场景3-并发处理和异步请求
java·开发语言·python
Source.Liu2 小时前
【Python自动化】 21.3 Pandas Series 核心数据结构完全指南
python·自动化·pandas
Sirius Wu2 小时前
私有化部署Ragflow的预训练模型
人工智能·python·语言模型·火山引擎
Mr_sun.2 小时前
Day04_苍穹外卖——套餐管理(实战)
开发语言·python
南棱笑笑生2 小时前
20250910在荣品RD-RK3588-MID开发板的Android13系统下修改短按power按键的休眠/唤醒为关闭/打开背光
开发语言·python·rockchip