Skip to content

Choosing specific Pytorch version when configuring accelerators with optional dependencies #9502

Closed
@eginhard

Description

@eginhard

On Ubuntu with uv 0.5.5 with the following pyproject.toml, adapted from https://docs.astral.sh/uv/guides/integration/pytorch/#configuring-accelerators-with-optional-dependencies (torchvision removed for simplicity and minimum Pytorch version lowered):

[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = []

[project.optional-dependencies]
cpu = [
  "torch>=2.0",
]
cu124 = [
  "torch>=2.0",
]

[tool.uv]
conflicts = [
  [
    { extra = "cpu" },
    { extra = "cu124" },
  ],
]

[tool.uv.sources]
torch = [
  { index = "pytorch-cpu", extra = "cpu", marker = "platform_system != 'Darwin'" },
  { index = "pytorch-cu124", extra = "cu124" },
]

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

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

My goal is to test specific CPU Pytorch versions in CI and I'm not sure which of the below commands should work. The following works, but counter-intuitively installs the GPU version:

$ uv run --extra cpu --with torch==2.2.2 python -c "import torch; print(torch.__version__)"
2.2.2+cu121

This doesn't work:

$ uv run --extra cpu --with torch==2.2.2+cpu python -c "import torch; print(torch.__version__)"
  × No solution found when resolving `--with` dependencies:
  ╰─▶ Because there is no version of torch==2.2.2+cpu and you require torch==2.2.2+cpu, we
      can conclude that your requirements are unsatisfiable.

This fails when there is no lockfile present yet:

$ uv run --with torch==2.2.2 --index https://download.pytorch.org/whl/cpu python -c "import torch; print(torch.__version__)"
error: Found duplicate package `torch==2.5.1+cpu @ registry+https://download.pytorch.org/whl/cpu`

But it works if I sync first:

$ uv sync
$ uv run --with torch==2.2.2 --index https://download.pytorch.org/whl/cpu python -c "import torch; print(torch.__version__)"
2.2.2+cpu

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsking for clarification or support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions