Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numba_dpex's kernel disregards strides of input array #572

Closed
oleksandr-pavlyk opened this issue Sep 27, 2021 · 8 comments · Fixed by #1285
Closed

numba_dpex's kernel disregards strides of input array #572

oleksandr-pavlyk opened this issue Sep 27, 2021 · 8 comments · Fixed by #1285
Assignees
Labels
dpnp Integration with dpnp kernel API About @numba_dpex.kernel decorator strides Issues related to arrays with strides
Milestone

Comments

@oleksandr-pavlyk
Copy link
Contributor

oleksandr-pavlyk commented Sep 27, 2021

import dpctl
import numba_dpex as dppy
import numpy as np
import dpnp

import dpctl.tensor as dpt


@dppy.kernel
def data_parallel_sum(a, b, c):
    """
    Vector addition using the ``kernel`` decorator.
    """
    i = dppy.get_global_id(0)
    c[i] = a[i] + b[i]


def driver(a, b, c, global_size):
    print("A : ", (a.shape, a.strides))
    print("B : ", (b.shape, b.strides))
    data_parallel_sum[dppy.Range(global_size)](a, b, c)
    print("C ", (c.shape, c.strides))
    # assert np.allclose(dpt.asnumpy(c), dpt.asnumpy(a) + dpt.asnumpy(b))


global_size = 10
N = global_size
print("N", N)

a = dpnp.random.random(N)  # C-contig
b = dpnp.random.random(N)  # C-contig
out = dpnp.zeros((2 * N,), dtype=a.dtype)  # C-contig
c = out[::2]  # Strided, every other element

driver(a, b, c, global_size)

print(c)
print(out)

Executing this script outputs

N 10
Using device ...
    Name            Intel(R) UHD Graphics [0x9bca]
    Driver version  1.1.20678
    Vendor          Intel(R) Corporation
    Profile         FULL_PROFILE
    Filter string   level_zero:gpu:0
A :  ((10,), (1,))
B :  ((10,), (1,))
C  ((10,), (2,))
Done...
[0.83166494 1.27176889 0.61416447 0.57984313 1.04820722 0.
 0.         0.         0.         0.        ]
[0.83166494 0.50883122 1.27176889 1.08111028 0.61416447 0.77052362
 0.57984313 1.55592601 1.04820722 1.12209132 0.         0.
 0.         0.         0.         0.         0.         0.
 0.         0.        ]

The expected result should have been

[0.83166494 0.50883122 1.27176889 1.08111028 0.61416447 0.77052362
 0.57984313 1.55592601 1.04820722 1.12209132]
[0.83166494 0.       0.50883122  0.       1.27176889  0.        1.08111028  0.        
0.61416447 0.        0.77052362  0.       0.57984313  0.        1.55592601  0.    
1.04820722  0.       1.12209132    0.  ]

UPDATE : Updated the reproducer to work with latest numba-dpex.

@PokhodenkoSA
Copy link
Contributor

This is a limitation of how numba-dppy supports dpnp features. I will qualify it as an enhancement for future work.

@PokhodenkoSA
Copy link
Contributor

I think for preventing this situation numba-dppy should raise exception if receives array w/ strides.

@reazulhoque reazulhoque removed their assignment Jan 11, 2022
@diptorupd diptorupd self-assigned this Oct 7, 2022
@diptorupd diptorupd changed the title numba_dppy's kernel disregards strides of input array numba_dpex's kernel disregards strides of input array Mar 11, 2023
@chudur-budur chudur-budur self-assigned this Dec 19, 2023
@chudur-budur
Copy link
Collaborator

chudur-budur commented Dec 30, 2023

A better minimal reproducer:

@dpex.kernel
def change_values(x):
    i = dpex.get_global_id(0)
    x[i] = -3

N = 10

out = dpnp.arange(0, 20, dtype=dpnp.int64)
b = out[::2]

print(f"out = {out}, out.shape = {out.shape}, out.strides = {out.strides}")
print(f"b = {b}, b.shape = {b.shape}, b.strides = {b.strides}")

change_values[dpex.Range(N)](b)

print(f"out = {out}, out.shape = {out.shape}, out.strides = {out.strides}")
print(f"b = {b}, b.shape = {b.shape}, b.strides = {b.strides}")

output:

out = [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19], out.shape = (20,), out.strides = (1,)
b = [ 0  2  4  6  8 10 12 14 16 18], b.shape = (10,), b.strides = (2,)
out = [          -3           -3 -12884901886  -8589934593   4294967295
           -3           -3 -12884901881  -8589934593   4294967295
           -3           -3           12           13           14
           15           16           17           18           19], out.shape = (20,), out.strides = (1,)
b = [          -3 -12884901886   4294967295           -3  -8589934593
           -3           12           14           16           18], b.shape = (10,), b.strides = (2,)

Expected output:

out = [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19], out.shape = (20,), out.strides = (1,)
b = [ 0  2  4  6  8 10 12 14 16 18], b.shape = (10,), b.strides = (2,)
out = [ -3 1 -3 3 -3 5 -3 7 -3 9 -3 11 -3 13 -3 15 -3 17 -3 19], out.shape = (20,), out.strides = (1,)
b = [-3 -3 -3 -3 -3 -3 -3 -3 -3 -3], b.shape = (10,), b.strides = (2,)

@chudur-budur
Copy link
Collaborator

This is related to

IntelPython/dpnp#1649

@chudur-budur
Copy link
Collaborator

Resolved by #1271

@chudur-budur
Copy link
Collaborator

chudur-budur commented Jan 14, 2024

#1271 works only in 1-dim case.

If the array dimension is more than 1, the indexing doesn't skip the right amount of rows across the axis-0.

Reopening.

@chudur-budur
Copy link
Collaborator

Resolved by #1285

@diptorupd
Copy link
Collaborator

Please close issues when the associated PR is merged. Reopening the ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dpnp Integration with dpnp kernel API About @numba_dpex.kernel decorator strides Issues related to arrays with strides
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants