From 4953f6096ab2966c62e0e4c30c9d670b9f83918d Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Mon, 16 Nov 2026 13:32:05 -0800 Subject: [PATCH 1/2] Fence the counter updates of the one-work-group radix sort on CPU devices rather than synchronize the work-group On a CPU device whose kernel takes sub-groups shorter than 16, each counter update was followed by a group_barrier, the work-around of CMPLRLLVM-64592 (int8 radix argsort comes out unstable). With 16 or more elements per work-item, from rows of 2049 elements, these barriers crash the OpenCL CPU compiler in its sycl-kernel-reduce-cross-barrier-values pass, so dpnp.sort and dpnp.argsort of such rows segfault; bool, int8, uint8, int16 and uint16 take the radix sort by default. Seen with every OpenCL CPU runtime from 2024.18 to 2026.22. A work-group fence in place of the barrier still keeps int8 argsort stable and adds no barrier region, so the compiler no longer crashes. Sort and argsort of all integer and floating data types are correct on the same runtimes, and GPU timings are unchanged since GPUs don't take the work-around. --- .../include/kernels/sorting/radix_sort.hpp | 15 ++++++---- dpnp/tests/tensor/test_usm_ndarray_sorting.py | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/dpnp/libtensor/include/kernels/sorting/radix_sort.hpp b/dpnp/libtensor/include/kernels/sorting/radix_sort.hpp index eea69ed052dc..4f58375eaccd 100644 --- a/dpnp/libtensor/include/kernels/sorting/radix_sort.hpp +++ b/dpnp/libtensor/include/kernels/sorting/radix_sort.hpp @@ -1086,8 +1086,9 @@ 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 unless each update is fenced (CMPLRLLVM-64592); a + // barrier in place of the fence crashes its barrier pass const bool work_around_needed = exec_q.get_device().has(sycl::aspect::cpu) && (krn_sg_size < 16); @@ -1198,8 +1199,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); } } } @@ -1234,8 +1236,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); } } } diff --git a/dpnp/tests/tensor/test_usm_ndarray_sorting.py b/dpnp/tests/tensor/test_usm_ndarray_sorting.py index 7311dea78358..9a261144d5c2 100644 --- a/dpnp/tests/tensor/test_usm_ndarray_sorting.py +++ b/dpnp/tests/tensor/test_usm_ndarray_sorting.py @@ -412,3 +412,33 @@ 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; 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, 1025, 2049, 4096, 8193, 16384]) +@pytest.mark.parametrize("dtype", ["?", "i1", "u1", "i2", "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: + info = np.iinfo(dtype) + x_np = rng.integers(info.min, info.max, size=(2, 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) From 3a188e7d719e41aceab3ba09a3dac25251b01953 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Mon, 16 Nov 2026 14:07:23 -0800 Subject: [PATCH 2/2] Add a changelog entry for the radix sort crash fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0470fd0bf4b..7f24e4b179ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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