1. 问题现象
在 WSL2 Ubuntu 的 conda 环境里安装 PyTorch(GPU/CPU 都可能遇到),执行:
python -c "import torch; print(torch.__version__)"
报错类似:
ImportError: .../torch/lib/libtorch_cpu.so: undefined symbol: iJIT_NotifyEvent
或者修复后继续报:
ImportError: .../libtorch_cpu.so: undefined symbol: iJIT_IsProfilingActive
导致 PyTorch 无法导入,后续编译自定义算子/训练全部卡死。
2. 原因分析(为什么会这样)
libtorch_cpu.so 在加载时依赖 Intel ITT/JIT profiling 相关符号(例如 iJIT_NotifyEvent、iJIT_IsProfilingActive)。
正常情况下,这些符号由 libittnotify.so(Intel ITT/VTune 相关运行库)提供。
但在某些 WSL2 + conda + PyTorch 组合下:
-
conda 环境内缺少
libittnotify.so -
或者依赖链冲突导致该库未被安装/未被正确加载
于是动态链接器找不到符号,import torch 直接失败。
说明:这些符号主要用于 VTune/性能分析。日常训练推理并不需要它“真的工作”,只要符号存在即可。
3. 尝试过但容易踩坑的方案(可选阅读)
很多人会尝试降级 MKL(如 mkl=2024.0.0),但在 pytorch/nvidia/defaults 与 conda-forge 混用时,可能触发 llvm-openmp 版本冲突,解不出来:
-
conda-forge 的 mkl 可能要求更高版本
llvm-openmp -
但 pytorch 2.4.0/torchaudio 2.4.0 可能要求
llvm-openmp <16
最终 conda 直接报 UnsatisfiableError。
因此本文给出一个不依赖 solver、命中率极高的方案:自建 stub 的 libittnotify.so + LD_PRELOAD。
4. 最终解决方案(已验证可用):自建 libittnotify.so + LD_PRELOAD
4.1 安装编译工具 gcc
sudo apt update sudo apt install -y gcc
4.2 激活 conda 环境
conda activate MOTIP2
环境名按你自己的修改(例如 MOTIP / MOTIP2)。
4.3 在环境里写一个 ITT stub(空实现,提供缺失符号)
cat > "$CONDA_PREFIX/lib/itt_stub.c" <<'EOF' #include <stdint.h> #ifdef __cplusplus extern "C" { #endif // No-op stubs for Intel ITT / JIT profiling symbols. // They satisfy the dynamic linker so PyTorch can load. // Profiling functionality is NOT provided (fine for normal training/inference). void iJIT_NotifyEvent(void) {} void iJIT_NotifyEventW(void) {} void iJIT_NotifyEventStr(void) {} void iJIT_NotifyEventEx(void) {} int iJIT_IsProfilingActive(void) { return 0; } uint32_t iJIT_GetNewMethodID(void) { return 1; } uint32_t iJIT_GetNewMethodIDEx(void) { return 1; } #ifdef __cplusplus } #endif EOF
4.4 编译生成 libittnotify.so
gcc -shared -fPIC -O2 \ -o "$CONDA_PREFIX/lib/libittnotify.so" \ "$CONDA_PREFIX/lib/itt_stub.c"
检查文件是否生成:
ls -l "$CONDA_PREFIX/lib/libittnotify.so"
4.5 临时验证(只对当前命令生效)
LD_PRELOAD="$CONDA_PREFIX/lib/libittnotify.so" \ python -c "import torch; print(torch.__version__, torch.cuda.is_available())"
如果能输出版本号且不再报错,说明修复成功。
5. 永久生效:每次 conda activate 自动 preload(推荐)
不想每次都手打 LD_PRELOAD=... 的话,可以添加 conda 的 activate/deactivate 脚本。
5.1 创建脚本目录
mkdir -p "$CONDA_PREFIX/etc/conda/activate.d" "$CONDA_PREFIX/etc/conda/deactivate.d"
5.2 激活时自动设置 LD_PRELOAD
cat > "$CONDA_PREFIX/etc/conda/activate.d/itt_preload.sh" <<'EOF' export _OLD_LD_PRELOAD="${LD_PRELOAD:-}" export LD_PRELOAD="$CONDA_PREFIX/lib/libittnotify.so${LD_PRELOAD:+:$LD_PRELOAD}" EOF
5.3 退出环境时恢复 LD_PRELOAD
cat > "$CONDA_PREFIX/etc/conda/deactivate.d/itt_preload.sh" <<'EOF' export LD_PRELOAD="$_OLD_LD_PRELOAD" unset _OLD_LD_PRELOAD EOF
5.4 重新激活验证
conda deactivate conda activate MOTIP2 python -c "import torch; print(torch.__version__, torch.cuda.is_available())"

1万+

被折叠的 条评论
为什么被折叠?



