|
22 | 22 |
|
23 | 23 | logger = logging.getLogger(__name__) |
24 | 24 |
|
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}") |
42 | 62 |
|
43 | 63 | from torchao.quantization import ( |
44 | 64 | autoquant, |
|
0 commit comments