Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi
* Fixed `dpnp.place` and `dpnp.tensor.place` to order the write after the cast of `vals` to the array's data type, which could put wrong values into the array on an out-of-order queue [#3164](https://github.com/IntelPython/dpnp/pull/3164)
* Fixed the example of handling duplicate indices in the `dpnp.tensor.put` docstring, which imported `dpctl.tensor`, did not parse and lost writes when `x` is longer than `indices` [#3165](https://github.com/IntelPython/dpnp/pull/3165)
* Fixed a segfault when setting `writable` on a `Flags` object created without an array [#3168](https://github.com/IntelPython/dpnp/pull/3168)
* Fixed a crash of the OpenCL CPU compiler in `dpnp.sort`, `dpnp.argsort`, `dpnp.tensor.sort` and `dpnp.tensor.argsort` by radix sort of rows of 2049 to 16384 elements [#3191](https://github.com/IntelPython/dpnp/pull/3191)

### Security

Expand Down
16 changes: 10 additions & 6 deletions dpnp/libtensor/include/kernels/sorting/radix_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,10 @@ struct subgroup_radix_sort
const std::uint32_t krn_sg_size = krn.template get_info<
sycl::info::kernel_device_specific::max_sub_group_size>(dev);

// due to a bug in CPU device implementation, an additional
// synchronization is necessary for short sub-group sizes
// the CPU device compiler miscompiles the counting for short
// sub-groups (CMPLRLLVM-64592), which a fence after each update
// avoids in this kernel; a barrier there crashes the compiler
// (CMPLRLLVM-79305)
const bool work_around_needed =
exec_q.get_device().has(sycl::aspect::cpu) &&
(krn_sg_size < 16);
Expand Down Expand Up @@ -1198,8 +1200,9 @@ struct subgroup_radix_sort
*counters[i] = indices[i] + 1;

if (work_around_needed) {
sycl::group_barrier(
ndit.get_group());
sycl::atomic_fence(
sycl::memory_order::acq_rel,
sycl::memory_scope::work_group);
}
}
}
Expand Down Expand Up @@ -1234,8 +1237,9 @@ struct subgroup_radix_sort
*counters[i] = indices[i] + 1;

if (work_around_needed) {
sycl::group_barrier(
ndit.get_group());
sycl::atomic_fence(
sycl::memory_order::acq_rel,
sycl::memory_scope::work_group);
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions dpnp/tests/tensor/test_usm_ndarray_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,40 @@ def test_radix_argsort_size_1_axis():
x4 = dpt.reshape(dpt.arange(10, dtype="i1"), (1, 10))
r4 = dpt.argsort(x4, axis=0, kind="radixsort")
assert dpt.asnumpy(r4 == 0).all()


# rows of every size class of the one-work-group radix sort above 256, and of
# its largest size; on the CPU device int8 argsort came out unstable and some
# data types crashed the compiler at block sizes of 16 and more
@pytest.mark.parametrize("n", [257, 513, 1025, 2049, 4097, 8193, 16384])
@pytest.mark.parametrize("dtype", ["?", "i1", "u1", "i2", "u2", "i4", "i8"])
@pytest.mark.parametrize("descending", [False, True])
def test_radix_sort_one_work_group(n, dtype, descending):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)

rng = np.random.default_rng(42)
if dtype == "?":
x_np = rng.integers(0, 2, size=(2, n)).astype(dtype)
else:
# full-range data, and data of three values with many ties to show an
# unstable argsort
info = np.iinfo(dtype)
x_np = np.stack(
[
rng.integers(info.min, info.max, size=n, dtype=dtype),
rng.integers(0, 3, size=n, dtype=dtype),
]
)
x = dpt.asarray(x_np, sycl_queue=q)

# stable in either order: ties keep the order of the input
_, ranks = np.unique(x_np, return_inverse=True)
ranks = ranks.reshape(x_np.shape)
expected_inds = np.argsort(-ranks if descending else ranks, kind="stable")
expected = np.take_along_axis(x_np, expected_inds, axis=-1)

s = dpt.sort(x, descending=descending, kind="radixsort")
assert_array_equal(dpt.asnumpy(s), expected)
inds = dpt.argsort(x, descending=descending, kind="radixsort")
assert_array_equal(dpt.asnumpy(inds), expected_inds)
Loading