Skip to content

Commit 0d294e6

Browse files
committed
Documentation, linting, minor fixes
Signed-off-by: Kamil Tokarski <ktokarski@nvidia.com>
1 parent df6c909 commit 0d294e6

File tree

10 files changed

+704
-189
lines changed

10 files changed

+704
-189
lines changed

cuda_core/cuda/core/experimental/_dlpack.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ cdef inline int _setup_dl_tensor_dtype(DLTensor* dl_tensor, object dtype) except
126126

127127
cpdef object make_py_capsule(
128128
object buf,
129-
intptr_t data_ptr,
130129
bint versioned,
130+
intptr_t data_ptr,
131131
StridedLayout layout=None,
132132
object dtype=None,
133133
):

cuda_core/cuda/core/experimental/_layout.pxd

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
cimport cython
66
from cython.operator cimport dereference as deref
77

8-
from libc.stdint cimport int64_t, uint32_t, intptr_t
8+
from libc.stdint cimport int64_t, uint32_t, uintptr_t
99
from libcpp cimport vector
1010

1111
ctypedef int64_t extent_t
@@ -49,14 +49,15 @@ cdef enum Property:
4949
PROP_IS_UNIQUE = 1 << 0
5050
PROP_IS_CONTIGUOUS_C = 1 << 1
5151
PROP_IS_CONTIGUOUS_F = 1 << 2
52-
PROP_IS_DENSE = 1 << 3
53-
PROP_OFFSET_BOUNDS = 1 << 4
54-
PROP_REQUIRED_SIZE_IN_BYTES = 1 << 5
55-
PROP_SHAPE = 1 << 6
56-
PROP_STRIDES = 1 << 7
57-
PROP_STRIDES_IN_BYTES = 1 << 8
58-
PROP_STRIDE_ORDER = 1 << 9
59-
PROP_VOLUME = 1 << 10
52+
PROP_IS_CONTIGUOUS_ANY = 1 << 3
53+
PROP_IS_DENSE = 1 << 4
54+
PROP_OFFSET_BOUNDS = 1 << 5
55+
PROP_REQUIRED_SIZE_IN_BYTES = 1 << 6
56+
PROP_SHAPE = 1 << 7
57+
PROP_STRIDES = 1 << 8
58+
PROP_STRIDES_IN_BYTES = 1 << 9
59+
PROP_STRIDE_ORDER = 1 << 10
60+
PROP_VOLUME = 1 << 11
6061

6162

6263
cdef struct BaseLayout:
@@ -109,14 +110,15 @@ cdef class StridedLayout:
109110
# Initialization
110111
# ==============================
111112

112-
cdef inline int _init(StridedLayout self, BaseLayout& base, int itemsize, bint strides_in_bytes=False) except -1 nogil:
113+
cdef inline int _init(StridedLayout self, BaseLayout& base, int itemsize, bint divide_strides=False) except -1 nogil:
113114
_validate_itemsize(itemsize)
114115

115-
if base.strides != NULL and strides_in_bytes:
116+
if base.strides != NULL and divide_strides:
116117
_divide_strides(base, itemsize)
117118

118119
self.itemsize = itemsize
119120
self.slice_offset = 0
121+
120122
_swap_layout(self.base, base)
121123
return 0
122124

@@ -142,20 +144,20 @@ cdef class StridedLayout:
142144
_mark_property_valid(self, PROP_VOLUME)
143145
return 0
144146

145-
cdef inline int init_from_ptr(StridedLayout self, int ndim, extent_t* shape, stride_t* strides, int itemsize, bint strides_in_bytes=False) except -1 nogil:
147+
cdef inline int init_from_ptr(StridedLayout self, int ndim, extent_t* shape, stride_t* strides, int itemsize, bint divide_strides=False) except -1 nogil:
146148
cdef BaseLayout base
147149
_init_base_layout_from_ptr(base, ndim, shape, strides)
148-
return self._init(base, itemsize, strides_in_bytes)
150+
return self._init(base, itemsize, divide_strides)
149151

150152
cdef inline int init_dense_from_ptr(StridedLayout self, int ndim, extent_t* shape, int itemsize, OrderFlag order_flag, axis_vec_t* stride_order=NULL) except -1 nogil:
151153
cdef BaseLayout base
152154
_init_base_layout_from_ptr(base, ndim, shape, NULL)
153155
return self._init_dense(base, itemsize, order_flag, stride_order)
154156

155-
cdef inline int init_from_tuple(StridedLayout self, tuple shape, tuple strides, int itemsize, bint strides_in_bytes=False) except -1:
157+
cdef inline int init_from_tuple(StridedLayout self, tuple shape, tuple strides, int itemsize, bint divide_strides=False) except -1:
156158
cdef BaseLayout base
157159
_init_base_layout_from_tuple(base, shape, strides)
158-
return self._init(base, itemsize, strides_in_bytes)
160+
return self._init(base, itemsize, divide_strides)
159161

160162
cdef inline int init_dense_from_tuple(StridedLayout self, tuple shape, int itemsize, object stride_order) except -1:
161163
cdef axis_vec_t stride_order_vec
@@ -237,28 +239,24 @@ cdef class StridedLayout:
237239
cdef inline bint get_is_contiguous_c(StridedLayout self) except -1 nogil:
238240
if _has_valid_property(self, PROP_IS_CONTIGUOUS_C):
239241
return _boolean_property(self, PROP_IS_CONTIGUOUS_C)
240-
cdef bint is_contiguous_c = (
241-
self.slice_offset == 0 and _is_contiguous_c(self.get_volume(), self.base)
242-
)
243-
return _set_boolean_property(self, PROP_IS_CONTIGUOUS_C, is_contiguous_c)
242+
return _set_boolean_property(self, PROP_IS_CONTIGUOUS_C, _is_contiguous_c(self.get_volume(), self.base))
244243

245244
cdef inline bint get_is_contiguous_f(StridedLayout self) except -1 nogil:
246245
if _has_valid_property(self, PROP_IS_CONTIGUOUS_F):
247246
return _boolean_property(self, PROP_IS_CONTIGUOUS_F)
248-
cdef bint is_contiguous_f = (
249-
self.slice_offset == 0 and _is_contiguous_f(self.get_volume(), self.base)
250-
)
251-
return _set_boolean_property(self, PROP_IS_CONTIGUOUS_F, is_contiguous_f)
247+
return _set_boolean_property(self, PROP_IS_CONTIGUOUS_F, _is_contiguous_f(self.get_volume(), self.base))
248+
249+
cdef inline bint get_is_contiguous_any(StridedLayout self) except -1 nogil:
250+
if _has_valid_property(self, PROP_IS_CONTIGUOUS_ANY):
251+
return _boolean_property(self, PROP_IS_CONTIGUOUS_ANY)
252+
cdef axis_vec_t stride_order
253+
self.get_stride_order(stride_order)
254+
return _set_boolean_property(self, PROP_IS_CONTIGUOUS_ANY, _is_contiguous_any(self.get_volume(), self.base, stride_order))
252255

253256
cdef inline bint get_is_dense(StridedLayout self) except -1 nogil:
254257
if _has_valid_property(self, PROP_IS_DENSE):
255258
return _boolean_property(self, PROP_IS_DENSE)
256-
cdef axis_vec_t stride_order
257-
self.get_stride_order(stride_order)
258-
cdef bint is_dense = (
259-
self.slice_offset == 0 and _is_dense(self.get_volume(), self.base, stride_order)
260-
)
261-
return _set_boolean_property(self, PROP_IS_DENSE, is_dense)
259+
return _set_boolean_property(self, PROP_IS_DENSE, self.slice_offset == 0 and self.get_is_contiguous_any())
262260

263261
cdef inline int get_offset_bounds(StridedLayout self, stride_t& min_offset, stride_t& max_offset) except -1 nogil:
264262
if _has_valid_property(self, PROP_OFFSET_BOUNDS):
@@ -314,7 +312,7 @@ cdef class StridedLayout:
314312
return _overflow_checked_mul(self.slice_offset, self.itemsize)
315313

316314
cdef axes_mask_t get_flattened_axis_mask(StridedLayout self) except? -1 nogil
317-
cdef int get_max_compatible_itemsize(StridedLayout self, int max_itemsize, intptr_t data_ptr, int axis=*) except -1 nogil
315+
cdef int get_max_compatible_itemsize(StridedLayout self, int max_itemsize, uintptr_t data_ptr, int axis=*) except -1 nogil
318316

319317
# ==============================
320318
# Layout manipulation
@@ -328,7 +326,7 @@ cdef class StridedLayout:
328326
cdef int squeeze_into(StridedLayout self, StridedLayout out_layout) except -1 nogil
329327
cdef int unsqueeze_into(StridedLayout self, StridedLayout out_layout, axis_vec_t& axis_vec) except -1 nogil
330328
cdef int broadcast_into(StridedLayout self, StridedLayout out_layout, BaseLayout& broadcast) except -1 nogil
331-
cdef int pack_into(StridedLayout self, StridedLayout out_layout, int itemsize, intptr_t data_ptr, bint keep_dim, int axis=*) except -1 nogil
329+
cdef int pack_into(StridedLayout self, StridedLayout out_layout, int itemsize, uintptr_t data_ptr, bint keep_dim, int axis=*) except -1 nogil
332330
cdef int unpack_into(StridedLayout self, StridedLayout out_layout, int itemsize, int axis=*) except -1 nogil
333331
cdef int slice_into(StridedLayout self, StridedLayout out_layout, tuple slices) except -1
334332

@@ -522,7 +520,7 @@ cdef inline bint _is_contiguous_f(int64_t volume, BaseLayout& base) except -1 no
522520
return True
523521

524522

525-
cdef inline bint _is_dense(int64_t volume, BaseLayout& base, axis_vec_t& axis_order) except -1 nogil:
523+
cdef inline bint _is_contiguous_any(int64_t volume, BaseLayout& base, axis_vec_t& axis_order) except -1 nogil:
526524
if volume == 0 or base.strides == NULL:
527525
return True
528526
cdef int64_t stride = 1

0 commit comments

Comments
 (0)