Most active commenters
  • alex_suzuki(4)
  • zanie(3)

Using uv with PyTorch

(docs.astral.sh)
167 points charliermarsh | 56 comments | | HN request time: 1.826s | source | bottom
1. minimaxir ◴[] No.42188914[source]
So uv caused a bit of an issue with me installing PyTorch over the weekend.

When installed with brew on my MacBook, uv currently has PyTorch 3.13 as a dependency, which is fine. But PyTorch does not currently have a stable wheel that's compatable with Python 3.13! This resulted in very confusing errors. (Solution was to point to the Nightly index)

That's technically PyTorch's fault, but it's indicitave why a specific page on installing PyTorch is necessary, and it's good to know the documentation specifically calls it out.

replies(2): >>42189345 #>>42191635 #
2. gdiamos ◴[] No.42189027[source]
uv significantly speeds up my pytorch in docker builds

  # Setup virtual env
  ENV VIRTUAL_ENV=/app/.venv
  ENV PATH="$VIRTUAL_ENV/bin:$PATH"
  RUN python3 -m venv $VIRTUAL_ENV
  RUN . $VIRTUAL_ENV/bin/activate

  # install using uv
  RUN pip install uv
  RUN uv pip install torch==${TORCH_VERSION} --index-url https://download.pytorch.org/whl/cpu
The index-url makes it really convenient.
replies(3): >>42189326 #>>42189506 #>>42190557 #
3. orf ◴[] No.42189326[source]
Use —copy-from to make it even faster, and use a cache mount

https://docs.astral.sh/uv/guides/integration/docker/#install...

replies(1): >>42190399 #
4. 0cf8612b2e1e ◴[] No.42189345[source]
I have run into multiple package problems with 3.13 with a non obvious root cause error message. Thankfully, uv makes it trivial to switch out to 3.12
replies(1): >>42189386 #
5. zanie ◴[] No.42189386{3}[source]
I work on our error messages, feel free to open an issue and we'll do our best to make it clearer
replies(1): >>42190377 #
6. samtheprogram ◴[] No.42189506[source]
Speeds up installation, or speeds up PyTorch in general?
replies(1): >>42190362 #
7. gleenn ◴[] No.42190362{3}[source]
Almost certainly only the install. Uv is basically a pip tool substitute with a few other bells and whistles too but shouldn't affect run time whatsoever.
8. emmanueloga_ ◴[] No.42190377{4}[source]
Hey there, I experienced a hairy error message recently too while trying to install aider-chat from pypi with Python 3.13 and Pixi (but I was told the error was coming from UV).

"Solution": `pixi add python=3.12`, then `pixi add --pypi aider-chat` succeeds without issues.

A message like "aider-chat seems to be incompatible with python=3.13, try downgrading to python-3.12" would be great, assuming this is really the case.

I can create an issue if it helps, but really quick here's the error I was getting and some talk on discord about it:

https://pastebin.com/UVkFstJH

https://discord.com/channels/1082332781146800168/12957237931...

replies(1): >>42194331 #
9. odie5533 ◴[] No.42190399{3}[source]
Also use pyproject.toml to specify dependencies, not manually installing stuff with uv pip
10. thundergolfer ◴[] No.42190458[source]
Kind of an aside as this doc is about the complexities of installing particular PyTorch versions, but will say that uv is way faster at installing PyTorch than pip.

We run internal benchmarks of our custom container image builder and in the 'install torch' benchmark the p50 time saved when using `uv` is 25 seconds! (71.4s vs. 43.74s)

---

Aside 2: Seems there's a missing "involves" in this sentence: "As such, installing PyTorch typically often configuring a project to use the PyTorch index."

replies(1): >>42191116 #
11. ttyprintk ◴[] No.42190557[source]
Apparently, uv respects a key in project.toml:

    [[tool.uv.index]]
    name = "pytorch-cu124"
    url = "https://download.pytorch.org/whl/cu124"
    explicit = true
replies(1): >>42190578 #
12. gdiamos ◴[] No.42190578{3}[source]
Nice, I wonder if there is a way to make it conditional, e.g. to pick a different key for a cpu vs cuda build.
replies(2): >>42190631 #>>42190644 #
13. ttyprintk ◴[] No.42190631{4}[source]
Eh, in that case the environment variable is a bit more evident in a Dockerfile.
14. kristjansson ◴[] No.42190644{4}[source]
That’s basically what TFA is about? Set up multiple index urls, and use a bog-standard platform marker on the PyTorch dep to pick between them.
15. mrbonner ◴[] No.42190760[source]
Does uv support global Python install now? I need something like Mise for this.
replies(3): >>42191001 #>>42192371 #>>42194345 #
16. shlomo_z ◴[] No.42191001[source]
uv python install 3.xx
replies(1): >>42191096 #
17. imjonse ◴[] No.42191096{3}[source]
That is not global. From the uv getting started docs:

"When Python is installed by uv, it will not be available globally (i.e. via the python command). Support for this feature is planned for a future release. In the meantime, use uv run or create and activate a virtual environment to use python directly."

So yes, one needs mise/asdf/pyenv or similar for global installs for now.

replies(1): >>42193327 #
18. cik ◴[] No.42191116[source]
Joining your aside to tout the benefits of uv. We use uv combined with a simple proxy I wrote, to cache python dependencies, and then install them in parallel. UV also makes it simple to regenerate a requirements file and know who requires the dependencies, which in turn makes it easy to manage the ecosystem, analyze packages, and determine if we can reduce our footprint.

Between that latter feature, the proxy, the parallelization we've reduced build times across ~100 engineers by a solid 10 minutes. There are other things we do as well, but uv is a must have nowadays.

replies(2): >>42191669 #>>42191693 #
19. paradite ◴[] No.42191360[source]
I was just getting used to pipenv and pyenv combo.

Is this worth switching to?

replies(5): >>42191409 #>>42191442 #>>42191833 #>>42192248 #>>42192347 #
20. kolja005 ◴[] No.42191388[source]
I recently watched a talk by the author of uv that was surprisingly fascinating [1]. He goes into a few of the more notable hacks that they had to come up with to make it as fast as it is. The most interesting thing for me was that package resolution in python given constraints defined (eg. in requirements.txt) maps to a boolean satisfiability problem which is NP-complete. So uv uses a custom SAT solver to do this. I totally under-appreciated how much goes into this software and I'm bummed I have to use Poetry at work after having watched this talk.

[1] https://www.youtube.com/watch?v=gSKTfG1GXYQ

edit: NP-complete not NP-hard

replies(1): >>42191993 #
21. breuleux ◴[] No.42191393[source]
I was trying to figure out how to set up a pyproject with uv that could support cuda, rocm and other device types this morning, and next thing I knew, there was a new release adding pretty much exactly what I needed.

The pace of development on uv is really impressive.

22. baggiponte ◴[] No.42191409[source]
Absolutely. No second thoughts.
23. jpalomaki ◴[] No.42191442[source]
I've been using a combination of pyenv, venv and Poetry in the past.

Now I have switched to uv with new projects. No problems so far. I definitely recommend giving it a go.

24. Mxbonn ◴[] No.42191569[source]
Now that PyTorch is also ending their anaconda package distribution, I think a lot of ml/ds people should give uv a shot.
replies(1): >>42192241 #
25. the_mitsuhiko ◴[] No.42191635[source]
Fun fact: that's called out in the doc you're commenting :)
26. freetonik ◴[] No.42191669{3}[source]
Is your caching proxy open source?
replies(1): >>42191983 #
27. campbel ◴[] No.42191693{3}[source]
working on switching over a lot of our python usage to UV, seeing similar speedups in builds and local environments
28. silvester23 ◴[] No.42191833[source]
I would also say absolutely. We've been using pipenv for ~6 years and have managed to build a pretty good workflow around it. But uv is just _so much faster_. So we've started moving everything over to uv and I don't think we'll ever look back.

Migrating is not super hard, we wrote a small script that moves all the information from a Pipfile to a pyproject.toml and it works like a charm.

29. pcwelder ◴[] No.42191858[source]
I just want to thank people behind uv. The tool is just amazing for development, packaging and running packages. And it's blazing fast!
30. cik ◴[] No.42191983{4}[source]
It's just nginx. Here's a link to something someone did. It's close enough to be honest, unless you have our specialized needs.

https://github.com/hauntsaninja/nginx_pypi_cache

31. wenc ◴[] No.42191993[source]
I haven’t used Conda since 2021 but recall it had a SAT solver that was very slow especially on degenerate cases.

How does uv’s sat solver compare?

replies(3): >>42192963 #>>42194275 #>>42194910 #
32. nickysielicki ◴[] No.42192241[source]
I had no idea this was happening. This comment just made my week. Thank God.
33. fernandotakai ◴[] No.42192248[source]
yes, 100%. it's not only faster, but it makes sense.
34. cbenz ◴[] No.42192347[source]
Absolutely. It changed my Python developer life \o/
35. antman ◴[] No.42192365[source]
Does anyone succeed in packaging a uv pytorch env to a pyinstaller exe or similar? I am having a hard time but I assume it can be automated
36. cbenz ◴[] No.42192371[source]
Yes via the "--system" option of the different commands.

Or via the global "python-preference" option set to "only-system".

Cf https://docs.astral.sh/uv/concepts/python-versions/#adjustin... and https://docs.astral.sh/uv/reference/settings/#python-prefere...

37. alex_suzuki ◴[] No.42192428[source]
In a nutshell, what do I gain from switching to uv from my current workflow, which is: 1) create a venv (`python3.xx -m venv venv`) 2) install packages from a requirements.txt into that venv?

One limitation I know of are the inability to detect stale packages.

Apart from „blazing fast“, which I‘m not convinced it really matters to me as I rarely touch the dependencies, what are the main reasons why uv is gaining traction?

replies(4): >>42192436 #>>42192507 #>>42192711 #>>42198294 #
38. alex_suzuki ◴[] No.42192436[source]
PS: one thing I like about my current workflow is no extra tools needed, base python install is all that‘s required.
replies(1): >>42193311 #
39. reubenmorais ◴[] No.42192507[source]
You get correct version resolution (checking compatibility across the entire tree of deps-of-deps, including against different Python versions) and a lock file which represents a global state of the entire tree and gives you reproducibility of a working setup.
replies(1): >>42192563 #
40. gugagore ◴[] No.42192563{3}[source]
Does pip-compile do the same? Or what's the difference?
replies(1): >>42192578 #
41. reubenmorais ◴[] No.42192578{4}[source]
pip-compile, poetry, Pipenv, et al all try do roughly the same, with various caveats and design differences (e.g. Pipenv is not meant to be used in libraries, only applications). uv is the latest kid in the block.
42. imjonse ◴[] No.42192711[source]
One reason is that it is not just for project venv/deps management but can replace other tools like pipx and pyenv for most scenarios.
43. chocolight ◴[] No.42192894[source]
I've read that Torch was dropping their Conda support, but won't everybody just move to Mamba which is a drop-in replacement of Conda?

Conda (and Mamba) allows to avoid duplicating packages on the disk between environments (not just the downloaded archives, but the resulting expanded files too).

How does uv compare in this regard?

44. sa1 ◴[] No.42192963{3}[source]
There are such cases in uv as well, and I’ve hit them quite often when I didn’t specify lower bounds (especially for boto3).
45. ◴[] No.42193265[source]
46. JimDabell ◴[] No.42193311{3}[source]
It’s similar with uv. You have exactly one dependency on the host system – it’s just uv instead of Python. uv will then obtain the correct version of Python for your project. And uv is easier to install than Python – it’s literally just one binary.
replies(1): >>42196535 #
47. JimDabell ◴[] No.42193327{4}[source]
You could always just `alias python="uv run python"`
48. montebicyclelo ◴[] No.42194275{3}[source]
Conda has a faster solver, as of 2022

https://www.anaconda.com/blog/a-faster-conda-for-a-growing-c...

49. zanie ◴[] No.42194331{5}[source]
Thanks! There's definitely room for improvement there. I'll see what we can do — in general it's a bit of an arcane task to extract clear suggestions from the resolver's error tree.
replies(1): >>42199924 #
50. zanie ◴[] No.42194345[source]
We're working on this! You can try it out with `uv python install --preview`.

The work can be tracked in https://github.com/astral-sh/uv/issues/6265

51. thundergolfer ◴[] No.42194910{3}[source]
In our internal benchmarks miniconda is about as fast as uv at installing torch.
52. alex_suzuki ◴[] No.42196535{4}[source]
Interesting, thanks for pointing that out. Will give it a try.
53. infamia ◴[] No.42198294[source]
You might gain a couple of things...

No need to activate venvs and the almost inevitable Python pathing "murder mysteries". uv installs in venvs first, and only then someplace else (e.g., globally).

No more clunky typing `python -m pip install foo` even when you have activated your venv (or you think you have). `uv pip install foo` is nicer and easier to remember.

uv add will add new dependencies to your pyproject.toml so you don't have to.

uv can setup skeletons for new projects in a nice, modern way

For older projects, you can have uv to resolve dependencies as of a certain date. I imagine this is great for older projects, especially with numerous dependencies.

It might remove the need for pyenv or the need to rely on your system provided Python, since uv can install Python for your project.

Cross-platform lock files

I've just started looking in to uv, so maybe my list isn't complete/very good. Some down sides include, it's still green (has some bugs naturally and lacks some features) and some might not trust/like that it's VC backed.

replies(1): >>42212940 #
54. emmanueloga_ ◴[] No.42199924{6}[source]
Sounds good, thank you!
55. alex_suzuki ◴[] No.42212940{3}[source]
> For older projects, you can have uv to resolve dependencies as of a certain date. I imagine this is great for older projects, especially with numerous dependencies.

Interesting. I imagine this is a selling point for corporate environments.