dpctl icon indicating copy to clipboard operation
dpctl copied to clipboard

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

Open antonwolfy opened this issue 1 year ago • 1 comments

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.

antonwolfy avatar Aug 24 '23 10:08 antonwolfy

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.

ndgrigorian avatar Aug 25 '23 00:08 ndgrigorian