Skip to content

Add prototype implementation of wheel variant specification - #12203

Draft
charliermarsh wants to merge 35 commits into
konsti/recheck-installed-wheel-variantsfrom
charlie/wheel-variant
Draft

charliermarsh wants to merge 35 commits into
konsti/recheck-installed-wheel-variantsfrom
charlie/wheel-variant

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Mar 16, 2025 •

Copy link
Copy Markdown
Member

(Branch taken over by @konstin)

See #22043 for a status tracker


This branch contains the prototype implementation of the wheel variants proposal. It builds directly on #22758, the highest branch in the wheel variant tracker. The diff shows the implementation still to review after that stack: 35 commits, about 8,400 added and 2,300 removed lines across 120 files. The work still missing from this branch is listed below. Please keep #22043 open as pieces land.

It supports uv pip install, uv lock and uv sync for the covered cases. Included are:

  • Installing and running explicitly authorized providers, or describing the target without executing a provider.
  • Variant ordering, dependency markers, and universal resolution when wheels have different dependencies.
  • Recording the selected properties, checking installed variants, and selecting again during a frozen sync.
  • Locking and authenticating variants.json, and carrying the metadata that a pylock.toml consumer needs.

Naturally, it is not ready for any serious usage yet. Expect that things break, please report problems.

release The installers below are for the historical WheelNext prototype releases. The commands under latest use this branch’s current interface.

Unix:

curl -LsSf https://astral.sh/uv/install.sh | INSTALLER_DOWNLOAD_URL=https://wheelnext.astral.sh/v0.0.2 sh

Windows:

powershell -c { $env:INSTALLER_DOWNLOAD_URL = 'https://wheelnext.astral.sh/v0.0.3'; irm https://astral.sh/uv/install.ps1 | iex }

On this branch, enable wheel variants with --preview-features wheel-variants or UV_PREVIEW_FEATURES=wheel-variants. This opt-in also applies to direct variant wheels and existing variant lockfiles.

latest Clone the repo and check out charlie/wheel-variant. This self-contained example creates wheels with different dependencies, writes one universal lock, then installs the preferred wheel and its dependency from that lock:

cargo test -p uv --test pip_install --features test-python lock_variant_wheel_specific_dependencies

To try an index serving the current metadata format, set VARIANT_INDEX to its simple-index URL and VARIANT_PACKAGE to a package it serves. As in the original prototype, uv venv creates a local environment for the install:

uv venv
cargo run -- pip install --preview-features wheel-variants --index "$VARIANT_INDEX" "$VARIANT_PACKAGE"

If you don’t have Rust installed, there’s a bootstrapping process from current uv, though it’s slow:

uvx --from "uv @ git+https://github.com/astral-sh/uv@charlie/wheel-variant" \
    uv pip install --preview-features wheel-variants --index "$VARIANT_INDEX" "$VARIANT_PACKAGE"

To describe a target without querying providers, pass a JSON file with --variant-target or UV_VARIANT_TARGET. Compatible values are ordered from most to least preferred. This example describes an x86-64 v3 target; {} describes a target with no variant properties. This is a preview input format, separate from the package’s variants.json.

{ "x86_64": [["level", ["v3", "v2", "v1"]]] }

PEP 825 updates

PEP 825 is provisionally accepted. The provider interface follows the PEP 850 draft. Filename and metadata parsing, basic selection and ordering, static providers, and the NVIDIA and direct-wheel work are already in main or the stack below this PR.

  • Verify index-advertised metadata hashes before caching, record the SHA-256 of the bytes used in uv.lock, and check that digest when reading the sidecar again. A wheel hash authenticates the wheel, not a separate index response. Index metadata
  • Evaluate variant_label and variant_properties for the wheel that declared a dependency. Universal resolution retains alternative wheels and makes differing dependencies conditional. Only labels declared by the verified sidecar contribute variant dependencies; an unlisted wheel cannot displace an ordinary wheel or source archive supplying baseline metadata. Variant markers
  • Select locked artifacts before evaluating their dependencies. A frozen sync can switch targets. It skips sidecars reachable only through an unselected extra, then reports a changed sidecar when that extra is selected. Direct locked wheels are authenticated before their metadata is read. Cached variant wheels must have been checked for ambiguous metadata in the original archive. Before installing a selected variant, uv checks that the wheel declares the same properties for its label as the metadata used to select it. Installation from an index
  • Keep provider execution under the user’s control. An external provider requires both --variant-provider namespace=package@module:object and a trusted --variant-provider-index; a wheel, project, or lockfile cannot grant that permission. Generated requirements and pylock headers omit the provider index, and an invalid provider index is reported without printing its credentials. External providers run as the current user. An explicit cross-platform target doesn’t inherit detected host properties. Provider execution

The local scenarios also cover namespace priority, fallback, and dependency markers from Packse #292.

Work still outstanding

  • Providers. Complete the x86 and Arm providers beyond the x86-64 and Apple Silicon architecture levels here, and add the AMD ROCm and Intel XPU providers from Wheel variant preview implementation tracker #22043. Discover independent providers in parallel during a locked install, as described in Wheel variant preview implementation tracker #22043. External provider environments still need a way to pin and verify every wheel; when hashes are required, supply static target properties.
  • Metadata and builds. Finish source builds that produce a variant wheel, authenticated frozen reads of Git-path wheels, and interoperability against the final metadata and provider specifications.
  • Resolution and reuse. Define the remaining cases where files with the same label disagree about dependencies, where selecting a variant can change which conflict branch needs it, and when an already compatible installation should move to a newly preferred variant.
  • Interchange. A requirements file cannot represent package inclusion conditional on the selected variant. pylock.toml can carry variant metadata, but its package and environment markers cannot express every such condition either. The affected exports fail explicitly. Pylock integration
  • ABI dependencies. abi_dependency remains separate, lower-priority work in Wheel variant preview implementation tracker #22043.

Review order

The commits build on one another, with tests beside the behavior they exercise. The median commit adds 256 lines. The first 23 commits extend the MVP, followed by the correctness and security changes needed to ship it. The shared wheel-cache changes, authenticated metadata reader, and locked selection are separate commits.

  1. 1–4. Carry the selected properties, describe a target, select its registry wheel, and switch an installed variant when the target changes.
  2. 5–13. Evaluate variant markers in the declaring package, resolve the selected wheel’s dependencies, and run an explicitly authorized Python provider.
  3. 14–23. Record the sidecar in uv.lock, select a locked wheel before following its dependencies, sync across targets, and carry per-wheel dependencies through a universal lock and pylock.toml where the format can represent them.
  4. 24–29. Respect --exclude-newer, skip discovery for ineligible wheels, resolve conflict-dependent reachability, and reject exports that would change the package set.
  5. 30–33. Verify sidecar hashes, reject ambiguous metadata in JSON or in a wheel archive, check cached variants, and keep provider-index credentials out of generated files and diagnostics.
  6. 34–35. Recheck optional provider opt-ins before reusing an installed wheel, and reject wheels whose declared properties differ from the metadata used for selection.

@charliermarsh charliermarsh added no-build test:skip Disable running tests for a pull request labels Mar 16, 2025
@charliermarsh
charliermarsh force-pushed the charlie/wheel-variant branch 3 times, most recently from 6ca5626 to f598b60 Compare March 16, 2025 17:38
@warsawnv

warsawnv commented Mar 17, 2025 •

Copy link
Copy Markdown

The build-wheels.sh step gives:

Successfully built wheels/provider_fictional_hw-1.0.0-py3-none-any.whl
+ uv build --out-dir ./wheels --project ./vendor/variantlib
warning: Failed to parse `pyproject.toml` during settings discovery:
  TOML parse error at line 101, column 11
      |
  101 | [[tool.uv.variant]]
      |           ^^^^^^^
  unknown field `variant`, expected one of `required-version`, `native-tls`, `offline`, `no-cache`, `cache-dir`, `preview`, `python-preference`, `python-downloads`, `concurrent-downloads`, `concurrent-builds`, `concurrent-installs`, `index`, `index-url`, `extra-index-url`, `no-index`, `find-links`, `index-strategy`, `keyring-provider`, `allow-insecure-host`, `resolution`, `prerelease`, `fork-strategy`, `dependency-metadata`, `config-settings`, `no-build-isolation`, `no-build-isolation-package`, `exclude-newer`, `link-mode`, `compile-bytecode`, `no-sources`, `upgrade`, `upgrade-package`, `reinstall`, `reinstall-package`, `no-build`, `no-build-package`, `no-binary`, `no-binary-package`, `python-install-mirror`, `pypy-install-mirror`, `publish-url`, `trusted-publishing`, `check-url`, `pip`, `cache-keys`, `override-dependencies`, `constraint-dependencies`, `build-constraint-dependencies`, `environments`, `required-environments`, `conflicts`, `workspace`, `sources`, `managed`, `package`, `default-groups`, `dev-dependencies`, `build-backend`

But then it seems to succeed.

@charliermarsh

Copy link
Copy Markdown
Member Author

Ah sorry. I’ll update the instructions. You can either remove that section from the pyproject.toml, then run the build script, then re-add it. Or run cargo build, then point the build script to ./target/debug/uv instead of your global uv.

@warsawnv

Copy link
Copy Markdown

And the cargo run step fails with:

     Running `target/debug/uv pip install dummy_project --find-links ./wheels --extra-index-url 'https://mockhouse.wheelnext.dev/pep-xxx-variants/' --no-cache --reinstall`
error: No virtual environment found; run `uv venv` to create an environment, or pass `--system` to install into a non-virtual environment

@warsawnv

Copy link
Copy Markdown

Or run cargo build, then point the build script to ./target/debug/uv instead of your global uv

I had a feeling about that!

@charliermarsh

Copy link
Copy Markdown
Member Author

You can run uv venv prior to the cargo run command to create a virtual environment in the current working directory. cargo run will then install into that virtual environment by default.

@warsawnv

Copy link
Copy Markdown
% git diff 
diff --git a/build-wheels.sh b/build-wheels.sh
index 96d89524d..193eed495 100755
--- a/build-wheels.sh
+++ b/build-wheels.sh
@@ -13,12 +13,14 @@
 
 set -euxo pipefail
 
+UV=./target/debug/uv
+
 # Create the destination directory if it doesn't exist.
 rm -rf wheels
 mkdir wheels
 
 # Build the wheels for the fictional hardware provider package.
-uv build --out-dir ./wheels --project ./vendor/provider_fictional_hw
+$UV build --out-dir ./wheels --project ./vendor/provider_fictional_hw
 
 # Build the wheels for the variant library.
-uv build --out-dir ./wheels --project ./vendor/variantlib
+$UV build --out-dir ./wheels --project ./vendor/variantlib

@seemethere

seemethere commented Mar 17, 2025 •

Copy link
Copy Markdown
Contributor
[[tool.uv.variant]]
backend = "provider_fictional_hw.plugin:build"
requires = ["provider_fictional_hw"]

For this configuration I'm curious if this would be end-user facing or would this be package maintainer facing?

From my POV I feel like this would be best served at the package maintainer side where a potential PyTorch packaging config might look like:

[[tool.uv.variant]]
backend = "torch.build.variants:detect_variants"
requires = ["cuda_variants", "rocm_variants", "xpu_variants", "your_fav_hardware_variants"]

fwiw I don't know if requiring people to install variant plugins themselves is necessarily going to be a way to adopt this heavily since this should be something that reduces the friction towards someone installing the correct package for their machine instead of them having to remember which variant plugins they're installing vs. not.

@charliermarsh

Copy link
Copy Markdown
Member Author

I was thinking it would be declared by the package maintainer, but possibly overridable by the end user (so, like, if they don't care about your_fav_hardware_variants, they can "skip" that variant provider).

@charliermarsh
charliermarsh force-pushed the charlie/wheel-variant branch from f598b60 to 189dd02 Compare March 17, 2025 23:14
@charliermarsh

Copy link
Copy Markdown
Member Author

(Applied your changes, thanks @warsawnv.)

Comment thread crates/uv-resolver/Cargo.toml Resolved
@konstin
konstin force-pushed the charlie/wheel-variant branch 4 times, most recently from 5ae7861 to f168655 Compare July 29, 2025 14:38
Record the location and SHA-256 of the exact sidecar used to resolve a package, independently of the hashes of its wheels.
Let the locked graph choose an artifact independently of walking the dependencies reachable from it.
… time

Apply the upload-time cutoff before preferring an ordinary wheel as a metadata source.
Provider authorization and package resolution have distinct trust boundaries. Credential-bearing provider URLs must stay out of generated command headers and argument errors.
vec![Index::from_index_url(index.clone())],
vec![],
vec![],
false,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Honor --no-index when resolving external providers

The provider resolver hard-codes no_index to false. With the wheel-variants preview and an external provider configured in the environment, a wheel from a local --find-links directory can trigger a registry download during uv pip install --no-index. uv then executes the downloaded provider under the user’s account. Inherit the invocation’s index policy when constructing the provider resolver.

Suggested change
false,
dispatch.index_locations.no_index(),

flat_index: &flat_index,
base_hasher: &hasher,
build_hash_checking: HashCheckingMode::Verify,
exclude_newer: ExcludeNewer::default(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Apply the dependency cooldown to provider wheels

Resetting exclude_newer lets the provider resolver install a provider or transitive dependency uploaded after the operator’s --exclude-newer cutoff. When an authorized provider has an unpinned dependency, a new release on the authorized index can run under the user’s account during variant detection, before the configured cooldown expires.

Suggested change
exclude_newer: ExcludeNewer::default(),
exclude_newer: ExcludeNewer::clone(&dispatch.exclude_newer),

BuildIsolation::SharedPackage(venv, packages)
}
};
let variant_client = has_variants.then(build_client).transpose()?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Suppress host provider execution during cross-platform sync

uv sync --frozen --python-platform uses the requested platform for wheel tags but leaves VariantPolicy::explicit_platform false. With the wheel-variants preview and an external provider enabled, a reachable sidecar can trigger Python code on the build host and use its hardware properties to choose the wheel and dependencies for the target. Set the flag on the policy shared by selection and installation, as the pip commands do.

Suggested change
let variant_client = has_variants.then(build_client).transpose()?;
let variant_policy = &variant_policy
.clone()
.with_explicit_platform(python_platform.is_some());
let variant_client = has_variants.then(build_client).transpose()?;

let mut table = InlineTable::new();
match &self.url {
VariantsJsonSource::Url { url } => {
table.insert("url", Value::from(url.as_ref()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Keep sidecar download credentials out of uv.lock

VariantsJsonEntry retains the advertised sidecar URL, and this write copies its raw string into uv.lock. If a private index returns a sidecar link with HTTP credentials or a signed query, anyone who receives the lockfile can reuse that authorization while it remains valid, even when the recorded index and wheel URLs are clean. Persist a credential-free locator and obtain download authorization at fetch time, using the locked SHA-256 to verify the sidecar.

.iter()
.enumerate()
.skip(first_candidate)
.filter(|(_, wheel)| wheel.index == sidecar.index)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Exclude already-yanked variants from new locks

On an index that yanks individual files, a new unpinned resolution can select a release through its ordinary wheel while retaining a variant already marked IncompatibleWheel::Yanked. The lock drops that status, and frozen sync ranks the retained variant by tags and sidecar properties. It can therefore install a wheel the index had already withdrawn for a security issue. Exclude rejected variants before collecting their dependencies and writing the lock, while the resolver still knows whether an explicit pin permits the yank.

let ordinary = registry.best_wheel().filename.variant().is_none();
return Ok(ordinary.then_some((node, None)));
};
registry.best_wheel_index = index;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Enforce the locked size of the selected registry variant

The selected registry wheel retains size_is_authoritative = false, so the downloader treats its uv.lock size as a progress hint. If a registry entry records a size without a digest, uv sync --frozen can install a different-length replacement from the artifact host. A recorded digest still protects the wheel contents when present.

Suggested change
registry.best_wheel_index = index;
registry.best_wheel_index = index;
registry.wheels[index].size_is_authoritative = true;

This branch was successfully deployed

1 active deployment
automations — 47b3370b Deployed Nov 23, 2026 by konsti-openai via check | crates / cargo-vet in . #54448
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:skip Disable running tests for a pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants