Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for installing common pytorch variants #5945

Open
zanieb opened this issue Aug 9, 2024 · 21 comments
Open

Add documentation for installing common pytorch variants #5945

zanieb opened this issue Aug 9, 2024 · 21 comments
Labels
documentation Improvements or additions to documentation help wanted Contribution especially encouraged

Comments

@zanieb
Copy link
Member

zanieb commented Aug 9, 2024

😭 but it must be done!

@zanieb zanieb added the documentation Improvements or additions to documentation label Aug 9, 2024
@nikhilweee
Copy link

I think the most common difficulty would be to specify the index url, but I'm glad uv makes it as easy as possible.

uv add torch --index-url https://download.pytorch.org/whl/cpu # within a project
uv pip install torch --index-url https://download.pytorch.org/whl/cpu # otherwise

@FishAlchemist
Copy link
Contributor

However, I've always used uv pip install for both PyTorch and TensorFlow, because on my Windows system, PyTorch uses CUDA 12.1 and TensorFlow includes tensorflow-intel, both of which are platform-dependent.

@baggiponte
Copy link

Available to help :) I am on ARM macOS and uv add torch recently has always been nice to me 😅 Regular version (w/o CUDA dependencies) are installed.

@baggiponte
Copy link

Would also like to point out there's been this python package (recently recommended by @muellerzr, HuggingFace Accelerate maintainer) to install torch with automatic backend detection. Might be a good candidate for a first uv plugin, I guess?

@valentincalomme
Copy link

Sharing my thoughts to help with the discussion.

I feel that the biggest challenge here is that the source is platform dependent. On MacOs, PyPi is sufficient. But for Linux and Windows, if one wishes to install a cuda enabled version, they need to specify a URL.

I would personally already be helped if I could specify platform-dependent sources for packages.

Now the cuda/not cuda question is difficult. I think that theoretically, using optional dependencies can be a way to deal with this. If you want the cuda version (for any platform), install it as my_package[cuda]. If you only need the cpu version (i.e. Github actions), then install it as my_package[cpu].

But it's more of a flag then an optional dependency. It's either/or. So not sure how to deal with that. Maybe there can be "modes" for dependencies, with a default. And if the "cuda" mode is specified, dependencies are installed based on that.

@zanieb
Copy link
Member Author

zanieb commented Aug 23, 2024

I would personally already be helped if I could specify platform-dependent sources for packages.

Regarding this.. #3397

@zanieb
Copy link
Member Author

zanieb commented Sep 11, 2024

Example at #7245 (comment)

@dan-jacobson
Copy link

dan-jacobson commented Sep 21, 2024

I've been banging my head against this for a couple days and can only report a list of things that don't work.

For reference, my goal is to install torch cpu-only, in two environments:

  • my m2 macbook
  • linux/amd64 (so I can build my image in github ci and run on Cloud Run).

Here's what I've tried so far:

  • uv add torch : doesn't work because torch defaults to installing a bunch of cuda on linux/amd64
  • uv add torch --extra-index-url https://download.pytorch.org/whl/cpu doesn't work because torch==2.4.1+cpu doesn't exist for macos
  • find-links = ["https://download.pytorch.org/whl/torch_stable.html"] somehow downloads cuda stuff on my mac?? this also fails if declare torch==2.4.1+cpu because the macos whl's in that registry don't have the +cpu local version identifier. (edit: i did also have to downgrade to torch==2.3.1 because 2.4.X isn't in the torch_stable registry)
  • dependencies = ["torch==2.4.1 ; sys_platform == 'darwin'", "torch==2.4.1+cpu ; sys_platform != 'darwin'"], with extra-index-url = "https://download.pytorch.org/whl/cpu" in [tool.uv].
  • dependencies = ["torch @ https://download.pytorch.org/whl/cpu/torch-2.4.1-cp312-none-macosx_11_0_arm64.whl ; sys_platform == 'darwin' and platform_machine == 'arm64'", "torch==2.4.1+cpu ; sys_platform !='darwin'"] again, with extra-index-url pointing to the torch cpu-only registry. i was pretty surprised this one didn't work, since i thought i was yoinking from @charliermarsh in this comment.

I've also tried all variations of the above with index-strategy = "unsafe-best-match", just to make sure that it's not a registry order problem.

anyways, i'm sure there's a way. i just haven't found it yet. i'll keep plugging away. i think my next idea is to define the macos as a dev-dependency, although that feels a little bit hacky.

@charliermarsh
Copy link
Member

Thanks, that’s a good concrete example. I’ll take a look at it (not tonight, but soon) and report back.

@dan-jacobson
Copy link

dan-jacobson commented Sep 22, 2024

Update: it seems that the last version I tried, dependencies = ["torch @ whatever ; sys_platform == 'darwin' and platform_machine == 'arm64'", "torch @ whatever+cpu ; sys_platform !='darwin'"] DOES work if I'm just careful about the paths. Also it works if I relax to just the local version identifier.

For anyone trying this in the future, here's the specific pyproject.toml that worked for me:

dependencies = [
    "torch==2.4.1 ; sys_platform == 'darwin' and platform_machine == 'arm64'",
    "torch==2.4.1+cpu ; sys_platform !='darwin'",
]

[tool.uv]
index-strategy = "unsafe-best-match"
extra-index-url = ["https://download.pytorch.org/whl/cpu"]

Edit: note that index-strategy = "unsafe-best-match" isn't strictly necessary for just torch, but some of the other deps in the https://download.pytorch.org/whl/cpu registry are behind their pypi equivalents. pillow, for example. so it was necessary to either 1) downgrade my pillow version to the one in the pytorch/cpu registry, or 2) use unsafe-best-match.

Edit 2: looking back at my logs, the reason I hadn't gotten this approach to work before was actually because I hadn't wrapped extra-index-url in a List -- I'd just been passing it as a string. This broke the TOML parsing, but the proximate error I was seeing was:

No solution found when resolving dependencies for split (sys_platform != 'darwin'):
  ╰─▶ Because there is no version of torch{sys_platform != 'darwin'}==2.4.1+cpu and your project depends on torch{sys_platform != 'darwin'}==2.4.1+cpu, we can
      conclude that your project's requirements are unsatisfiable.

And I had concluded I was writing my dependencies wrong. So, whoops, skill issue for not reading the full stacktrace.

@charliermarsh
Copy link
Member

And I had concluded I was writing my dependencies wrong. So, whoops, skill issue for not reading the full stacktrace.

We had a few releases where we dropped this error on the floor, so it might not have even been visible to you sadly.

@charliermarsh
Copy link
Member

charliermarsh commented Sep 23, 2024

I do think that what you ended up with is right, though:

[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["torch==2.4.1 ; sys_platform == 'darwin'", "torch==2.4.1+cpu ;  sys_platform != 'darwin'"]

[tool.uv]
index-strategy = "unsafe-best-match"
extra-index-url = ["https://download.pytorch.org/whl/cpu"]

@charliermarsh
Copy link
Member

Does anyone know why PyTorch has some wheels with +cpu and some without here? This is on the https://download.pytorch.org/whl/cpu index.

Screenshot 2024-09-23 at 3 26 14 PM

@dan-jacobson
Copy link

Honestly, I'm not entirely sure -- my vague understanding is that the wheels that could have CUDA support have explicit +cpu versions, but the ones without CUDA support are implicitly cpu-only. Either way, quite frustrating.

One other thing, as I've continued to work on this project: using the above pyproject.toml, I can't uv add torchvision without automatically getting torchvision+cpu. Not entirely sure why. What I'm seeing is:

uv add torchvision
error: distribution torchvision==0.19.1+cpu @ registry+https://download.pytorch.org/whl/cpu can't be installed because it doesn't have a source distribution or wheel for the current platform

This is on my macbook, where I would have expected it to resolve to just torchvision, sans +cpu.

However, if I manually add torchvision==0.19.1 to my pyproject.toml and uv sync, everything behaves as expected.

@Leon0402
Copy link

Variant 1:

uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
⠼ lit==15.0.7                                                                                                                          × Failed to download and build `lit==15.0.7`
  ├─▶ Failed to resolve requirements from `build-system.requires`
  ├─▶ No solution found when resolving: `setuptools>=40.8.0`, `wheel`
  ╰─▶ Because wheel was not found in the package registry and you require wheel, we can conclude that your requirements are
      unsatisfiable.

      hint: An index URL (https://download.pytorch.org/whl/cu118) could not be queried due to a lack of valid authentication
      credentials (403 Forbidden).

Variant 2:

uv add torch torchvision torchaudio --default-index https://download.pytorch.org/whl/cu118
error: Distribution `torchaudio==2.0.0 @ registry+https://download.pytorch.org/whl/cu118` can't be installed because it doesn't have a source distribution or wheel for the current platform

Any idea why neither is working?

@FishAlchemist
Copy link
Contributor

FishAlchemist commented Oct 18, 2024

@Leon0402 I think this isn't a UV issue. I accessed it a few minutes ago through the browser and it didn't have the correct content, but it seems to be working now. Do you want to try it again?

The result of my visit a few minutes ago:
image

@Leon0402
Copy link

@FishAlchemist Issue persists. I also tried cu121, but same issue. I am running the right commands though, right? I am new to uv, so maybe I did not use it correctly.
I also just created a new fresh project with uv init and then above command, but same issue.

@FishAlchemist
Copy link
Contributor

@Leon0402 I am unable to replicate this issue on my machine. Would you consider filing a separate issue and providing more specific details based on the prompts?

@Leon0402
Copy link

@Leon0402 I am unable to replicate this issue on my machine. Would you consider filing a separate issue and providing more specific details based on the prompts?

Sure: #8344, Let me know what other output you need there.

@fzyzcjy
Copy link

fzyzcjy commented Oct 19, 2024

Hi I am facing problem in macos for pytorch cpu installation (#8358), is there any suggestions? Thanks!

@amiralik16
Copy link

Hi UV team. First let me thank you for all the work you're doing on the project. I've just started using it and it is FAST and pretty intuitive (especially coming from poetry).

For my use case, I'm trying to generate a lock file that would install either a cpu only or gpu enabled version of pytorch when on a linux platform and just regular pypi pytorch when using a mac.
According to pytorch, one needs to point to different indexes:

  1. Linux and cpu: https://download.pytorch.org/whl/cpu
  2. Linux + cuda 11.8 + gpu: https://download.pytorch.org/whl/cu118
  3. mac: pypi
    I have the following pyproject.toml (the idea is I'd be able to do uv sync --extra torch-gpu/torch-cpu) that would install the gpu/cpu version on a linux platform.
[project]
name = "uv"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
    # bunch of irrelevant deps
]

[project.optional-dependencies]
torch-gpu = [
    "torch==2.4.1+cu118;sys_platform == 'linux' and extra == 'torch-gpu' and extra != 'torch-cpu'", 
    "torch==2.4.1;sys_platform == 'darwin'",
    "torchmetrics>=1.4.3",
]
torch-cpu = [
    "torch==2.4.1+cpu;sys_platform == 'linux' and extra != 'torch-gpu' and extra == 'torch-cpu'", 
    "torch==2.4.1;sys_platform == 'darwin'",
    "torchmetrics>=1.4.3",
]
classic = [
    "lightgbm>=4.5.0",
    "scikit-learn>=1.5.2",
    "xgboost>=2.1.1",
]


[tool.uv.sources]
torch = [
  { index = "torch-cpu", marker = "sys_platform == 'linux' and extra != 'torch-gpu' and extra == 'torch-cpu'"},
  { index = "torch-cu118", marker = "sys_platform == 'linux' and extra == 'torch-gpu' and extra != 'torch-cpu'"},
]

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

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

However, I am getting the below error. I'm not sure where the torch{sys_platform == 'linux' and extra != 'torch-gpu'}is coming from since my expectation is that torch2.4.1+cpu is ONLY installed if extra torch-cpu is requested and extra torch-gpu isn't requested.

 × No solution found when resolving dependencies for split (sys_platform != 'darwin'):
  ╰─▶ Because there is no version of torch{sys_platform == 'linux' and extra != 'torch-gpu'}==2.4.1+cpu and uv[torch-cpu] depends on torch{sys_platform == 'linux' and extra
      != 'torch-gpu'}==2.4.1+cpu, we can conclude that uv[torch-cpu]'s requirements are unsatisfiable.
      And because your project requires uv[torch-cpu], we can conclude that your projects's requirements are unsatisfiable.

I'm mainly looking for any suggestions on how to achieve the end goal and if anyone from the UV team can help me understand why torch2.4.1+cpu is being considered for torch{sys_platform == 'linux' and extra != 'torch-gpu'}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Contribution especially encouraged
Projects
No open projects
Status: No status
Development

No branches or pull requests

10 participants