diff --git a/CHANGELOG.md b/CHANGELOG.md index c0470fd0bf4..7f24e4b179f 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 diff --git a/dpnp/libtensor/include/kernels/sorting/radix_sort.hpp b/dpnp/libtensor/include/kernels/sorting/radix_sort.hpp index eea69ed052d..4f58375eacc 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 7311dea7835..9a261144d5c 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)