使用 uv 配置依赖于 Pytorch 的 pyproject 并单独配置针对cpu或cuda版本.

Refrence:

ps . uv 官方真的太贴心了,这是喂饭级的,把各种cpu,cu11,cu12,intel gpu…的基础配置都写出来了。

我印象里面看到poetry用户在安装的时候考虑用指定whl路径的方法,但是存在很大局限性,因为 wh l是锁死 python 版本和系统的。

Installing a specific PyTorch build (f/e CPU-only) with Poetry

问题描述:

正常情况下,我们使用 uv add torch==2.1.0时,安装的是 cpu+cuda 版本的 torch:

xnne@xnne-PC:~/code/Auto_Caption_Generated_Offline$ uv add torch==2.1.0 torchaudio==2.1.0
⠴ nvidia-cusparse-cu12==12.1.0.106         ^C
To start, consider the following (default) configuration, which would be generated by running uv init --python 3.12 followed by uv add torch torchvision.
首先,请考虑以下(默认)配置,运行 uv init --python 3.12 后再运行 uv add torch torchvision 即可生成该配置。

In this case, PyTorch would be installed from PyPI, which hosts CPU-only wheels for Windows and macOS, and GPU-accelerated wheels on Linux (targeting CUDA 12.4):
在这种情况下,可以从 PyPI 安装 PyTorch,PyTorch 在 Windows 和 macOS 上只支持 CPU 驱动轮,在 Linux 上支持 GPU 加速驱动轮(针对 CUDA 12.4):
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
  "torch>=2.6.0",
  "torchvision>=0.21.0",
]

这有时候并不是我们想要的,比如我的 deepin 上面没有装 nvidia 的驱动,那么我下了3~4G的环境我压根用不到。

我尝试用uv add torch==2.1.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

xnne@xnne-PC:~/code/Auto_Caption_Generated_Offline$ uv add torch==2.1.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
 
Resolved 63 packages in 10.37s
      Built auto-caption-generate-offline @ fil
⠹ Preparing packages... (4/5)
torch      ------------------------------ 19.06 MiB/176.29 MiB

它确实下载到了 cpu 版本的torch,但是,在我尝试从我的远程仓库安装时,我碰到了问题:

xnne@xnne-PC:~/code/test/Auto_Caption_Generated_Offline$ uv pip install git+https://github.com/MrXnneHang/[email protected]
    Updated https://github.com/MrXnneHang/Auto_Caption_Generated_Offline (12065e01ec1dc11f8f224fbb132cfd1c18ec3ac1)
  × No solution found when resolving dependencies:
  ╰─▶ Because there is no version of torch==2.1.0+cpu and auto-caption-generate-offline==2.4.0 depends on torch==2.1.0+cpu, we can conclude that auto-caption-generate-offline==2.4.0 cannot be used.
      And because only auto-caption-generate-offline==2.4.0 is available and you require auto-caption-generate-offline, we can conclude that your requirements are unsatisfiable.

原因:

原因是 pytorch 上传的镜像并不在 pypi 的 index 上。

From a packaging perspective, PyTorch has a few uncommon characteristics:
从包装的角度来看,PyTorch 有几个不同寻常的特点:

Many PyTorch wheels are hosted on a dedicated index, rather than the Python Package Index (PyPI). As such, installing PyTorch often requires configuring a project to use the PyTorch index.
许多 PyTorch 轮子托管在专用索引上,而不是 Python 包索引 (PyPI)。因此,安装 PyTorch 通常需要将项目配置为使用 PyTorch 索引。

PyTorch produces distinct builds for each accelerator (e.g., CPU-only, CUDA). Since there's no standardized mechanism for specifying these accelerators when publishing or installing, PyTorch encodes them in the local version specifier. As such, PyTorch versions will often look like 2.5.1+cpu, 2.5.1+cu121, etc.
PyTorch 会为每种加速器(如纯 CPU、CUDA)生成不同的编译版本。由于在发布或安装时没有指定这些加速器的标准化机制,PyTorch 将它们编码在本地版本说明符中。因此,PyTorch 版本通常看起来像 2.5.1+cpu , 2.5.1+cu121 等。

Builds for different accelerators are published to different indexes. For example, the +cpu builds are published on https://download.pytorch.org/whl/cpu, while the +cu121 builds are published on https://download.pytorch.org/whl/cu121.
不同加速器的编译会发布到不同的索引中。例如, +cpu 版本发布在 https://download.pytorch.org/whl/cpu 上,而 +cu121 版本发布在 https://download.pytorch.org/whl/cu121 上。

解决:

最后我自己敲定的相关配置是这样的:

dependencies = [
    "funasr==1.2.4",
    "pyaml==25.1.0",
    "torch==2.1.0",
    "torchaudio==2.1.0",
]

[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true

[tool.uv.sources]
torch = [
  { index = "pytorch-cpu" },
]
torchaudio = [
  { index = "pytorch-cpu" },
]

之后我们 uv lock, 然后 push 上去。

ps: 如果在配置cuda版本的时候,我们应该考虑使用previous版本的cuda,比如使用11.8.而不是使用最新的12.x甚至13.x因为用户的驱动不会一直是最新的,而新的驱动是兼容旧的 cu 版本的。除非在性能上有非常高的提升,但是一般来说是没有太大区别的。

从 github 上安装:

最终成功安装了=-=。

xnne@xnne-PC:~/code/test/Auto_Caption_Generated_Offline$ uv venv -p 3.10 --seed
Using CPython 3.10.16
Creating virtual environment with seed packages at: .venv
 + pip==25.0.1
 + setuptools==75.8.2
 + wheel==0.45.1

xnne@xnne-PC:~/code/test/Auto_Caption_Generated_Offline$ uv pip install git+https://github.com/MrXnneHang/[email protected]
Resolved 63 packages in 5.85s
Prepared 2 packages in 11m 45s
...
 + torch==2.1.0+cpu
 + torch-complex==0.4.4
 + torchaudio==2.1.0+cpu
 + tqdm==4.67.1
...
...

不过在运行的时候 torch 的 numpy 和 funasr 的 numpy 出现了一点冲突:

xnne@xnne-PC:~/code/test/Auto_Caption_Generated_Offline$ uv run test-ACGO
      Built auto-caption-generate-offline @ file:///home/xnne/code/test/Auto_Caption_Generated_Offline
Uninstalled 1 package in 0.57ms
Installed 1 package in 0.95ms

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.1.3 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "/home/xnne/code/test/Auto_Caption_Generated_Offline/.venv/bin/test-ACGO", line 4, in <module>
    from uiya.test import main
  File "/home/xnne/code/test/Auto_Caption_Generated_Offline/src/uiya/test.py", line 1, in <module>
    import funasr

手动降级到1.26.4后解决。

uv add numpy==1.26.4
uv lock
xnne@xnne-PC:~/code/test$ uv venv -p 3.10 --seed 
Using CPython 3.10.16
Creating virtual environment with seed packages at: .venv
 + pip==25.0.1
 + setuptools==75.8.2
 + wheel==0.45.1
Activate with: source .venv/bin/activate

xnne@xnne-PC:~/code/test$ uv pip install git+https://github.com/MrXnneHang/[email protected]
Resolved 63 packages in 7.90s
Prepared 2 packages in 603ms
Installed 63 packages in 259ms
 + aliyun-python-sdk-core==2.16.0
 + aliyun-python-sdk-kms==2.16.5
 + antlr4-python3-runtime==4.9.3
 + audioread==3.0.1
 + auto-caption-generate-offline==2.4.0 (from git+https://github.com/MrXnneHang/Auto_Caption_Generated_Offline@5f03a04ebdbe4b7a1329302b551e58092e8af9ee)
 ...
 + torch==2.1.0+cpu
 + torch-complex==0.4.4
 + torchaudio==2.1.0+cpu
 + tqdm==4.67.1
 ...

xnne@xnne-PC:~/code/test$ uv run test-ACGO
funasr:1.2.4
torch:2.1.0+cpu
torchaudio:2.1.0+cpu

乌龙

中间还有一个小乌龙。就是我在项目目录下,运行uv pip installuv run,而我目录下存在pyproject.toml和uv.lock,那么运行的肯定不是我安装的那个,而是我根据我项目中 pyproject.toml 生成的版本。于是乎我也就陷入了一个没有更新代码,uv run一直报原来的bug的问题。git pull 后解决。

所以,如果git pull了,就不用uv pip install git+了。直接uv run即可。

如果之后 cuda 版本我遇到问题可能会再次补充。

7 个赞

太强了,大佬! :tieba_087:

1 个赞

我还是比较好奇,Python版本的镜像怎么配置的,国内没有找到合适的加速镜像。

也许:

dependencies = [
     "numpy==1.26.4",
]
[[tool.uv.index]]
name = "tsinghua"
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
explicit = true

[tool.uv.sources]
numpy = [
      { index = "tsinghua"},
]

因为实际上,换源也只是换一个index-url.


上面的有点问题。不能设置explicit=true.需要设置defualt=true

具体参见:

uv真的好用

1 个赞

这是包的源,不是Python版本的,官方默认走的是GitHub拉的release~index这个是包的环境变量,这个国内镜像还挺多的

python版本我倾向于用conda管理。

xnne@xnne-PC:~/code/test-uv-index$ conda env list
# conda environments:
#
base                     /home/xnne/miniconda3
10                       /home/xnne/miniconda3/envs/10

像这样。

因为 uv venv 的时候会先在当前环境下寻找可用的 python 解释器。

当我conda activate 10(存放python3.10的环境)

然后 uv venv -p 3.10 就直接从conda那边把python解释器借过来了,不需要额外下载。

但我安装环境的时候需要dectivate,不然会装到虚拟环境内。

我发现了一个问题, 在容器中(Ubuntu24)中,使用南京大学pytorch镜像下载torch, 会导致错误
以下是pyproject.toml文件

[project]
name = "test"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
    "numpy>=2.2.4",
    "torch>=2.6.0",
]
[[tool.uv.index]]
name = "bfsu"
url = "https://mirrors.bfsu.edu.cn/pypi/web/simple"
default = true

[[tool.uv.index]]
name = "pytorch"
url = "https://mirror.nju.edu.cn/pytorch/whl/cu126"

[[tool.uv.index]]
name = "nju"
url = "https://mirror.nju.edu.cn/pypi/web/simple"

[tool.uv.sources]
torch = { index = "pytorch" }
torchvision = { index = "pytorch" }
torchaudio = { index = "pytorch" }

运行命令uv add torch numpy之后, 检验torch是否安装完好:

root@610fb880e99e:~/test# uv run python
Python 3.10.16 (main, Mar 17 2025, 21:01:46) [Clang 20.1.0 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/test/.venv/lib/python3.10/site-packages/torch/__init__.py", line 405, in <module>
    from torch._C import *  # noqa: F403
ImportError: libcusparseLt.so.0: cannot open shared object file: No such file or directory
>>> torch.cuda.is_available()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'torch' is not defined
>>>

发现缺少cuda/cudnn相关依赖, 需要再到Nvdia去下载对应缺失的deb再安装

但是跟着官方仓库下载uv pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126, uv add torch, 发现会将相应的包也下载下来, 比如cudnn等, import torch的时候就没有任何问题, torch.cuda.is_available()也显示为True, 但是上面的情况并不会发生在Windows上(至少我的Windows环境使用NJU下载torch是正常的).
所以是Linux环境的话, 最好还是直接通过官方仓库下载torch, 以免出现各种类型的报错
或者说是我的操作哪里有问题吗, 有没有佬友有解决措施的

1 个赞

我正好要给我项目新建 cuda 分支。我用你的配置在winows上复现一下看看(我linux都没装 nvidia 驱动 :melting_face:,感觉会耗我电)

等等,只在 Linux 上报错 =-= .

我想想,问题不大。没有驱动导入个包验证一下还是可以的。

第一个建议是,对于复杂环境。

我们最好用 uv lockuv sync 来装环境,它会提前检查依赖。

比如对于你写的 pyproject.toml:

(pre-commmit) xnne@xnne-PC:~/code/Auto_Caption_Generated_Offline$ uv venv -p 3.10 
Using CPython 3.10.16
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
(pre-commmit) xnne@xnne-PC:~/code/Auto_Caption_Generated_Offline$ uv lock
  × No solution found when resolving dependencies for split (python_full_version >= '3.12'):
  ╰─▶ Because only numpy<=2.1.2 is available and your project depends on numpy>=2.2.4, we can conclude
      that your project's requirements are unsatisfiable.

      hint: `numpy` was found on https://mirror.nju.edu.cn/pytorch/whl/cu126, but not at the requested
      version (numpy>=2.2.4). A compatible version may be available on a subsequent index (e.g.,
      https://mirror.nju.edu.cn/pypi/web/simple). By default, uv will only consider versions that are
      published on the first index that contains a given package, to avoid dependency confusion attacks.
      If all indexes are equally trusted, use `--index-strategy unsafe-best-match` to consider all
      versions from all indexes, regardless of the order in which they were defined.

你直接 uv add 不带版本的 numpy 和 pytorch 是最新的。
但是,即使最新的 pytorch 也还没兼容最新的 numpy 。最高兼容2.12。lock检查直接报错。

(pre-commmit) xnne@xnne-PC:~/code/Auto_Caption_Generated_Offline$ uv lock
Resolved 11 packages in 1ms
(pre-commmit) xnne@xnne-PC:~/code/Auto_Caption_Generated_Offline$ uv sync
Resolved 11 packages in 1ms
error: Distribution `markupsafe==3.0.2 @ registry+https://mirror.nju.edu.cn/pytorch/whl/cu126` can't be installed because it doesn't have a source distribution or wheel for the current platform

hint: You're using CPython 3.10 (`cp310`), but `markupsafe` (v3.0.2) only has wheels with the following Python implementation tag: `cp313`

去掉 numpy 然后只留下 torch,依赖检查成功了

但是 2.6.0 的 pytorch 编译的时候只编译了 cp313 的版本。应该用3.13。

[project]
name = "Auto-Caption-Generate-Offline"
version = "2.4.0"
description = ""
readme = "README.md"
requires-python = ">=3.11,<3.12"
dependencies = [
    "funasr==1.2.6",
    "modelscope>=1.23.1",
    "numpy==1.26.4",
    "torch==2.2.0",
    "torchaudio==2.2.0",
    "tomli_w==1.2.0",
    "pydantic==2.10.6",
    "setuptools==76.0.0",
    "streamlit==1.43.2",
    "streamlit-antd-components==0.3.2",
    "pandas==2.2.3",
]

[tool.uv.workspace]
members = ["packages/*"]

[[tool.uv.index]]
name = "pytorch-cu118"
url = "https://mirror.nju.edu.cn/pytorch/whl/cu118"
explicit = true

[tool.uv.sources]
torch = [
  { index = "pytorch-cu118", marker = "sys_platform != 'Darwin'"  },
]
torchaudio = [
  { index = "pytorch-cu118", marker = "sys_platform != 'Darwin'" },
]

这是我项目 cuda 分支的配置。

用南京源它是可以下载到相关的依赖的。

但即使是官方源在安装个别版本的 pytorch 的时候也会存在问题。

比如:There is no available `triton` in windows but it required when I download torch==2.1.0+cu118 · Issue #12460 · astral-sh/uv · GitHub

另外对于 torch 版本的选择。

在项目里建议是选旧不选新,cuda version 也是选旧不选新。

除非是在开发深度学习框架,那才有必要跟进 nightly 以及源代码。不然的话最新版本的 torch 和很多库都有脱节。

mark一下!!!