在 CentOS 上测试(验证)PyTorch 是否可用,通常分几步来做。下面按最常见场景说明(CPU / GPU 都覆盖)。
python3 --version
python3
或(如果你用虚拟环境):
source your_env/bin/activate
python
在 Python 交互环境中输入:
import torch
print(torch.__version__)
✅ 能正常输出版本号(如 2.1.0),说明安装成功。
x = torch.rand(3, 3)
y = torch.rand(3, 3)
print(x + y)
如果没有报错,说明 CPU 版 PyTorch 正常。
nvidia-smi
✅ 能看到显卡信息说明驱动已装。
import torch
print(torch.cuda.is_available())
True → CUDA 可用False → 要么装的是 CPU 版,要么驱动/CUDA 不匹配if torch.cuda.is_available():
x = torch.rand(3, 3).cuda()
y = torch.rand(3, 3).cuda()
print(x + y)
print(torch.cuda.get_device_name(0))
libstdc++.so.6: version 'GLIBCXX_3.4.21' not foundCentOS 7 常见,升级 GCC:
sudo yum install centos-release-scl
sudo yum install devtoolset-9
scl enable devtoolset-9 bash
torch.cuda.is_available() 为 False检查:
nvcc --version
用离线 wheel 安装:
pip install torch-xxx.whl
保存为 test_torch.py:
import torch
print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())
if torch.cuda.is_available():
print("GPU:", torch.cuda.get_device_name(0))
x = torch.rand(3, 3).cuda()
print(x)
else:
x = torch.rand(3, 3)
print(x)
运行:
python3 test_torch.py
如果你愿意,可以告诉我:
我可以给你精确安装 + 测试命令。