在 CentOS 上运行 PyTorch 出现“闪退”(程序直接崩溃、无报错或只报一句 Segmentation fault / Aborted),通常不是代码问题,而是环境/依赖/硬件不匹配导致的。下面按最常见原因 → 排查方法 → 解决方式说明。
PyTorch 官方 wheel 包通常要求:
现象
import torch 就退Segmentation fault (core dumped)
检查
ldd --version
gcc --version
解决
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch
比如在 无 GPU / 驱动不匹配 的机器上装了 cu118
现象
torch.cuda.is_available() 直接崩检查
nvidia-smi
nvcc --version
解决
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
CentOS 上常见 libgomp 冲突
现象
double free / Aborted解决
export OMP_NUM_THREADS=1
或修改:
export MKL_SERVICE_FORCE_INTEL=1
PyTorch 不支持:
检查
python --version
检查
free -h
import torch
print(torch.__version__)
print(torch.cuda.is_available())
如果这里就闪退 → 环境/依赖问题
python -X faulthandler test.py
或:
gdb --args python test.py
run
bt
CentOS 上 conda 是最稳方案
conda create -n torch python=3.9
conda activate torch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
| CentOS | 建议 |
|---|---|
| CentOS 6 | ❌ 基本不支持 |
| CentOS 7 | ✅ conda 可用 |
| CentOS Stream 8/9 | ✅ 正常 |
你可以直接贴:
我可以 精确定位是哪一种闪退原因。