PyTorch 2.8镜像代码实例:torch.cuda.is_available()验证与多GPU识别脚本

张开发
2026/4/14 5:19:18 15 分钟阅读

分享文章

PyTorch 2.8镜像代码实例:torch.cuda.is_available()验证与多GPU识别脚本
PyTorch 2.8镜像代码实例torch.cuda.is_available()验证与多GPU识别脚本1. 镜像环境概述PyTorch 2.8深度学习镜像是一个经过深度优化的通用训练和推理环境专为高性能计算任务设计。这个镜像基于RTX 4090D 24GB显卡和CUDA 12.4进行了特别优化确保您能够充分利用硬件性能。核心配置亮点GPURTX 4090D 24GB显存CUDA版本12.4驱动版本550.90.07CPU10核心内存120GB存储系统盘50GB 数据盘40GB2. 环境验证基础脚本2.1 基础GPU可用性检查最简单的验证方式是使用PyTorch内置的torch.cuda模块进行检查。以下是一个完整的Python脚本示例import torch def check_gpu_availability(): # 检查PyTorch版本 print(fPyTorch版本: {torch.__version__}) # 检查CUDA是否可用 cuda_available torch.cuda.is_available() print(fCUDA可用: {是 if cuda_available else 否}) if cuda_available: # 获取GPU数量 gpu_count torch.cuda.device_count() print(f检测到的GPU数量: {gpu_count}) # 显示每个GPU的信息 for i in range(gpu_count): print(f\nGPU {i}信息:) print(f名称: {torch.cuda.get_device_name(i)}) print(f计算能力: {torch.cuda.get_device_capability(i)}) print(f总显存: {torch.cuda.get_device_properties(i).total_memory/1024**3:.2f} GB) else: print(警告: 未检测到可用的CUDA设备) if __name__ __main__: check_gpu_availability()运行这个脚本您将看到类似以下的输出PyTorch版本: 2.8.0 CUDA可用: 是 检测到的GPU数量: 1 GPU 0信息: 名称: NVIDIA GeForce RTX 4090D 计算能力: (8, 9) 总显存: 24.00 GB2.2 命令行快速验证如果您只需要快速检查而不想保存脚本可以直接在终端运行以下命令python -c import torch; print(PyTorch版本:, torch.__version__); print(CUDA可用:, torch.cuda.is_available()); print(GPU数量:, torch.cuda.device_count())3. 高级GPU信息获取3.1 获取详细GPU信息以下脚本可以获取更详细的GPU信息包括当前显存使用情况import torch def get_detailed_gpu_info(): if not torch.cuda.is_available(): print(没有可用的CUDA设备) return device_count torch.cuda.device_count() print(f系统中有 {device_count} 个GPU设备\n) for idx in range(device_count): print(f GPU {idx} ) props torch.cuda.get_device_properties(idx) # 基础信息 print(f设备名称: {props.name}) print(f计算能力: {props.major}.{props.minor}) print(f总显存: {props.total_memory/1024**3:.2f} GB) # 当前使用情况 print(f当前显存使用: {torch.cuda.memory_allocated(idx)/1024**3:.2f} GB) print(f最大显存使用: {torch.cuda.max_memory_allocated(idx)/1024**3:.2f} GB) print(f缓存显存: {torch.cuda.memory_reserved(idx)/1024**3:.2f} GB) # 其他信息 print(f多处理器数量: {props.multi_processor_count}) print(f时钟频率: {props.clock_rate/1000:.2f} MHz) print() if __name__ __main__: get_detailed_gpu_info()3.2 多GPU并行环境检查如果您的工作站配置了多个GPU以下脚本可以帮助您验证多GPU并行环境import torch import time def test_multi_gpu(): if not torch.cuda.is_available(): print(CUDA不可用) return device_count torch.cuda.device_count() print(f检测到 {device_count} 个GPU设备) # 测试每个GPU的性能 for i in range(device_count): print(f\n测试 GPU {i} ({torch.cuda.get_device_name(i)})...) # 创建一个大张量测试计算能力 start_time time.time() x torch.randn(10000, 10000, devicefcuda:{i}) y torch.randn(10000, 10000, devicefcuda:{i}) z x y # 矩阵乘法 elapsed time.time() - start_time print(f10000x10000矩阵乘法耗时: {elapsed:.4f}秒) print(f当前显存使用: {torch.cuda.memory_allocated(i)/1024**3:.2f} GB) # 清理 del x, y, z torch.cuda.empty_cache() if __name__ __main__: test_multi_gpu()4. 常见问题排查4.1 CUDA不可用时的检查步骤如果torch.cuda.is_available()返回False可以按照以下步骤排查检查PyTorch版本与CUDA版本匹配import torch print(torch.__version__) # 应该显示2.8.x print(torch.version.cuda) # 应该显示12.4验证系统CUDA工具包nvcc --version检查GPU驱动nvidia-smi确认PyTorch是GPU版本import torch print(torch.cuda.is_available()) # 应该为True4.2 性能优化建议为了充分利用RTX 4090D的性能建议启用Tensor Cores:torch.backends.cuda.matmul.allow_tf32 True torch.backends.cudnn.allow_tf32 True使用混合精度训练:scaler torch.cuda.amp.GradScaler()启用cudnn基准测试:torch.backends.cudnn.benchmark True5. 总结本文介绍了在PyTorch 2.8镜像中验证GPU可用性和获取GPU信息的多种方法。从基础的torch.cuda.is_available()检查到详细的多GPU性能测试这些脚本将帮助您快速评估和验证深度学习环境。关键要点回顾使用torch.cuda.is_available()快速验证CUDA可用性torch.cuda.device_count()获取GPU数量torch.cuda.get_device_properties()获取详细硬件信息多GPU环境下可以并行测试每个设备的性能遇到问题时按照系统化步骤排查对于RTX 4090D用户特别推荐启用Tensor Core和混合精度训练以充分发挥硬件性能。PyTorch 2.8针对这一代GPU做了特别优化能够提供出色的计算性能。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。

更多文章