Skip to content

[native_aot] Install DSL runtimes for supported Pythons - #196747

Closed
slayton58 wants to merge 5 commits into
gh/slayton58/173/basefrom
gh/slayton58/173/head
Closed

slayton58 wants to merge 5 commits into
gh/slayton58/173/basefrom
gh/slayton58/173/head

Conversation

@slayton58

@slayton58 slayton58 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Human Notes:

Fix the manywheel build issue on 3.10 that caused the revert of #190899

Codex:

The native-AOT verdict considers CPython 3.10 and 3.11 supported by the pinned
DSL wheels, but install_cutlass_dsl returned success without installing anything
below Python 3.12. Once the first declaration enabled stage 2, CUDA manywheel
therefore failed when it required the missing cutlass and tvm_ffi modules.

Remove the duplicate interpreter gate so stage 2 remains the single owner of
eligibility, and select the CUDA 13 extra so those builds install the matching
dialect runtime.

Once installation was enabled, Python 3.10 exposed a second issue in parallel
export. Affected CPython releases start the forkserver through python -c without
first applying the parent process's sys.path. The server therefore preloaded the
checkout's torch instead of the newly installed wheel, silently swallowed that
failed import, and forked workers with torch's one-import guard set.

Preload torch once on CPython releases containing the forkserver fix. On older
releases, disable only preloading so each worker imports torch after restoring
the parent's sys.path. This keeps direct exporter invocation correct as well as
the stage-2 path. Unrelated builder import failures are also reported as such
instead of being misdiagnosed as missing DSL runtimes.

Changing only the stage-2 working directory would leave direct invocation
broken. A custom preload helper could preserve copy-on-write imports on affected
Pythons, but adds an environment protocol and path rewriting for a startup
optimization. Version-gated preloading keeps the compatibility path smaller.

Skipping native-AOT below Python 3.12 would be smaller, but would unnecessarily
omit embedded kernels from supported wheels.

Authored with assistance from an AI coding assistant.

Test Plan:

PYTHONPATH=. python tools/test/test_native_aot_tools.py
bash -n .ci/pytorch/common_utils.sh

[ghstack-poisoned]
@slayton58
slayton58 requested a review from a team as a code owner September 11, 2026 12:29
@pytorch-bot

pytorch-bot Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/196747

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 7327f75 with merge base 335414f (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
[ghstack-poisoned]
@ngimel

ngimel commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

astra suggestion

  I see two simpler alternatives to the PR’s environment variable and helper module:

  1. For the stage-2 build, change the exporter’s working directory. At tools/native_aot/build_stage2.py:905:

     _run_child(export, "exporting kernels", cwd=HERE, env=env)

     Its existing torch probes already use this approach. The exporter filename and artifact directory are absolute, so this preserves the normal build’s paths
     and retains the preload optimization. It doesn’t cover someone invoking export.py directly from the checkout root on older Python.

  2. For a general solution across entry points, disable preloading on affected Python versions:

     ctx.set_forkserver_preload([])

     Keep forkserver. Workers restore the parent’s sys.path before importing the builder, so they select the correct torch. The cost is importing torch
     separately in each worker. Switching to spawn also works, with the same loss of shared torch initialization.

  I verified with a small shadowed-package probe: Python 3.10.20 and 3.12.0 preload the wrong package; 3.12.13 selects the correct one. Both changing cwd and
  disabling preloading fix the older-version case. Environment variables inherited in every case.

  My preference would be ordinary torch preloading on fixed Python versions, empty preloading on older ones. That handles direct invocation too, with much less
  machinery than this PR.

@slayton58

Copy link
Copy Markdown
Contributor Author

Option 2 looks to be more suitable, and significantly simpler -- switching to that approach and adding appropriate testing.

[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #190899

pytorchmergebot pushed a commit that referenced this pull request Sep 14, 2026
Human Notes:

Necessary to make 3.10 builds not fail CI when AOT ops come in.

Note: We could change schema behavior to be consistent across 3.10 onwards, but
that feels like a much bigger risk than this..

Codex:

Installing CuTeDSL on Python 3.10 made the vendored QuACK import tests execute
there for the first time. QuACK's custom-op signatures use IntEnum defaults,
whose symbolic Python 3.10 string form produces invalid inferred dispatcher
schemas before any test body can run.

This is independent of native-AOT and its Python 3.10 support. Skip only the
QuACK import and operator-registration tests below Python 3.11; the patch-series
and vendoring reproducibility tests remain enabled. Normalizing IntEnum defaults
in torch.library would also make these imports work, but would change core
Python 3.10 behavior for an unrelated compatibility check.

Authored with assistance from an AI coding assistant.

Test Plan:

```
python - <<'PY'
import runpy
import sys
from unittest import mock

sys.path.pop(0)
import torch

with mock.patch.object(sys, "version_info", (3, 10)):
    module = runpy.run_path("test/python_native/test_quack_vendor.py")

quack = module["TestQuackVendor"]
patches = module["TestQuackFlexGemmPatches"]
assert quack.__unittest_skip__
assert quack.__unittest_skip_why__ == "QuACK custom-op imports require Python 3.11 or newer"
assert not getattr(patches, "__unittest_skip__", False)
PY
```
Pull Request resolved: #196757
Approved by: https://github.com/ngimel
ghstack dependencies: #196747
pytorchmergebot pushed a commit that referenced this pull request Sep 14, 2026
…e2e tests (#190899)

Human Notes:

Finally do a thing! Add `topk` as an AOT-provided override in libtorch_cuda.

Claude:

Declares a 48-point grid (fp32/bf16 x N{2048..16384} x K{64,128,256}
x both determinism modes) over the radix-select kernel. The det
specialization (stable prefix-sum gather + lex (ord, -idx) sort) is
bit-exact vs aten including tie resolution, so det-mode calls are
served AOT too; the dispatch chain keys on
deterministicAlgorithms(). The kernel gains
in_dtype and index_dtype compile parameters; the AOT route writes
int64 indices directly (no widening copy). build(spec) lives beside
the JIT wrappers, so both routes compile the same class. fp16 and
off-grid shapes keep their JIT eligibility, as do calls the prelude
declines: a non-last dim, largest/sorted false, sub-full-wave M, or a
base pointer less aligned than the kernels were compiled for.

ARCHS is stated rather than defaulted, and covers Hopper as well as
Blackwell: measured on an H100, the AOT kernels beat aten on 48/48 of
the grid, median 2.16x (min 1.26x at M=4096/N=2048, max 2.99x at
M=256/N=4096 fp32). Both spellings of each capability are listed --
archs_from_cuda_arch_list maps 9.0 and 9.0a to different nvcc targets,
and a wheel built with the plain list would otherwise export trees
generation then filters out.

CI: a native_aot test config (test.sh) asserts the wheel actually
embeds the kernels -- a silently artifact-free wheel would make every
routing test vacuously pass on the JIT/aten fallbacks -- then runs the
python_native suites. One build+test pair per capability in
native-aot.yml: cuda-arch-list 9.0a tested on linux.aws.h100 and
10.0a on linux.dgx.b200, both built on a GPU-less builder
(path-triggered + nightly). test_uncovered_fp32_served_by_jit_layer
skips below sm_100, since the JIT topk override is gated there while
these AOT kernels are not. This lands with the first op so the AOT path
is exercised in CI from this commit onward.

covered_axes() and cpp_covers() decline a dim other than the last, largest=False
and sorted=False, matching the stub's gates. Coverage wider than the stub is not
wrong -- the stub declines and stock aten serves -- but it costs those calls
their JIT route, since coverage subtracts them from it (review: #190899).

Authored with an AI assistant (Claude Code).

Test Plan:

```
.venv/bin/python test/python_native/test_native_aot.py
# incl. det-mode routing + bit-exact-with-ties tests
# (test_deterministic_mode_routes_to_aot_bit_exact,
#  test_deterministic_ties_bit_exact)
.venv/bin/python test/python_native/test_topk_radix_kernel.py
.venv/bin/python test/python_native/test_topk_cutedsl.py
```

On an H100, against the kernels a `spin develop` embedded:

```
.venv/bin/python test/python_native/test_native_aot.py    # 11 tests, 1 skipped
.venv/bin/python test/python_native/test_aot_manifest.py  # 18 tests
.venv/bin/python test/python_native/test_registry.py      # 35 tests
```
Pull Request resolved: #190899
Approved by: https://github.com/MatthiasKohl, https://github.com/ngimel
ghstack dependencies: #196747, #196757
@github-actions
github-actions Bot deleted the gh/slayton58/173/head branch November 19, 2026 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants