Skip to content

Commit 9a96b75

Browse files
committed
Adjust advice based on Sam Gross's knowlede
1 parent 1372553 commit 9a96b75

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

advanced_source/cpp_custom_ops.rst

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ If you need to compile CUDA code (for example, ``.cu`` files), then instead use
7878
Please see `extension-cpp <https://github.com/pytorch/extension-cpp>`_ for an
7979
example for how this is set up.
8080

81-
The above example represents what we refer to as a CPython agnostic wheel, meaning
82-
we are building a single wheel that can be run across multiple CPython versions (similar
83-
to pure Python packages). CPython agnosticism is desirable in minimizing the number of wheels your
84-
custom library needs to support and release. To achieve this, there are three key lines to note.
81+
The above example represents what we refer to as a CPython agnostic wheel, meaning we are
82+
building a single wheel that can be run across multiple CPython versions (similar to pure
83+
Python packages). CPython agnosticism is desirable in minimizing the number of wheels your
84+
custom library needs to support and release. The minimum version we'd like to support is
85+
3.9, since it is the oldest supported version currently, so we use the corresponding hexcode
86+
and specifier throughout the setup code. We suggest building the extension in the same
87+
environment as the minimum CPython version you'd like to support to minimize unknown behavior,
88+
so, here, we build the extension in a CPython 3.9 environment. When built, this single wheel
89+
will be runnable in any CPython environment 3.9+. To achieve this, there are three key lines
90+
to note.
8591

8692
The first is the specification of ``Py_LIMITED_API`` in ``extra_compile_args`` to the
8793
minimum CPython version you would like to support:
@@ -90,15 +96,20 @@ minimum CPython version you would like to support:
9096
9197
extra_compile_args={"cxx": ["-DPy_LIMITED_API=0x03090000"]},
9298
93-
Defining the ``Py_LIMITED_API`` flag helps guarantee that the extension is in fact
99+
Defining the ``Py_LIMITED_API`` flag helps verify that the extension is in fact
94100
only using the `CPython Stable Limited API <https://docs.python.org/3/c-api/stable.html>`_,
95101
which is a requirement for the building a CPython agnostic wheel. If this requirement
96102
is not met, it is possible to build a wheel that looks CPython agnostic but will crash,
97103
or worse, be silently incorrect, in another CPython environment. Take care to avoid
98104
using unstable CPython APIs, for example APIs from libtorch_python (in particular
99105
pytorch/python bindings,) and to only use APIs from libtorch (ATen objects, operators
100106
and the dispatcher). We strongly recommend defining the ``Py_LIMITED_API`` flag to
101-
ensure the extension is compliant and safe as a CPython agnostic wheel.
107+
help ascertain the extension is compliant and safe as a CPython agnostic wheel. Note that
108+
defining this flag is not a full guarantee that the built wheel is CPython agnostic, but
109+
it is better than the wild wild west. There are several caveats mentioned in the
110+
`Python docs <https://docs.python.org/3/c-api/stable.html#limited-api-caveats>`_,
111+
and you should test and verify yourself that the wheel is truly agnostic for the relevant
112+
CPython versions.
102113

103114
The second and third lines specifying ``py_limited_api`` inform setuptools that you intend
104115
to build a CPython agnostic wheel and will influence the naming of the wheel accordingly:
@@ -116,9 +127,9 @@ to build a CPython agnostic wheel and will influence the naming of the wheel acc
116127
117128
It is necessary to specify ``py_limited_api=True`` as an argument to CppExtension/
118129
CUDAExtension and also as an option to the ``"bdist_wheel"`` command with the minimal
119-
supported CPython version (in this case, 3.9, as it is the oldest supported version
120-
currently). Consequently, the ``setup`` in our tutorial would build one wheel that could
121-
be installed across multiple CPython versions ``>=3.9``.
130+
supported CPython version (in this case, 3.9). Consequently, the ``setup`` in our
131+
tutorial would build one properly named wheel that could be installed across multiple
132+
CPython versions ``>=3.9``.
122133

123134
If your extension uses CPython APIs outside the stable limited set, then you cannot
124135
build a CPython agnostic wheel! You should build one wheel per CPython version instead,

0 commit comments

Comments
 (0)