Skip to content

Commit 67feff8

Browse files
committed
Add DLPackArray and array_as_dlpack
1 parent 7adaaaf commit 67feff8

10 files changed

Lines changed: 392 additions & 23 deletions

File tree

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
Python API reference
2-
====================
1+
2+
.. DO NOT EDIT DIRECTLY, automatically generated by ./scripts/update-declarations.py.
3+
4+
DLPack types
5+
============
6+
7+
The following elements from the C header ``dlpack.h`` are available as
8+
``ctypes`` types for Python users.
39

410
Macros
511
------

docs/src/index.rst

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
ctypes-dlpack
2-
==============
2+
=============
33

4-
Helpers for DLPack interop based on ``ctypes``.
4+
The `DLPack`_ standard allows to share arrays/tensors data between different
5+
frameworks. It is defined as a C API, and the standard way to use it from Python
6+
is the ``__dlpack__`` protocol defined in the `array api <py-dlpack>`_, which
7+
relies on ``PyCapsule`` to pass the pointer around through Python code.
8+
9+
However, there might be times where one does not have a ``PyCapsule`` but just a
10+
raw pointer to the DLPack C struct. In particular, this might be the case when
11+
adding bindings to a C library through ``ctypes`` instead of going through a
12+
CPython extension module. This library is here to bridge this gap, creating
13+
ctypes pointers from the ``__dlpack__`` protocol, and making existing pointers
14+
compatible with this protocol.
15+
16+
This library exposes both the C structs/enums/constants from the DLPack C header
17+
as ``ctypes`` types (containing among other the version of the DLPack library
18+
that was used to generate the ctypes declarations), and a Python API to convert
19+
between DLPack pointers and Python objects.
20+
21+
You can install the code with ``pip install ctypes-dlpack``.
22+
23+
.. _DLPack: https://dmlc.github.io/dlpack/latest/
24+
.. _py-dlpack: https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html
525

626
.. toctree::
727
:maxdepth: 2
8-
:caption: Reference
928

10-
reference
29+
c-api
30+
python-api

docs/src/python-api.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Python API reference
2+
====================
3+
4+
5+
.. autoclass:: ctypes_dlpack::DLPackArray
6+
:members:
7+
8+
.. autofunction:: ctypes_dlpack::array_as_dlpack

scripts/update-declarations.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"https://raw.githubusercontent.com/dmlc/dlpack/{version}/include/dlpack/dlpack.h"
1919
)
2020
OUTPUT_PATH = ROOT / "src" / "ctypes_dlpack" / "_c_api.py"
21-
REFERENCE_PATH = ROOT / "docs" / "src" / "reference.rst"
21+
REFERENCE_PATH = ROOT / "docs" / "src" / "c-api.rst"
2222

2323

2424
GENERATOR = c_generator.CGenerator()
@@ -332,8 +332,16 @@ def generate_python_module(
332332
def generate_reference_rst(
333333
defines: dict[str, str], parsed: HeaderVisitor, file: TextIO
334334
) -> None:
335-
file.write("Python API reference\n")
336-
file.write("====================\n\n")
335+
file.write("""
336+
.. DO NOT EDIT DIRECTLY, automatically generated by ./scripts/update-declarations.py.
337+
338+
DLPack types
339+
============
340+
341+
The following elements from the C header ``dlpack.h`` are available as
342+
``ctypes`` types for Python users.
343+
344+
""")
337345

338346
file.write("Macros\n")
339347
file.write("------\n\n")

src/ctypes_dlpack/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . import _c_api
2+
from ._dlpack import DLPackArray, array_as_dlpack
23

34

45
__version__ = "0.1.0"
@@ -16,4 +17,11 @@
1617
value.__module__ = __name__
1718

1819

20+
__all__ = [
21+
*_public_members.keys(),
22+
"DLPackArray",
23+
"array_as_dlpack",
24+
]
25+
26+
1927
del _public_members

src/ctypes_dlpack/_c_api.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
DLPACK_FLAG_BITMASK_READ_ONLY = 1 << 0
77
DLPACK_MAJOR_VERSION = 1
88
DLPACK_MINOR_VERSION = 3
9-
10-
119
class _EnumType(type(ctypes.c_int32)):
1210
def __new__(metacls, name, bases, namespace):
1311
if "_members_" not in namespace:
@@ -42,7 +40,6 @@ def __eq__(self, other):
4240

4341
return type(self) is type(other) and self.value == other.value
4442

45-
4643
class DLDeviceType(_Enum):
4744
pass
4845
kDLCPU = 1

0 commit comments

Comments
 (0)