←back to thread

Using uv with PyTorch

(docs.astral.sh)
167 points charliermarsh | 9 comments | | HN request time: 1.03s | source | bottom
1. 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 #
2. 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 #
3. samtheprogram ◴[] No.42189506[source]
Speeds up installation, or speeds up PyTorch in general?
replies(1): >>42190362 #
4. gleenn ◴[] No.42190362[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.
5. odie5533 ◴[] No.42190399[source]
Also use pyproject.toml to specify dependencies, not manually installing stuff with uv pip
6. 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 #
7. gdiamos ◴[] No.42190578[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 #
8. ttyprintk ◴[] No.42190631{3}[source]
Eh, in that case the environment variable is a bit more evident in a Dockerfile.
9. kristjansson ◴[] No.42190644{3}[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.