Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[GPU] enable radix sort on f32 precision (#37941)

Michal Miotk6f4e50b57eb2
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.cl
index 2c20ebe727b..3823289e935 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
@@ -58,6 +58,18 @@
#define NUM_BUCKETS 256
+// Coarse-to-fine radix select: one 8-bit digit per pass, MSD first.
+#define NUM_PASSES (SORTABLE_BITS / 8)
+
+// Ordering of (key, index) pairs. Equal values are broken by the smallest original index,
+// matching TF/ONNX TopK behaviour.
+#ifdef MAX_OUT
+ #define PAIR_GT(key_a, idx_a, key_b, idx_b) ((key_a) != (key_b) ? (key_a) > (key_b) : (idx_a) < (idx_b))
+#else
+ #define PAIR_GT(key_a, idx_a, key_b, idx_b) ((key_a) != (key_b) ? (key_a) > (key_b) : (idx_a) > (idx_b))
+#endif
+#define FILL_IDX 0xFFFFFFFFu
+
inline void FUNC(get_indices_from_dims)(OPTIONAL_SHAPE_INFO_ARG
const uint output_idx,
uint* indices)
@@ -99,27 +111,34 @@ inline void FUNC(get_indices_from_dims)(OPTIONAL_SHAPE_INFO_ARG
#endif
}
-// Convert f16 to a sortable uint16:
-// For positive f16: the bit pattern is already sortable (larger value = larger uint)
-// For negative f16: flip all bits
-// This gives a monotonically increasing uint16 mapping
-inline uint FUNC(f16_to_sortable)(INPUT0_TYPE val) {
- // Use as_ushort to get the bit pattern of f16
+// Convert a float value to a monotonically increasing unsigned integer:
+// for positive values the bit pattern is already ordered, so only the sign bit is flipped;
+// for negative values all bits are flipped.
+#if SORTABLE_BITS == 16
+inline uint FUNC(to_sortable)(INPUT0_TYPE val) {
ushort bits = as_ushort(convert_half(val));
- // If sign bit is set (negative), flip all bits
- // If sign bit is clear (positive), flip only sign bit
- // This maps f16 range to 0..65535 monotonically
ushort mask = (bits >> 15) ? (ushort)0xFFFF : (ushort)0x8000;
return (uint)(bits ^ mask);
}
-inline INPUT0_TYPE FUNC(sortable_to_f16)(uint sortable) {
+inline INPUT0_TYPE FUNC(from_sortable)(uint sortable) {
ushort bits = (ushort)sortable;
ushort mask = (bits & 0x8000) ? (ushort)0x8000 : (ushort)0xFFFF;
- bits = bits ^ mask;
- return convert_float(as_half(bits));
+ return TO_INPUT0_TYPE(as_half((ushort)(bits ^ mask)));
+}
+#else
+inline uint FUNC(to_sortable)(INPUT0_TYPE val) {
+ uint bits = as_uint(convert_float(val));
+ uint mask = (bits >> 31) ? 0xFFFFFFFFu : 0x80000000u;
+ return bits ^ mask;
}
+inline INPUT0_TYPE FUNC(from_sortable)(uint sortable) {
+ uint mask = (sortable & 0x80000000u) ? 0x80000000u : 0xFFFFFFFFu;
+ return TO_INPUT0_TYPE(as_float(sortable ^ mask));
+}
+#endif
+
REQD_SUB_GROUP_SIZE(16)
KERNEL(arg_max_min_topk_radix)(
const __global INPUT0_TYPE* input
@@ -150,122 +169,96 @@ KERNEL(arg_max_min_topk_radix)(
__local uint sort_idxs[PADDED_K]; // original indices
__local uint threshold_bucket;
__local uint count_above;
- __local uint fine_threshold;
- __local uint total_above_threshold;
__local uint gather_count;
// ============================================================
// Phase 0: Read input ONCE, convert to sortable keys, cache in global buffer
- // Also compute coarse histogram in the same pass
// ============================================================
- if (lid < NUM_BUCKETS) {
- histogram[lid] = 0;
- }
- barrier(CLK_LOCAL_MEM_FENCE);
-
for (uint i = lid; i < VALUES_NUM; i += WG_SIZE) {
base_indices[AXIS] = i;
INPUT0_TYPE val = input[FUNC_CALL(get_input_index)(OPTIONAL_SHAPE_INFO_TENSOR
base_indices[0], base_indices[1], 0, base_indices[2], base_indices[3], base_indices[4])];
- uint sortable = FUNC_CALL(f16_to_sortable)(val);
- my_sortable[i] = sortable;
- uint bucket = sortable >> 8;
- atomic_add(&histogram[bucket], 1);
+ my_sortable[i] = FUNC_CALL(to_sortable)(val);
}
barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);
- // Find coarse bucket containing K-th element
- if (lid == 0) {
- uint cumulative = 0;
-#ifdef MAX_OUT
- for (int b = NUM_BUCKETS - 1; b >= 0; b--) {
- cumulative += histogram[b];
- if (cumulative >= TOP_K) {
- threshold_bucket = (uint)b;
- count_above = cumulative - histogram[b];
- break;
- }
- }
-#else
- for (uint b = 0; b < NUM_BUCKETS; b++) {
- cumulative += histogram[b];
- if (cumulative >= TOP_K) {
- threshold_bucket = b;
- count_above = cumulative - histogram[b];
- break;
- }
- }
-#endif
- }
- barrier(CLK_LOCAL_MEM_FENCE);
-
- uint coarse_bucket = threshold_bucket;
- uint already_above = count_above;
-
// ============================================================
- // Phase 1b: Fine histogram (bottom 8 bits within coarse bucket)
- // Read from cached sortable buffer (consistent data)
+ // Phase 1: MSD radix select - resolve the K-th key one byte at a time.
+ // All NUM_PASSES bytes are resolved so the threshold is an exact key value,
+ // which is what phase 2b relies on to break ties by index.
// ============================================================
- if (lid < NUM_BUCKETS) {
- histogram[lid] = 0;
- }
- barrier(CLK_LOCAL_MEM_FENCE);
+ uint prefix = 0; // bytes of the threshold resolved so far
+ uint already_above = 0; // elements strictly beyond the resolved prefix
- for (uint i = lid; i < VALUES_NUM; i += WG_SIZE) {
- uint sortable = my_sortable[i];
- if ((sortable >> 8) == coarse_bucket) {
- atomic_add(&histogram[sortable & 0xFF], 1);
+ for (uint pass = 0; pass < NUM_PASSES; pass++) {
+ const uint shift = SORTABLE_BITS - 8 * (pass + 1);
+ const uint hi_shift = shift + 8;
+
+ if (lid < NUM_BUCKETS) {
+ histogram[lid] = 0;
}
- }
- barrier(CLK_LOCAL_MEM_FENCE);
+ barrier(CLK_LOCAL_MEM_FENCE);
- // Find exact threshold
- if (lid == 0) {
- uint cumulative = already_above;
-#ifdef MAX_OUT
- for (int b = NUM_BUCKETS - 1; b >= 0; b--) {
- cumulative += histogram[b];
- if (cumulative >= TOP_K) {
- fine_threshold = (coarse_bucket << 8) | (uint)b;
- total_above_threshold = cumulative - histogram[b];
- break;
+ for (uint i = lid; i < VALUES_NUM; i += WG_SIZE) {
+ uint sortable = my_sortable[i];
+ uint hi = (hi_shift >= SORTABLE_BITS) ? 0u : (sortable >> hi_shift);
+ if (hi == prefix) {
+ atomic_add(&histogram[(sortable >> shift) & 0xFF], 1);
}
}
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ if (lid == 0) {
+ uint cumulative = already_above;
+#ifdef MAX_OUT
+ for (int b = NUM_BUCKETS - 1; b >= 0; b--) {
+ cumulative += histogram[b];
+ if (cumulative >= TOP_K) {
+ threshold_bucket = (uint)b;
+ count_above = cumulative - histogram[b];
+ break;
+ }
+ }
#else
- for (uint b = 0; b < NUM_BUCKETS; b++) {
- cumulative += histogram[b];
- if (cumulative >= TOP_K) {
- fine_threshold = (coarse_bucket << 8) | b;
- total_above_threshold = cumulative - histogram[b];
- break;
+ for (uint b = 0; b < NUM_BUCKETS; b++) {
+ cumulative += histogram[b];
+ if (cumulative >= TOP_K) {
+ threshold_bucket = b;
+ count_above = cumulative - histogram[b];
+ break;
+ }
}
- }
#endif
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ prefix = (prefix << 8) | threshold_bucket;
+ already_above = count_above;
+ barrier(CLK_LOCAL_MEM_FENCE);
+ }
+
+ if (lid == 0) {
gather_count = 0;
}
barrier(CLK_LOCAL_MEM_FENCE);
- uint threshold = fine_threshold;
+ const uint threshold = prefix;
// ============================================================
- // Initialize SLM sort buffers with fill values
- // Combined key = (sortable << 16) | tiebreaker
- // For MAX_OUT (descending): fill=0 so unused slots sort to end
- // For MIN_OUT (ascending): fill=0xFFFFFFFF so unused slots sort to end
+ // Initialize SLM sort buffers with fill values so unused slots sort to the end
// ============================================================
for (uint i = lid; i < PADDED_K; i += WG_SIZE) {
#ifdef MAX_OUT
- sort_keys[i] = 0;
+ sort_keys[i] = 0u;
#else
- sort_keys[i] = 0xFFFFFFFF;
+ sort_keys[i] = 0xFFFFFFFFu;
#endif
- sort_idxs[i] = 0;
+ sort_idxs[i] = FILL_IDX;
}
barrier(CLK_LOCAL_MEM_FENCE);
// ============================================================
// Phase 2a: Gather elements strictly ABOVE threshold into SLM
- // Use combined key: (sortable << 16) | tiebreaker(index) for deterministic ordering
// Read from cached sortable buffer
// ============================================================
for (uint i = lid; i < VALUES_NUM; i += WG_SIZE) {
@@ -279,12 +272,7 @@ KERNEL(arg_max_min_topk_radix)(
if (is_above) {
uint pos = atomic_add(&gather_count, 1);
if (pos < PADDED_K) {
- // Combined key encodes value and index for deterministic tiebreak
-#ifdef MAX_OUT
- sort_keys[pos] = (sortable << 16) | (0xFFFF - (i & 0xFFFF));
-#else
- sort_keys[pos] = (sortable << 16) | (i & 0xFFFF);
-#endif
+ sort_keys[pos] = sortable;
sort_idxs[pos] = i;
}
}
@@ -316,11 +304,7 @@ KERNEL(arg_max_min_topk_radix)(
if (sortable == threshold) {
uint pos = atomic_add(&gather_count, 1);
if (pos < PADDED_K) {
-#ifdef MAX_OUT
- sort_keys[pos] = (sortable << 16) | (0xFFFF - (i & 0xFFFF));
-#else
- sort_keys[pos] = (sortable << 16) | (i & 0xFFFF);
-#endif
+ sort_keys[pos] = sortable;
sort_idxs[pos] = i;
}
}
@@ -333,11 +317,7 @@ KERNEL(arg_max_min_topk_radix)(
for (uint i = 0; i < VALUES_NUM && pos < PADDED_K; i++) {
uint sortable = my_sortable[i];
if (sortable == threshold) {
-#ifdef MAX_OUT
- sort_keys[pos] = (sortable << 16) | (0xFFFF - (i & 0xFFFF));
-#else
- sort_keys[pos] = (sortable << 16) | (i & 0xFFFF);
-#endif
+ sort_keys[pos] = sortable;
sort_idxs[pos] = i;
pos++;
}
@@ -357,19 +337,21 @@ KERNEL(arg_max_min_topk_radix)(
for (uint i = lid; i < PADDED_K; i += WG_SIZE) {
uint partner = i ^ bj;
if (partner > i) {
- uint ki = sort_keys[i];
- uint kp = sort_keys[partner];
+ uint key_i = sort_keys[i];
+ uint key_p = sort_keys[partner];
+ uint idx_i = sort_idxs[i];
+ uint idx_p = sort_idxs[partner];
bool ascending = ((i & bk) == 0);
#ifdef MAX_OUT
ascending = !ascending;
#endif
- bool need_swap = ascending ? (ki > kp) : (ki < kp);
+ bool need_swap = ascending ? PAIR_GT(key_i, idx_i, key_p, idx_p)
+ : PAIR_GT(key_p, idx_p, key_i, idx_i);
if (need_swap) {
- sort_keys[i] = kp;
- sort_keys[partner] = ki;
- uint ti = sort_idxs[i];
- sort_idxs[i] = sort_idxs[partner];
- sort_idxs[partner] = ti;
+ sort_keys[i] = key_p;
+ sort_keys[partner] = key_i;
+ sort_idxs[i] = idx_p;
+ sort_idxs[partner] = idx_i;
}
}
}
@@ -379,11 +361,9 @@ KERNEL(arg_max_min_topk_radix)(
// ============================================================
// Phase 4: Write sorted top-K results to output
- // sort_keys contains combined key: (sortable << 16) | tiebreaker
- // Extract upper 16 bits to recover the sortable value
// ============================================================
for (uint k = lid; k < TOP_K; k += WG_SIZE) {
- INPUT0_TYPE val = FUNC_CALL(sortable_to_f16)(sort_keys[k] >> 16);
+ INPUT0_TYPE val = FUNC_CALL(from_sortable)(sort_keys[k]);
uint idx = sort_idxs[k];
base_indices[AXIS] = k;
@@ -412,3 +392,7 @@ KERNEL(arg_max_min_topk_radix)(
#undef WG_SIZE
#undef PADDED_K
#undef NUM_BUCKETS
+#undef SORTABLE_BITS
+#undef NUM_PASSES
+#undef PAIR_GT
+#undef FILL_IDX
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/arg_max_min/arg_max_min_kernel_topk_radix.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/arg_max_min/arg_max_min_kernel_topk_radix.cpp
index 91501e88c5a..6b4099de601 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/arg_max_min/arg_max_min_kernel_topk_radix.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/arg_max_min/arg_max_min_kernel_topk_radix.cpp
@@ -52,6 +52,7 @@ size_t GetSortSize(const arg_max_min_params& params) {
ParamsKey ArgMaxMinKernelTopKRadix::GetSupportedKey() const {
ParamsKey k;
k.EnableInputDataType(Datatype::F16);
+ k.EnableInputDataType(Datatype::F32);
k.EnableAllOutputDataType();
k.EnableInputLayout(DataLayout::bfyx);
k.EnableOutputLayout(DataLayout::bfyx);
@@ -76,8 +77,8 @@ bool ArgMaxMinKernelTopKRadix::Validate(const Params& p) const {
const auto& params = static_cast<const arg_max_min_params&>(p);
- // Only f16 input for radix approach (bit manipulation)
- if (params.inputs[0].GetDType() != Datatype::F16) {
+ // Radix approach relies on bit manipulation of the IEEE-754 representation
+ if (params.inputs[0].GetDType() != Datatype::F16 && params.inputs[0].GetDType() != Datatype::F32) {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
@@ -91,11 +92,6 @@ bool ArgMaxMinKernelTopKRadix::Validate(const Params& p) const {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
- // Combined key uses (i & 0xFFFF) as tiebreaker, so N must fit in 16 bits
- if (sort_size > 65535) {
- DO_NOT_USE_THIS_KERNEL(p.layerID);
- }
-
// PADDED_K (next power of 2 >= topK) must fit in SLM:
// sort_keys[PADDED_K] + sort_idxs[PADDED_K] + histogram[256] + scalars
size_t padded_k = 1;
@@ -145,6 +141,7 @@ JitConstants ArgMaxMinKernelTopKRadix::GetJitConstants(const arg_max_min_params&
auto jit = ArgMaxMinKernelBase::GetJitConstants(params);
jit.AddConstant(MakeJitConstant("WG_SIZE", kWgSize));
+ jit.AddConstant(MakeJitConstant("SORTABLE_BITS", params.inputs[0].GetDType() == Datatype::F16 ? 16 : 32));
// PADDED_K: next power of 2 >= TOP_K (for bitonic sort)
size_t padded_k = 1;
diff --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.cpp
index 9812ce1593b..c0fde6e3906 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
@@ -1099,7 +1099,7 @@ TEST(arg_max_min_test, check_second_output_data_type) {
// =============================================================================
// Tests for arg_max_min_topk_radix kernel
-// Conditions: f16 input, SORT_VALUES, N >= 2, N <= 65535, SLM fits
+// Conditions: f16/f32 input, SORT_VALUES, N >= 2, N <= 65535, SLM fits
// =============================================================================
// Helper: create ExecutionConfig that forces the radix TopK kernel
@@ -1111,6 +1111,40 @@ inline ExecutionConfig get_radix_topk_config(const cldnn::engine& engine) {
return config;
}
+// Helper: check that forcing actually resulted in the radix kernel being used
+inline void assert_radix_topk_selected(const cldnn::network& net) {
+ auto info = net.get_primitive_info("arg_max");
+ ASSERT_TRUE(info.find("arg_max_min_topk_radix") != std::string::npos)
+ << "Expected arg_max_min_topk_radix, got: " << info;
+}
+
+namespace {
+// The radix kernel supports both floating point input types, so every case below runs for both.
+using topk_radix_data_types = ::testing::Types<ov::float16, float>;
+
+struct topk_radix_type_names {
+ template <typename T>
+ static std::string GetName(int) {
+ return std::is_same<T, ov::float16>::value ? "f16" : "f32";
+ }
+};
+
+template <typename T>
+std::vector<T> topk_radix_typed_vec(const std::vector<float>& values) {
+ return std::vector<T>(values.begin(), values.end());
+}
+
+// f16 storage rounds the reference values, f32 results are expected to be bit-exact.
+template <typename T>
+constexpr float topk_radix_tolerance() {
+ return std::is_same<T, ov::float16>::value ? 0.02f : 0.01f;
+}
+} // namespace
+
+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);
+
// --------------------------------------------------------------------------
// Parameterized test: value-only verification with various configurations
// --------------------------------------------------------------------------
@@ -1120,20 +1154,21 @@ struct TopKRadixValueTestParam {
int32_t feature_num;
int top_k;
int axis;
- std::vector<ov::float16> input_vec;
+ std::vector<float> input_vec;
std::vector<float> ref_values; // expected top-K values in sorted order
std::string name;
};
-class TopKRadixValueTest : public testing::TestWithParam<TopKRadixValueTestParam> {};
+class TopKRadixValueTest : public testing::TestWithParam<std::tuple<TopKRadixValueTestParam, data_types>> {};
-TEST_P(TopKRadixValueTest, values_match) {
- const auto& p = GetParam();
+template <typename T>
+void run_topk_radix_value_case(const TopKRadixValueTestParam& p) {
auto& engine = get_test_engine();
+ const data_types dt = ov::element::from<T>();
- auto input = engine.allocate_memory({data_types::f16, format::bfyx,
+ auto input = engine.allocate_memory({dt, format::bfyx,
{p.batch_num, p.feature_num, 1, 1}});
- set_values(input, p.input_vec);
+ set_values(input, topk_radix_typed_vec<T>(p.input_vec));
topology topology;
topology.add(input_layout("input", input->get_layout()));
@@ -1145,64 +1180,81 @@ TEST_P(TopKRadixValueTest, values_match) {
ov::op::TopKSortType::SORT_VALUES,
true,
false,
- data_types::f16,
+ dt,
2));
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 output = outputs.at("arg_max").get_memory();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> output_ptr(output, get_test_stream());
for (size_t i = 0; i < p.ref_values.size(); i++) {
float val = static_cast<float>(output_ptr[i]);
- ASSERT_NEAR(val, p.ref_values[i], 0.02f) << "value mismatch at i=" << i;
+ ASSERT_NEAR(val, p.ref_values[i], topk_radix_tolerance<T>()) << "value mismatch at i=" << i;
}
}
+TEST_P(TopKRadixValueTest, values_match) {
+ const auto& p = std::get<0>(GetParam());
+ if (std::get<1>(GetParam()) == data_types::f16)
+ run_topk_radix_value_case<ov::float16>(p);
+ else
+ run_topk_radix_value_case<float>(p);
+}
+
INSTANTIATE_TEST_SUITE_P(arg_max_gpu_topk_radix, TopKRadixValueTest,
- testing::Values(
- TopKRadixValueTestParam{
- ov::op::TopKMode::MAX, 1, 16, 4, 1,
- {ov::float16(1.0f), ov::float16(5.0f), ov::float16(3.0f), ov::float16(9.0f),
- ov::float16(2.0f), ov::float16(7.0f), ov::float16(4.0f), ov::float16(8.0f),
- ov::float16(6.0f), ov::float16(0.5f), ov::float16(10.0f), ov::float16(0.1f),
- ov::float16(15.0f), ov::float16(12.0f), ov::float16(11.0f), ov::float16(0.2f)},
- {15.0f, 12.0f, 11.0f, 10.0f},
- "f16_max_axis_feature"
- },
- TopKRadixValueTestParam{
- ov::op::TopKMode::MIN, 1, 16, 4, 1,
- {ov::float16(1.0f), ov::float16(5.0f), ov::float16(3.0f), ov::float16(9.0f),
- ov::float16(2.0f), ov::float16(7.0f), ov::float16(4.0f), ov::float16(8.0f),
- ov::float16(6.0f), ov::float16(0.5f), ov::float16(-1.0f), ov::float16(0.1f),
- ov::float16(15.0f), ov::float16(12.0f), ov::float16(11.0f), ov::float16(-2.0f)},
- {-2.0f, -1.0f, 0.1f, 0.5f},
- "f16_min_axis_feature"
- },
- TopKRadixValueTestParam{
- ov::op::TopKMode::MAX, 1, 8, 3, 1,
- {ov::float16(-5.0f), ov::float16(-1.0f), ov::float16(-8.0f), ov::float16(-0.5f),
- ov::float16(-3.0f), ov::float16(-10.0f), ov::float16(-2.0f), ov::float16(-0.1f)},
- {-0.1f, -0.5f, -1.0f},
- "f16_max_negatives"
- }
+ testing::Combine(
+ testing::Values(
+ TopKRadixValueTestParam{
+ ov::op::TopKMode::MAX, 1, 16, 4, 1,
+ {1.0f, 5.0f, 3.0f, 9.0f,
+ 2.0f, 7.0f, 4.0f, 8.0f,
+ 6.0f, 0.5f, 10.0f, 0.1f,
+ 15.0f, 12.0f, 11.0f, 0.2f},
+ {15.0f, 12.0f, 11.0f, 10.0f},
+ "max_axis_feature"
+ },
+ TopKRadixValueTestParam{
+ ov::op::TopKMode::MIN, 1, 16, 4, 1,
+ {1.0f, 5.0f, 3.0f, 9.0f,
+ 2.0f, 7.0f, 4.0f, 8.0f,
+ 6.0f, 0.5f, -1.0f, 0.1f,
+ 15.0f, 12.0f, 11.0f, -2.0f},
+ {-2.0f, -1.0f, 0.1f, 0.5f},
+ "min_axis_feature"
+ },
+ TopKRadixValueTestParam{
+ ov::op::TopKMode::MAX, 1, 8, 3, 1,
+ {-5.0f, -1.0f, -8.0f, -0.5f,
+ -3.0f, -10.0f, -2.0f, -0.1f},
+ {-0.1f, -0.5f, -1.0f},
+ "max_negatives"
+ }
+ ),
+ testing::Values(data_types::f16, data_types::f32)
),
- [](const testing::TestParamInfo<TopKRadixValueTestParam>& info) { return info.param.name; }
+ [](const testing::TestParamInfo<TopKRadixValueTest::ParamType>& info) {
+ return std::get<0>(info.param).name +
+ (std::get<1>(info.param) == data_types::f16 ? "_f16" : "_f32");
+ }
);
-// Separate parameterized case for large N on batch axis (generated input)
-TEST(arg_max_gpu_topk_radix, f16_max_large_n_axis_batch) {
+// Separate case for large N on batch axis (generated input)
+TYPED_TEST(arg_max_gpu_topk_radix, max_large_n_axis_batch) {
+ using T = TypeParam;
+ const data_types dt = ov::element::from<T>();
static const int32_t batch_num = 1000;
auto& engine = get_test_engine();
const int top_k = 10;
- auto input = engine.allocate_memory({data_types::f16, format::bfyx, {batch_num, 1, 1, 1}});
+ auto input = engine.allocate_memory({dt, format::bfyx, {batch_num, 1, 1, 1}});
- std::vector<ov::float16> input_vec(batch_num);
+ std::vector<float> input_vec(batch_num);
for (int i = 0; i < batch_num; i++) {
- input_vec[i] = ov::float16(static_cast<float>(i) * 0.01f);
+ input_vec[i] = static_cast<float>(i) * 0.01f;
}
- set_values(input, input_vec);
+ set_values(input, topk_radix_typed_vec<T>(input_vec));
topology topology;
topology.add(input_layout("input", input->get_layout()));
@@ -1212,17 +1264,19 @@ TEST(arg_max_gpu_topk_radix, f16_max_large_n_axis_batch) {
top_k,
0,
ov::op::TopKSortType::SORT_VALUES,
- true, false, data_types::f16));
+ true, false, dt));
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 output = outputs.at("arg_max").get_memory();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> output_ptr(output, get_test_stream());
for (int i = 0; i < top_k; i++) {
- float expected = static_cast<float>(batch_num - 1 - i) * 0.01f;
- ASSERT_NEAR(static_cast<float>(output_ptr[i]), expected, 0.02f) << "mismatch at i=" << i;
+ float expected = input_vec[batch_num - 1 - i];
+ ASSERT_NEAR(static_cast<float>(output_ptr[i]), expected, topk_radix_tolerance<T>())
+ << "mismatch at i=" << i;
}
}
@@ -1230,19 +1284,21 @@ TEST(arg_max_gpu_topk_radix, f16_max_large_n_axis_batch) {
// Non-parameterized tests: each has unique verification logic
// --------------------------------------------------------------------------
-// Radix test: f16 MAX, second output (indices), axis=feature(1)
-TEST(arg_max_gpu_topk_radix, f16_max_second_output_indices) {
+// Radix test: MAX, second output (indices), axis=feature(1)
+TYPED_TEST(arg_max_gpu_topk_radix, max_second_output_indices) {
+ using T = TypeParam;
+ const data_types dt = ov::element::from<T>();
static const int32_t feature_num = 32;
auto& engine = get_test_engine();
const int top_k = 8;
- auto input = engine.allocate_memory({data_types::f16, format::bfyx, {1, feature_num, 1, 1}});
- auto top_k_input = engine.allocate_memory({data_types::f16, format::bfyx, {1, 1, 1, 1}});
+ auto input = engine.allocate_memory({dt, format::bfyx, {1, feature_num, 1, 1}});
+ auto top_k_input = engine.allocate_memory({dt, format::bfyx, {1, 1, 1, 1}});
auto second_output = engine.allocate_memory({data_types::f32, format::bfyx, {1, top_k, 1, 1}});
- std::vector<ov::float16> input_vec(feature_num);
+ std::vector<float> input_vec(feature_num);
for (int i = 0; i < feature_num; i++)
- input_vec[i] = ov::float16(static_cast<float>(i * 3 % 17));
- set_values(input, input_vec);
+ input_vec[i] = static_cast<float>(i * 3 % 17);
+ set_values(input, topk_radix_typed_vec<T>(input_vec));
topology topology;
topology.add(input_layout("input", input->get_layout()));
@@ -1251,14 +1307,15 @@ TEST(arg_max_gpu_topk_radix, f16_max_second_output_indices) {
topology.add(arg_max_min("arg_max",
{input_info("input"), input_info("const"), input_info("second_output")},
ov::op::TopKMode::MAX, top_k, 1,
- ov::op::TopKSortType::SORT_VALUES, true, false, data_types::f16));
+ ov::op::TopKSortType::SORT_VALUES, true, false, dt));
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 output = outputs.at("arg_max").get_memory();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> output_ptr(output, get_test_stream());
for (int i = 1; i < top_k; i++) {
ASSERT_GE(static_cast<float>(output_ptr[i - 1]), static_cast<float>(output_ptr[i]))
<< "values not sorted descending at i=" << i;
@@ -1270,16 +1327,18 @@ TEST(arg_max_gpu_topk_radix, f16_max_second_output_indices) {
}
// Radix test: duplicate values tiebreaking (smaller index first)
-TEST(arg_max_gpu_topk_radix, f16_max_duplicate_values_tiebreak) {
+TYPED_TEST(arg_max_gpu_topk_radix, max_duplicate_values_tiebreak) {
+ using T = TypeParam;
+ const data_types dt = ov::element::from<T>();
auto& engine = get_test_engine();
const int top_k = 5;
- auto input = engine.allocate_memory({data_types::f16, format::bfyx, {1, 10, 1, 1}});
- auto top_k_input = engine.allocate_memory({data_types::f16, format::bfyx, {1, 1, 1, 1}});
+ auto input = engine.allocate_memory({dt, format::bfyx, {1, 10, 1, 1}});
+ auto top_k_input = engine.allocate_memory({dt, format::bfyx, {1, 1, 1, 1}});
auto second_output = engine.allocate_memory({data_types::f32, format::bfyx, {1, top_k, 1, 1}});
// 5,5,5,5,5,3,3,3,1,1 — top5 are all 5.0, indices 0..4
- set_values(input, {ov::float16(5.0f), ov::float16(5.0f), ov::float16(5.0f), ov::float16(5.0f), ov::float16(5.0f),
- ov::float16(3.0f), ov::float16(3.0f), ov::float16(3.0f), ov::float16(1.0f), ov::float16(1.0f)});
+ set_values(input, topk_radix_typed_vec<T>({5.0f, 5.0f, 5.0f, 5.0f, 5.0f,
+ 3.0f, 3.0f, 3.0f, 1.0f, 1.0f}));
topology topology;
topology.add(input_layout("input", input->get_layout()));
@@ -1288,14 +1347,15 @@ TEST(arg_max_gpu_topk_radix, f16_max_duplicate_values_tiebreak) {
topology.add(arg_max_min("arg_max",
{input_info("input"), input_info("const"), input_info("second_output")},
ov::op::TopKMode::MAX, top_k, 1,
- ov::op::TopKSortType::SORT_VALUES, true, false, data_types::f16));
+ ov::op::TopKSortType::SORT_VALUES, true, false, dt));
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 output = outputs.at("arg_max").get_memory();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> output_ptr(output, get_test_stream());
for (int i = 0; i < top_k; i++)
ASSERT_NEAR(static_cast<float>(output_ptr[i]), 5.0f, 0.01f) << "value mismatch at i=" << i;
@@ -1305,32 +1365,35 @@ TEST(arg_max_gpu_topk_radix, f16_max_duplicate_values_tiebreak) {
}
// Radix test: YOLOv26n-like scenario (N=8400, K=300, axis=feature)
-TEST(arg_max_gpu_topk_radix, f16_max_yolov26n_like) {
+TYPED_TEST(arg_max_gpu_topk_radix, max_yolov26n_like) {
+ using T = TypeParam;
+ const data_types dt = ov::element::from<T>();
static const int32_t feature_num = 8400;
auto& engine = get_test_engine();
const int top_k = 300;
- auto input = engine.allocate_memory({data_types::f16, format::bfyx, {1, feature_num, 1, 1}});
+ auto input = engine.allocate_memory({dt, format::bfyx, {1, feature_num, 1, 1}});
- std::vector<ov::float16> input_vec(feature_num);
+ std::vector<float> input_vec(feature_num);
for (int i = 0; i < feature_num; i++)
- input_vec[i] = ov::float16(static_cast<float>(i % 100) * 0.01f);
+ input_vec[i] = static_cast<float>(i % 100) * 0.01f;
for (int i = 0; i < 300; i++)
- input_vec[i * 28] = ov::float16(100.0f + static_cast<float>(300 - i) * 0.1f);
- set_values(input, input_vec);
+ input_vec[i * 28] = 100.0f + static_cast<float>(300 - i) * 0.1f;
+ set_values(input, topk_radix_typed_vec<T>(input_vec));
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, false, data_types::f16));
+ ov::op::TopKSortType::SORT_VALUES, true, false, dt));
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 output = outputs.at("arg_max").get_memory();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> output_ptr(output, get_test_stream());
for (int i = 1; i < top_k; i++) {
ASSERT_GE(static_cast<float>(output_ptr[i - 1]), static_cast<float>(output_ptr[i]))
<< "values not sorted descending at i=" << i;
@@ -1340,20 +1403,22 @@ TEST(arg_max_gpu_topk_radix, f16_max_yolov26n_like) {
}
// Radix test: CI failure reproducer - many duplicate values, verify indices tiebreaking
-TEST(arg_max_gpu_topk_radix, f16_max_duplicates_n70_k5_indices_tiebreak) {
+TYPED_TEST(arg_max_gpu_topk_radix, max_duplicates_n70_k5_indices_tiebreak) {
+ using T = TypeParam;
+ const data_types dt = ov::element::from<T>();
static const int32_t batch_num = 4, feature_num = 70;
auto& engine = get_test_engine();
const int top_k = 5;
- auto input = engine.allocate_memory({data_types::f16, format::bfyx, {batch_num, feature_num, 1, 1}});
- auto top_k_input = engine.allocate_memory({data_types::f16, format::bfyx, {1, 1, 1, 1}});
+ auto input = engine.allocate_memory({dt, format::bfyx, {batch_num, feature_num, 1, 1}});
+ auto top_k_input = engine.allocate_memory({dt, format::bfyx, {1, 1, 1, 1}});
auto second_output = engine.allocate_memory({data_types::f32, format::bfyx, {batch_num, top_k, 1, 1}});
- std::vector<ov::float16> input_vec(batch_num * feature_num);
+ std::vector<float> input_vec(batch_num * feature_num);
std::mt19937 rng(42);
std::uniform_int_distribution<int> dist(0, 14);
for (size_t i = 0; i < input_vec.size(); i++)
- input_vec[i] = ov::float16(static_cast<float>(dist(rng)));
- set_values(input, input_vec);
+ input_vec[i] = static_cast<float>(dist(rng));
+ set_values(input, topk_radix_typed_vec<T>(input_vec));
topology topology;
topology.add(input_layout("input", input->get_layout()));
@@ -1362,14 +1427,15 @@ TEST(arg_max_gpu_topk_radix, f16_max_duplicates_n70_k5_indices_tiebreak) {
topology.add(arg_max_min("arg_max",
{input_info("input"), input_info("const"), input_info("second_output")},
ov::op::TopKMode::MAX, top_k, 1,
- ov::op::TopKSortType::SORT_VALUES, true, false, data_types::f16));
+ ov::op::TopKSortType::SORT_VALUES, true, false, dt));
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 output = outputs.at("arg_max").get_memory();
- cldnn::mem_lock<ov::float16, mem_lock_type::read> val_ptr(output, get_test_stream());
+ cldnn::mem_lock<T, mem_lock_type::read> val_ptr(output, get_test_stream());
cldnn::mem_lock<float> idx_ptr(second_output, get_test_stream());
for (int b = 0; b < batch_num; b++) {
@@ -1378,7 +1444,7 @@ TEST(arg_max_gpu_topk_radix, f16_max_duplicates_n70_k5_indices_tiebreak) {
std::vector<std::pair<float, int>> ref;
for (int i = 0; i < feature_num; i++)
- ref.push_back({static_cast<float>(input_vec[input_offset + i]), i});
+ ref.push_back({input_vec[input_offset + i], i});
std::stable_sort(ref.begin(), ref.end(),
[](const auto& a, const auto& b) { return a.first > b.first; });
@@ -1391,6 +1457,52 @@ TEST(arg_max_gpu_topk_radix, f16_max_duplicates_n70_k5_indices_tiebreak) {
}
}
+// Radix test: very large sort size (N=100000, K=256, axis=feature)
+TYPED_TEST(arg_max_gpu_topk_radix, max_n100k_k256_axis_feature) {
+ using T = TypeParam;
+ const data_types dt = ov::element::from<T>();
+ static const int32_t feature_num = 100000;
+ auto& engine = get_test_engine();
+ const int top_k = 256;
+ const int stride = feature_num / top_k;
+ auto input = engine.allocate_memory({dt, format::bfyx, {1, feature_num, 1, 1}});
+ auto top_k_input = engine.allocate_memory({dt, format::bfyx, {1, 1, 1, 1}});
+ auto second_output = engine.allocate_memory({data_types::f32, format::bfyx, {1, top_k, 1, 1}});
+
+ // Background stays below 1.0, the K planted values are distinct integers
+ // (exactly representable in f16) so both values and indices are unambiguous.
+ std::vector<float> input_vec(feature_num);
+ for (int i = 0; i < feature_num; i++)
+ input_vec[i] = static_cast<float>(i % 100) * 0.01f;
+ for (int i = 0; i < top_k; i++)
+ input_vec[i * stride] = 100.0f + static_cast<float>(top_k - i);
+ set_values(input, topk_radix_typed_vec<T>(input_vec));
+
+ topology topology;
+ topology.add(input_layout("input", input->get_layout()));
+ topology.add(cldnn::data("const", top_k_input));
+ topology.add(mutable_data("second_output", second_output));
+ topology.add(arg_max_min("arg_max",
+ {input_info("input"), input_info("const"), input_info("second_output")},
+ ov::op::TopKMode::MAX, top_k, 1,
+ ov::op::TopKSortType::SORT_VALUES, true, false, dt));
+
+ 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 output = outputs.at("arg_max").get_memory();
+ cldnn::mem_lock<T, mem_lock_type::read> val_ptr(output, get_test_stream());
+ cldnn::mem_lock<float> idx_ptr(second_output, get_test_stream());
+
+ for (int k = 0; k < top_k; k++) {
+ ASSERT_NEAR(static_cast<float>(val_ptr[k]), 100.0f + static_cast<float>(top_k - k),
+ topk_radix_tolerance<T>()) << "value mismatch at k=" << k;
+ ASSERT_EQ(static_cast<int>(idx_ptr[k]), k * stride) << "index mismatch at k=" << k;
+ }
+}
+
// Verify that arg_max_min_axis is selected over radix for small sort_size and k=1
TEST(arg_max_gpu_topk_radix, fallback_to_axis_for_small_sort_size_and_topk1) {
auto& engine = get_test_engine();