Skip to content

Support for uv package manager#74

Open
DekuD2 wants to merge 1 commit intoufal:masterfrom
DekuD2:master
Open

Support for uv package manager#74
DekuD2 wants to merge 1 commit intoufal:masterfrom
DekuD2:master

Conversation

@DekuD2
Copy link

@DekuD2 DekuD2 commented Feb 24, 2026

This code makes sure that uv sync pulls all torch packages (except for torchmetrics) from https://download.pytorch.org/whl/cu128

@foxik
Copy link
Member

foxik commented Feb 24, 2026

Thanks for making our package uv-friendly :-)

The reason I use the argument --extra-index-url=https://download.pytorch.org/whl/cu128 on the command line and not in pyproject.toml is because some users might want to override it with other values (I already know of all of cpu, cu126, and rocm7.1 being used). But, if you do not specify the extra index URL, you will get PyPI torch packaged, which are CUDA 12.8. Therefore, hard-coding the extra index URL to pyproject.toml (like in your change) is not really needed.

Ideally I would like to have different extras for various targets, i.e., have npfl138[cp126] or npfl138[cpu]. But last I check that was not possible in pip (for security reasons). So I have two questions:

  • Is there any uv configuration what would allow me specifying for example npfl138[cp126] or npfl138[cpu] with extra index URLs?
  • Can you somehow specify the extra-index-url on the commandline wit uv?

@DekuD2
Copy link
Author

DekuD2 commented Feb 24, 2026

I didn't know that the default ones come with cuda, it actually didn't work for me originally but I suppose it was the other fix that fixed cuda not working 😅.

Given that it works by default with cuda, this is probably not an issue whatsoever. Nonetheless, here is what I found:

There is --extra-index-url argument in uv, but it doesn't work in this particular scenario, because it finds torchmetrics in that url, after which it locks it down as the source for torchmetrics, and then it fails because there is no version that works with the dependency specifications. That's why I was manually selecting the sources in there.

For uv, it is possible to make torch* optional dependencies ([cuda], [cpu], [rocm]) and have them select different sources. Unfortunately however, you lose the "default". I.e. you have to always choose one of them explicitly when installing it with uv (uv sync --extra cuda). Without it, it doesn't install torch even though I kept it the dependencies. I have no idea what regular pip would do, as that is basically impossible to check with NixOS.

@foxik
Copy link
Member

foxik commented Feb 24, 2026

It is unfortunate that --extra-index-url does not work the same way; pip gladly installs torchmetrics from the default index url even if it extra-index-url is given. I will install uv and I will think about how to make it work.

@gamecraftCZ
Copy link
Contributor

I got uv install working usign uv add --index=https://download.pytorch.org/whl/cu128 --index-strategy unsafe-first-match npfl139
The --index-strategy unsafe-first-match flag makes sure that the package index matching is the same as using pip.

@foxik
Copy link
Member

foxik commented Feb 25, 2026

What do you think about 314361b?

  • It mentions --index-strategy unsafe-best-match should be added if the installation should be performed with uv pip . While the unsafe-best-match is not very elegant, it allows to use a command very similar to pip, so I ended up using it.

  • If uv add is required, it contains instructions of first adding PyTorch with a specified index URL and then adding npfl138 in a straightforward way.

@DekuD2
Copy link
Author

DekuD2 commented Feb 25, 2026

That's a good find Patrik.

I think the new section looks good. I suppose the reason to not specifically mention that one doesn't have to use the --extra-index-url at all and avoid the complications (at least for cuda) is to keep it simple and consistent everywhere? Since that option isn't mentioned in the pip version either.

Also a bit tangential but the reason I was doing this in the first place was to make it work on my NixOS. Whilst I would be surprised if anyone else in the class also used NixOS, I could share the "flake" for it so any other NixOS user could use it easily.

If you wanted to do that, there are basically two options:

  • I could put my flake.nix and flake.lock files in the labs/ subdirectory.
  • Or I could add one line of instructions to the FAQ saying to do nix flake init --template github:DekuD2/npfl138-public/flake?dir=labs inside of the labs/ directory, which basically copies the files there whilst not including them in the main repo.

In the end, this would allow anyone on NixOS (or I believe using the nix package manager as well) to then use nix develop to enter a fully setup python environment in that directory.

(Also the option of not doing either of that is fine.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants