Skip to content

Bug in asnumpy copy_utils #2138

@abagusetty

Description

@abagusetty

It looks like the order-C is being ignored and the C-contiguous checks fails when using dpnp.asnumpy(). Please refer to the below reproducer and output with dpnp+dpctl & cupy, more specifically 3c section of the output.

Upon a quick peek, much of the asnumpy is being forwarded to dpt.__copy_utils__ and so opened an issue here. Apologies if this is not the right repo and should move it to DPNP.

Reproducer:

import numpy as np
import dpnp as dp

print("dpnp version:", getattr(dp, "__version__", "unknown"))

# Build the same data as in the larger code
ctr_offsets_slice = [
    dp.array([0, 0, 0], dtype=np.int32),
    dp.array([3, 3, 3], dtype=np.int32),
    dp.array([5, 5, 5], dtype=np.int32),
    dp.array([7, 7, 7], dtype=np.int32),
    dp.array([10, 10, 10], dtype=np.int32),
    dp.array([11, 11, 11], dtype=np.int32),
    dp.array([12, 12, 12], dtype=np.int32),
]

# 3a: stack -> dpnp_array (should be C-contiguous)
temp1 = dp.stack(ctr_offsets_slice)

# 3b: transpose -> dpnp_array view (not C-contiguous)
temp2 = dp.stack(ctr_offsets_slice).T

# 3c: device->host with "order='C'"
# EXPECTED (per docstring "works exactly like numpy.asarray"): C-contiguous NumPy array
# ACTUAL: order is ignored for dpnp_array, result keeps non-C layout
temp3 = dp.asnumpy(temp2, order='C')

print("3a. Testing : temp1: ", bool(temp1.flags['C_CONTIGUOUS']), type(temp1), len(temp1), temp1)
print("3b. Testing : temp2: ", bool(temp2.flags['C_CONTIGUOUS']), type(temp2), len(temp2), temp2)
print("3c. Testing : temp3: ", bool(temp3.flags['C_CONTIGUOUS']), type(temp3), len(temp3), temp3)

# Programmatic check to make the failure obvious:
if not temp3.flags['C_CONTIGUOUS']:
    print("\nBUG: dpnp.asnumpy(dpnp_array, order='C') returned a non-C-contiguous NumPy array.")
    print("     strides:", temp3.strides, "| shape:", temp3.shape)

Output from DPNP+DPCTL:

dpnp version: 0.19.0dev3+8.gac795ab9.dirty
3a. Testing : temp1:  True <class 'dpnp.dpnp_array.dpnp_array'> 7 [[ 0  0  0]
 [ 3  3  3]
 [ 5  5  5]
 [ 7  7  7]
 [10 10 10]
 [11 11 11]
 [12 12 12]]
3b. Testing : temp2:  False <class 'dpnp.dpnp_array.dpnp_array'> 3 [[ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]]
3c. Testing : temp3:  False <class 'numpy.ndarray'> 3 [[ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]]

BUG: dpnp.asnumpy(dpnp_array, order='C') returned a non-C-contiguous NumPy array.
     strides: (4, 12) | shape: (3, 7)

Output from CUPY

cupy version: 13.4.1
3a. Testing : temp1:  True <class 'cupy.ndarray'> 7 [[ 0  0  0]
 [ 3  3  3]
 [ 5  5  5]
 [ 7  7  7]
 [10 10 10]
 [11 11 11]
 [12 12 12]]
3b. Testing : temp2:  False <class 'cupy.ndarray'> 3 [[ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]]
3c. Testing : temp3:  True <class 'numpy.ndarray'> 3 [[ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]
 [ 0  3  5  7 10 11 12]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions