Using uv with PyTorchPyTorch と一緒に uv を使用する
The PyTorch ecosystem is a popular choice for deep learning research and development. You can use uv to manage PyTorch projects and PyTorch dependencies across different Python versions and environments, even controlling for the choice of accelerator (e.g., CPU-only vs. CUDA).このPyTorchエコシステムは、深層学習の研究と開発に人気の選択肢です。uvを使用して、異なるPythonバージョンや環境間でPyTorchプロジェクトとPyTorch依存関係を管理できます。アクセラレータの選択(例:CPUのみ vs. CUDA)を制御することも可能です。
Note注意
Some of the features outlined in this guide require uv version 0.5.3 or later. We recommend upgrading prior to configuring PyTorch.このガイドに記載されている機能のいくつかは、uvバージョン0.5.3以降が必要です。PyTorchの設定を行う前にアップグレードすることをお勧めします。
Installing PyTorchPyTorchのインストール
From a packaging perspective, PyTorch has a few uncommon characteristics:パッケージングの観点から、PyTorchにはいくつかの一般的でない特性があります:
- Many PyTorch wheels are hosted on a dedicated index, rather than the Python Package Index (PyPI). As such, installing PyTorch often requires configuring a project to use the PyTorch index.多くのPyTorchホイールは、Pythonパッケージインデックス(PyPI)ではなく、専用のインデックスにホストされています。そのため、PyTorchをインストールするには、プロジェクトをPyTorchインデックスを使用するように設定する必要があります。
- PyTorch produces distinct builds for each accelerator (e.g., CPU-only, CUDA). Since there's no
standardized mechanism for specifying these accelerators when publishing or installing, PyTorch
encodes them in the local version specifier. As such, PyTorch versions will often look like
2.5.1+cpu,2.5.1+cu121, etc.PyTorchは、各アクセラレータ(例:CPUのみ、CUDA)用に異なるビルドを生成します。これらのアクセラレータを公開またはインストールする際に指定する標準化されたメカニズムがないため、PyTorchはローカルバージョンスペシファイアにそれらをエンコードします。そのため、PyTorchのバージョンはしばしば2.5.1+cpu、2.5.1+cu121のようになります。 - Builds for different accelerators are published to different indexes. For example, the
+cpubuilds are published on https://download.pytorch.org/whl/cpu, while the+cu121builds are published on https://download.pytorch.org/whl/cu121.異なるアクセラレータ用のビルドは異なるインデックスに公開されます。たとえば、+cpuビルドはhttps://download.pytorch.org/whl/cpuに公開され、+cu121ビルドはhttps://download.pytorch.org/whl/cu121に公開されます。
As such, the necessary packaging configuration will vary depending on both the platforms you need to support and the accelerators you want to enable.そのため、必要なパッケージ構成は、サポートするプラットフォームと有効にしたいアクセラレーターの両方によって異なります。
To start, consider the following (default) configuration, which would be generated by running
uv init --python 3.12 followed by uv add torch torchvision.まず、次の(デフォルトの)構成を考慮してください。これは、uv init --python 3.12を実行し、その後にuv add torch torchvisionを実行することで生成されます。
In this case, PyTorch would be installed from PyPI, which hosts CPU-only wheels for Windows and macOS, and GPU-accelerated wheels on Linux (targeting CUDA 12.6):この場合、PyTorchはPyPIからインストールされ、WindowsおよびmacOS用のCPU専用ホイールと、Linux用のGPUアクセラレーションホイール(CUDA 12.6をターゲット)をホストします。
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"torch>=2.7.0",
"torchvision>=0.22.0",
]
Supported Python versionsサポートされているPythonバージョン
At time of writing, PyTorch does not yet publish wheels for Python 3.14; as such projects with
requires-python = ">=3.14" may fail to resolve. See the
compatibility matrix.執筆時点では、PyTorchはまだPython 3.14用のホイールを公開していません。そのため、requires-python = ">=3.14"を持つプロジェクトは解決に失敗する可能性があります。互換性マトリックスを参照してください。
This is a valid configuration for projects that want to use CPU builds on Windows and macOS, and CUDA-enabled builds on Linux. However, if you need to support different platforms or accelerators, you'll need to configure the project accordingly.これは、WindowsおよびmacOSでCPUビルドを使用し、LinuxでCUDA対応ビルドを使用したいプロジェクトにとって有効な構成です。ただし、異なるプラットフォームやアクセラレーターをサポートする必要がある場合は、プロジェクトをそれに応じて構成する必要があります。
Using a PyTorch indexPyTorchインデックスの使用
In some cases, you may want to use a specific PyTorch variant across all platforms. For example, you may want to use the CPU-only builds on Linux too.場合によっては、すべてのプラットフォームで特定のPyTorchバリアントを使用したいことがあります。たとえば、LinuxでもCPU専用ビルドを使用したい場合です。
In such cases, the first step is to add the relevant PyTorch index to your pyproject.toml:その場合、最初のステップは、関連するPyTorchインデックスをpyproject.tomlに追加することです。
We recommend the use of explicit = true to ensure that the index is only used for torch,
torchvision, and other PyTorch-related packages, as opposed to generic dependencies like jinja2,
which should continue to be sourced from the default index (PyPI).explicit = trueの使用を推奨します。これにより、インデックスがのみtorch、torchvision、およびその他のPyTorch関連パッケージに使用され、jinja2のような一般的な依存関係はデフォルトインデックス(PyPI)から引き続き取得されることが保証されます。
Next, update the pyproject.toml to point torch and torchvision to the desired index:次に、pyproject.tomlを更新して、torchとtorchvisionを希望のインデックスにポイントします:
PyTorch doesn't publish CUDA builds for macOS. As such, we gate on sys_platform to instruct uv to use
the PyTorch index on Linux and Windows, but fall back to PyPI on macOS:PyTorchはmacOS用のCUDAビルドを公開していません。そのため、sys_platformを使用して、uvにLinuxとWindowsでPyTorchインデックスを使用するよう指示しますが、macOSではPyPIにフォールバックします:
PyTorch doesn't publish CUDA builds for macOS. As such, we gate on sys_platform to instruct uv to limit
the PyTorch index to Linux and Windows, falling back to PyPI on macOS:PyTorchはmacOS用のCUDAビルドを公開していません。そのため、sys_platformを使用して、uvにPyTorchインデックスをLinuxとWindowsに制限するよう指示し、macOSではPyPIにフォールバックします:
PyTorch doesn't publish CUDA builds for macOS. As such, we gate on sys_platform to instruct uv to limit
the PyTorch index to Linux and Windows, falling back to PyPI on macOS:PyTorchはmacOS用のCUDAビルドを公開していません。そのため、sys_platformを使用して、uvにPyTorchインデックスをLinuxとWindowsに制限するよう指示し、macOSではPyPIにフォールバックします:
PyTorch doesn't publish ROCm6 builds for macOS or Windows. As such, we gate on sys_platform to instruct uv
to limit the PyTorch index to Linux, falling back to PyPI on macOS and Windows:PyTorchはmacOSやWindows用のROCm6ビルドを公開していません。そのため、sys_platformを使用して、uvにPyTorchインデックスをLinuxに制限するよう指示し、macOSとWindowsではPyPIにフォールバックします:
[tool.uv.sources]
torch = [
{ index = "pytorch-rocm", marker = "sys_platform == 'linux'" },
]
torchvision = [
{ index = "pytorch-rocm", marker = "sys_platform == 'linux'" },
]
# ROCm6 support relies on `pytorch-triton-rocm`, which should also be installed from the PyTorch index
# (and included in `project.dependencies`).
pytorch-triton-rocm = [
{ index = "pytorch-rocm", marker = "sys_platform == 'linux'" },
]
PyTorch doesn't publish Intel GPU builds for macOS. As such, we gate on sys_platform to instruct uv to limit
the PyTorch index to Linux and Windows, falling back to PyPI on macOS:PyTorchはmacOS用のIntel GPUビルドを公開していません。そのため、sys_platformを使用して、uvにPyTorchインデックスをLinuxとWindowsに制限するよう指示し、macOSではPyPIにフォールバックします:
[tool.uv.sources]
torch = [
{ index = "pytorch-xpu", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
torchvision = [
{ index = "pytorch-xpu", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
# Intel GPU support relies on `pytorch-triton-xpu`, which should also be installed from the PyTorch index
# (and included in `project.dependencies`).
pytorch-triton-xpu = [
{ index = "pytorch-xpu", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
As a complete example, the following project would use PyTorch's CPU-only builds on all platforms:完全な例として、以下のプロジェクトはすべてのプラットフォームでPyTorchのCPU専用ビルドを使用します:
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = [
"torch>=2.7.0",
"torchvision>=0.22.0",
]
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu" },
]
torchvision = [
{ index = "pytorch-cpu" },
]
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
Configuring accelerators with environment markers環境マーカーを使用したアクセラレーターの設定
In some cases, you may want to use CPU-only builds in one environment (e.g., macOS and Windows), and CUDA-enabled builds in another (e.g., Linux).場合によっては、1つの環境(例:macOSやWindows)でCPU専用ビルドを使用し、別の環境(例:Linux)でCUDA対応ビルドを使用したいことがあります。
With tool.uv.sources, you can use environment markers to specify the desired index for each
platform. For example, the following configuration would use PyTorch's CUDA-enabled builds on Linux,
and CPU-only builds on all other platforms (e.g., macOS and Windows):tool.uv.sourcesを使用すると、環境マーカーを使用して各プラットフォームの希望のインデックスを指定できます。たとえば、以下の設定では、LinuxでPyTorchのCUDA対応ビルドを使用し、他のすべてのプラットフォーム(例:macOSやWindows)ではCPU専用ビルドを使用します:
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = [
"torch>=2.7.0",
"torchvision>=0.22.0",
]
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu", marker = "sys_platform != 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
]
torchvision = [
{ index = "pytorch-cpu", marker = "sys_platform != 'linux'" },
{ index = "pytorch-cu128", marker = "sys_platform == 'linux'" },
]
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
[[tool.uv.index]]
name = "pytorch-cu128"
url = "https://download.pytorch.org/whl/cu128"
explicit = true
Similarly, the following configuration would use PyTorch's AMD GPU builds on Linux, and CPU-only builds on Windows and macOS (by way of falling back to PyPI):同様に、以下の構成はLinuxでPyTorchのAMD GPUビルドを使用し、WindowsおよびmacOSではCPU専用ビルドを使用します(PyPIにフォールバックすることによって)。
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = [
"torch>=2.7.0",
"torchvision>=0.22.0",
"pytorch-triton-rocm>=3.3.0 ; sys_platform == 'linux'",
]
[tool.uv.sources]
torch = [
{ index = "pytorch-rocm", marker = "sys_platform == 'linux'" },
]
torchvision = [
{ index = "pytorch-rocm", marker = "sys_platform == 'linux'" },
]
pytorch-triton-rocm = [
{ index = "pytorch-rocm", marker = "sys_platform == 'linux'" },
]
[[tool.uv.index]]
name = "pytorch-rocm"
url = "https://download.pytorch.org/whl/rocm6.3"
explicit = true
Or, for Intel GPU builds:また、Intel GPUビルドの場合は:
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = [
"torch>=2.7.0",
"torchvision>=0.22.0",
"pytorch-triton-xpu>=3.3.0 ; sys_platform == 'win32' or sys_platform == 'linux'",
]
[tool.uv.sources]
torch = [
{ index = "pytorch-xpu", marker = "sys_platform == 'win32' or sys_platform == 'linux'" },
]
torchvision = [
{ index = "pytorch-xpu", marker = "sys_platform == 'win32' or sys_platform == 'linux'" },
]
pytorch-triton-xpu = [
{ index = "pytorch-xpu", marker = "sys_platform == 'win32' or sys_platform == 'linux'" },
]
[[tool.uv.index]]
name = "pytorch-xpu"
url = "https://download.pytorch.org/whl/xpu"
explicit = true
Configuring accelerators with optional dependenciesオプションの依存関係を持つアクセラレータの構成
In some cases, you may want to use CPU-only builds in some cases, but CUDA-enabled builds in others,
with the choice toggled by a user-provided extra (e.g., uv sync --extra cpu vs.
uv sync --extra cu128).場合によっては、CPU専用ビルドを使用したいが、他の場合ではCUDA対応ビルドを使用したいことがあります。その選択はユーザー提供のエクストラによって切り替えられます(例:uv sync --extra cpu 対 uv sync --extra cu128)。
With tool.uv.sources, you can use extra markers to specify the desired index for each enabled
extra. For example, the following configuration would use PyTorch's CPU-only for
uv sync --extra cpu and CUDA-enabled builds for uv sync --extra cu128:tool.uv.sourcesを使用すると、各有効なエクストラのために希望するインデックスを指定するためのエクストラマーカーを使用できます。たとえば、以下の構成は、uv sync --extra cpuに対してPyTorchのCPU専用を使用し、uv sync --extra cu128に対してCUDA対応ビルドを使用します。
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12.0"
dependencies = []
[project.optional-dependencies]
cpu = [
"torch>=2.7.0",
"torchvision>=0.22.0",
]
cu128 = [
"torch>=2.7.0",
"torchvision>=0.22.0",
]
[tool.uv]
conflicts = [
[
{ extra = "cpu" },
{ extra = "cu128" },
],
]
[tool.uv.sources]
torch = [
{ index = "pytorch-cpu", extra = "cpu" },
{ index = "pytorch-cu128", extra = "cu128" },
]
torchvision = [
{ index = "pytorch-cpu", extra = "cpu" },
{ index = "pytorch-cu128", extra = "cu128" },
]
[[tool.uv.index]]
name = "pytorch-cpu"
url = "https://download.pytorch.org/whl/cpu"
explicit = true
[[tool.uv.index]]
name = "pytorch-cu128"
url = "https://download.pytorch.org/whl/cu128"
explicit = true
Note注意
Since GPU-accelerated builds aren't available on macOS, the above configuration will fail to install
on macOS when the cu128 extra is enabled.GPUアクセラレートビルドはmacOSでは利用できないため、上記の構成はcu128エクストラが有効な場合、macOSにインストールすることに失敗します。
The uv pip interfaceuv pipインターフェース
While the above examples are focused on uv's project interface (uv lock, uv sync, uv run,
etc.), PyTorch can also be installed via the uv pip interface.上記の例はuvのプロジェクトインターフェース(uv lock、uv sync、uv runなど)に焦点を当てていますが、PyTorchはuv pipインターフェースを介してもインストールできます。
PyTorch itself offers a dedicated interface to determine the appropriate pip command to run for a given target configuration. For example, you can install stable, CPU-only PyTorch on Linux with:PyTorch自体は、特定のターゲット構成に対して実行する適切なpipコマンドを決定するための専用インターフェースを提供しています。たとえば、Linuxで安定したCPU専用のPyTorchを次のようにインストールできます:
To use the same workflow with uv, replace pip3 with uv pip:uvで同じワークフローを使用するには、pip3をuv pipに置き換えます:
Automatic backend selection自動バックエンド選択
uv supports automatic selection of the appropriate PyTorch index via the --torch-backend=auto
command-line argument (or the UV_TORCH_BACKEND=auto environment variable), as in:uvは、--torch-backend=auto
コマンドライン引数(またはUV_TORCH_BACKEND=auto環境変数)を介して、適切なPyTorchインデックスの自動選択をサポートしています。具体的には、
$ # With a command-line argument.
$ uv pip install torch --torch-backend=auto
$ # With an environment variable.
$ UV_TORCH_BACKEND=auto uv pip install torch
When enabled, uv will query for the installed CUDA driver, AMD GPU versions, and Intel GPU presence,
then use the most-compatible PyTorch index for all relevant packages (e.g., torch, torchvision,
etc.). If no such GPU is found, uv will fall back to the CPU-only index. uv will continue to respect
existing index configuration for any packages outside the PyTorch ecosystem.有効にすると、uvはインストールされているCUDAドライバー、AMD GPUのバージョン、およびIntel GPUの存在を照会し、
関連するすべてのパッケージ(例:torch、torchvisionなど)に対して最も互換性のあるPyTorchインデックスを使用します。該当するGPUが見つからない場合、uvはCPU専用インデックスにフォールバックします。uvは、PyTorchエコシステム外のパッケージに対して既存のインデックス設定を尊重し続けます。
You can also select a specific backend (e.g., CUDA 12.6) with --torch-backend=cu126 (or
UV_TORCH_BACKEND=cu126):特定のバックエンド(例:CUDA 12.6)を選択することもできます。--torch-backend=cu126(またはUV_TORCH_BACKEND=cu126)を使用します:
$ # With a command-line argument.
$ uv pip install torch torchvision --torch-backend=cu126
$ # With an environment variable.
$ UV_TORCH_BACKEND=cu126 uv pip install torch torchvision
At present, --torch-backend is only available in the uv pip interface.現在、--torch-backendはuv pipインターフェースでのみ利用可能です。