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

dpctl.tensor.put with mode="wrap" and negative indexes #1365

Open
antonwolfy opened this issue Aug 24, 2023 · 1 comment
Open

dpctl.tensor.put with mode="wrap" and negative indexes #1365

antonwolfy opened this issue Aug 24, 2023 · 1 comment

Comments

@antonwolfy
Copy link
Collaborator

antonwolfy commented Aug 24, 2023

The below example works differently with numpy:

a = dpt.zeros(7)
ind = dpt.asarray([-8])
dpt.put(a, ind, 2, mode="wrap")

a
# Out: usm_ndarray([2., 0., 0., 0., 0., 0., 0.], dtype=float32)

b = numpy.zeros(7)
nind = numpy.array([-8])
numpy.put(b, nind, 2, mode="wrap")

b
# Out: array([0., 0., 0., 0., 0., 0., 2.])

The description for mode in dpctl.tensor.put states:

mode – How out-of-bounds indices will be handled. “wrap” - clamps indices to (-n <= i < n), then wraps negative indices. “clip” - clips indices to (0 <= i < n) Default: “wrap”.

This is not the same like numpy handles wrap mode. In numpy it's like

if (ind < 0) {
    while (ind < 0) {
        ind += max_ind;
    }
}
else if (ind >= max_ind) {
    while (ind >= max_ind) {
        ind -= max_ind;
    }
}

The question here is the described misalignment with numpy is expected or was introduced by a mistake.

@ndgrigorian
Copy link
Collaborator

This was an intentional change. The original implementation aligned with Numpy's indices wrapping, but was found to perform poorly.
This style of wrapping is more closely aligned with advanced indexing, which is also why it's the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants