This guide explains how to set up fairseq2 using UV, a modern Python package installer and resolver.
We will show you how to configure the pyproject.toml file to install fairseq2 with various version presets.
With UV, you can:
Create reproducible environments and avoid conflicts (CPU vs. various CUDA versions).
Unify environment specifications in a single pyproject.toml.
Easily pick “extras” for hardware support (--extracpu, --extracu121, etc.) and “groups” for extra features (dev, lint, data, experimental, etc.).
The pyproject.toml file is a simplified example. You can customize it to fit your needs.
We will explain the structure of the pyproject.toml file in the next section.
In the pyproject.toml file, we have defined the following conflicting extras:
cpu: CPU-only PyTorch and fairseq2
cu121: CUDA 12.1 PyTorch and fairseq2
cu124: CUDA 12.4 PyTorch and fairseq2
We have also defined the following groups:
dev: Development tools
lint: Linting tools
data: Data processing tools
experimental: Experimental features
For each extra, we have defined the following index sources to pull the corresponding PyTorch and fairseq2 packages:
pytorch-cpu: CPU-only PyTorch
pytorch-cu121: CUDA 12.1 PyTorch
pytorch-cu124: CUDA 12.4 PyTorch
fairseq2-cpu: CPU-only fairseq2 (pinned to a specific commit in fairseq2)
fairseq2-cu121: CUDA 12.1 fairseq2
fairseq2-cu124: CUDA 12.4 fairseq2
Below are a few common workflows. Adjust the commands to suit your environment and cluster:
# (1) Sync with CPU extra, which is handy for ci pipelines
uvsync--extracpu
# (2) Lint checks
uvxruffcheck.
uvxruffformat--check.
uvrun--extracpu--with=types-PyYAMLmypy
# (3) Run tests
uvrun--extracpupytest-rP--verbose
Switch CUDA versions easily:
# Switch to CUDA 12.4
uvsync--extracu124
# Run fairseq2 from the new environment
uvrunfairseq2assetslist
# Or activate .venvsource.venv/bin/activate
fairseq2assetslist
For local development or “editable installs” of fairseq2:
# Once the environment is set up, install fairseq2 in editable mode:source.venv/bin/activate
uvpipinstall-e/path/to/fairseq2
Extras
Defined in [project.optional-dependencies]. They are typically mutually exclusive variants (e.g., CPU vs. CUDA).
Groups
Listed in [dependency-groups] and combined in default-groups under [tool.uv]. They are additive sets of packages
(e.g., dev tools, lint, data).
Conflicts
Prevent mixing hardware extras that don’t make sense together (e.g., CPU + CUDA).
Index Sources
Let you specify custom or nightly wheels in [tool.uv.sources]. For each extra, UV pulls from the correct index or pinned Git revision.
That’s all! With UV, setting up fairseq2 can be as simple as one line of code.
Enjoy faster environment creation, conflict-free installs, and effortless reproducibility.