@@ -78,10 +78,16 @@ If you need to compile CUDA code (for example, ``.cu`` files), then instead use
78
78
Please see `extension-cpp <https://github.com/pytorch/extension-cpp >`_ for an
79
79
example for how this is set up.
80
80
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.
85
91
86
92
The first is the specification of ``Py_LIMITED_API `` in ``extra_compile_args `` to the
87
93
minimum CPython version you would like to support:
@@ -90,15 +96,20 @@ minimum CPython version you would like to support:
90
96
91
97
extra_compile_args= {" cxx" : [" -DPy_LIMITED_API=0x03090000" ]},
92
98
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
94
100
only using the `CPython Stable Limited API <https://docs.python.org/3/c-api/stable.html >`_,
95
101
which is a requirement for the building a CPython agnostic wheel. If this requirement
96
102
is not met, it is possible to build a wheel that looks CPython agnostic but will crash,
97
103
or worse, be silently incorrect, in another CPython environment. Take care to avoid
98
104
using unstable CPython APIs, for example APIs from libtorch_python (in particular
99
105
pytorch/python bindings,) and to only use APIs from libtorch (ATen objects, operators
100
106
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.
102
113
103
114
The second and third lines specifying ``py_limited_api `` inform setuptools that you intend
104
115
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
116
127
117
128
It is necessary to specify ``py_limited_api=True `` as an argument to CppExtension/
118
129
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 ``.
122
133
123
134
If your extension uses CPython APIs outside the stable limited set, then you cannot
124
135
build a CPython agnostic wheel! You should build one wheel per CPython version instead,
0 commit comments