@@ -364,6 +364,11 @@ def build_extensions(self) -> None:
364
364
extension .extra_compile_args [ext ] = []
365
365
366
366
self ._add_compile_flag (extension , '-DTORCH_API_INCLUDE_EXTENSION_H' )
367
+ # See note [Pybind11 ABI constants]
368
+ for name in ["COMPILER_TYPE" , "STDLIB" , "BUILD_ABI" ]:
369
+ val = getattr (torch ._C , f"_PYBIND11_{ name } " )
370
+ if val is not None and not IS_WINDOWS :
371
+ self ._add_compile_flag (extension , f'-DPYBIND11_{ name } ="{ val } "' )
367
372
self ._define_torch_extension_name (extension )
368
373
self ._add_gnu_cpp_abi_flag (extension )
369
374
@@ -1590,6 +1595,24 @@ def _write_ninja_file_to_build_library(path,
1590
1595
1591
1596
common_cflags = [f'-DTORCH_EXTENSION_NAME={ name } ' ]
1592
1597
common_cflags .append ('-DTORCH_API_INCLUDE_EXTENSION_H' )
1598
+
1599
+ # Note [Pybind11 ABI constants]
1600
+ #
1601
+ # Pybind11 before 2.4 used to build an ABI strings using the following pattern:
1602
+ # f"__pybind11_internals_v{PYBIND11_INTERNALS_VERSION}{PYBIND11_INTERNALS_KIND}{PYBIND11_BUILD_TYPE}__"
1603
+ # Since 2.4 compier type, stdlib and build abi parameters are also encoded like this:
1604
+ # f"__pybind11_internals_v{PYBIND11_INTERNALS_VERSION}{PYBIND11_INTERNALS_KIND}{PYBIND11_COMPILER_TYPE}{PYBIND11_STDLIB}{PYBIND11_BUILD_ABI}{PYBIND11_BUILD_TYPE}__"
1605
+ #
1606
+ # This was done in order to further narrow down the chances of compiler ABI incompatibility
1607
+ # that can cause a hard to debug segfaults.
1608
+ # For PyTorch extensions we want to relax those restrictions and pass compiler, stdlib and abi properties
1609
+ # captured during PyTorch native library compilation in torch/csrc/Module.cpp
1610
+
1611
+ for pname in ["COMPILER_TYPE" , "STDLIB" , "BUILD_ABI" ]:
1612
+ pval = getattr (torch ._C , f"_PYBIND11_{ pname } " )
1613
+ if pval is not None and not IS_WINDOWS :
1614
+ common_cflags .append (f'-DPYBIND11_{ pname } =\\ "{ pval } \\ "' )
1615
+
1593
1616
common_cflags += [f'-I{ include } ' for include in user_includes ]
1594
1617
common_cflags += [f'-isystem { include } ' for include in system_includes ]
1595
1618
0 commit comments