@@ -36,7 +36,12 @@ from .._backend cimport (
3636 DPCTLSyclDeviceRef,
3737 DPCTLSyclUSMRef,
3838)
39- from ._usmarray cimport USM_ARRAY_WRITABLE, usm_ndarray
39+ from ._usmarray cimport (
40+ USM_ARRAY_C_CONTIGUOUS,
41+ USM_ARRAY_F_CONTIGUOUS,
42+ USM_ARRAY_WRITABLE,
43+ usm_ndarray,
44+ )
4045
4146import ctypes
4247
@@ -266,6 +271,7 @@ cpdef to_dlpack_capsule(usm_ndarray usm_ary):
266271 cdef int64_t * shape_strides_ptr = NULL
267272 cdef int i = 0
268273 cdef int device_id = - 1
274+ cdef int flags = 0
269275 cdef Py_ssize_t element_offset = 0
270276 cdef Py_ssize_t byte_offset = 0
271277 cdef Py_ssize_t si = 1
@@ -291,14 +297,29 @@ cpdef to_dlpack_capsule(usm_ndarray usm_ary):
291297 for i in range (nd):
292298 shape_strides_ptr[i] = shape_ptr[i]
293299 strides_ptr = usm_ary.get_strides()
300+ flags = usm_ary.flags_
294301 if strides_ptr:
295302 for i in range (nd):
296303 shape_strides_ptr[nd + i] = strides_ptr[i]
297304 else :
298- si = 1
299- for i in range (0 , nd):
300- shape_strides_ptr[nd + i] = si
301- si = si * shape_ptr[i]
305+ if flags & USM_ARRAY_C_CONTIGUOUS:
306+ si = 1
307+ for i in range (nd - 1 , - 1 , - 1 ):
308+ shape_strides_ptr[nd + i] = si
309+ si = si * shape_ptr[i]
310+ elif flags & USM_ARRAY_F_CONTIGUOUS:
311+ si = 1
312+ for i in range (0 , nd):
313+ shape_strides_ptr[nd + i] = si
314+ si = si * shape_ptr[i]
315+ else :
316+ stdlib.free(shape_strides_ptr)
317+ stdlib.free(dlm_tensor)
318+ raise BufferError(
319+ " to_dlpack_capsule: Invalid array encountered "
320+ " when building strides"
321+ )
322+
302323 strides_ptr = < Py_ssize_t * > & shape_strides_ptr[nd]
303324
304325 ary_dt = usm_ary.dtype
@@ -409,10 +430,24 @@ cpdef to_dlpack_versioned_capsule(usm_ndarray usm_ary, bint copied):
409430 for i in range (nd):
410431 shape_strides_ptr[nd + i] = strides_ptr[i]
411432 else :
412- si = 1
413- for i in range (0 , nd):
414- shape_strides_ptr[nd + i] = si
415- si = si * shape_ptr[i]
433+ if flags & USM_ARRAY_C_CONTIGUOUS:
434+ si = 1
435+ for i in range (nd - 1 , - 1 , - 1 ):
436+ shape_strides_ptr[nd + i] = si
437+ si = si * shape_ptr[i]
438+ elif flags & USM_ARRAY_F_CONTIGUOUS:
439+ si = 1
440+ for i in range (0 , nd):
441+ shape_strides_ptr[nd + i] = si
442+ si = si * shape_ptr[i]
443+ else :
444+ stdlib.free(shape_strides_ptr)
445+ stdlib.free(dlmv_tensor)
446+ raise BufferError(
447+ " to_dlpack_versioned_capsule: Invalid array encountered "
448+ " when building strides"
449+ )
450+
416451 strides_ptr = < Py_ssize_t * > & shape_strides_ptr[nd]
417452
418453 # this can all be a function for building the dl_tensor
0 commit comments