安装 torch-tensorrt 时,torch 被升级到 2.9.1,但 torchaudio 和 torchvision 仍需要 2.5.1+cu121,导致版本冲突。
# 1. 卸载所有冲突的包
pip uninstall -y torch torchvision torchaudio torch-tensorrt
# 2. 安装匹配的 PyTorch 版本(CUDA 12.1)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# 3. 安装 torch-tensorrt(会自动匹配 PyTorch 版本)
pip install torch-tensorrt
直接运行:
fix_torch_dependencies.bat
如果 torch-tensorrt 安装有问题,可以只安装 TensorRT 核心库:
# 卸载 torch-tensorrt
pip uninstall -y torch-tensorrt
# 只安装 TensorRT(用于 ONNX → TensorRT 方案)
pip install nvidia-tensorrt
然后使用 convert_to_tensorrt.py 脚本(ONNX 转换方案)。
运行以下命令验证:
python -c "import torch; print(f'PyTorch: {torch.__version__}')"
python -c "import torchvision; print(f'TorchVision: {torchvision.__version__}')"
python -c "import torchaudio; print(f'TorchAudio: {torchaudio.__version__}')"
python -c "import torch_tensorrt; print('Torch-TensorRT: OK')"
所有版本应该匹配,且没有错误。
nvidia-smi
确保 PyTorch 的 CUDA 版本与系统 CUDA 版本匹配。
# 完全清理
pip uninstall -y torch torchvision torchaudio torch-tensorrt tensorrt
# 重新安装
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
pip install torch-tensorrt
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
pip install torch-tensorrt
torch、torchvision、torchaudio 必须版本匹配如果 torch-tensorrt 安装一直有问题,可以使用:
ONNX → TensorRT 方案(更稳定)
nvidia-tensorrttorch-tensorrtconvert_to_tensorrt.py 脚本只使用 PyTorch FP16(当前方案)