Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <ATen/ops/cat_cuda_dispatch.h>
#include <ATen/ops/norm_cuda_dispatch.h>
#include <ATen/ops/unsqueeze.h>
#include <torch/extension.h>
#include <torch/library.h>

at::Tensor ultra_norm(at::TensorList inputs) {
auto res = at::native::foreach_tensor_norm_cuda(inputs);
Expand Down
47 changes: 36 additions & 11 deletions torch/utils/cpp_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ def _join_sycl_home(*paths) -> str:
'win-amd64' : 'x86_amd64',
}

min_supported_cpython = "0x03090000" # Python 3.9 hexcode

def get_cxx_compiler():
if IS_WINDOWS:
compiler = os.environ.get('CXX', 'cl')
Expand Down Expand Up @@ -573,11 +575,22 @@ def build_extensions(self) -> None:
extension.extra_compile_args[ext] = []

self._add_compile_flag(extension, '-DTORCH_API_INCLUDE_EXTENSION_H')
# See note [Pybind11 ABI constants]
for name in ["COMPILER_TYPE", "STDLIB", "BUILD_ABI"]:
val = getattr(torch._C, f"_PYBIND11_{name}")
if val is not None and not IS_WINDOWS:
self._add_compile_flag(extension, f'-DPYBIND11_{name}="{val}"')

if extension.py_limited_api:
# compile any extension that has passed in py_limited_api to the
# Extension constructor with the Py_LIMITED_API flag set to our
# min supported CPython version.
# See https://docs.python.org/3/c-api/stable.html#c.Py_LIMITED_API
self._add_compile_flag(extension, f'-DPy_LIMITED_API={min_supported_cpython}')
else:
# pybind11 is not CPython API stable so don't add these flags used when
# compiling pybind11 when pybind11 is not even used. otherwise, the build
# logs are confusing.
# See note [Pybind11 ABI constants]
for name in ["COMPILER_TYPE", "STDLIB", "BUILD_ABI"]:
val = getattr(torch._C, f"_PYBIND11_{name}")
if val is not None and not IS_WINDOWS:
self._add_compile_flag(extension, f'-DPYBIND11_{name}="{val}"')
self._define_torch_extension_name(extension)
self._add_gnu_cpp_abi_flag(extension)

Expand Down Expand Up @@ -972,7 +985,7 @@ def CppExtension(name, sources, *args, **kwargs):
constructor. Full list arguments can be found at
https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#extension-api-reference

.. note::
.. warning::
The PyTorch python API (as provided in libtorch_python) cannot be built
with the flag ``py_limited_api=True``. When this flag is passed, it is
the user's responsibility in their library to not use APIs from
Expand All @@ -981,6 +994,14 @@ def CppExtension(name, sources, *args, **kwargs):
example, to give access to custom ops from python, the library should
register the ops through the dispatcher.

Contrary to CPython setuptools, who does not define -DPy_LIMITED_API
as a compile flag when py_limited_api is specified as an option for
the "bdist_wheel" command in ``setup``, PyTorch does! We will specify
-DPy_LIMITED_API=min_supported_cpython to best enforce consistency,
safety, and sanity in order to encourage best practices. To target a
different version, set min_supported_cpython to the hexcode of the
CPython version of choice.

Example:
>>> # xdoctest: +SKIP
>>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CPP_EXT)
Expand Down Expand Up @@ -1036,7 +1057,7 @@ def CUDAExtension(name, sources, *args, **kwargs):
constructor. Full list arguments can be found at
https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#extension-api-reference

.. note::
.. warning::
The PyTorch python API (as provided in libtorch_python) cannot be built
with the flag ``py_limited_api=True``. When this flag is passed, it is
the user's responsibility in their library to not use APIs from
Expand All @@ -1045,6 +1066,14 @@ def CUDAExtension(name, sources, *args, **kwargs):
example, to give access to custom ops from python, the library should
register the ops through the dispatcher.

Contrary to CPython setuptools, who does not define -DPy_LIMITED_API
as a compile flag when py_limited_api is specified as an option for
the "bdist_wheel" command in ``setup``, PyTorch does! We will specify
-DPy_LIMITED_API=min_supported_cpython to best enforce consistency,
safety, and sanity in order to encourage best practices. To target a
different version, set min_supported_cpython to the hexcode of the
CPython version of choice.

Example:
>>> # xdoctest: +SKIP
>>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CPP_EXT)
Expand Down Expand Up @@ -1408,10 +1437,6 @@ def _get_pybind11_abi_build_flags():
# that can cause a hard to debug segfaults.
# For PyTorch extensions we want to relax those restrictions and pass compiler, stdlib and abi properties
# captured during PyTorch native library compilation in torch/csrc/Module.cpp
#
# Note that these flags don't have side effects even if the PyTorch extension does not
# require nor use pybind, so we do not do anything differently for them in the py_limited_api
# case.

abi_cflags = []
for pname in ["COMPILER_TYPE", "STDLIB", "BUILD_ABI"]:
Expand Down