Commit d35db53
Build torch._C extension module and version.py via CMake (pytorch#180243)
Move the build of the `torch._C` Python extension and the install of
`torch/version.py` from `setup.py` into `torch/CMakeLists.txt`, so
both the existing setuptools backend and the upcoming
scikit-build-core backend (pytorch#180247) go through the same code path.
The new `Python_add_library(_C MODULE WITH_SOABI ...)` target builds
the same `torch/csrc/stub.c` and installs at the wheel root (mapped to
`torch/` by `wheel.install-dir`); `setup.py`'s `Extension("torch._C",
...)` block and its companion `extra_compile_args` /
`extra_link_args` / `make_relative_rpath_args` plumbing are removed.
`version.py` is gitignored, so it is installed explicitly via
`install(FILES ... DESTINATION ".")` rather than relying on Python
package collection.
Adds a `BinaryDistribution` class in `setup.py` that forces
`has_ext_modules=True` so `bdist_wheel` uses the binary layout
(package_data + data_files only) instead of the purelib layout, which
would otherwise dump CMake's `build/lib/` tree -- including test
artifacts -- into the wheel root. `package_data` collects the
CMake-built extension via the interpreter's exact module name
(`f"_C{sysconfig.get_config_var('EXT_SUFFIX')}"`) rather than a `_C*`
glob, so a `build/` dir reused across Python versions cannot leak
other ABIs' `_C` modules into the wheel.
Windows handling: the manual `setup.py` copy of `_C.lib` from
`build_temp` to `build_lib` is replaced by the `_C` target's
`ARCHIVE DESTINATION "lib"`. For MSVC, the linker is told where to
find `pythonXX.lib` via `/LIBPATH` (Python headers use `#pragma
comment(lib, "pythonXX")` which expects a bare-name lookup); for
Windows venvs, FindPython's library search path is extended with
`sys.base_prefix/libs` since the import library lives in the base
install, not the venv. `nnapi_backend` gains
`TORCH_PYTHON_LINK_FLAGS` so it picks up the same MSVC linker
options as `torch_python`.
## Test plan
Verification is via artifact-content comparison against the parent
(pytorch#180242). Expectation: the wheel must be content-equivalent modulo
the one file the PR rebuilds (`torch/_C.cpython-*.so`), which must
remain functionally identical (same exports, same imports, same
rpath).
- [x] **No regression in artifact sizes** across every manywheel /
libtorch artifact (sub-0.001 % to 0.001 % delta, zip-timestamp
noise consistent across artifacts this PR can't directly affect):
| artifact | parent (pytorch#180242) | this PR (pytorch#180243) | delta |
| --- | ---: | ---: | ---: |
| manywheel-py3_12-rocm7_2 | 6,153,280,701 | 6,153,275,144 | -5,557 (-0.000%) |
| manywheel-py3_12-rocm7_1 | 5,788,503,789 | 5,788,499,957 | -3,832 (-0.000%) |
| manywheel-py3_12-cuda12_6 | 841,797,521 | 841,797,813 | +292 (+0.000%) |
| manywheel-py3_12-cuda13_2 | 624,213,880 | 624,212,127 | -1,753 (-0.000%) |
| manywheel-py3_12-cpu | 189,248,452 | 189,248,117 | -335 (-0.000%) |
| manywheel-py3_12-xpu | 778,712,445 | 778,711,285 | -1,160 (-0.000%) |
| libtorch-rocm7_2-shared-with-deps-release | 12,117,315,856 | 12,117,314,539 | -1,317 (-0.000%) |
| libtorch-cpu-shared-with-deps-release | 257,626,615 | 257,624,612 | -2,003 (-0.001%) |
- [x] **Wheel manifest equivalence vs parent**
(`manywheel-py3_12-rocm7_2`): both wheels contain **26,779 entries**;
filename diff is empty -- no files added or removed. Confirms the
`BinaryDistribution` layout fix worked (no `build/lib/` spillage
into the wheel root).
- [x] **Per-file size diff: exactly one file changed.**
```
torch/_C.cpython-312-x86_64-linux-gnu.so 22,960 -> 15,720 (-7,240)
```
Every other one of 26,778 files is byte-identical in size. That is
precisely the file the PR rebuilds via CMake.
- [x] **`torch._C` is functionally equivalent** between parent and PR.
Extracted both `.so` files and compared:
- **Defined symbols**: zero-line diff.
- **Undefined symbols** (imports from `libtorch_python`): zero-line
diff.
- **RPATH**: identical (`$ORIGIN:$ORIGIN/lib`).
- **NEEDED**: 180242's `.so` includes a redundant
`libpthread.so.0` (merged into `libc.so.6` since glibc 2.34); the
CMake build drops it. The size shrinkage matches the cleaner
NEEDED + tighter section layout from the CMake toolchain.
- [x] **`torch/version.py`** is present in both wheels at 339 bytes,
byte-identical except for the expected `git_version` SHA (each
commit has its own). `__version__`, `debug`, `cuda`, `hip`, `rocm`,
`xpu` fields all match -- the CMake-driven generation produces the
same output as the setuptools path.
- [x] **Exactly one `_C` ABI per wheel.** On a fresh multi-ABI
`manywheel-cpu-build` (build_all.sh builds all CPython ABIs in one
checkout), each wheel ships only its own `torch/_C.cpython-XY.so`.
An earlier greedy `_C*.so` `package_data` glob leaked stale per-ABI
stubs (a cp312 wheel carried cp310/cp311/cp312); pinning to the
exact `EXT_SUFFIX` fixes it.
```
unzip -l manywheel-py3_13-cpu.whl | grep 'torch/_C\.cpython'
# -> only torch/_C.cpython-313-x86_64-linux-gnu.so
```
### Coverage notes
- **NNAPI MSVC link-flag fix** and **Windows venv `pythonXX.lib`
discovery fix** are not exercised by the ROCm-Linux artifacts
above. They are exercised by the Windows pull-CI (where NNAPI
builds run and venv-based discovery is relevant); the green Windows
CI is the evidence for those.
- ROCm wheels are Linux-only by build matrix
(`.github/scripts/generate_binary_build_matrix.py:130-135`,
`supported_platforms=["Linux"]`).
Co-Generated with [Claude Code](https://claude.com/claude-code)
Pull Request resolved: pytorch#180243
Approved by: https://github.com/malfet1 parent 21d63ff commit d35db53
3 files changed
Lines changed: 81 additions & 117 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
814 | 814 | | |
815 | 815 | | |
816 | 816 | | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
817 | 832 | | |
818 | 833 | | |
819 | 834 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
313 | 313 | | |
314 | 314 | | |
315 | 315 | | |
316 | | - | |
| 316 | + | |
317 | 317 | | |
318 | 318 | | |
319 | 319 | | |
| |||
973 | 973 | | |
974 | 974 | | |
975 | 975 | | |
976 | | - | |
977 | | - | |
978 | | - | |
979 | | - | |
980 | | - | |
981 | | - | |
982 | | - | |
983 | | - | |
984 | | - | |
985 | | - | |
986 | | - | |
987 | | - | |
988 | | - | |
989 | | - | |
990 | | - | |
991 | | - | |
992 | | - | |
993 | 976 | | |
994 | 977 | | |
995 | 978 | | |
| |||
1031 | 1014 | | |
1032 | 1015 | | |
1033 | 1016 | | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
1034 | 1028 | | |
1035 | 1029 | | |
1036 | 1030 | | |
| |||
1059 | 1053 | | |
1060 | 1054 | | |
1061 | 1055 | | |
1062 | | - | |
1063 | 1056 | | |
1064 | 1057 | | |
1065 | | - | |
1066 | | - | |
1067 | | - | |
1068 | | - | |
1069 | | - | |
1070 | | - | |
1071 | | - | |
1072 | | - | |
1073 | | - | |
1074 | | - | |
1075 | | - | |
1076 | | - | |
1077 | | - | |
1078 | | - | |
1079 | | - | |
1080 | | - | |
1081 | | - | |
1082 | | - | |
1083 | | - | |
1084 | | - | |
1085 | | - | |
1086 | | - | |
1087 | | - | |
1088 | | - | |
1089 | | - | |
1090 | | - | |
1091 | | - | |
1092 | | - | |
1093 | | - | |
1094 | | - | |
1095 | | - | |
1096 | | - | |
1097 | | - | |
1098 | | - | |
1099 | | - | |
1100 | | - | |
1101 | | - | |
1102 | | - | |
1103 | | - | |
1104 | | - | |
1105 | | - | |
1106 | | - | |
1107 | | - | |
1108 | | - | |
1109 | | - | |
1110 | | - | |
1111 | | - | |
1112 | | - | |
1113 | | - | |
1114 | 1058 | | |
1115 | 1059 | | |
1116 | 1060 | | |
| |||
1120 | 1064 | | |
1121 | 1065 | | |
1122 | 1066 | | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
1135 | | - | |
1136 | | - | |
1137 | | - | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
1148 | | - | |
1149 | | - | |
1150 | | - | |
1151 | | - | |
1152 | 1067 | | |
1153 | 1068 | | |
1154 | 1069 | | |
1155 | 1070 | | |
| 1071 | + | |
| 1072 | + | |
1156 | 1073 | | |
1157 | 1074 | | |
1158 | 1075 | | |
| |||
1163 | 1080 | | |
1164 | 1081 | | |
1165 | 1082 | | |
1166 | | - | |
1167 | | - | |
1168 | | - | |
1169 | | - | |
1170 | | - | |
1171 | | - | |
1172 | | - | |
1173 | | - | |
1174 | | - | |
1175 | | - | |
1176 | | - | |
1177 | | - | |
1178 | | - | |
1179 | | - | |
1180 | | - | |
1181 | | - | |
1182 | | - | |
1183 | | - | |
1184 | 1083 | | |
1185 | 1084 | | |
1186 | 1085 | | |
| |||
1272 | 1171 | | |
1273 | 1172 | | |
1274 | 1173 | | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
1275 | 1179 | | |
1276 | 1180 | | |
1277 | 1181 | | |
| |||
1378 | 1282 | | |
1379 | 1283 | | |
1380 | 1284 | | |
| 1285 | + | |
1381 | 1286 | | |
1382 | 1287 | | |
1383 | 1288 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
111 | 120 | | |
112 | 121 | | |
113 | 122 | | |
| |||
424 | 433 | | |
425 | 434 | | |
426 | 435 | | |
427 | | - | |
| 436 | + | |
428 | 437 | | |
429 | 438 | | |
430 | 439 | | |
431 | 440 | | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
432 | 470 | | |
433 | 471 | | |
434 | 472 | | |
| |||
454 | 492 | | |
455 | 493 | | |
456 | 494 | | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
457 | 498 | | |
458 | 499 | | |
459 | 500 | | |
| |||
470 | 511 | | |
471 | 512 | | |
472 | 513 | | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
473 | 517 | | |
474 | 518 | | |
475 | 519 | | |
| |||
0 commit comments