Skip to content

Set Python_SOABI when cross-compiling so extension modules get a valid suffix - #189388

Closed
zklaus wants to merge 1 commit into
pytorch:mainfrom
zklaus:fix-c-soabi-crosscompile
Closed

zklaus wants to merge 1 commit into
pytorch:mainfrom
zklaus:fix-c-soabi-crosscompile

Conversation

@zklaus

@zklaus zklaus commented Jul 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Since #180243 the torch._C extension is built by CMake via
Python_add_library(_C MODULE WITH_SOABI ...). WITH_SOABI names the module
using Python_SOABI, which CMake's FindPython only computes by running the
interpreter when not cross-compiling: under CMAKE_CROSSCOMPILING the query
is skipped unless CMAKE_CROSSCOMPILING_EMULATOR is set (policy CMP0190), so
Python_SOABI is left empty and WITH_SOABI silently emits an untagged _C.so.

That breaks cross builds: the extension is installed under a name the target
interpreter will not import, so import torch._C fails. This is the conda-forge
linux-aarch64 failure in
pytorch-cpu-feedstock#526.

When this was first diagnosed the symptom was different: setup.py collected the
extension via package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}", the
untagged name did not match, and _C was dropped from the wheel entirely.
#180248 has since removed setup.py, and _C is now collected by
install(TARGETS _C ...), so it ships under the wrong name rather than going
missing. Either way the target cannot import it.

#190003 has since widened which builds hit this. It forwards CMAKE_SYSTEM_NAME
and CMAKE_SYSTEM_PROCESSOR from the environment before project(), which is
exactly what puts CMake into cross-compile mode, so a cross setup that passes
them that way now takes the empty-Python_SOABI path too. Such a build
previously stayed in native mode -- CMAKE_CROSSCOMPILING false, Python_SOABI
computed normally -- and so never produced an untagged module. That makes this
the completion of #190003 rather than a conda-forge-only concern.

CMake's sanctioned cross path (CMP0190 + an emulator) assumes the target
interpreter is run under qemu/wine. That does not fit conda-forge's crossenv,
where the interpreter is a build-native binary that reports the target's config
and is directly runnable -- no emulator. So when Python_SOABI is empty while
cross-compiling, we query SOABI from the interpreter (which crossenv answers
with the target's value) and set it once, right where FindPython resolves the
Development.Module component. Every downstream WITH_SOABI consumer (_C, and
the CUDA-only _nccl_ep) then gets the correct suffix, and WITH_SOABI still
composes the platform-specific filename.

The override deliberately only fills an empty Python_SOABI. A non-empty
value is authoritative and left untouched: with an emulator FindPython ran the
target interpreter to compute it, whereas this bare invocation would run the
wrong interpreter (or fail to exec the target binary), so it must not
second-guess a value FindPython already produced.

It sets Python_SOABI centrally rather than overriding each target's SUFFIX
because FindPython recomputes Python_SOABI on every find_package(Python), so
a manual value only survives if nothing re-finds Python afterwards. That now
holds unconditionally: since #189386 replaced the libnvrtc shorthash with
file(SHA256), no find_package(Python) runs after this one at all.

An alternative considered was passing -DPython_SOABI=...; FindPython unsets and
recomputes the variable on each find, so a command-line value is not honored.

This PR was authored with the assistance of an AI coding assistant.

Test Plan

Cross-build validation (real linux-aarch64 crossenv)

Validated against a genuine conda-forge-style cross environment: cross compilers
plus a linux-aarch64 target prefix, joined by crossenv, so Python_EXECUTABLE
is an interpreter that runs on x86_64 but reports the target's config
(SOABI cpython-312-aarch64-linux-gnu, platform linux-aarch64) -- the setup
this fix is written for.

mamba create -p $ROOT/build-env -c conda-forge python=3.12 crossenv cmake ninja \
      gcc_linux-aarch64 gxx_linux-aarch64 sysroot_linux-aarch64
conda create -p $ROOT/target-env --platform linux-aarch64 -c conda-forge python=3.12 numpy
export PATH="$ROOT/build-env/bin:$PATH"
$ROOT/build-env/bin/python -m crossenv \
  --sysconfigdata-file $ROOT/target-env/lib/python3.12/_sysconfigdata_aarch64_conda_linux_gnu.py \
  --cc aarch64-conda-linux-gnu-gcc --cxx aarch64-conda-linux-gnu-c++ \
  --ar aarch64-conda-linux-gnu-ar \
  --sysroot $ROOT/build-env/aarch64-conda-linux-gnu/sysroot \
  $ROOT/target-env/bin/python $ROOT/cross-venv

PyTorch itself was then configured twice with that interpreter, the two runs
differing only in cmake/Dependencies.cmake:

cmake -S . -B $B -G Ninja \
  -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
  -DCMAKE_C_COMPILER=aarch64-conda-linux-gnu-gcc \
  -DCMAKE_CXX_COMPILER=aarch64-conda-linux-gnu-c++ \
  -DCMAKE_SYSROOT=$ROOT/build-env/aarch64-conda-linux-gnu/sysroot \
  -DPython_EXECUTABLE=$ROOT/cross-venv/cross/bin/python \
  -DPython_INCLUDE_DIR=$ROOT/target-env/include/python3.12 \
  -DUSE_CUDA=0 -DUSE_DISTRIBUTED=0 -DBUILD_TEST=0 -DUSE_MKLDNN=0 \
  -DUSE_NUMPY=0 -DUSE_OPENMP=0 -DUSE_KINETO=0 -DBLAS=Eigen

Both configures succeed; the resulting _C target in the generated build system:

_C output in build.ninja
without this patch (current viable/strict) torch/_C.so -- untagged, the reported bug
with this patch torch/_C.cpython-312-aarch64-linux-gnu.so

The configure log shows the override firing with
cpython-312-aarch64-linux-gnu. This also exercises the central-override design
end to end: the value survives from the override in Dependencies.cmake all the
way to target generation, with no later find_package(Python) resetting it.

Scope: this covers configure and the generated build system, where the module
name is decided. A full aarch64 compile was not run, and the box has no
qemu-user, so importing an aarch64 artifact there was not possible; end-to-end
execution remains the conda-forge build below.

Supporting checks

In a standalone FindPython project: (a) a non-empty Python_SOABI is preserved
and only an empty one takes the override path, and (b) setting Python_SOABI
before the Python_add_library calls (no intervening find_package) drives the
suffix of every WITH_SOABI module:

set(Python_SOABI "cpython-312-aarch64-linux-gnu")
Python_add_library(_C MODULE WITH_SOABI stub.c)        # -> _C.cpython-312-aarch64-linux-gnu.so
Python_add_library(_nccl_ep MODULE WITH_SOABI stub.c)  # -> _nccl_ep.cpython-312-aarch64-linux-gnu.so

Native builds are unchanged: the CMAKE_CROSSCOMPILING guard is not taken and
FindPython's Python_SOABI already equals EXT_SUFFIX there.

The #190003 interaction was checked separately (cmake 3.28.3), since it governs
which builds reach the empty-SOABI condition in the first place:

# native, no toolchain vars
cmake -S . -B b1
# -> CMAKE_CROSSCOMPILING=FALSE  Python_SOABI=cpython-310-x86_64-linux-gnu

# toolchain vars in the environment, WITHOUT #190003's forwarding block
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b4
# -> CMAKE_CROSSCOMPILING=FALSE  Python_SOABI=cpython-310-x86_64-linux-gnu

# same, WITH the forwarding block (current main)
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b2
# -> CMAKE_CROSSCOMPILING=TRUE   Python_SOABI=''

# -D flags (the conda-forge CMAKE_ARGS path)
cmake -S . -B b3 -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64
# -> CMAKE_CROSSCOMPILING=TRUE   Python_SOABI=''

macOS is unaffected: CMAKE_OSX_ARCHITECTURES=x86_64 on an arm64 host leaves
CMAKE_CROSSCOMPILING false, and macOS SOABI (cpython-3XY-darwin) carries no
architecture component, so no arch-mismatched module name can arise there.

End-to-end validation is a conda-forge linux-aarch64 build
(pytorch-cpu-feedstock#526)
confirming _C.cpython-3XY-aarch64-linux-gnu.so is installed and
import torch._C succeeds.

@pytorch-bot

pytorch-bot Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (20 Unrelated Failures)

As of commit 58d7bc7 with merge base 547c669 (image):

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

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

@zklaus
zklaus force-pushed the fix-c-soabi-crosscompile branch from 62704b4 to a57ca76 Compare July 9, 2026 12:00
@zklaus zklaus added the release notes: build release notes category label Jul 9, 2026
@zklaus
zklaus marked this pull request as ready for review July 9, 2026 12:29
@zklaus zklaus added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 9, 2026
@zklaus

zklaus commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

Now validated on the conda-forge feedstock at conda-forge/pytorch-cpu-feedstock#526

@jbschlosser jbschlosser added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jul 10, 2026
…d suffix

Since pytorch#180243 the torch._C extension is built by CMake via
Python_add_library(_C MODULE WITH_SOABI ...). WITH_SOABI names the module using
Python_SOABI, which CMake's FindPython only computes by running the interpreter
when NOT cross-compiling: under CMAKE_CROSSCOMPILING the query is skipped unless
CMAKE_CROSSCOMPILING_EMULATOR is set (policy CMP0190), so Python_SOABI is left
empty and WITH_SOABI silently emits an untagged _C.so.

That breaks cross builds: the extension is installed under a name the target
interpreter will not import, so import torch._C fails. This is the conda-forge
linux-aarch64 failure in pytorch-cpu-feedstock pytorch#526. (When this was first
diagnosed the symptom was different: setup.py collected the extension via
package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}", the untagged name
did not match, and _C was dropped from the wheel entirely. pytorch#180248 has since
removed setup.py and _C is collected by install(TARGETS _C ...) in
torch/CMakeLists.txt, so it now ships under the wrong name rather than going
missing. Either way the target cannot import it.)

pytorch#190003 has since widened which builds hit this. It forwards CMAKE_SYSTEM_NAME
and CMAKE_SYSTEM_PROCESSOR from the environment before project(), so a cross
setup that passes them that way now genuinely enters cross-compile mode. Such a
build previously stayed in native mode -- CMAKE_CROSSCOMPILING FALSE, and
FindPython computed Python_SOABI normally -- and so never produced an untagged
module. It now takes the empty-SOABI path as well, which makes this fix the
completion of that one rather than a conda-forge-only concern.

CMake's sanctioned cross path (CMP0190 + an emulator) assumes the target
interpreter is run under qemu/wine. That does not fit conda-forge's crossenv,
where the interpreter is a build-native binary that reports the target's config
and is directly runnable -- no emulator. So when Python_SOABI is empty while
cross-compiling, we query SOABI from the interpreter (which crossenv answers with
the target's value) and set it once, right where FindPython resolves the
Development.Module component. Every downstream WITH_SOABI consumer (_C, and the
CUDA-only _nccl_ep) then gets the correct suffix, and WITH_SOABI still composes
the platform-specific filename.

The override deliberately only fills an *empty* Python_SOABI. A non-empty value is
authoritative and left untouched: with an emulator FindPython ran the target
interpreter to compute it, whereas this bare invocation would run the wrong
interpreter (or fail to exec the target binary), so it must not second-guess a
value FindPython already produced. The empty case is both unambiguous and the
actual failure observed (an untagged _C.so).

This overrides Python_SOABI centrally rather than each target's SUFFIX because
FindPython recomputes Python_SOABI on every find_package(Python), so a manual
value only survives if nothing re-finds Python afterwards. That holds here: the
last find is in this file, the override runs right after it, and the only later
use of FindPython -- cmake/public/cuda.cmake, re-included from torch/CMakeLists.txt
under USE_CUDA to hash libnvrtc -- is short-circuited on that second entry by the
already-computed CUDA_NVRTC_SHORTHASH. So the override is not threatened by a
lingering find_package and lands independently.

An alternative considered was passing -DPython_SOABI=...; FindPython unsets and
recomputes the variable on each find, so a command-line value is not honored.

Authored with assistance from Claude Code.

Test Plan:
Confirmed in a standalone FindPython project that (a) a non-empty Python_SOABI is
preserved and only an empty one takes the override path, and (b) setting
Python_SOABI before the Python_add_library calls (no intervening find_package)
drives the suffix of every WITH_SOABI module:

```
set(Python_SOABI "cpython-312-aarch64-linux-gnu")
Python_add_library(_C MODULE WITH_SOABI stub.c)        # -> _C.cpython-312-aarch64-linux-gnu.so
Python_add_library(_nccl_ep MODULE WITH_SOABI stub.c)  # -> _nccl_ep.cpython-312-aarch64-linux-gnu.so
```

This equals f"_C{EXT_SUFFIX}" since EXT_SUFFIX == "." + SOABI + shared-module
suffix on Linux/macOS targets. Native builds are unchanged (the CMAKE_CROSSCOMPILING
guard is not taken; FindPython's Python_SOABI already equals EXT_SUFFIX there).

The pytorch#190003 interaction was checked separately on the same standalone project
(cmake 3.28.3), reproducing the empty-SOABI condition from both directions:

```
# native, no toolchain vars
cmake -S . -B b1                                   # CROSSCOMPILING=FALSE  SOABI=cpython-310-x86_64-linux-gnu
# env vars WITHOUT the pytorch#190003 forwarding block (pre-pytorch#190003 behaviour)
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b4
                                                   # CROSSCOMPILING=FALSE  SOABI=cpython-310-x86_64-linux-gnu
# env vars WITH the forwarding block (post-pytorch#190003)
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b2
                                                   # CROSSCOMPILING=TRUE   SOABI=''
# -D flags (the conda-forge CMAKE_ARGS path)
cmake -S . -B b3 -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64
                                                   # CROSSCOMPILING=TRUE   SOABI=''
```

End-to-end validation is a conda-forge linux-aarch64 build
(pytorch-cpu-feedstock pytorch#526), where import torch._C succeeds.
@zklaus
zklaus force-pushed the fix-c-soabi-crosscompile branch from a57ca76 to 58d7bc7 Compare August 18, 2026 10:13
@zklaus

zklaus commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator Author
Two notes on how this ages, since both came up: the upstream CMake fix and the
riscv64 job.

### Upstream CMake

The crossenv case is tracked upstream as
[cmake#27996](https://gitlab.kitware.com/cmake/cmake/-/work_items/27996)
(filed by @isuruf): `CMP0190` demands a `CMAKE_CROSSCOMPILING_EMULATOR`, which a
crossenv setup does not have. Two changes landed there:

- [MR 12380](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/12380),
  merged 2026-08-07, milestone **4.5.0** — adds `Python_CROSSCOMPILING_EMULATOR`,
  which may be set to the *empty string* to mean "the target interpreter is
  directly runnable, no emulator", i.e. exactly the crossenv case. It only takes
  effect under `CMP0190=NEW`.
- [FindPython: fix CROSSCOMPILING with CMP0190=OLD](https://gitlab.kitware.com/cmake/cmake/-/commit/7b74c14b02c2),
  fixing [cmake#28029](https://gitlab.kitware.com/cmake/cmake/-/work_items/28029)
  (@henryiii) — sets `_Python_CROSSCOMPILING` unconditionally when `CMP0190` is
  `OLD`, so the interpreter gets queried again. **Released in 4.4.3 on 2026-08-25.**

The second one is what matters for us. `cmake_minimum_required(VERSION 3.27)`
leaves `CMP0190` at `OLD`, so on 4.4.3 and up `Python_SOABI` comes back populated
by itself and this block turns into a no-op — it is guarded on `NOT Python_SOABI`.
Below that it is needed across the whole range we support:

| CMake | guard on the SOABI query | `Python_SOABI` when cross-compiling |
| --- | --- | --- |
| 3.27 – 4.0 | `AND NOT CMAKE_CROSSCOMPILING` (no `CMP0190` at all) | empty |
| 4.1 – 4.4.2 | `AND (_Python_CROSSCOMPILING OR NOT CMAKE_CROSSCOMPILING)`, and `_Python_CROSSCOMPILING` requires `CMP0190=NEW` **and** an emulator | empty |
| ≥ 4.4.3 | same guard, but the `OLD` branch now sets `_Python_CROSSCOMPILING` | correct |

Our floor is `cmake>=3.27`, so the workaround stays for everyone below 4.4.3 —
but it ages out on its own as the ecosystem moves up, rather than needing a
follow-up removal.

### riscv64

We have an in-tree cross build that is meant to be the same shape as the
conda-forge one: `.github/workflows/riscv64.yml` →
`pytorch-linux-noble-riscv64-py3_12-gcc14-cross-build`. `.ci/pytorch/build.sh`
activates a crossenv (`/opt/riscv-cross-env`) and exports `CMAKE_CROSSCOMPILING=TRUE`
plus `CMAKE_SYSTEM_NAME`/`CMAKE_SYSTEM_PROCESSOR`, and the image pins `cmake==4.0.0`
— the first row of the table above.

It has not been doing any of that. #187821 narrowed the riscv guards from
`*riscv*` to `*riscv*cross*` across two scripts that key off different variables:
`.ci/docker/build.sh` tests the image name, `.ci/pytorch/build.sh` tests
`BUILD_ENVIRONMENT`. Only the image name gained the marker — `BUILD_ENVIRONMENT`
is still `linux-noble-riscv64-py3.12-gcc14` — so the guard has been false since
2026-06-24 and the entire cross block is skipped. From the last run's log
([28817304405](https://github.com/pytorch/pytorch/actions/runs/28817304405)):

```
+ [[ linux-noble-riscv64-py3.12-gcc14 == *riscv64*cross* ]]
+ [[ linux-noble-riscv64-py3.12-gcc14 == *vulkan* ]]
```

So the job has not been cross-compiling at all, which also means it has not been
quietly shipping an untagged `_C.so`: CMake was never in cross mode, so
`Python_SOABI` was never empty. The exposure only starts once the job works again.

Aligning `build-environment` with the image name fixes the routing, and I have
that on a branch. It will not turn the job green by itself — the last run predates
both #190887 and the scikit-build-core migration, and the crossenv is provisioned
with `setuptools pyyaml typing_extensions wheel` while the build now calls
`python -m build --wheel --no-isolation`.

Once it is green it is the natural home for a regression test on this: assert that
the built `_C` carries a SOABI tag. Nothing else would catch it, since the job
never imports torch — the smoke test in `build.sh` is gated on `*full-debug*`.

@zklaus

zklaus commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator Author

Currently, we are not exercising the cross compilation in our CI, because the only job doing that has been broken for some time. While this PR is validated by other means (see the test plan in the description), there is now also #194880 to fix our own cross compilation check.

@jeffdaily jeffdaily left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I was requested as a reviewer though this is somewhat out of my area to approve. That said, @zklaus 's explanation about how this PR ages out gracefully with newer CMake versions convinced me that this workaround is safe.

@zklaus

zklaus commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 29, 2026
This job builds a wheel and never imports it -- the smoke test further down is
gated on *full-debug*, which this build environment is not -- so an extension
module named wrongly passes silently. That is not hypothetical: run 33078360947,
on this branch two days ago, linked `torch/_C.so` untagged and went green.

The cause was pytorchgh-189388, which has since landed: FindPython does not run the
interpreter to compute Python_SOABI while cross-compiling, so WITH_SOABI composes
a name with no ABI tag. Run 33243561273, on the rebase that picked the fix up,
links `torch/_C.cpython-312-riscv64-linux-gnu.so` instead. Nothing in CI checks
that, which is how the class went unnoticed long enough to be reported from
conda-forge rather than found here.

The check asks whether the wheel is self-consistent, rather than comparing it
against the running interpreter. A wheel states its tags twice -- in the filename
and in .dist-info/WHEEL -- and the extension module inside has to agree with
both. So it verifies that those two agree, that exactly one torch/_C module is
present, that it is not the untagged torch/_C.so, and that its ABI tag matches
the wheel's, cp312 against cpython-312.

Deriving the expectation from sysconfig instead, as a first draft did, makes the
result depend on which interpreter happens to be active -- for a cross build that
is the crossenv interpreter reporting the target's values, not the obvious one.
That held here, but only by construction: it is a property of where the call sits
relative to the crossenv activation, invisible at the call site, and it would
silently become a false failure if either moved. Reading the wheel's own tags has
no such coupling, and lets the check run under any interpreter.

It also catches more. A wheel whose filename and metadata disagree, or whose
module is tagged for a different Python than the wheel claims, are both real
mis-builds that the sysconfig form would have passed whenever the checker
happened to run under the matching interpreter.

The checker lives in .ci/pytorch/check_wheel_soabi.py rather than inline: as a
file it is linted and directly testable, which an embedded heredoc is not, and
.ci/pytorch already holds print_sccache_log.py and rocm_preflight.py. It takes
any number of wheels so the caller passes the glob directly -- dist/ is not
guaranteed to hold exactly one, pip_install_whl in common_utils.sh already splits
a multi-path argument for that reason, and an earlier draft using
`$(echo dist/*.whl)` would have silently checked whichever wheel sorted first.

It is gated on the riscv64 cross environment. A native build satisfies it
trivially, and adding wheel inspection to every build job is a wider change than
this branch should make; the check is cheap enough that generalising it later is
worth considering.

There is adjacent prior art. .ci/pytorch/smoke_test/check_wheel_tags.py, added in
pytorch#177609, validates a wheel's declared tags against the running interpreter and
TARGET_OS -- answering "did we build for the target we meant to", which does need
an external reference. It never inspects the extension module's filename, so an
untagged torch/_C.so inside a correctly tagged wheel passes it cleanly; that is
the gap pytorchgh-189388 fell through. It is also reachable only via smoke_test.py,
which imports torch and therefore cannot run in a cross job. Folding the
module-name check in there would cover every platform's binary builds rather than
this one job, and is worth doing, but it is filed separately: that file is
smoke-test-only, so the cross job would still need its own call site.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

The assertion needs a ciflow/riscv64 run to exercise in place, and should pass:
run 33243561273 already produced the tagged name it looks for.

Because the check reads only the wheel, the riscv64 case is testable without a
riscv64 toolchain -- a wheel is a zip. These were built to order and run under
**host CPython 3.10 on x86_64**, deliberately neither the Python nor the
architecture the wheels claim, which is the property the sysconfig form lacked:

| wheel under test | exit |
| --- | --- |
| `cp312-cp312-linux_riscv64` shipping `_C.cpython-312-riscv64-linux-gnu.so` | 0 |
| same, shipping untagged `torch/_C.so` (the pytorchgh-189388 regression) | 1 |
| same, shipping no `torch/_C*` at all (the conda-forge symptom) | 1 |
| filename says `cp312-...`, `.dist-info/WHEEL` says `cp310-...` | 1 |
| declares `cp312`, ships `_C.cpython-310-riscv64-linux-gnu.so` | 1 |
| ships both the tagged module and an untagged `torch/_C.so` | 1 |
| a libtorch wheel plus a good torch wheel (former skipped) | 0 |
| a libtorch wheel alone, so nothing was verified | 1 |

Each failure names what it found and what it expected. Exit codes were read
directly rather than through a pipe, since `head` would otherwise mask them.

Lint:

```
lintrunner --paths-cmd='git diff --cached --name-only'
bash -n .ci/pytorch/build.sh
```
zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 30, 2026
This job builds a wheel and never imports it -- the smoke test further down is
gated on *full-debug*, which this build environment is not -- so an extension
module named wrongly passes silently. That is not hypothetical: run 33078360947,
on this branch two days ago, linked `torch/_C.so` untagged and went green.

The cause was pytorchgh-189388, which has since landed: FindPython does not run the
interpreter to compute Python_SOABI while cross-compiling, so WITH_SOABI composes
a name with no ABI tag. Run 33243561273, on the rebase that picked the fix up,
links `torch/_C.cpython-312-riscv64-linux-gnu.so` instead. Nothing in CI checks
that, which is how the class went unnoticed long enough to be reported from
conda-forge rather than found here.

The check asks whether the wheel is self-consistent, rather than comparing it
against the running interpreter. A wheel states its tags twice -- in the filename
and in .dist-info/WHEEL -- and the extension module inside has to agree with
both. So it verifies that those two agree, that exactly one torch/_C module is
present, that it is not the untagged torch/_C.so, and that its ABI tag matches
the wheel's, cp312 against cpython-312.

Deriving the expectation from sysconfig instead, as a first draft did, makes the
result depend on which interpreter happens to be active -- for a cross build that
is the crossenv interpreter reporting the target's values, not the obvious one.
That held here, but only by construction: it is a property of where the call sits
relative to the crossenv activation, invisible at the call site, and it would
silently become a false failure if either moved. Reading the wheel's own tags has
no such coupling, and lets the check run under any interpreter.

It also catches more. A wheel whose filename and metadata disagree, or whose
module is tagged for a different Python than the wheel claims, are both real
mis-builds that the sysconfig form would have passed whenever the checker
happened to run under the matching interpreter.

The checker lives in .ci/pytorch/check_wheel_soabi.py rather than inline: as a
file it is linted and directly testable, which an embedded heredoc is not, and
.ci/pytorch already holds print_sccache_log.py and rocm_preflight.py. It takes
any number of wheels so the caller passes the glob directly -- dist/ is not
guaranteed to hold exactly one, pip_install_whl in common_utils.sh already splits
a multi-path argument for that reason, and an earlier draft using
`$(echo dist/*.whl)` would have silently checked whichever wheel sorted first.

It is gated on the riscv64 cross environment. A native build satisfies it
trivially, and adding wheel inspection to every build job is a wider change than
this branch should make; the check is cheap enough that generalising it later is
worth considering.

There is adjacent prior art. .ci/pytorch/smoke_test/check_wheel_tags.py, added in
pytorch#177609, validates a wheel's declared tags against the running interpreter and
TARGET_OS -- answering "did we build for the target we meant to", which does need
an external reference. It never inspects the extension module's filename, so an
untagged torch/_C.so inside a correctly tagged wheel passes it cleanly; that is
the gap pytorchgh-189388 fell through. It is also reachable only via smoke_test.py,
which imports torch and therefore cannot run in a cross job. Folding the
module-name check in there would cover every platform's binary builds rather than
this one job, and is worth doing, but it is filed separately: that file is
smoke-test-only, so the cross job would still need its own call site.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

The assertion needs a ciflow/riscv64 run to exercise in place, and should pass:
run 33243561273 already produced the tagged name it looks for.

Because the check reads only the wheel, the riscv64 case is testable without a
riscv64 toolchain -- a wheel is a zip. These were built to order and run under
**host CPython 3.10 on x86_64**, deliberately neither the Python nor the
architecture the wheels claim, which is the property the sysconfig form lacked:

| wheel under test | exit |
| --- | --- |
| `cp312-cp312-linux_riscv64` shipping `_C.cpython-312-riscv64-linux-gnu.so` | 0 |
| same, shipping untagged `torch/_C.so` (the pytorchgh-189388 regression) | 1 |
| same, shipping no `torch/_C*` at all (the conda-forge symptom) | 1 |
| filename says `cp312-...`, `.dist-info/WHEEL` says `cp310-...` | 1 |
| declares `cp312`, ships `_C.cpython-310-riscv64-linux-gnu.so` | 1 |
| ships both the tagged module and an untagged `torch/_C.so` | 1 |
| a libtorch wheel plus a good torch wheel (former skipped) | 0 |
| a libtorch wheel alone, so nothing was verified | 1 |

Each failure names what it found and what it expected. Exit codes were read
directly rather than through a pipe, since `head` would otherwise mask them.

Lint:

```
lintrunner --paths-cmd='git diff --cached --name-only'
bash -n .ci/pytorch/build.sh
```
zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 30, 2026
Second self-review pass on the checker added earlier in this branch. Three
corrections, none of which the riscv64 job would hit today, but all of which it
would hit once the check is generalised to other wheels -- which is the filed
follow-up.

`torch/_C` was matched as a plain prefix. That is loose in two directions:
`torch/_C/` is a real directory of .pyi stubs, and a sibling extension module
named `torch/_C_something.so` would match while not being the module in
question. A wheel shipping one alongside `_C` would have been rejected with
"ships several torch/_C modules". Matching `torch/_C\.[^/]+` instead names the
form exactly: `torch/_C.<abi>.<ext>`, or the untagged `torch/_C.<ext>`.

`.dylib` was in the accepted suffix list. The suffixes CPython's import system
recognises are those in importlib.machinery.EXTENSION_SUFFIXES -- `.so` on POSIX
including macOS, `.pyd` on Windows -- implemented by _PyImport_DynLoadFiletab in
Python/dynload_shlib.c, which macOS uses (configure.ac selects dynload_shlib.o
wherever dlopen() exists). `.dylib` is not among them. It is not an idle
distinction: macOS uses `.dylib` heavily for shared libraries, and torch's own
macOS wheel ships seven of them next to the module, so accepting it widened what
could be mistaken for an extension module on exactly the platform where the
mistake was available.

Comparing the filename's tags to the ``Tag:`` lines as strings is wrong for a
compressed tag set. PEP 427 allows a filename to carry ``py2.py3-none-any`` while
WHEEL lists each expansion on its own line, so the string compare fails on a
correct wheel. The expansions are now compared as a set, and the ABI check
accepts any member of a compressed ABI set. torch does not ship compressed tags,
so this is latent rather than live.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

Wheels built to order, run under host CPython 3.10 on x86_64 -- deliberately
neither the Python nor the architecture they claim:

| wheel | exit | why |
| --- | --- | --- |
| tagged module plus `torch/_C/*.pyi` stubs | 0 | the stub directory is not a module |
| tagged module plus a sibling `torch/_C_foo.so` | 0 | **rejected before this commit** |
| untagged `torch/_C.so` | 1 | the pytorchgh-189388 regression |
| `cp312.cp313-abi3-linux_riscv64`, both Tag: lines present, ships `_C.abi3.so` | 0 | **rejected before this commit** |
| same, one Tag: line missing from WHEEL | 1 | genuinely inconsistent |

The earlier cases from the previous commit were re-run and are unchanged: correct
wheel passes, missing module fails, filename/metadata mismatch fails, module
tagged for another Python fails, two `torch/_C.*` modules fail, libtorch-only
wheel is skipped, libtorch-only alone fails.

Lint:

```
lintrunner --paths-cmd='git diff --name-only origin/viable/strict'
```
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…d suffix (pytorch#189388)

## Summary

Since pytorch#180243 the `torch._C` extension is built by CMake via
`Python_add_library(_C MODULE WITH_SOABI ...)`. `WITH_SOABI` names the module
using `Python_SOABI`, which CMake's FindPython only computes by running the
interpreter **when not cross-compiling**: under `CMAKE_CROSSCOMPILING` the query
is skipped unless `CMAKE_CROSSCOMPILING_EMULATOR` is set (policy `CMP0190`), so
`Python_SOABI` is left empty and `WITH_SOABI` silently emits an untagged `_C.so`.

That breaks cross builds: the extension is installed under a name the target
interpreter will not import, so `import torch._C` fails. This is the conda-forge
linux-aarch64 failure in
[pytorch-cpu-feedstock#526](conda-forge/pytorch-cpu-feedstock#526).

When this was first diagnosed the symptom was different: `setup.py` collected the
extension via `package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}"`, the
untagged name did not match, and `_C` was dropped from the wheel entirely.
pytorch#180248 has since removed `setup.py`, and `_C` is now collected by
`install(TARGETS _C ...)`, so it ships under the wrong name rather than going
missing. Either way the target cannot import it.

pytorch#190003 has since widened which builds hit this. It forwards `CMAKE_SYSTEM_NAME`
and `CMAKE_SYSTEM_PROCESSOR` from the environment before `project()`, which is
exactly what puts CMake into cross-compile mode, so a cross setup that passes
them that way now takes the empty-`Python_SOABI` path too. Such a build
previously stayed in native mode -- `CMAKE_CROSSCOMPILING` false, `Python_SOABI`
computed normally -- and so never produced an untagged module. That makes this
the completion of pytorch#190003 rather than a conda-forge-only concern.

CMake's sanctioned cross path (`CMP0190` + an emulator) assumes the target
interpreter is run under qemu/wine. That does not fit conda-forge's crossenv,
where the interpreter is a build-native binary that reports the target's config
and is directly runnable -- no emulator. So when `Python_SOABI` is empty while
cross-compiling, we query `SOABI` from the interpreter (which crossenv answers
with the target's value) and set it once, right where FindPython resolves the
`Development.Module` component. Every downstream `WITH_SOABI` consumer (`_C`, and
the CUDA-only `_nccl_ep`) then gets the correct suffix, and `WITH_SOABI` still
composes the platform-specific filename.

The override deliberately only fills an **empty** `Python_SOABI`. A non-empty
value is authoritative and left untouched: with an emulator FindPython ran the
target interpreter to compute it, whereas this bare invocation would run the
wrong interpreter (or fail to exec the target binary), so it must not
second-guess a value FindPython already produced.

It sets `Python_SOABI` centrally rather than overriding each target's `SUFFIX`
because FindPython recomputes `Python_SOABI` on every `find_package(Python)`, so
a manual value only survives if nothing re-finds Python afterwards. That now
holds unconditionally: since pytorch#189386 replaced the libnvrtc shorthash with
`file(SHA256)`, no `find_package(Python)` runs after this one at all.

An alternative considered was passing `-DPython_SOABI=...`; FindPython unsets and
recomputes the variable on each find, so a command-line value is not honored.

This PR was authored with the assistance of an AI coding assistant.

## Test Plan

### Cross-build validation (real linux-aarch64 crossenv)

Validated against a genuine conda-forge-style cross environment: cross compilers
plus a `linux-aarch64` target prefix, joined by `crossenv`, so `Python_EXECUTABLE`
is an interpreter that **runs on x86_64 but reports the target's config**
(SOABI `cpython-312-aarch64-linux-gnu`, platform `linux-aarch64`) -- the setup
this fix is written for.

```
mamba create -p $ROOT/build-env -c conda-forge python=3.12 crossenv cmake ninja \
      gcc_linux-aarch64 gxx_linux-aarch64 sysroot_linux-aarch64
conda create -p $ROOT/target-env --platform linux-aarch64 -c conda-forge python=3.12 numpy
export PATH="$ROOT/build-env/bin:$PATH"
$ROOT/build-env/bin/python -m crossenv \
  --sysconfigdata-file $ROOT/target-env/lib/python3.12/_sysconfigdata_aarch64_conda_linux_gnu.py \
  --cc aarch64-conda-linux-gnu-gcc --cxx aarch64-conda-linux-gnu-c++ \
  --ar aarch64-conda-linux-gnu-ar \
  --sysroot $ROOT/build-env/aarch64-conda-linux-gnu/sysroot \
  $ROOT/target-env/bin/python $ROOT/cross-venv
```

PyTorch itself was then configured twice with that interpreter, the two runs
differing **only** in `cmake/Dependencies.cmake`:

```
cmake -S . -B $B -G Ninja \
  -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
  -DCMAKE_C_COMPILER=aarch64-conda-linux-gnu-gcc \
  -DCMAKE_CXX_COMPILER=aarch64-conda-linux-gnu-c++ \
  -DCMAKE_SYSROOT=$ROOT/build-env/aarch64-conda-linux-gnu/sysroot \
  -DPython_EXECUTABLE=$ROOT/cross-venv/cross/bin/python \
  -DPython_INCLUDE_DIR=$ROOT/target-env/include/python3.12 \
  -DUSE_CUDA=0 -DUSE_DISTRIBUTED=0 -DBUILD_TEST=0 -DUSE_MKLDNN=0 \
  -DUSE_NUMPY=0 -DUSE_OPENMP=0 -DUSE_KINETO=0 -DBLAS=Eigen
```

Both configures succeed; the resulting `_C` target in the generated build system:

| | `_C` output in `build.ninja` |
| --- | --- |
| without this patch (current `viable/strict`) | `torch/_C.so` -- untagged, the reported bug |
| with this patch | `torch/_C.cpython-312-aarch64-linux-gnu.so` |

The configure log shows the override firing with
`cpython-312-aarch64-linux-gnu`. This also exercises the central-override design
end to end: the value survives from the override in `Dependencies.cmake` all the
way to target generation, with no later `find_package(Python)` resetting it.

Scope: this covers configure and the generated build system, where the module
name is decided. A full aarch64 compile was not run, and the box has no
`qemu-user`, so importing an aarch64 artifact there was not possible; end-to-end
execution remains the conda-forge build below.

### Supporting checks

In a standalone FindPython project: (a) a non-empty `Python_SOABI` is preserved
and only an empty one takes the override path, and (b) setting `Python_SOABI`
before the `Python_add_library` calls (no intervening `find_package`) drives the
suffix of every `WITH_SOABI` module:

```cmake
set(Python_SOABI "cpython-312-aarch64-linux-gnu")
Python_add_library(_C MODULE WITH_SOABI stub.c)        # -> _C.cpython-312-aarch64-linux-gnu.so
Python_add_library(_nccl_ep MODULE WITH_SOABI stub.c)  # -> _nccl_ep.cpython-312-aarch64-linux-gnu.so
```

Native builds are unchanged: the `CMAKE_CROSSCOMPILING` guard is not taken and
FindPython's `Python_SOABI` already equals `EXT_SUFFIX` there.

The pytorch#190003 interaction was checked separately (cmake 3.28.3), since it governs
*which* builds reach the empty-`SOABI` condition in the first place:

```
# native, no toolchain vars
cmake -S . -B b1
# -> CMAKE_CROSSCOMPILING=FALSE  Python_SOABI=cpython-310-x86_64-linux-gnu

# toolchain vars in the environment, WITHOUT pytorch#190003's forwarding block
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b4
# -> CMAKE_CROSSCOMPILING=FALSE  Python_SOABI=cpython-310-x86_64-linux-gnu

# same, WITH the forwarding block (current main)
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b2
# -> CMAKE_CROSSCOMPILING=TRUE   Python_SOABI=''

# -D flags (the conda-forge CMAKE_ARGS path)
cmake -S . -B b3 -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64
# -> CMAKE_CROSSCOMPILING=TRUE   Python_SOABI=''
```

macOS is unaffected: `CMAKE_OSX_ARCHITECTURES=x86_64` on an arm64 host leaves
`CMAKE_CROSSCOMPILING` false, and macOS SOABI (`cpython-3XY-darwin`) carries no
architecture component, so no arch-mismatched module name can arise there.

End-to-end validation is a conda-forge linux-aarch64 build
([pytorch-cpu-feedstock#526](conda-forge/pytorch-cpu-feedstock#526))
confirming `_C.cpython-3XY-aarch64-linux-gnu.so` is installed and
`import torch._C` succeeds.

Pull Request resolved: pytorch#189388
Approved by: https://github.com/isuruf, https://github.com/jeffdaily
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…d suffix (pytorch#189388)

## Summary

Since pytorch#180243 the `torch._C` extension is built by CMake via
`Python_add_library(_C MODULE WITH_SOABI ...)`. `WITH_SOABI` names the module
using `Python_SOABI`, which CMake's FindPython only computes by running the
interpreter **when not cross-compiling**: under `CMAKE_CROSSCOMPILING` the query
is skipped unless `CMAKE_CROSSCOMPILING_EMULATOR` is set (policy `CMP0190`), so
`Python_SOABI` is left empty and `WITH_SOABI` silently emits an untagged `_C.so`.

That breaks cross builds: the extension is installed under a name the target
interpreter will not import, so `import torch._C` fails. This is the conda-forge
linux-aarch64 failure in
[pytorch-cpu-feedstock#526](conda-forge/pytorch-cpu-feedstock#526).

When this was first diagnosed the symptom was different: `setup.py` collected the
extension via `package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}"`, the
untagged name did not match, and `_C` was dropped from the wheel entirely.
pytorch#180248 has since removed `setup.py`, and `_C` is now collected by
`install(TARGETS _C ...)`, so it ships under the wrong name rather than going
missing. Either way the target cannot import it.

pytorch#190003 has since widened which builds hit this. It forwards `CMAKE_SYSTEM_NAME`
and `CMAKE_SYSTEM_PROCESSOR` from the environment before `project()`, which is
exactly what puts CMake into cross-compile mode, so a cross setup that passes
them that way now takes the empty-`Python_SOABI` path too. Such a build
previously stayed in native mode -- `CMAKE_CROSSCOMPILING` false, `Python_SOABI`
computed normally -- and so never produced an untagged module. That makes this
the completion of pytorch#190003 rather than a conda-forge-only concern.

CMake's sanctioned cross path (`CMP0190` + an emulator) assumes the target
interpreter is run under qemu/wine. That does not fit conda-forge's crossenv,
where the interpreter is a build-native binary that reports the target's config
and is directly runnable -- no emulator. So when `Python_SOABI` is empty while
cross-compiling, we query `SOABI` from the interpreter (which crossenv answers
with the target's value) and set it once, right where FindPython resolves the
`Development.Module` component. Every downstream `WITH_SOABI` consumer (`_C`, and
the CUDA-only `_nccl_ep`) then gets the correct suffix, and `WITH_SOABI` still
composes the platform-specific filename.

The override deliberately only fills an **empty** `Python_SOABI`. A non-empty
value is authoritative and left untouched: with an emulator FindPython ran the
target interpreter to compute it, whereas this bare invocation would run the
wrong interpreter (or fail to exec the target binary), so it must not
second-guess a value FindPython already produced.

It sets `Python_SOABI` centrally rather than overriding each target's `SUFFIX`
because FindPython recomputes `Python_SOABI` on every `find_package(Python)`, so
a manual value only survives if nothing re-finds Python afterwards. That now
holds unconditionally: since pytorch#189386 replaced the libnvrtc shorthash with
`file(SHA256)`, no `find_package(Python)` runs after this one at all.

An alternative considered was passing `-DPython_SOABI=...`; FindPython unsets and
recomputes the variable on each find, so a command-line value is not honored.

This PR was authored with the assistance of an AI coding assistant.

## Test Plan

### Cross-build validation (real linux-aarch64 crossenv)

Validated against a genuine conda-forge-style cross environment: cross compilers
plus a `linux-aarch64` target prefix, joined by `crossenv`, so `Python_EXECUTABLE`
is an interpreter that **runs on x86_64 but reports the target's config**
(SOABI `cpython-312-aarch64-linux-gnu`, platform `linux-aarch64`) -- the setup
this fix is written for.

```
mamba create -p $ROOT/build-env -c conda-forge python=3.12 crossenv cmake ninja \
      gcc_linux-aarch64 gxx_linux-aarch64 sysroot_linux-aarch64
conda create -p $ROOT/target-env --platform linux-aarch64 -c conda-forge python=3.12 numpy
export PATH="$ROOT/build-env/bin:$PATH"
$ROOT/build-env/bin/python -m crossenv \
  --sysconfigdata-file $ROOT/target-env/lib/python3.12/_sysconfigdata_aarch64_conda_linux_gnu.py \
  --cc aarch64-conda-linux-gnu-gcc --cxx aarch64-conda-linux-gnu-c++ \
  --ar aarch64-conda-linux-gnu-ar \
  --sysroot $ROOT/build-env/aarch64-conda-linux-gnu/sysroot \
  $ROOT/target-env/bin/python $ROOT/cross-venv
```

PyTorch itself was then configured twice with that interpreter, the two runs
differing **only** in `cmake/Dependencies.cmake`:

```
cmake -S . -B $B -G Ninja \
  -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
  -DCMAKE_C_COMPILER=aarch64-conda-linux-gnu-gcc \
  -DCMAKE_CXX_COMPILER=aarch64-conda-linux-gnu-c++ \
  -DCMAKE_SYSROOT=$ROOT/build-env/aarch64-conda-linux-gnu/sysroot \
  -DPython_EXECUTABLE=$ROOT/cross-venv/cross/bin/python \
  -DPython_INCLUDE_DIR=$ROOT/target-env/include/python3.12 \
  -DUSE_CUDA=0 -DUSE_DISTRIBUTED=0 -DBUILD_TEST=0 -DUSE_MKLDNN=0 \
  -DUSE_NUMPY=0 -DUSE_OPENMP=0 -DUSE_KINETO=0 -DBLAS=Eigen
```

Both configures succeed; the resulting `_C` target in the generated build system:

| | `_C` output in `build.ninja` |
| --- | --- |
| without this patch (current `viable/strict`) | `torch/_C.so` -- untagged, the reported bug |
| with this patch | `torch/_C.cpython-312-aarch64-linux-gnu.so` |

The configure log shows the override firing with
`cpython-312-aarch64-linux-gnu`. This also exercises the central-override design
end to end: the value survives from the override in `Dependencies.cmake` all the
way to target generation, with no later `find_package(Python)` resetting it.

Scope: this covers configure and the generated build system, where the module
name is decided. A full aarch64 compile was not run, and the box has no
`qemu-user`, so importing an aarch64 artifact there was not possible; end-to-end
execution remains the conda-forge build below.

### Supporting checks

In a standalone FindPython project: (a) a non-empty `Python_SOABI` is preserved
and only an empty one takes the override path, and (b) setting `Python_SOABI`
before the `Python_add_library` calls (no intervening `find_package`) drives the
suffix of every `WITH_SOABI` module:

```cmake
set(Python_SOABI "cpython-312-aarch64-linux-gnu")
Python_add_library(_C MODULE WITH_SOABI stub.c)        # -> _C.cpython-312-aarch64-linux-gnu.so
Python_add_library(_nccl_ep MODULE WITH_SOABI stub.c)  # -> _nccl_ep.cpython-312-aarch64-linux-gnu.so
```

Native builds are unchanged: the `CMAKE_CROSSCOMPILING` guard is not taken and
FindPython's `Python_SOABI` already equals `EXT_SUFFIX` there.

The pytorch#190003 interaction was checked separately (cmake 3.28.3), since it governs
*which* builds reach the empty-`SOABI` condition in the first place:

```
# native, no toolchain vars
cmake -S . -B b1
# -> CMAKE_CROSSCOMPILING=FALSE  Python_SOABI=cpython-310-x86_64-linux-gnu

# toolchain vars in the environment, WITHOUT pytorch#190003's forwarding block
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b4
# -> CMAKE_CROSSCOMPILING=FALSE  Python_SOABI=cpython-310-x86_64-linux-gnu

# same, WITH the forwarding block (current main)
CMAKE_SYSTEM_NAME=Linux CMAKE_SYSTEM_PROCESSOR=aarch64 cmake -S . -B b2
# -> CMAKE_CROSSCOMPILING=TRUE   Python_SOABI=''

# -D flags (the conda-forge CMAKE_ARGS path)
cmake -S . -B b3 -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64
# -> CMAKE_CROSSCOMPILING=TRUE   Python_SOABI=''
```

macOS is unaffected: `CMAKE_OSX_ARCHITECTURES=x86_64` on an arm64 host leaves
`CMAKE_CROSSCOMPILING` false, and macOS SOABI (`cpython-3XY-darwin`) carries no
architecture component, so no arch-mismatched module name can arise there.

End-to-end validation is a conda-forge linux-aarch64 build
([pytorch-cpu-feedstock#526](conda-forge/pytorch-cpu-feedstock#526))
confirming `_C.cpython-3XY-aarch64-linux-gnu.so` is installed and
`import torch._C` succeeds.

Pull Request resolved: pytorch#189388
Approved by: https://github.com/isuruf, https://github.com/jeffdaily
zklaus added a commit to zklaus/pytorch that referenced this pull request Sep 14, 2026
This job builds a wheel and never imports it -- the smoke test further down is
gated on *full-debug*, which this build environment is not -- so an extension
module named wrongly passes silently. That is not hypothetical: run 33078360947,
on this branch two days ago, linked `torch/_C.so` untagged and went green.

The cause was pytorchgh-189388, which has since landed: FindPython does not run the
interpreter to compute Python_SOABI while cross-compiling, so WITH_SOABI composes
a name with no ABI tag. Run 33243561273, on the rebase that picked the fix up,
links `torch/_C.cpython-312-riscv64-linux-gnu.so` instead. Nothing in CI checks
that, which is how the class went unnoticed long enough to be reported from
conda-forge rather than found here.

The check asks whether the wheel is self-consistent, rather than comparing it
against the running interpreter. A wheel states its tags twice -- in the filename
and in .dist-info/WHEEL -- and the extension module inside has to agree with
both. So it verifies that those two agree, that exactly one torch/_C module is
present, that it is not the untagged torch/_C.so, and that its ABI tag matches
the wheel's, cp312 against cpython-312.

Deriving the expectation from sysconfig instead, as a first draft did, makes the
result depend on which interpreter happens to be active -- for a cross build that
is the crossenv interpreter reporting the target's values, not the obvious one.
That held here, but only by construction: it is a property of where the call sits
relative to the crossenv activation, invisible at the call site, and it would
silently become a false failure if either moved. Reading the wheel's own tags has
no such coupling, and lets the check run under any interpreter.

It also catches more. A wheel whose filename and metadata disagree, or whose
module is tagged for a different Python than the wheel claims, are both real
mis-builds that the sysconfig form would have passed whenever the checker
happened to run under the matching interpreter.

The checker lives in .ci/pytorch/check_wheel_soabi.py rather than inline: as a
file it is linted and directly testable, which an embedded heredoc is not, and
.ci/pytorch already holds print_sccache_log.py and rocm_preflight.py. It takes
any number of wheels so the caller passes the glob directly -- dist/ is not
guaranteed to hold exactly one, pip_install_whl in common_utils.sh already splits
a multi-path argument for that reason, and an earlier draft using
`$(echo dist/*.whl)` would have silently checked whichever wheel sorted first.

It is gated on the riscv64 cross environment. A native build satisfies it
trivially, and adding wheel inspection to every build job is a wider change than
this branch should make; the check is cheap enough that generalising it later is
worth considering.

There is adjacent prior art. .ci/pytorch/smoke_test/check_wheel_tags.py, added in
TARGET_OS -- answering "did we build for the target we meant to", which does need
an external reference. It never inspects the extension module's filename, so an
untagged torch/_C.so inside a correctly tagged wheel passes it cleanly; that is
the gap pytorchgh-189388 fell through. It is also reachable only via smoke_test.py,
which imports torch and therefore cannot run in a cross job. Folding the
module-name check in there would cover every platform's binary builds rather than
this one job, and is worth doing, but it is filed separately: that file is
smoke-test-only, so the cross job would still need its own call site.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

The assertion needs a ciflow/riscv64 run to exercise in place, and should pass:
run 33243561273 already produced the tagged name it looks for.

Because the check reads only the wheel, the riscv64 case is testable without a
riscv64 toolchain -- a wheel is a zip. These were built to order and run under
**host CPython 3.10 on x86_64**, deliberately neither the Python nor the
architecture the wheels claim, which is the property the sysconfig form lacked:

| wheel under test | exit |
| --- | --- |
| `cp312-cp312-linux_riscv64` shipping `_C.cpython-312-riscv64-linux-gnu.so` | 0 |
| same, shipping untagged `torch/_C.so` (the pytorchgh-189388 regression) | 1 |
| same, shipping no `torch/_C*` at all (the conda-forge symptom) | 1 |
| filename says `cp312-...`, `.dist-info/WHEEL` says `cp310-...` | 1 |
| declares `cp312`, ships `_C.cpython-310-riscv64-linux-gnu.so` | 1 |
| ships both the tagged module and an untagged `torch/_C.so` | 1 |
| a libtorch wheel plus a good torch wheel (former skipped) | 0 |
| a libtorch wheel alone, so nothing was verified | 1 |

Each failure names what it found and what it expected. Exit codes were read
directly rather than through a pipe, since `head` would otherwise mask them.

Lint:

```
lintrunner --paths-cmd='git diff --cached --name-only'
bash -n .ci/pytorch/build.sh
```
zklaus added a commit to zklaus/pytorch that referenced this pull request Sep 14, 2026
Second self-review pass on the checker added earlier in this branch. Three
corrections, none of which the riscv64 job would hit today, but all of which it
would hit once the check is generalised to other wheels -- which is the filed
follow-up.

`torch/_C` was matched as a plain prefix. That is loose in two directions:
`torch/_C/` is a real directory of .pyi stubs, and a sibling extension module
named `torch/_C_something.so` would match while not being the module in
question. A wheel shipping one alongside `_C` would have been rejected with
"ships several torch/_C modules". Matching `torch/_C\.[^/]+` instead names the
form exactly: `torch/_C.<abi>.<ext>`, or the untagged `torch/_C.<ext>`.

`.dylib` was in the accepted suffix list. The suffixes CPython's import system
recognises are those in importlib.machinery.EXTENSION_SUFFIXES -- `.so` on POSIX
including macOS, `.pyd` on Windows -- implemented by _PyImport_DynLoadFiletab in
Python/dynload_shlib.c, which macOS uses (configure.ac selects dynload_shlib.o
wherever dlopen() exists). `.dylib` is not among them. It is not an idle
distinction: macOS uses `.dylib` heavily for shared libraries, and torch's own
macOS wheel ships seven of them next to the module, so accepting it widened what
could be mistaken for an extension module on exactly the platform where the
mistake was available.

Comparing the filename's tags to the ``Tag:`` lines as strings is wrong for a
compressed tag set. PEP 427 allows a filename to carry ``py2.py3-none-any`` while
WHEEL lists each expansion on its own line, so the string compare fails on a
correct wheel. The expansions are now compared as a set, and the ABI check
accepts any member of a compressed ABI set. torch does not ship compressed tags,
so this is latent rather than live.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

Wheels built to order, run under host CPython 3.10 on x86_64 -- deliberately
neither the Python nor the architecture they claim:

| wheel | exit | why |
| --- | --- | --- |
| tagged module plus `torch/_C/*.pyi` stubs | 0 | the stub directory is not a module |
| tagged module plus a sibling `torch/_C_foo.so` | 0 | **rejected before this commit** |
| untagged `torch/_C.so` | 1 | the pytorchgh-189388 regression |
| `cp312.cp313-abi3-linux_riscv64`, both Tag: lines present, ships `_C.abi3.so` | 0 | **rejected before this commit** |
| same, one Tag: line missing from WHEEL | 1 | genuinely inconsistent |

The earlier cases from the previous commit were re-run and are unchanged: correct
wheel passes, missing module fails, filename/metadata mismatch fails, module
tagged for another Python fails, two `torch/_C.*` modules fail, libtorch-only
wheel is skipped, libtorch-only alone fails.

Lint:

```
lintrunner --paths-cmd='git diff --name-only origin/viable/strict'
```
zklaus added a commit to zklaus/pytorch that referenced this pull request Sep 29, 2026
This job builds a wheel and never imports it -- the smoke test further down is
gated on *full-debug*, which this build environment is not -- so an extension
module named wrongly passes silently. That is not hypothetical: run 33078360947,
on this branch two days ago, linked `torch/_C.so` untagged and went green.

The cause was pytorchgh-189388, which has since landed: FindPython does not run the
interpreter to compute Python_SOABI while cross-compiling, so WITH_SOABI composes
a name with no ABI tag. Run 33243561273, on the rebase that picked the fix up,
links `torch/_C.cpython-312-riscv64-linux-gnu.so` instead. Nothing in CI checks
that, which is how the class went unnoticed long enough to be reported from
conda-forge rather than found here.

The check asks whether the wheel is self-consistent, rather than comparing it
against the running interpreter. A wheel states its tags twice -- in the filename
and in .dist-info/WHEEL -- and the extension module inside has to agree with
both. So it verifies that those two agree, that exactly one torch/_C module is
present, that it is not the untagged torch/_C.so, and that its ABI tag matches
the wheel's, cp312 against cpython-312.

Deriving the expectation from sysconfig instead, as a first draft did, makes the
result depend on which interpreter happens to be active -- for a cross build that
is the crossenv interpreter reporting the target's values, not the obvious one.
That held here, but only by construction: it is a property of where the call sits
relative to the crossenv activation, invisible at the call site, and it would
silently become a false failure if either moved. Reading the wheel's own tags has
no such coupling, and lets the check run under any interpreter.

It also catches more. A wheel whose filename and metadata disagree, or whose
module is tagged for a different Python than the wheel claims, are both real
mis-builds that the sysconfig form would have passed whenever the checker
happened to run under the matching interpreter.

The checker lives in .ci/pytorch/check_wheel_soabi.py rather than inline: as a
file it is linted and directly testable, which an embedded heredoc is not, and
.ci/pytorch already holds print_sccache_log.py and rocm_preflight.py. It takes
any number of wheels so the caller passes the glob directly -- dist/ is not
guaranteed to hold exactly one, pip_install_whl in common_utils.sh already splits
a multi-path argument for that reason, and an earlier draft using
`$(echo dist/*.whl)` would have silently checked whichever wheel sorted first.

It is gated on the riscv64 cross environment. A native build satisfies it
trivially, and adding wheel inspection to every build job is a wider change than
this branch should make; the check is cheap enough that generalising it later is
worth considering.

There is adjacent prior art. .ci/pytorch/smoke_test/check_wheel_tags.py, added in
TARGET_OS -- answering "did we build for the target we meant to", which does need
an external reference. It never inspects the extension module's filename, so an
untagged torch/_C.so inside a correctly tagged wheel passes it cleanly; that is
the gap pytorchgh-189388 fell through. It is also reachable only via smoke_test.py,
which imports torch and therefore cannot run in a cross job. Folding the
module-name check in there would cover every platform's binary builds rather than
this one job, and is worth doing, but it is filed separately: that file is
smoke-test-only, so the cross job would still need its own call site.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

The assertion needs a ciflow/riscv64 run to exercise in place, and should pass:
run 33243561273 already produced the tagged name it looks for.

Because the check reads only the wheel, the riscv64 case is testable without a
riscv64 toolchain -- a wheel is a zip. These were built to order and run under
**host CPython 3.10 on x86_64**, deliberately neither the Python nor the
architecture the wheels claim, which is the property the sysconfig form lacked:

| wheel under test | exit |
| --- | --- |
| `cp312-cp312-linux_riscv64` shipping `_C.cpython-312-riscv64-linux-gnu.so` | 0 |
| same, shipping untagged `torch/_C.so` (the pytorchgh-189388 regression) | 1 |
| same, shipping no `torch/_C*` at all (the conda-forge symptom) | 1 |
| filename says `cp312-...`, `.dist-info/WHEEL` says `cp310-...` | 1 |
| declares `cp312`, ships `_C.cpython-310-riscv64-linux-gnu.so` | 1 |
| ships both the tagged module and an untagged `torch/_C.so` | 1 |
| a libtorch wheel plus a good torch wheel (former skipped) | 0 |
| a libtorch wheel alone, so nothing was verified | 1 |

Each failure names what it found and what it expected. Exit codes were read
directly rather than through a pipe, since `head` would otherwise mask them.

Lint:

```
lintrunner --paths-cmd='git diff --cached --name-only'
bash -n .ci/pytorch/build.sh
```
zklaus added a commit to zklaus/pytorch that referenced this pull request Sep 29, 2026
Second self-review pass on the checker added earlier in this branch. Three
corrections, none of which the riscv64 job would hit today, but all of which it
would hit once the check is generalised to other wheels -- which is the filed
follow-up.

`torch/_C` was matched as a plain prefix. That is loose in two directions:
`torch/_C/` is a real directory of .pyi stubs, and a sibling extension module
named `torch/_C_something.so` would match while not being the module in
question. A wheel shipping one alongside `_C` would have been rejected with
"ships several torch/_C modules". Matching `torch/_C\.[^/]+` instead names the
form exactly: `torch/_C.<abi>.<ext>`, or the untagged `torch/_C.<ext>`.

`.dylib` was in the accepted suffix list. The suffixes CPython's import system
recognises are those in importlib.machinery.EXTENSION_SUFFIXES -- `.so` on POSIX
including macOS, `.pyd` on Windows -- implemented by _PyImport_DynLoadFiletab in
Python/dynload_shlib.c, which macOS uses (configure.ac selects dynload_shlib.o
wherever dlopen() exists). `.dylib` is not among them. It is not an idle
distinction: macOS uses `.dylib` heavily for shared libraries, and torch's own
macOS wheel ships seven of them next to the module, so accepting it widened what
could be mistaken for an extension module on exactly the platform where the
mistake was available.

Comparing the filename's tags to the ``Tag:`` lines as strings is wrong for a
compressed tag set. PEP 427 allows a filename to carry ``py2.py3-none-any`` while
WHEEL lists each expansion on its own line, so the string compare fails on a
correct wheel. The expansions are now compared as a set, and the ABI check
accepts any member of a compressed ABI set. torch does not ship compressed tags,
so this is latent rather than live.

This commit was authored with the assistance of an AI coding assistant.

Test Plan:

Wheels built to order, run under host CPython 3.10 on x86_64 -- deliberately
neither the Python nor the architecture they claim:

| wheel | exit | why |
| --- | --- | --- |
| tagged module plus `torch/_C/*.pyi` stubs | 0 | the stub directory is not a module |
| tagged module plus a sibling `torch/_C_foo.so` | 0 | **rejected before this commit** |
| untagged `torch/_C.so` | 1 | the pytorchgh-189388 regression |
| `cp312.cp313-abi3-linux_riscv64`, both Tag: lines present, ships `_C.abi3.so` | 0 | **rejected before this commit** |
| same, one Tag: line missing from WHEEL | 1 | genuinely inconsistent |

The earlier cases from the previous commit were re-run and are unchanged: correct
wheel passes, missing module fails, filename/metadata mismatch fails, module
tagged for another Python fails, two `torch/_C.*` modules fail, libtorch-only
wheel is skipped, libtorch-only alone fails.

Lint:

```
lintrunner --paths-cmd='git diff --name-only origin/viable/strict'
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: build release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants