问题背景

Conda、Docker 等技术的确非常强大,它们可以实现“一次开发,到处部署”,在操作系统层面保证兼容性。然而,当遇到 硬件不兼容 时,这类工具就无能为力,需要手动干预。

例如,我想运行别人提供的深度学习项目,并使用 Conda 管理环境。原项目的 env.yaml 中指定了:

1
2
dependencies:
pytorch=1.10.2=py3.7_cuda11.3_cudnn8.2.0_0

然而我的显卡是 RTX 5070 Ti(50 系列),直接conda create -f 后运行项目会报错:

1
2
3
NVIDIA GeForce RTX 5070 Ti with CUDA capability sm_120 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 sm_80 sm_86 compute_37.
If you want to use the NVIDIA GeForce RTX 5070 Ti GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

这种情况下,只有两个选择:要么换机器,要么重新构建依赖环境。

更麻烦的是,支持 50 系显卡的 PyTorch 版本至少要求 Python 3.10,也就是说小改 env.yaml 根本无法解决问题(其他库版本过老又没法支持 Python 3.10了),只能做较大幅度的调整。

全升级方案

可以先试一试不管库的版本,直接升级到 Python3.10.

依然先构建环境

1
conda create -f env.yaml

然后导出pip依赖

1
pip freeze > requirements.txt

清理所有固定版本和本地/远程来源,只保留包名,使用python脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
import re

input_file = "requirements.txt"
output_file = "requirements.txt"

cleaned = []

with open(input_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue

# 去掉 @ 后面的内容
pkg = re.split(r"\s*@\s*", line)[0]

# 去掉 ==、>=、<=、~=、>、< 等版本信息
pkg = re.split(r"[><=~!]+", pkg)[0]

pkg = pkg.strip()
if pkg:
cleaned.append(pkg)

with open(output_file, "w", encoding="utf-8") as f:
f.write("\n".join(cleaned))

print(f"✔ 清理完成!已生成: {output_file}")

然后另建虚拟环境pip install -r requirements.txt即可。

注意某些gpu版本的库应该从requirements.txt中删去,然后根据官网教程自行安装,可参考

这个方法简单粗暴,然而如果项目中有些库的用法过时了,这个方法后续也很麻烦,要么手动降低报错库的版本,要么改代码。

吐槽

配了块 5070Ti 显卡,虽然打游戏很爽,但是配 AI 环境真是处处碰壁。2025 年 10 月之前,PyTorch 的 stable 版本甚至完全不支持 50 系显卡,只能用 nightly 版本——那种每晚自动构建的“滚动快照”。记得我 10 月 3 日安装的虚拟环境还能用,结果 10 月 4 日玩另一个项目装完虚拟环境用就直接报错。现在11月终于有stable版本了,但是一些老项目还是不能直接跑,真是牢完了。