@@ -689,6 +689,15 @@ def build_extensions(self) -> None:
689
689
# min supported CPython version.
690
690
# See https://docs.python.org/3/c-api/stable.html#c.Py_LIMITED_API
691
691
self ._add_compile_flag (extension , f'-DPy_LIMITED_API={ min_supported_cpython } ' )
692
+ else :
693
+ # pybind11 is not CPython API stable so don't add these flags used when
694
+ # compiling pybind11 when pybind11 is not even used. otherwise, the build
695
+ # logs are confusing.
696
+ # See note [Pybind11 ABI constants]
697
+ for name in ["COMPILER_TYPE" , "STDLIB" , "BUILD_ABI" ]:
698
+ val = getattr (torch ._C , f"_PYBIND11_{ name } " )
699
+ if val is not None and not IS_WINDOWS :
700
+ self ._add_compile_flag (extension , f'-DPYBIND11_{ name } ="{ val } "' )
692
701
self ._define_torch_extension_name (extension )
693
702
694
703
if 'nvcc_dlink' in extension .extra_compile_args :
@@ -1705,6 +1714,25 @@ def load(name,
1705
1714
is_standalone ,
1706
1715
keep_intermediates = keep_intermediates )
1707
1716
1717
+ def _get_pybind11_abi_build_flags ():
1718
+ # Note [Pybind11 ABI constants]
1719
+ #
1720
+ # Pybind11 before 2.4 used to build an ABI strings using the following pattern:
1721
+ # f"__pybind11_internals_v{PYBIND11_INTERNALS_VERSION}{PYBIND11_INTERNALS_KIND}{PYBIND11_BUILD_TYPE}__"
1722
+ # Since 2.4 compier type, stdlib and build abi parameters are also encoded like this:
1723
+ # f"__pybind11_internals_v{PYBIND11_INTERNALS_VERSION}{PYBIND11_INTERNALS_KIND}{PYBIND11_COMPILER_TYPE}{PYBIND11_STDLIB}{PYBIND11_BUILD_ABI}{PYBIND11_BUILD_TYPE}__"
1724
+ #
1725
+ # This was done in order to further narrow down the chances of compiler ABI incompatibility
1726
+ # that can cause a hard to debug segfaults.
1727
+ # For PyTorch extensions we want to relax those restrictions and pass compiler, stdlib and abi properties
1728
+ # captured during PyTorch native library compilation in torch/csrc/Module.cpp
1729
+
1730
+ abi_cflags = []
1731
+ for pname in ["COMPILER_TYPE" , "STDLIB" , "BUILD_ABI" ]:
1732
+ pval = getattr (torch ._C , f"_PYBIND11_{ pname } " )
1733
+ if pval is not None and not IS_WINDOWS :
1734
+ abi_cflags .append (f'-DPYBIND11_{ pname } =\\ "{ pval } \\ "' )
1735
+ return abi_cflags
1708
1736
1709
1737
def check_compiler_is_gcc (compiler ):
1710
1738
if not IS_LINUX :
@@ -1835,6 +1863,7 @@ def build_precompile_header(pch_cmd):
1835
1863
common_cflags += ['-DTORCH_API_INCLUDE_EXTENSION_H' ]
1836
1864
1837
1865
common_cflags += ['-std=c++17' , '-fPIC' ]
1866
+ common_cflags += [f"{ x } " for x in _get_pybind11_abi_build_flags ()]
1838
1867
common_cflags_str = listToString (common_cflags )
1839
1868
1840
1869
pch_cmd = format_precompiler_header_cmd (compiler , head_file , head_file_pch , common_cflags_str , torch_include_dirs_str , extra_cflags_str , extra_include_paths_str )
@@ -2669,6 +2698,8 @@ def _write_ninja_file_to_build_library(path,
2669
2698
common_cflags .append (f'-DTORCH_EXTENSION_NAME={ name } ' )
2670
2699
common_cflags .append ('-DTORCH_API_INCLUDE_EXTENSION_H' )
2671
2700
2701
+ common_cflags += [f"{ x } " for x in _get_pybind11_abi_build_flags ()]
2702
+
2672
2703
# Windows does not understand `-isystem` and quotes flags later.
2673
2704
if IS_WINDOWS :
2674
2705
common_cflags += [f'-I{ include } ' for include in user_includes + system_includes ]
0 commit comments