From 69e50de671ecbda46e330a82b95a3e2c6c778f05 Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Fri, 23 Aug 2024 16:51:11 +0200 Subject: [PATCH 01/10] docs: Add PyTorch installation guide --- docs/guides/integration/pytorch.md | 199 +++++++++++++++++++++++++++++ mkdocs.template.yml | 1 + 2 files changed, 200 insertions(+) create mode 100644 docs/guides/integration/pytorch.md diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md new file mode 100644 index 0000000000000..b2ac36215994f --- /dev/null +++ b/docs/guides/integration/pytorch.md @@ -0,0 +1,199 @@ +# Installing pytorch with uv + +[PyTorch](https://pytorch.org/) is a popular open-source deep-learning learning library that can run +both on CPUs and GPUs. For this reason, PyTorch can be notoriously cumbersome to install, especially +on Linux/Windows. + +[PyTorch website](https://pytorch.org/get-started/locally/) offers a simple tool to determine what +`pip` command you should run to install PyTorch. This guide should help you do the same with `uv`. +If the following instructions fail, you might want to check the original source for any difference, +and open an issue to report the discrepancy. In this case, or if you run into any other issue, +please refer to [Getting Help](../../getting-started/help.md). + +!!! tip "TL;DR" + + * If want PyTorch on macOS, just run `uv add torch`/`uv pip install torch`. + * If want PyTorch on Windows *with CPU support*, just run `uv add torch`/`uv pip install torch`. + * If want to install PyTorch on Linux with CPU support, add `--extra-index-url=https://download.pytorch.org/whl/cpu` to the `uv add`/`uv pip install` command. + * For Windows and Linux: + 1. If you need CUDA 11.8, add `--extra-index-url=https://download.pytorch.org/whl/cu118` to the `uv add`/`uv pip install` command. + 2. If you need CUDA 12.1, add `--extra-index-url=https://download.pytorch.org/whl/cu121` to the `uv add`/`uv pip install` command. + 3. If you need CUDA 12.4, add `--extra-index-url=https://download.pytorch.org/whl/cu124` to the `uv add`/`uv pip install` command. + +## On macOS + +Since CUDA is not available on macOS, you should just run: + +```sh +# in a project +uv add torch + +# make sure there is a virtual environment in the directory +uv pip install torch +``` + +## On Linux + +### CPU Only + +```sh +uv add --extra-index-url=https://download.pytorch.org/whl/cpu torch + +uv pip install --extra-index-url=https://download.pytorch.org/whl/cpu torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cpu" } +``` + +### CUDA 11.8 Support + +```sh +# in a project +uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch + +# make sure there is a virtual environment in the directory +uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cu118" } +``` + +### CUDA 12.1 Support + +```sh +# in a project +uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch + +# make sure there is a virtual environment in the directory +uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cu121" } +``` + +### CUDA 12.4 Support + +```sh +# in a project +uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch + +# make sure there is a virtual environment in the directory +uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cu124" } +``` + +## On Windows + +### CPU Only + +```sh +# in a project +uv add torch + +# make sure there is a virtual environment in the directory +uv pip install torch +``` + +### CUDA 11.8 Support + +```sh +# in a project +uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch + +# make sure there is a virtual environment in the directory +uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cu118" } +``` + +### CUDA 12.1 Support + +```sh +# in a project +uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch + +# make sure there is a virtual environment in the directory +uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cu121" } +``` + +### CUDA 12.4 Support + +```sh +# in a project +uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch + +# make sure there is a virtual environment in the directory +uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch +``` + +Alternatively, add the following to the `pyproject.toml` file: + +```toml +[project] +dependencies = [ + "torch", +] + +[tool.uv.sources] +torch = { url = "https://download.pytorch.org/whl/cu124" } +``` diff --git a/mkdocs.template.yml b/mkdocs.template.yml index eff7a40589377..938a5a6e043a4 100644 --- a/mkdocs.template.yml +++ b/mkdocs.template.yml @@ -115,6 +115,7 @@ nav: - GitLab CI/CD: guides/integration/gitlab.md - Pre-commit: guides/integration/pre-commit.md - FastAPI: guides/integration/fastapi.md + - PyTorch: guides/integration/pytorch.md - Alternative indexes: guides/integration/alternative-indexes.md - Dependency bots: guides/integration/dependency-bots.md - The pip interface: From d4fe0cecae07d26ef3d6bdd83b752dec14a625c0 Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Fri, 23 Aug 2024 20:12:16 +0200 Subject: [PATCH 02/10] docs: remove "add section to pyproject" --- docs/guides/integration/pytorch.md | 84 ------------------------------ 1 file changed, 84 deletions(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index b2ac36215994f..bd6adf1f8421a 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -42,18 +42,6 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cpu torch uv pip install --extra-index-url=https://download.pytorch.org/whl/cpu torch ``` -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cpu" } -``` - ### CUDA 11.8 Support ```sh @@ -64,18 +52,6 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch ``` -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cu118" } -``` - ### CUDA 12.1 Support ```sh @@ -86,18 +62,6 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch ``` -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cu121" } -``` - ### CUDA 12.4 Support ```sh @@ -108,18 +72,6 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch ``` -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cu124" } -``` - ## On Windows ### CPU Only @@ -142,18 +94,6 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch ``` -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cu118" } -``` - ### CUDA 12.1 Support ```sh @@ -164,18 +104,6 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch ``` -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cu121" } -``` - ### CUDA 12.4 Support ```sh @@ -185,15 +113,3 @@ uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch # make sure there is a virtual environment in the directory uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch ``` - -Alternatively, add the following to the `pyproject.toml` file: - -```toml -[project] -dependencies = [ - "torch", -] - -[tool.uv.sources] -torch = { url = "https://download.pytorch.org/whl/cu124" } -``` From a3c1d49af74b075d9f32427a6daf5df0d47a0d4d Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Fri, 13 Sep 2024 09:09:47 +0200 Subject: [PATCH 03/10] docs: update TLDR; add caveats --- docs/guides/integration/pytorch.md | 39 +++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index bd6adf1f8421a..409d2053a98bf 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -12,13 +12,32 @@ please refer to [Getting Help](../../getting-started/help.md). !!! tip "TL;DR" - * If want PyTorch on macOS, just run `uv add torch`/`uv pip install torch`. - * If want PyTorch on Windows *with CPU support*, just run `uv add torch`/`uv pip install torch`. - * If want to install PyTorch on Linux with CPU support, add `--extra-index-url=https://download.pytorch.org/whl/cpu` to the `uv add`/`uv pip install` command. - * For Windows and Linux: - 1. If you need CUDA 11.8, add `--extra-index-url=https://download.pytorch.org/whl/cu118` to the `uv add`/`uv pip install` command. - 2. If you need CUDA 12.1, add `--extra-index-url=https://download.pytorch.org/whl/cu121` to the `uv add`/`uv pip install` command. - 3. If you need CUDA 12.4, add `--extra-index-url=https://download.pytorch.org/whl/cu124` to the `uv add`/`uv pip install` command. + As of 09/2024: + + * Use `uv add torch` or `uv pip install torch` for: + * on macOS (CPU only) + * on Linux (CUDA 12.1 only) + * on Windows (CPU only) + * Use `uv add --extra-index-url=https://download.pytorch.org/whl/cpu torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cpu torch` for: + * on Linux (CPU only) + * `uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch` will work: + * on Windows and Linux (CUDA 11.8 only) + * `uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch` will work: + * on Windows (CUDA 12.1 only) + * `uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch` will work: + * on Windows (CUDA 12.4 only) + * on Linux (CUDA 12.4 only) + + +!!! info "Why `--extra-index-url`?" + + uv `--extra-index-url` behaves differently than `pip --extra-index-url`. See more [here](https://docs.astral.sh/uv/pip/compatibility/#packages-that-exist-on-multiple-indexes). + +!!! warning "About lockfile cross-compatibility" + + Currently, uv does not support pinning a package to a specific index (though progress is tracked [here](https://github.com/astral-sh/uv/issues/171)). + In case of PyTorch, this means that the lockfile might not be cross-compatible between different platforms. For example: suppose a Windows user runs `uv add torch` and then a Linux user + runs `uv sync` to synchronise the lockfile. The Windows user will get CPU-only PyTorch, while the Linux user will get CUDA 12.1 PyTorch. ## On macOS @@ -54,12 +73,14 @@ uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch ### CUDA 12.1 Support +Currently, wheels on PyPI for PyTorch on Linux come with CUDA 12.1. + ```sh # in a project -uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch +uv add torch # make sure there is a virtual environment in the directory -uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch +uv pip install torch ``` ### CUDA 12.4 Support From 0aa7c7d64540546bb67e2e1066cf26aa795af6c8 Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Sat, 14 Sep 2024 17:49:23 +0200 Subject: [PATCH 04/10] docs: mention lockfile lack of cross compatibility --- docs/guides/integration/pytorch.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index 409d2053a98bf..026aef2a2bda4 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -31,13 +31,15 @@ please refer to [Getting Help](../../getting-started/help.md). !!! info "Why `--extra-index-url`?" - uv `--extra-index-url` behaves differently than `pip --extra-index-url`. See more [here](https://docs.astral.sh/uv/pip/compatibility/#packages-that-exist-on-multiple-indexes). + uv `--extra-index-url` behaves slightly differently from `pip --extra-index-url`. See more [here](https://docs.astral.sh/uv/pip/compatibility/#packages-that-exist-on-multiple-indexes). !!! warning "About lockfile cross-compatibility" - Currently, uv does not support pinning a package to a specific index (though progress is tracked [here](https://github.com/astral-sh/uv/issues/171)). - In case of PyTorch, this means that the lockfile might not be cross-compatible between different platforms. For example: suppose a Windows user runs `uv add torch` and then a Linux user - runs `uv sync` to synchronise the lockfile. The Windows user will get CPU-only PyTorch, while the Linux user will get CUDA 12.1 PyTorch. + Currently, uv does not support pinning a package to a specific index (though progress is tracked [here](https://github.com/astral-sh/uv/issues/171)). As a result, + the lockfiles are not guaranteed to be cross-compatible between different platforms. For example: suppose a Windows user runs `uv add torch` and then a Linux user runs + `uv sync` to synchronise the lockfile. The Windows user will get CPU-only PyTorch, while the Linux user will get CUDA 12.1 PyTorch. + + If you specify the `[tool.uv.sources]` section in your `pyproject.toml`, users from different platform might not be able to solve for the package dependencies. ## On macOS From 89f4721ab08ec1c0eef35308136d76e1734fb3fc Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Sun, 20 Oct 2024 12:46:10 +0200 Subject: [PATCH 05/10] docs: rewrite from scratch for uv 0.4.24 docs: fix link and tab --- docs/guides/integration/pytorch.md | 213 ++++++++++++++++------------- 1 file changed, 119 insertions(+), 94 deletions(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index 026aef2a2bda4..2c3838ea40cf2 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -1,138 +1,163 @@ -# Installing pytorch with uv +# Installing PyTorch with uv -[PyTorch](https://pytorch.org/) is a popular open-source deep-learning learning library that can run -both on CPUs and GPUs. For this reason, PyTorch can be notoriously cumbersome to install, especially -on Linux/Windows. +[PyTorch](https://pytorch.org/) is a popular open-source deep-learning learning framework that has first-class support for acceleration via GPUs. Installation, however, can be complex, as you won't find all the wheels for PyTorch on PyPI and you have to manage this through external indexes. This guide aims to help you set up a `pyproject.toml` file using `uv` [indexes features](../../configuration/indexes.md). -[PyTorch website](https://pytorch.org/get-started/locally/) offers a simple tool to determine what -`pip` command you should run to install PyTorch. This guide should help you do the same with `uv`. -If the following instructions fail, you might want to check the original source for any difference, -and open an issue to report the discrepancy. In this case, or if you run into any other issue, -please refer to [Getting Help](../../getting-started/help.md). +!!! info "Available PyTorch indexes" -!!! tip "TL;DR" + By default, from PyPI you will download: + * CPU-only wheels on Windows and macOS. + * CUDA (i.e. GPU) on Linux. At the time of writing, for PyTorch stable version (2.5.0) this defaults to CUDA 12.4. On older versions, this might be different. - As of 09/2024: + If you want to install CPU-only PyTorch wheels on Linux, or a PyTorch version that supports a different CUDA version, you need to resort to external indexes: - * Use `uv add torch` or `uv pip install torch` for: - * on macOS (CPU only) - * on Linux (CUDA 12.1 only) - * on Windows (CPU only) - * Use `uv add --extra-index-url=https://download.pytorch.org/whl/cpu torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cpu torch` for: - * on Linux (CPU only) - * `uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch` will work: - * on Windows and Linux (CUDA 11.8 only) - * `uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch` will work: - * on Windows (CUDA 12.1 only) - * `uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch` or `uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch` will work: - * on Windows (CUDA 12.4 only) - * on Linux (CUDA 12.4 only) + * https://download.pytorch.org/whl/cu118 + * https://download.pytorch.org/whl/cu121 + * https://download.pytorch.org/whl/cu124 + * https://download.pytorch.org/whl/cpu + * https://download.pytorch.org/whl/rocm6.2 (AMD GPUs, only available on Linux) -!!! info "Why `--extra-index-url`?" +The [PyTorch website](https://pytorch.org/get-started/locally/) offers a simple interface to determine what `pip` command you should run to install PyTorch. This guide should help you do the same with `uv`. If the following instructions fail, you might want to check the link for any difference, and open an issue to report the discrepancy. In this case, or if you run into any other issue, please refer to [Getting Help](../../getting-started/help.md). - uv `--extra-index-url` behaves slightly differently from `pip --extra-index-url`. See more [here](https://docs.astral.sh/uv/pip/compatibility/#packages-that-exist-on-multiple-indexes). +## Initialise a project -!!! warning "About lockfile cross-compatibility" +Create a new project with `uv init`: - Currently, uv does not support pinning a package to a specific index (though progress is tracked [here](https://github.com/astral-sh/uv/issues/171)). As a result, - the lockfiles are not guaranteed to be cross-compatible between different platforms. For example: suppose a Windows user runs `uv add torch` and then a Linux user runs - `uv sync` to synchronise the lockfile. The Windows user will get CPU-only PyTorch, while the Linux user will get CUDA 12.1 PyTorch. +```sh +uv init +``` - If you specify the `[tool.uv.sources]` section in your `pyproject.toml`, users from different platform might not be able to solve for the package dependencies. +!!! tip "Supported Python versions" -## On macOS + Make sure to use a Python version that is supported by PyTorch. You can find the compatibility matrix [here](https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix). -Since CUDA is not available on macOS, you should just run: -```sh -# in a project -uv add torch +## Add PyTorch indexes -# make sure there is a virtual environment in the directory -uv pip install torch -``` +Open the `pyproject.toml` file, and create a custom index matching the CUDA version you have available to instruct `uv` where to find the PyTorch wheels. -## On Linux +=== "CPU-only" -### CPU Only + ```toml + [[tool.uv.index]] + name = "torch-cpu" + url = "https://download.pytorch.org/whl/cpu" + explicit = true + ``` -```sh -uv add --extra-index-url=https://download.pytorch.org/whl/cpu torch +=== "CUDA 11.8" -uv pip install --extra-index-url=https://download.pytorch.org/whl/cpu torch -``` + ```toml + [[tool.uv.index]] + name = "torch-cu118" + url = "https://download.pytorch.org/whl/cu118" + explicit = true + ``` -### CUDA 11.8 Support +=== "CUDA 12.1" -```sh -# in a project -uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch + ```toml + [[tool.uv.index]] + name = "torch-cu121" + url = "https://download.pytorch.org/whl/cu121" + explicit = true + ``` -# make sure there is a virtual environment in the directory -uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch -``` +=== "CUDA 12.4" -### CUDA 12.1 Support + ```toml + [[tool.uv.index]] + name = "torch-cu124" + url = "https://download.pytorch.org/whl/cu124" + explicit = true + ``` -Currently, wheels on PyPI for PyTorch on Linux come with CUDA 12.1. +=== "ROCm6" -```sh -# in a project -uv add torch + ```toml + [[tool.uv.index]] + name = "torch-rocm" + url = "https://download.pytorch.org/whl/rocm6.2" + explicit = true + ``` -# make sure there is a virtual environment in the directory -uv pip install torch -``` +Note that we also specify the `explicit` option: this prevents packages from being installed from that index unless explicitly pinned to it (see the step below). This means that only PyTorch will be installed from this index, while all other packages will be looked up on PyPI. -### CUDA 12.4 Support +## Pin PyTorch to the custom index -```sh -# in a project -uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch +Now we need to pin specific PyTorch versions to the appropriate indexes. We do this by adding/editing the `sources` section in the `pyproject.toml`. -# make sure there is a virtual environment in the directory -uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch -``` +=== "CPU-only" -## On Windows + Note that we use the `platform_system` marker to instruct `uv` to look into this index on Linux and Windows. -### CPU Only + ```toml + [tool.uv.sources] + torch = [ + { index = "torch-cpu", marker = "platform_system != 'Darwin'"}, + ] + ``` -```sh -# in a project -uv add torch +=== "CUDA 11.8" -# make sure there is a virtual environment in the directory -uv pip install torch -``` + Note that we use the `platform_system` marker to instruct `uv` to look into this index on Linux and Windows. -### CUDA 11.8 Support + ```toml + [tool.uv.sources] + torch = [ + { index = "torch-cu118", marker = "platform_system != 'Darwin'"}, + ] + ``` -```sh -# in a project -uv add --extra-index-url=https://download.pytorch.org/whl/cu118 torch +=== "CUDA 12.1" -# make sure there is a virtual environment in the directory -uv pip install --extra-index-url=https://download.pytorch.org/whl/cu118 torch -``` + Note that we use the `platform_system` marker to instruct `uv` to look into this index on Linux and Windows. -### CUDA 12.1 Support + ```toml + [tool.uv.sources] + torch = [ + { index = "torch-cu121", marker = "platform_system != 'Darwin'"}, + ] + ``` -```sh -# in a project -uv add --extra-index-url=https://download.pytorch.org/whl/cu121 torch +=== "CUDA 12.4" -# make sure there is a virtual environment in the directory -uv pip install --extra-index-url=https://download.pytorch.org/whl/cu121 torch -``` + Note that we use the `platform_system` marker to instruct `uv` to look into this index on Linux and Windows. + + ```toml + [tool.uv.sources] + torch = [ + { index = "torch-cu124", marker = "platform_system != 'Darwin'"}, + ] + ``` + +=== "ROCm6" -### CUDA 12.4 Support + Note that we use the `platform_system` marker to instruct `uv` to look into this index when on Linux only. + + ```toml + [tool.uv.sources] + torch = [ + { index = "torch-rocm", marker = "platform_system == 'Linux'"}, + ] + ``` + +## Add PyTorch to your dependencies + +Finally, we can add PyTorch to the `project.dependencies` section of the `pyproject.toml`. You can do this by hand, or using `uv`: ```sh -# in a project -uv add --extra-index-url=https://download.pytorch.org/whl/cu124 torch +uv add torch +``` + +However, if you want to be more explicit, you could also: -# make sure there is a virtual environment in the directory -uv pip install --extra-index-url=https://download.pytorch.org/whl/cu124 torch +```toml +[project] +dependencies = [ + "torch==2.5.0 ; platform_system == 'Darwin'", + "torch==2.5.0+cu124 ; platform_system != 'Darwin'", +] ``` + +This will install PyTorch 2.5.0 on macOS, and PyTorch 2.5.0+cu124 on Linux and Windows. From ced6b006106c92211de96c89ec3a050261586f96 Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Mon, 21 Oct 2024 11:46:34 +0200 Subject: [PATCH 06/10] docs: add macos version disclaimer --- docs/guides/integration/pytorch.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index 2c3838ea40cf2..cfff560125b2d 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -161,3 +161,7 @@ dependencies = [ ``` This will install PyTorch 2.5.0 on macOS, and PyTorch 2.5.0+cu124 on Linux and Windows. + +!!! warning "PyTorch on Intel Macs" + + Note that the last version to support Intel Macs is PyTorch 2.3.0. In other words, if you try to install PyTorch 2.4.0 or more on an Intel Mac, you will get an error. From acbf6090fd4362d3f98288fb9e7127a59a0f3770 Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Tue, 22 Oct 2024 07:35:06 +0200 Subject: [PATCH 07/10] docs: fix macOS intel latest supported version --- docs/guides/integration/pytorch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index cfff560125b2d..19069401afe5d 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -164,4 +164,4 @@ This will install PyTorch 2.5.0 on macOS, and PyTorch 2.5.0+cu124 on Linux and W !!! warning "PyTorch on Intel Macs" - Note that the last version to support Intel Macs is PyTorch 2.3.0. In other words, if you try to install PyTorch 2.4.0 or more on an Intel Mac, you will get an error. + Note that the last minor version to support Intel Macs is PyTorch 2.2. In other words, if you try to install PyTorch 2.3.0 or more on an Intel Mac, you will get an error. Alternatively, you can install PyTorch with conda From dcca680043f00fd79e3b33b33b1efa07d4133dfe Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Tue, 22 Oct 2024 07:35:21 +0200 Subject: [PATCH 08/10] docs: add torchvision to the example for clarity --- docs/guides/integration/pytorch.md | 49 ++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index 19069401afe5d..feb471f2d7964 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -34,13 +34,13 @@ uv init ## Add PyTorch indexes -Open the `pyproject.toml` file, and create a custom index matching the CUDA version you have available to instruct `uv` where to find the PyTorch wheels. +Open the `pyproject.toml` file, and create a custom index matching the channel you want to use (CPU, NVIDIA or AMD), to instruct `uv` where to find the PyTorch wheels. === "CPU-only" ```toml [[tool.uv.index]] - name = "torch-cpu" + name = "pytorch-cpu" url = "https://download.pytorch.org/whl/cpu" explicit = true ``` @@ -49,7 +49,7 @@ Open the `pyproject.toml` file, and create a custom index matching the CUDA vers ```toml [[tool.uv.index]] - name = "torch-cu118" + name = "pytorch-cu118" url = "https://download.pytorch.org/whl/cu118" explicit = true ``` @@ -58,7 +58,7 @@ Open the `pyproject.toml` file, and create a custom index matching the CUDA vers ```toml [[tool.uv.index]] - name = "torch-cu121" + name = "pytorch-cu121" url = "https://download.pytorch.org/whl/cu121" explicit = true ``` @@ -67,7 +67,7 @@ Open the `pyproject.toml` file, and create a custom index matching the CUDA vers ```toml [[tool.uv.index]] - name = "torch-cu124" + name = "pytorch-cu124" url = "https://download.pytorch.org/whl/cu124" explicit = true ``` @@ -76,16 +76,16 @@ Open the `pyproject.toml` file, and create a custom index matching the CUDA vers ```toml [[tool.uv.index]] - name = "torch-rocm" + name = "pytorch-rocm" url = "https://download.pytorch.org/whl/rocm6.2" explicit = true ``` -Note that we also specify the `explicit` option: this prevents packages from being installed from that index unless explicitly pinned to it (see the step below). This means that only PyTorch will be installed from this index, while all other packages will be looked up on PyPI. +Note the `explicit` option: this prevents packages from being installed from that index unless explicitly pinned to it (see the step below). This means that only PyTorch will be installed from this index, while all other packages will be looked up on PyPI (the default index). ## Pin PyTorch to the custom index -Now we need to pin specific PyTorch versions to the appropriate indexes. We do this by adding/editing the `sources` section in the `pyproject.toml`. +Now you need to pin specific PyTorch versions to the appropriate indexes. The `tool.uv.sources` table in the `pyproject.toml` is a mapping that matches a package to a list of indexes inside of which to look. Note that you need to create a new entry for every library you want to install. In other words, if your project depends on both PyTorch and torchvision, you need to do as follows: === "CPU-only" @@ -94,7 +94,10 @@ Now we need to pin specific PyTorch versions to the appropriate indexes. We do t ```toml [tool.uv.sources] torch = [ - { index = "torch-cpu", marker = "platform_system != 'Darwin'"}, + { index = "pytorch-cpu", marker = "platform_system != 'Darwin'"}, + ] + torchvision = [ + { index = "pytorch-cpu", marker = "platform_system != 'Darwin'"}, ] ``` @@ -105,7 +108,10 @@ Now we need to pin specific PyTorch versions to the appropriate indexes. We do t ```toml [tool.uv.sources] torch = [ - { index = "torch-cu118", marker = "platform_system != 'Darwin'"}, + { index = "pytorch-cu118", marker = "platform_system != 'Darwin'"}, + ] + torchvision = [ + { index = "pytorch-cu118", marker = "platform_system != 'Darwin'"}, ] ``` @@ -116,7 +122,10 @@ Now we need to pin specific PyTorch versions to the appropriate indexes. We do t ```toml [tool.uv.sources] torch = [ - { index = "torch-cu121", marker = "platform_system != 'Darwin'"}, + { index = "pytorch-cu121", marker = "platform_system != 'Darwin'"}, + ] + torchvision = [ + { index = "pytorch-cu121", marker = "platform_system != 'Darwin'"}, ] ``` @@ -127,7 +136,10 @@ Now we need to pin specific PyTorch versions to the appropriate indexes. We do t ```toml [tool.uv.sources] torch = [ - { index = "torch-cu124", marker = "platform_system != 'Darwin'"}, + { index = "pytorch-cu124", marker = "platform_system != 'Darwin'"}, + ] + torchvision = [ + { index = "pytorch-cu124", marker = "platform_system != 'Darwin'"}, ] ``` @@ -138,16 +150,19 @@ Now we need to pin specific PyTorch versions to the appropriate indexes. We do t ```toml [tool.uv.sources] torch = [ - { index = "torch-rocm", marker = "platform_system == 'Linux'"}, + { index = "pytorch-rocm", marker = "platform_system == 'Linux'"}, + ] + torchvision = [ + { index = "pytorch-rocm", marker = "platform_system == 'Linux'"}, ] ``` ## Add PyTorch to your dependencies -Finally, we can add PyTorch to the `project.dependencies` section of the `pyproject.toml`. You can do this by hand, or using `uv`: +Finally, we can add PyTorch to the `project.dependencies` section of the `pyproject.toml`. To install PyTorch and torchvision, run: ```sh -uv add torch +uv add torch torchvision ``` However, if you want to be more explicit, you could also: @@ -157,10 +172,12 @@ However, if you want to be more explicit, you could also: dependencies = [ "torch==2.5.0 ; platform_system == 'Darwin'", "torch==2.5.0+cu124 ; platform_system != 'Darwin'", + "torchvision==0.20.0 ; platform_system == 'Darwin'", + "torchvision==0.20.0+cu124 ; platform_system != 'Darwin'", ] ``` -This will install PyTorch 2.5.0 on macOS, and PyTorch 2.5.0+cu124 on Linux and Windows. +This will install PyTorch 2.5.0 and torchvision 0.19 on macOS, and PyTorch 2.5.0+cu124 with torchvision 0.20.0+cu124 on Linux and Windows. !!! warning "PyTorch on Intel Macs" From 47c228ead75e58c828081685a32549c524890bda Mon Sep 17 00:00:00 2001 From: baggiponte <57922983+baggiponte@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:35:58 +0100 Subject: [PATCH 09/10] docs: Update docs/guides/integration/pytorch.md Co-authored-by: Santiago Castro --- docs/guides/integration/pytorch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index feb471f2d7964..4ba1017f36919 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -1,6 +1,6 @@ # Installing PyTorch with uv -[PyTorch](https://pytorch.org/) is a popular open-source deep-learning learning framework that has first-class support for acceleration via GPUs. Installation, however, can be complex, as you won't find all the wheels for PyTorch on PyPI and you have to manage this through external indexes. This guide aims to help you set up a `pyproject.toml` file using `uv` [indexes features](../../configuration/indexes.md). +[PyTorch](https://pytorch.org/) is a popular open-source deep learning framework that has first-class support for acceleration via GPUs. Installation, however, can be complex, as you won't find all the wheels for PyTorch on PyPI and you have to manage this through external indexes. This guide aims to help you set up a `pyproject.toml` file using `uv` [indexes features](../../configuration/indexes.md). !!! info "Available PyTorch indexes" From c273d487129f786e825f721b11fc609bd6223b98 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 18 Nov 2024 19:59:54 -0500 Subject: [PATCH 10/10] Run Prettier --- docs/guides/integration/pytorch.md | 32 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/docs/guides/integration/pytorch.md b/docs/guides/integration/pytorch.md index 4ba1017f36919..1dcc78ffa000e 100644 --- a/docs/guides/integration/pytorch.md +++ b/docs/guides/integration/pytorch.md @@ -1,6 +1,10 @@ # Installing PyTorch with uv -[PyTorch](https://pytorch.org/) is a popular open-source deep learning framework that has first-class support for acceleration via GPUs. Installation, however, can be complex, as you won't find all the wheels for PyTorch on PyPI and you have to manage this through external indexes. This guide aims to help you set up a `pyproject.toml` file using `uv` [indexes features](../../configuration/indexes.md). +[PyTorch](https://pytorch.org/) is a popular open-source deep learning framework that has +first-class support for acceleration via GPUs. Installation, however, can be complex, as you won't +find all the wheels for PyTorch on PyPI and you have to manage this through external indexes. This +guide aims to help you set up a `pyproject.toml` file using `uv` +[indexes features](../../configuration/indexes.md). !!! info "Available PyTorch indexes" @@ -16,8 +20,11 @@ * https://download.pytorch.org/whl/cpu * https://download.pytorch.org/whl/rocm6.2 (AMD GPUs, only available on Linux) - -The [PyTorch website](https://pytorch.org/get-started/locally/) offers a simple interface to determine what `pip` command you should run to install PyTorch. This guide should help you do the same with `uv`. If the following instructions fail, you might want to check the link for any difference, and open an issue to report the discrepancy. In this case, or if you run into any other issue, please refer to [Getting Help](../../getting-started/help.md). +The [PyTorch website](https://pytorch.org/get-started/locally/) offers a simple interface to +determine what `pip` command you should run to install PyTorch. This guide should help you do the +same with `uv`. If the following instructions fail, you might want to check the link for any +difference, and open an issue to report the discrepancy. In this case, or if you run into any other +issue, please refer to [Getting Help](../../getting-started/help.md). ## Initialise a project @@ -31,10 +38,10 @@ uv init Make sure to use a Python version that is supported by PyTorch. You can find the compatibility matrix [here](https://github.com/pytorch/pytorch/blob/main/RELEASE.md#release-compatibility-matrix). - ## Add PyTorch indexes -Open the `pyproject.toml` file, and create a custom index matching the channel you want to use (CPU, NVIDIA or AMD), to instruct `uv` where to find the PyTorch wheels. +Open the `pyproject.toml` file, and create a custom index matching the channel you want to use (CPU, +NVIDIA or AMD), to instruct `uv` where to find the PyTorch wheels. === "CPU-only" @@ -81,11 +88,16 @@ Open the `pyproject.toml` file, and create a custom index matching the channel y explicit = true ``` -Note the `explicit` option: this prevents packages from being installed from that index unless explicitly pinned to it (see the step below). This means that only PyTorch will be installed from this index, while all other packages will be looked up on PyPI (the default index). +Note the `explicit` option: this prevents packages from being installed from that index unless +explicitly pinned to it (see the step below). This means that only PyTorch will be installed from +this index, while all other packages will be looked up on PyPI (the default index). ## Pin PyTorch to the custom index -Now you need to pin specific PyTorch versions to the appropriate indexes. The `tool.uv.sources` table in the `pyproject.toml` is a mapping that matches a package to a list of indexes inside of which to look. Note that you need to create a new entry for every library you want to install. In other words, if your project depends on both PyTorch and torchvision, you need to do as follows: +Now you need to pin specific PyTorch versions to the appropriate indexes. The `tool.uv.sources` +table in the `pyproject.toml` is a mapping that matches a package to a list of indexes inside of +which to look. Note that you need to create a new entry for every library you want to install. In +other words, if your project depends on both PyTorch and torchvision, you need to do as follows: === "CPU-only" @@ -159,7 +171,8 @@ Now you need to pin specific PyTorch versions to the appropriate indexes. The `t ## Add PyTorch to your dependencies -Finally, we can add PyTorch to the `project.dependencies` section of the `pyproject.toml`. To install PyTorch and torchvision, run: +Finally, we can add PyTorch to the `project.dependencies` section of the `pyproject.toml`. To +install PyTorch and torchvision, run: ```sh uv add torch torchvision @@ -177,7 +190,8 @@ dependencies = [ ] ``` -This will install PyTorch 2.5.0 and torchvision 0.19 on macOS, and PyTorch 2.5.0+cu124 with torchvision 0.20.0+cu124 on Linux and Windows. +This will install PyTorch 2.5.0 and torchvision 0.19 on macOS, and PyTorch 2.5.0+cu124 with +torchvision 0.20.0+cu124 on Linux and Windows. !!! warning "PyTorch on Intel Macs"