OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Work around miscompile of radix TopK constant-stride loops (#39141)
Min, Byungila05daefea7cb
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/arg_max_min_topk_radix.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/arg_max_min_topk_radix.clindex c1585bd78f4..2a1559f1dae 100644--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/arg_max_min_topk_radix.cl+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/arg_max_min_topk_radix.cl@@ -185,6 +185,7 @@ KERNEL(arg_max_min_topk_radix)(){const uint lid = (uint)get_local_id(0);+ const uint lws = (uint)get_local_size(0);#ifdef RADIX_CHUNK_STAGEconst uint chunk_group_idx = (uint)get_group_id(0);const uint output_idx = chunk_group_idx / RADIX_CHUNKS;@@ -226,7 +227,7 @@ KERNEL(arg_max_min_topk_radix)(// ============================================================// Phase 0: Read input ONCE, convert to sortable keys, cache in global buffer// ============================================================- for (uint i = lid; i < values_count; i += WG_SIZE) {+ for (uint i = lid; i < values_count; i += lws) {base_indices[AXIS] = chunk_begin + i;INPUT0_TYPE val = input[FUNC_CALL(get_input_index)(OPTIONAL_SHAPE_INFO_TENSORbase_indices[0], base_indices[1], 0, base_indices[2], base_indices[3], base_indices[4])];@@ -252,7 +253,7 @@ KERNEL(arg_max_min_topk_radix)(}barrier(CLK_LOCAL_MEM_FENCE);- for (uint i = lid; i < values_count; i += WG_SIZE) {+ for (uint i = lid; i < values_count; i += lws) {uint sortable = my_sortable[i];uint hi = (hi_shift >= SORTABLE_BITS) ? 0u : (sortable >> hi_shift);if (hi == prefix) {@@ -320,7 +321,7 @@ KERNEL(arg_max_min_topk_radix)(// ============================================================// Initialize SLM sort buffers with fill values so unused slots sort to the end// ============================================================- for (uint i = lid; i < PADDED_K; i += WG_SIZE) {+ for (uint i = lid; i < PADDED_K; i += lws) {#ifdef MAX_OUTsort_keys[i] = 0u;#else@@ -334,7 +335,7 @@ KERNEL(arg_max_min_topk_radix)(// Phase 2a: Gather elements strictly ABOVE threshold into SLM// Read from cached sortable buffer// ============================================================- for (uint i = lid; i < values_count; i += WG_SIZE) {+ for (uint i = lid; i < values_count; i += lws) {uint sortable = my_sortable[i];bool is_above;#ifdef MAX_OUT@@ -373,7 +374,7 @@ KERNEL(arg_max_min_topk_radix)(if (at_count <= available_slots) {// Fast path: all at-threshold elements fit, parallel gather is safe- for (uint i = lid; i < values_count; i += WG_SIZE) {+ for (uint i = lid; i < values_count; i += lws) {uint sortable = my_sortable[i];if (sortable == threshold) {uint pos = atomic_add(&gather_count, 1);@@ -432,7 +433,7 @@ KERNEL(arg_max_min_topk_radix)(// ============================================================for (uint bk = 2; bk <= PADDED_K; bk <<= 1) {for (uint bj = bk >> 1; bj > 0; bj >>= 1) {- for (uint i = lid; i < PADDED_K; i += WG_SIZE) {+ for (uint i = lid; i < PADDED_K; i += lws) {uint partner = i ^ bj;if (partner > i) {uint key_i = sort_keys[i];@@ -461,7 +462,7 @@ KERNEL(arg_max_min_topk_radix)(// ============================================================// Phase 4: Store this chunk's sorted top-K as merge candidates// ============================================================- for (uint k = lid; k < TOP_K; k += WG_SIZE) {+ for (uint k = lid; k < TOP_K; k += lws) {candidate_keys[chunk_group_idx * TOP_K + k] = sort_keys[k];candidate_idxs[chunk_group_idx * TOP_K + k] = chunk_begin + sort_idxs[k];}@@ -471,7 +472,7 @@ KERNEL(arg_max_min_topk_radix)(// Zero and NaN values are reloaded from the input, because their keys drop the sign of zero// and the NaN payload.// ============================================================- for (uint k = lid; k < TOP_K; k += WG_SIZE) {+ for (uint k = lid; k < TOP_K; k += lws) {#ifdef RADIX_MERGE_STAGEuint idx = candidate_idxs[output_idx * RADIX_CANDIDATES_NUM + sort_idxs[k]];#elsediff --git a/src/plugins/intel_gpu/tests/unit/test_cases/arg_max_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/arg_max_gpu_test.cppindex 2ba9b1da2d1..e227d99efd6 100644--- a/src/plugins/intel_gpu/tests/unit/test_cases/arg_max_gpu_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/test_cases/arg_max_gpu_test.cpp@@ -1249,6 +1249,38 @@ template <typename T>class arg_max_gpu_topk_radix : public ::testing::Test {};TYPED_TEST_SUITE(arg_max_gpu_topk_radix, topk_radix_data_types, topk_radix_type_names);+// Using the compile-time WG_SIZE as the increment in work-item-strided loops was+// miscompiled on Xe3 when K=N=257, corrupting the sorted values and indices.+TEST(arg_max_gpu_topk_radix, k_equals_axis_values) {+ auto& engine = get_test_engine();+ const int top_k = 257;+ auto input = engine.allocate_memory({data_types::f16, format::bfyx, {1, top_k, 1, 1}});+ std::vector<ov::float16> input_values(top_k);+ for (int i = 0; i < top_k; i++) {+ input_values[i] = static_cast<float>((i * 73) % top_k) / top_k;+ }+ set_values(input, input_values);++ topology topology;+ topology.add(input_layout("input", input->get_layout()));+ topology.add(arg_max_min("arg_max", {input_info("input")},+ ov::op::TopKMode::MAX, top_k, 1,+ ov::op::TopKSortType::SORT_VALUES, true, true, data_types::f16, 1));++ network network(engine, topology, get_radix_topk_config(engine));+ ASSERT_NO_FATAL_FAILURE(assert_radix_topk_selected(network));+ network.set_input_data("input", input);+ auto outputs = network.execute();++ auto values_output = outputs.at("arg_max").get_memory();+ cldnn::mem_lock<ov::float16, mem_lock_type::read> out_ptr(values_output, get_test_stream());+ for (int i = 0; i < top_k; i++) {+ const int expected_idx = ((top_k - 1 - i) * 169) % top_k; // 169 is the inverse of 73 modulo 257.+ ASSERT_EQ(static_cast<float>(out_ptr[i]), static_cast<float>(input_values[expected_idx]))+ << "value mismatch at i=" << i;+ }+}+// --------------------------------------------------------------------------// Parameterized test: value-only verification with various configurations// --------------------------------------------------------------------------