astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
26.41k stars 769 forks source link

uv failing to create multi-platform env for torch #8930

Open sharmuz opened 1 week ago

sharmuz commented 1 week ago

uv version: 0.5.0 OS/platform: macOS Sonoma 14.7.1 (on a MBP 2020 Intel)

Hey folks - YAO pytorch issue :)

I'm attempting to create an env which will install a given torch and torchvision version from a specified index, depending on whether the OS is darwin/win/linux.

I'm going off of the info in #6523 and #8746.

Here's a minimal excerpt of my pyproject.toml:

[project]
name = "minimal"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = "==3.10.*"
dependencies = [
  "torch==1.13.1 ; platform_system == 'darwin'",
  "torch==1.13.1+cpu ; platform_system == 'win32'",
  "torch==1.13.1+cu116 ; platform_system == 'linux'",
  "torchvision==0.14.1 ; platform_system == 'darwin'",
  "torchvision==0.14.1+cpu ; platform_system == 'win32'",
  "torchvision==0.14.1+cu116 ; platform_system == 'linux'",
]

[tool.uv.sources]
torch = [
  { index = "torch-cpu", marker = "platform_system != 'linux'" },
  { index = "torch-cuda", marker = "platform_system == 'linux'" },
]
torchvision = [
  { index = "torch-cpu", marker = "platform_system != 'linux'" },
  { index = "torch-cuda", marker = "platform_system == 'linux'" },
]

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

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

uv sync runs without error but the generated uv.lock looks off:

[[package]]
name = "torch"
version = "1.13.1"
source = { registry = "https://download.pytorch.org/whl/cpu" }
resolution-markers = [
    "platform_system == 'darwin'",
]
dependencies = [
    { name = "typing-extensions", marker = "platform_system == 'darwin'" },
]

[[package]]
name = "torch"
version = "1.13.1"
source = { registry = "https://pypi.org/simple" }
resolution-markers = [
    "platform_system != 'darwin' and platform_system != 'linux' and platform_system != 'win32'",
]
dependencies = [
    { name = "typing-extensions", marker = "platform_system != 'darwin' and platform_system != 'linux' and platform_system != 'win32'" },
]
wheels = [
    { url = "https://files.pythonhosted.org/packages/81/58/431fd405855553af1a98091848cf97741302416b01462bbf9909d3c422b3/torch-1.13.1-cp310-cp310-manylinux1_x86_64.whl", hash = "sha256:fd12043868a34a8da7d490bf6db66991108b00ffbeecb034228bfcbbd4197143", size = 887450534 },
    { url = "https://files.pythonhosted.org/packages/2c/45/43233d36e8e7ec5588fa802bb098337ae73c314863190c68797287f2fbdd/torch-1.13.1-cp310-cp310-manylinux2014_aarch64.whl", hash = "sha256:d9fe785d375f2e26a5d5eba5de91f89e6a3be5d11efb497e76705fdf93fa3c2e", size = 60541101 },
    { url = "https://files.pythonhosted.org/packages/33/bd/e174e6737daba03f8eaa7c051b9971d361022eb37b86cbe5db0b08cab00e/torch-1.13.1-cp310-cp310-win_amd64.whl", hash = "sha256:98124598cdff4c287dbf50f53fb455f0c1e3a88022b39648102957f3445e9b76", size = 162597315 },
    { url = "https://files.pythonhosted.org/packages/82/d8/0547f8a22a0c8aeb7e7e5e321892f1dcf93ea021829a99f1a25f1f535871/torch-1.13.1-cp310-none-macosx_10_9_x86_64.whl", hash = "sha256:393a6273c832e047581063fb74335ff50b4c566217019cc6ace318cd79eb0566", size = 135290591 },
    { url = "https://files.pythonhosted.org/packages/24/45/61e41ef8a84e1d6200ff10b7cb87e23e211599ab62420396a363295f973c/torch-1.13.1-cp310-none-macosx_11_0_arm64.whl", hash = "sha256:0122806b111b949d21fa1a5f9764d1fd2fcc4a47cb7f8ff914204fd4fc752ed5", size = 53188434 },
]

[[package]]
name = "torch"
version = "1.13.1+cpu"
source = { registry = "https://download.pytorch.org/whl/cpu" }
resolution-markers = [
    "platform_system == 'win32'",
]
dependencies = [
    { name = "typing-extensions", marker = "platform_system == 'win32'" },
]
wheels = [
    { url = "https://download.pytorch.org/whl/cpu/torch-1.13.1%2Bcpu-cp310-cp310-linux_x86_64.whl", hash = "sha256:11692523b87c45b79ddfb5148b12a713d85235d399915490d94e079521f7e014" },
]

[[package]]
name = "torch"
version = "1.13.1+cu116"
source = { registry = "https://download.pytorch.org/whl/cu116" }
resolution-markers = [
    "platform_system == 'linux'",
]
dependencies = [
    { name = "typing-extensions", marker = "platform_system == 'linux'" },
]
wheels = [
    { url = "https://download.pytorch.org/whl/cu116/torch-1.13.1%2Bcu116-cp310-cp310-linux_x86_64.whl", hash = "sha256:51d5870cdf05b6208b1c739fe0ba511b977eca37f9507829675596acc11b6ca4" },
]

(equivalent output for torchvision)

I see two problems:

  1. torch==1.13.1+cpu is marked as for win32 but the binary listed is for linux
  2. The entry for torch==1.13.1for darwin lists no binary, even though this is definitely present (I've tested with the url supplied directly instead of index

I've tried:

Am I missing something? Ideally I'd like pyproject.toml to not need any direct url sources and work for mac-x86/mac-arm/win/linux-cuda

FishAlchemist commented 1 week ago

In reality, it should be quite rare to see win32 for platform_system, as it's not difficult to replace win32 with Windows in its judgment. image

https://docs.python.org/3.10/library/platform.html#platform.system image https://docs.astral.sh/uv/concepts/dependencies/#dependency-specifiers-pep-508

To generate more precise results, I recommend using Linux, Windows, or Darwin as the marker value. Please note that these values are case-sensitive.

The value win32 is associated with the marker sys_platform. https://docs.python.org/3.10/library/sys.html#sys.platform image

Edit: I'm unsure if platform_system is case-sensitive, but using the correct capitalization for the lock file is usually best.

sharmuz commented 1 week ago

Thanks @FishAlchemist - that did the trick!

Looks like case-sensitivity is important for platform_system. FWIW I originally was using sys_platform instead (hence the win32) but still had issues (can't recall if were same).

Might be worth updating the dependency docs to use platform_system? Just a thought

Anyway here's my working pyproject.toml in case is of use:

[project]
name = "minimal"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = "==3.10.*"
dependencies = [
  "torch==1.13.1 ; platform_system == 'Darwin'",
  "torch==1.13.1+cpu ; platform_system == 'Windows'",
  "torch==1.13.1+cu116 ; platform_system == 'Linux'",
  "torchvision==0.14.1 ; platform_system == 'Darwin'",
  "torchvision==0.14.1+cpu ; platform_system == 'Windows'",
  "torchvision==0.14.1+cu116 ; platform_system == 'Linux'",
]

[tool.uv.sources]
torch = [
  { index = "torch-cpu", marker = "platform_system != 'Linux'" },
  { index = "torch-cuda", marker = "platform_system == 'Linux'" },
]
torchvision = [
  { index = "torch-cpu", marker = "platform_system != 'Linux'" },
  { index = "torch-cuda", marker = "platform_system == 'Linux'" },
]

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

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

(Btw I'm aware pytorch advise installing from pypi for macos - I just prefer this way)

sharmuz commented 2 days ago

Sorry to say I'm getting a new issue with this with 0.5.1 :(

uv fails to install the env, saying it cannot find a compatible file for torch==1.13.1+cpu for my platform (Mac/Darwin).

However, as you can see above it should not be attempting to install that version of torch on Mac. Scanning the lockfile I see that it has indeed erroneously added platform_system == 'Darwin' to the entry for torch==1.13.1+cpu instead of torch==1.13.1.

Downgrading to 0.5.0 did not resolve it. (I must've been mistaken with the ver I wrote at the top).

Changing the tool.uv.sources entry for torch so that index = "torch-cpu" is only for Windows does resolve it. I.e. Darwin installs from pypi. But before it worked from torch-cpu.

Downgrading to 0.4.28 does resolve it fully - it creates a lockfile consistent with the toml and installs the correct torch packages from pytorch.org for Mac.

FishAlchemist commented 2 days ago

@sharmuz Can you provide a pyproject.toml file that can reproduce the issue you're facing? Will this issue occur in version 0.4.29? Since 0.5.0 is a major update, confirming this issue in 0.4.29, the last version in the 0.4.X series, will help narrow down the scope of uv's bug-hunting efforts.

charliermarsh commented 2 days ago

@sharmuz -- You likely need to do torch==1.13.1, !==1.13.1+cpu ; platform_system == 'Darwin', or torch===1.13.1 ; platform_system == 'Darwin'.

1.13.1+cpu is a valid version for torch==1.13.1, per the Python standards.

charliermarsh commented 2 days ago

I believe this is a duplicate of https://github.com/astral-sh/uv/issues/9036.