Skip to content

Commit d22bbf5

Browse files
committed
torchao init: do not load .so files for known incompatible torch version
Summary: Short term fix for #2901 to unblock the 0.13.0 release. Long version: 1. torchao's c++ kernels are not using libtorch and therefore are not guaranteed to work across different PyTorch versions 2. looks like we got lucky with (1) as torchao kernels just happened to work across PyTorch versions <= 2.8, but PyTorch nightlies in 2.9 introduce a breaking ABI change (I don't know what specifically). Therefore, if we build torchao with torch 2.8, and then import it in an environment with torch 2.9+, the Python process will crash with `Aborted (core dumped)`. For now, I just gate out the "known broken" case where we detect that the torch version used to build torchao is < 2.9, and the torch version in the environment when torchao is imported is >= 2.9. If this is detected, this PR skips importing the `.so` files and logs a warning, to at least have most of the torchao Python API still work and give the user some information about how to get the custom kernels working. For future releases, we'll need to make this more robust - leaving that for future PRs. Test Plan: ```bash // install the 0.13.0 RC, built with PyTorch 2.8 with-proxy pip install torchao==0.13.0 --extra-index-url https://download.pytorch.org/whl/test/cu128 // copy over these changes to the local __init__.py file in the installation: // ~/.conda/envs/pytorch_nightly/lib/python3.11/site-packages/torchao/__init__.py // install PyTorch 2.9.x nightly with-proxy pip3 install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu128 // import torchao, verify no more crash and the warning message is emitted (pytorch_nightly) [vasiliy@devgpu007.eag6 ~/local]$ python -X faulthandler -c "import torch; print(torch.__version__); import torchao" 2.9.0.dev20250829+cu128 Skipping import of cpp extensions due to incompatible torch version 2.9.0.dev20250829+cu128 for torchao version 0.13.0+cu128 ``` Reviewers: Subscribers: Tasks: Tags:
1 parent 2f78cfe commit d22bbf5

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

torchao/__init__.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,43 @@
2222

2323
logger = logging.getLogger(__name__)
2424

25-
try:
26-
from pathlib import Path
27-
28-
so_files = list(Path(__file__).parent.glob("_C*.so"))
29-
if len(so_files) > 0:
30-
for file in so_files:
31-
torch.ops.load_library(str(file))
32-
from . import ops
33-
34-
# The following library contains CPU kernels from torchao/experimental
35-
# They are built automatically by ao/setup.py if on an ARM machine.
36-
# They can also be built outside of the torchao install process by
37-
# running the script `torchao/experimental/build_torchao_ops.sh <aten|executorch>`
38-
# For more information, see https://github.com/pytorch/ao/blob/main/torchao/experimental/docs/readme.md
39-
from torchao.experimental.op_lib import * # noqa: F403
40-
except Exception as e:
41-
logger.debug(f"Skipping import of cpp extensions: {e}")
25+
skip_loading_so_files = False
26+
# if torchao version has "+git", assume it's locally built and we don't know
27+
# anything about the PyTorch version used to build it
28+
# otherwise, assume it's prebuilt by torchao's build scripts and we can make
29+
# assumptions about the PyTorch version used to build it.
30+
if (not "+git" in __version__) and not ("unknown" in __version__):
31+
# torchao v0.13.0 is built with PyTorch 2.8.0. We know that torchao .so
32+
# files built using PyTorch 2.8.0 are not ABI compatible with PyTorch 2.9+.
33+
# The following code skips importing the .so files if PyTorch 2.9+ is
34+
# detected, to avoid crashing the Python process with "Aborted (core
35+
# dumped)".
36+
# TODO(#2901, and before next torchao release): make this generic for
37+
# future torchao and torch versions
38+
if __version__.startswith("0.13.0") and torch.__version__ > "2.8":
39+
logger.warning(
40+
f"Skipping import of cpp extensions due to incompatible torch version {torch.__version__} for torchao version {__version__}"
41+
)
42+
skip_loading_so_files = True
43+
44+
if not skip_loading_so_files:
45+
try:
46+
from pathlib import Path
47+
48+
so_files = list(Path(__file__).parent.glob("_C*.so"))
49+
if len(so_files) > 0:
50+
for file in so_files:
51+
torch.ops.load_library(str(file))
52+
from . import ops
53+
54+
# The following library contains CPU kernels from torchao/experimental
55+
# They are built automatically by ao/setup.py if on an ARM machine.
56+
# They can also be built outside of the torchao install process by
57+
# running the script `torchao/experimental/build_torchao_ops.sh <aten|executorch>`
58+
# For more information, see https://github.com/pytorch/ao/blob/main/torchao/experimental/docs/readme.md
59+
from torchao.experimental.op_lib import * # noqa: F403
60+
except Exception as e:
61+
logger.warning(f"Skipping import of cpp extensions: {e}")
4262

4363
from torchao.quantization import (
4464
autoquant,

0 commit comments

Comments
 (0)