Skip to content

UV

安装

Linux 和 macOS 使用 curl 安装:

bash
curl -LsSf https://astral.sh/uv/install.sh | sh

Windows 使用 powershell 安装:

powershell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

也可以使用 pip 安装:

bash
pip install uv

配置

在 Windows 上 UV 默认缓存在 C 盘,这样会导致安装的包无法使用硬链接,影响效率并无法使用 UV 节省空间的优势,可以将缓存目录设置到工作用的盘符上。例如配置环境变量,将缓存目录设置到 D 盘:

ini
UV_CACHE_DIR=D:\.uv-cache

用例

安装 PyTorch:

toml
dependencies = [
    "torch==2.7.0+cu128; sys_platform != 'darwin'",
    "torch==2.7.0+cpu; sys_platform == 'darwin'",
    "torchaudio==2.7.0+cu128; sys_platform != 'darwin'",
    "torchaudio==2.7.0+cpu; sys_platform == 'darwin'",
    "torchvision==0.22.0+cu128; sys_platform != 'darwin'",
    "torchvision==0.22.0+cpu; sys_platform == 'darwin'",
    "flash-attn==2.7.4.post1; sys_platform != 'darwin'",
]

配置 [tool.uv.sources]

toml
[tool.uv.sources]
torch = [
    { index = "pytorch-cpu", marker = "sys_platform == 'darwin'" },
    { index = "pytorch-cu128", marker = "sys_platform != 'darwin'" },
]
torchaudio = [
    { index = "pytorch-cpu", marker = "sys_platform == 'darwin'" },
    { index = "pytorch-cu128", marker = "sys_platform != 'darwin'" },
]
torchvision = [
    { index = "pytorch-cpu", marker = "sys_platform == 'darwin'" },
    { index = "pytorch-cu128", marker = "sys_platform != 'darwin'" },
]
flash-attn = [
    { url = "https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.4.post1/flash_attn-2.7.4.post1+cu128torch2.7.0cxx11abiFALSE-cp312-cp312-win_amd64.whl", marker = "sys_platform == 'win32'" },
    { url = "https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.4.post1/flash_attn-2.7.4.post1+cu128torch2.7.0cxx11abiFALSE-cp312-cp312-linux_x86_64.whl", marker = "sys_platform == 'linux'" },
]

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

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