Skip to content

Add C++ tensor library (from THD fork) - #526

Merged
colesbury merged 2 commits into
pytorch:masterfrom
colesbury:THPP
Jan 20, 2017
Merged

colesbury merged 2 commits into
pytorch:masterfrom
colesbury:THPP

Conversation

@colesbury

Copy link
Copy Markdown
Contributor

I've done some minimal testing. I'll be using it more in the autograd refactoring.

@apaszke, does this make sense?

@apaszke apaszke left a comment

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.

Just remove the CMake part that was used to build THD tests. Apart from this LGTM

Comment thread torch/lib/THPP/CMakeLists.txt Outdated
# Test executables
# ENABLE_TESTING()

# FIND_PACKAGE(Threads)

This comment was marked as off-topic.

@colesbury
colesbury merged commit c9db9c2 into pytorch:master Jan 20, 2017
ashishfarmer pushed a commit to ashishfarmer/pytorch that referenced this pull request Mar 16, 2020
jjsjann123 pushed a commit to jjsjann123/pytorch that referenced this pull request Nov 25, 2020
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 9, 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 packaging on cross builds: setup.py collects the extension via
package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}", i.e. the target
suffix (_C.cpython-3XY-aarch64-linux-gnu.so). The untagged name does not match,
so _C is dropped from the wheel and import torch._C fails. This is the
conda-forge linux-aarch64 failure in pytorch-cpu-feedstock pytorch#526.

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 rather than relying on WITH_SOABI's
computation, when cross-compiling we query SOABI from the interpreter (which
crossenv answers with the target's value) and set Python_SOABI 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 is guarded
on a non-empty result, so a genuine emulator-based cross-build -- where this bare
invocation cannot run the target interpreter -- keeps FindPython's already-correct
value.

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 setting Python_SOABI before the
Python_add_library calls (with 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).
End-to-end validation is a conda-forge linux-aarch64 build (pytorch-cpu-feedstock
pytorch#526) confirming _C.cpython-3XY-aarch64-linux-gnu.so lands in the wheel and
import torch._C succeeds.
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 9, 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 packaging on cross builds: setup.py collects the extension via
package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}", i.e. the target
suffix (_C.cpython-3XY-aarch64-linux-gnu.so). The untagged name does not match,
so _C is dropped from the wheel and import torch._C fails. This is the
conda-forge linux-aarch64 failure in pytorch-cpu-feedstock pytorch#526.

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).
End-to-end validation is a conda-forge linux-aarch64 build (pytorch-cpu-feedstock
pytorch#526) confirming _C.cpython-3XY-aarch64-linux-gnu.so lands in the wheel and
import torch._C succeeds.
zklaus added a commit to zklaus/pytorch that referenced this pull request Jul 9, 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 packaging on cross builds: setup.py collects the extension via
package_data = f"_C{sysconfig.get_config_var('EXT_SUFFIX')}", i.e. the target
suffix (_C.cpython-3XY-aarch64-linux-gnu.so). The untagged name does not match,
so _C is dropped from the wheel and import torch._C fails. This is the
conda-forge linux-aarch64 failure in pytorch-cpu-feedstock pytorch#526.

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).
End-to-end validation is a conda-forge linux-aarch64 build (pytorch-cpu-feedstock
pytorch#526) confirming _C.cpython-3XY-aarch64-linux-gnu.so lands in the wheel and
import torch._C succeeds.
zklaus added a commit to zklaus/pytorch that referenced this pull request Aug 18, 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants