Skip to content

Commit 0878f31

Browse files
Convert .pxi include files to .pxd/.pyx in dpnp/tensor (#2913)
This PR proposes to replace Cython textual includes with proper module imports in `dpnp/tensor` .pxi files are converted into corresponding .pxd and .pyx modules for _slicing.pxi, _stride_utils.pxi and _types.pxi` This change replaces the include `.pxi` pattern with standard Cython cimport statements and improving modularity
1 parent a8eca1d commit 0878f31

12 files changed

Lines changed: 318 additions & 131 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This release is compatible with NumPy 2.4.5.
1919
* Changed `dpnp.meshgrid` and `dpnp.tensor.meshgrid` to return a tuple instead of a list, aligning with NumPy 2.5+ behavior and 2025.12 version of the Python array API standard [#2854](https://github.com/IntelPython/dpnp/pull/2854)
2020
* Updated `searchsorted` implementations to align with the 2025.12 array API spec [gh-2902](https://github.com/IntelPython/dpnp/pull/2902)
2121
* Updated tests to align with NumPy 2.4.5 compatibility [gh-2920](https://github.com/IntelPython/dpnp/pull/2920)
22+
* Replaced `.pxi` includes in `dpnp.tensor` with modular `.pxd`/`.pyx` Cython imports [#2913](https://github.com/IntelPython/dpnp/pull/2913)
2223

2324
### Deprecated
2425

dpnp/backend/include/dpnp4pybind11.hpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
#include "dpctl4pybind11.hpp"
3333

3434
// Include generated Cython headers for usm_ndarray
35-
// (struct definition and constants only)
3635
#include "dpnp/tensor/_usmarray.h"
3736
#include "dpnp/tensor/_usmarray_api.h"
37+
// Include usm_ndarray constants (flags, type numbers)
38+
#include "../../tensor/include/usm_ndarray_constants.h"
3839

3940
#include <array>
4041
#include <cassert>
@@ -191,47 +192,47 @@ class dpnp_capi
191192
this->UsmNDArray_MakeSimpleFromPtr_ = UsmNDArray_MakeSimpleFromPtr;
192193
this->UsmNDArray_MakeFromPtr_ = UsmNDArray_MakeFromPtr;
193194

194-
// constants
195-
this->USM_ARRAY_C_CONTIGUOUS_ = USM_ARRAY_C_CONTIGUOUS;
196-
this->USM_ARRAY_F_CONTIGUOUS_ = USM_ARRAY_F_CONTIGUOUS;
197-
this->USM_ARRAY_WRITABLE_ = USM_ARRAY_WRITABLE;
198-
this->UAR_BOOL_ = UAR_BOOL;
199-
this->UAR_BYTE_ = UAR_BYTE;
200-
this->UAR_UBYTE_ = UAR_UBYTE;
201-
this->UAR_SHORT_ = UAR_SHORT;
202-
this->UAR_USHORT_ = UAR_USHORT;
203-
this->UAR_INT_ = UAR_INT;
204-
this->UAR_UINT_ = UAR_UINT;
205-
this->UAR_LONG_ = UAR_LONG;
206-
this->UAR_ULONG_ = UAR_ULONG;
207-
this->UAR_LONGLONG_ = UAR_LONGLONG;
208-
this->UAR_ULONGLONG_ = UAR_ULONGLONG;
209-
this->UAR_FLOAT_ = UAR_FLOAT;
210-
this->UAR_DOUBLE_ = UAR_DOUBLE;
211-
this->UAR_CFLOAT_ = UAR_CFLOAT;
212-
this->UAR_CDOUBLE_ = UAR_CDOUBLE;
213-
this->UAR_TYPE_SENTINEL_ = UAR_TYPE_SENTINEL;
214-
this->UAR_HALF_ = UAR_HALF;
195+
// constants from usm_ndarray_constants.h
196+
this->USM_ARRAY_C_CONTIGUOUS_ = USM_ARRAY_C_CONTIGUOUS_VALUE;
197+
this->USM_ARRAY_F_CONTIGUOUS_ = USM_ARRAY_F_CONTIGUOUS_VALUE;
198+
this->USM_ARRAY_WRITABLE_ = USM_ARRAY_WRITABLE_VALUE;
199+
this->UAR_BOOL_ = UAR_BOOL_VALUE;
200+
this->UAR_BYTE_ = UAR_BYTE_VALUE;
201+
this->UAR_UBYTE_ = UAR_UBYTE_VALUE;
202+
this->UAR_SHORT_ = UAR_SHORT_VALUE;
203+
this->UAR_USHORT_ = UAR_USHORT_VALUE;
204+
this->UAR_INT_ = UAR_INT_VALUE;
205+
this->UAR_UINT_ = UAR_UINT_VALUE;
206+
this->UAR_LONG_ = UAR_LONG_VALUE;
207+
this->UAR_ULONG_ = UAR_ULONG_VALUE;
208+
this->UAR_LONGLONG_ = UAR_LONGLONG_VALUE;
209+
this->UAR_ULONGLONG_ = UAR_ULONGLONG_VALUE;
210+
this->UAR_FLOAT_ = UAR_FLOAT_VALUE;
211+
this->UAR_DOUBLE_ = UAR_DOUBLE_VALUE;
212+
this->UAR_CFLOAT_ = UAR_CFLOAT_VALUE;
213+
this->UAR_CDOUBLE_ = UAR_CDOUBLE_VALUE;
214+
this->UAR_TYPE_SENTINEL_ = UAR_TYPE_SENTINEL_VALUE;
215+
this->UAR_HALF_ = UAR_HALF_VALUE;
215216

216217
// deduced disjoint types
217-
this->UAR_INT8_ = UAR_BYTE;
218-
this->UAR_UINT8_ = UAR_UBYTE;
219-
this->UAR_INT16_ = UAR_SHORT;
220-
this->UAR_UINT16_ = UAR_USHORT;
218+
this->UAR_INT8_ = UAR_BYTE_VALUE;
219+
this->UAR_UINT8_ = UAR_UBYTE_VALUE;
220+
this->UAR_INT16_ = UAR_SHORT_VALUE;
221+
this->UAR_UINT16_ = UAR_USHORT_VALUE;
221222
this->UAR_INT32_ =
222223
platform_typeid_lookup<std::int32_t, long, int, short>(
223-
UAR_LONG, UAR_INT, UAR_SHORT);
224+
UAR_LONG_VALUE, UAR_INT_VALUE, UAR_SHORT_VALUE);
224225
this->UAR_UINT32_ =
225226
platform_typeid_lookup<std::uint32_t, unsigned long, unsigned int,
226-
unsigned short>(UAR_ULONG, UAR_UINT,
227-
UAR_USHORT);
227+
unsigned short>(
228+
UAR_ULONG_VALUE, UAR_UINT_VALUE, UAR_USHORT_VALUE);
228229
this->UAR_INT64_ =
229230
platform_typeid_lookup<std::int64_t, long, long long, int>(
230-
UAR_LONG, UAR_LONGLONG, UAR_INT);
231+
UAR_LONG_VALUE, UAR_LONGLONG_VALUE, UAR_INT_VALUE);
231232
this->UAR_UINT64_ =
232233
platform_typeid_lookup<std::uint64_t, unsigned long,
233234
unsigned long long, unsigned int>(
234-
UAR_ULONG, UAR_ULONGLONG, UAR_UINT);
235+
UAR_ULONG_VALUE, UAR_ULONGLONG_VALUE, UAR_UINT_VALUE);
235236

236237
py::object py_default_usm_memory =
237238
::dpctl::detail::dpctl_capi::get().default_usm_memory_pyobj();

dpnp/tensor/_slicing.pxd

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2026, Intel Corporation
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# - Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
# - Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
# - Neither the name of the copyright holder nor the names of its contributors
13+
# may be used to endorse or promote products derived from this software
14+
# without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26+
# THE POSSIBILITY OF SUCH DAMAGE.
27+
# *****************************************************************************
28+
29+
# distutils: language = c++
30+
# cython: language_level=3
31+
32+
cdef bint _is_buffer(object o)
33+
34+
cdef Py_ssize_t _slice_len(
35+
Py_ssize_t sl_start,
36+
Py_ssize_t sl_stop,
37+
Py_ssize_t sl_step
38+
)
39+
40+
cdef bint _is_integral(object x) except *
41+
42+
cdef bint _is_boolean(object x) except *
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
# THE POSSIBILITY OF SUCH DAMAGE.
2727
# *****************************************************************************
2828

29-
import numbers
29+
# distutils: language = c++
30+
# cython: language_level=3
31+
3032
from operator import index
3133
from cpython.buffer cimport PyObject_CheckBuffer
3234
from numpy import ndarray
3335

36+
from ._usmarray cimport usm_ndarray
37+
3438

3539
cdef bint _is_buffer(object o):
3640
return PyObject_CheckBuffer(o)

dpnp/tensor/_stride_utils.pxd

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2026, Intel Corporation
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# - Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
# - Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
# - Neither the name of the copyright holder nor the names of its contributors
13+
# may be used to endorse or promote products derived from this software
14+
# without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26+
# THE POSSIBILITY OF SUCH DAMAGE.
27+
# *****************************************************************************
28+
29+
# distutils: language = c++
30+
# cython: language_level=3
31+
32+
cdef int ERROR_MALLOC
33+
cdef int ERROR_INTERNAL
34+
cdef int ERROR_INCORRECT_ORDER
35+
cdef int ERROR_UNEXPECTED_STRIDES
36+
37+
cdef Py_ssize_t shape_to_elem_count(int nd, Py_ssize_t *shape_arr)
38+
39+
cdef int _from_input_shape_strides(
40+
int nd, object shape, object strides, int itemsize, char order,
41+
Py_ssize_t **shape_ptr, Py_ssize_t **strides_ptr,
42+
Py_ssize_t *nelems, Py_ssize_t *min_disp, Py_ssize_t *max_disp,
43+
int *contig
44+
)
45+
46+
cdef object _make_int_tuple(int nd, const Py_ssize_t *ary)
47+
48+
cdef object _make_reversed_int_tuple(int nd, const Py_ssize_t *ary)
49+
50+
cdef object _c_contig_strides(int nd, Py_ssize_t *shape)
51+
52+
cdef object _f_contig_strides(int nd, Py_ssize_t *shape)
53+
54+
cdef object _swap_last_two(tuple t)
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@
2929
# distutils: language = c++
3030
# cython: language_level=3
3131

32-
from cpython.mem cimport PyMem_Malloc
32+
from cpython.mem cimport PyMem_Free, PyMem_Malloc
3333
from cpython.ref cimport Py_INCREF
3434
from cpython.tuple cimport PyTuple_New, PyTuple_SetItem
3535

3636

37+
cdef extern from "usm_ndarray_constants.h":
38+
int USM_ARRAY_C_CONTIGUOUS_VALUE
39+
int USM_ARRAY_F_CONTIGUOUS_VALUE
40+
41+
cdef int USM_ARRAY_C_CONTIGUOUS = USM_ARRAY_C_CONTIGUOUS_VALUE
42+
cdef int USM_ARRAY_F_CONTIGUOUS = USM_ARRAY_F_CONTIGUOUS_VALUE
43+
3744
cdef int ERROR_MALLOC = 1
3845
cdef int ERROR_INTERNAL = -1
3946
cdef int ERROR_INCORRECT_ORDER = 2
4047
cdef int ERROR_UNEXPECTED_STRIDES = 3
4148

42-
cdef int USM_ARRAY_C_CONTIGUOUS = 1
43-
cdef int USM_ARRAY_F_CONTIGUOUS = 2
44-
cdef int USM_ARRAY_WRITABLE = 4
45-
4649

4750
cdef Py_ssize_t shape_to_elem_count(int nd, Py_ssize_t *shape_arr):
4851
"""

dpnp/tensor/_types.pxd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2026, Intel Corporation
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# - Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
# - Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
# - Neither the name of the copyright holder nor the names of its contributors
13+
# may be used to endorse or promote products derived from this software
14+
# without specific prior written permission.
15+
#
16+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26+
# THE POSSIBILITY OF SUCH DAMAGE.
27+
# *****************************************************************************
28+
29+
# distutils: language = c++
30+
# cython: language_level=3
31+
32+
33+
cdef int type_bytesize(int typenum)
34+
35+
cdef str _make_typestr(int typenum)
36+
37+
cdef int typenum_from_format(str s)
38+
39+
cdef int descr_to_typenum(object dtype)
40+
41+
cdef int dtype_to_typenum(dtype)
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,11 @@
2626
# THE POSSIBILITY OF SUCH DAMAGE.
2727
# *****************************************************************************
2828

29-
# these typenum values are aligned to values in NumPy
30-
cdef:
31-
int UAR_BOOL = 0 # pragma: no cover
32-
int UAR_BYTE = 1 # pragma: no cover
33-
int UAR_UBYTE = 2 # pragma: no cover
34-
int UAR_SHORT = 3 # pragma: no cover
35-
int UAR_USHORT = 4 # pragma: no cover
36-
int UAR_INT = 5 # pragma: no cover
37-
int UAR_UINT = 6 # pragma: no cover
38-
int UAR_LONG = 7 # pragma: no cover
39-
int UAR_ULONG = 8 # pragma: no cover
40-
int UAR_LONGLONG = 9 # pragma: no cover
41-
int UAR_ULONGLONG = 10 # pragma: no cover
42-
int UAR_FLOAT = 11 # pragma: no cover
43-
int UAR_DOUBLE = 12 # pragma: no cover
44-
int UAR_CFLOAT = 14 # pragma: no cover
45-
int UAR_CDOUBLE = 15 # pragma: no cover
46-
int UAR_TYPE_SENTINEL = 17 # pragma: no cover
47-
int UAR_HALF = 23 # pragma: no cover
29+
# distutils: language = c++
30+
# cython: language_level=3
31+
32+
import numpy as np
33+
4834

4935
cdef int type_bytesize(int typenum):
5036
"""

dpnp/tensor/_usmarray.pxd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@
3232
cimport dpctl
3333

3434

35+
cdef extern from "usm_ndarray_constants.h":
36+
int USM_ARRAY_C_CONTIGUOUS_VALUE
37+
int USM_ARRAY_F_CONTIGUOUS_VALUE
38+
int USM_ARRAY_WRITABLE_VALUE
39+
40+
int UAR_BOOL_VALUE
41+
int UAR_BYTE_VALUE
42+
int UAR_UBYTE_VALUE
43+
int UAR_SHORT_VALUE
44+
int UAR_USHORT_VALUE
45+
int UAR_INT_VALUE
46+
int UAR_UINT_VALUE
47+
int UAR_LONG_VALUE
48+
int UAR_ULONG_VALUE
49+
int UAR_LONGLONG_VALUE
50+
int UAR_ULONGLONG_VALUE
51+
int UAR_FLOAT_VALUE
52+
int UAR_DOUBLE_VALUE
53+
int UAR_CFLOAT_VALUE
54+
int UAR_CDOUBLE_VALUE
55+
int UAR_TYPE_SENTINEL_VALUE
56+
int UAR_HALF_VALUE
57+
3558
cdef public api int USM_ARRAY_C_CONTIGUOUS
3659
cdef public api int USM_ARRAY_F_CONTIGUOUS
3760
cdef public api int USM_ARRAY_WRITABLE

0 commit comments

Comments
 (0)