Skip to content

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

[GPU] Remove implicit use of non-uniform workgroup kernel dispatch (#39675)

Joonho Yeomd7fe851a6798
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp
index 44efc2ef469..f617c627561 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.cpp
@@ -440,23 +440,52 @@ size_t get_group_size(const MOECompressed::Config& config, size_t k) {
return config.group_size == std::numeric_limits<size_t>::max() ? k : config.group_size;
}
-// Validate GEMV kernel compatibility: ELEMS_PER_LANE = FAKE_GROUP_SIZE / SUBGROUP_SIZE must be >= 2.
-// Smaller values have no kernel branch and would silently produce wrong results.
-void check_gemv_group_size(const RuntimeParams& params) {
+// Validate the shape for the batched GEMV kernels (moe_3gemm_swiglu_mlp.cl). The constraints come from the kernels,
+// and they also keep the dispatch uniform (no non-uniform work-group).
+void check_gemv_shape(const RuntimeParams& params) {
const auto& config = params.typed_desc<moe_3gemm_fused_compressed>()->_config;
- const auto gate_up_group_size = get_group_size(config, config.hidden_size);
+ const size_t hidden_size = config.hidden_size;
+ const size_t inter_size = config.inter_size;
+ const size_t gate_up_group_size = get_group_size(config, hidden_size);
+ const size_t down_group_size = get_group_size(config, inter_size);
const size_t sg = (params.get_device_info().arch >= gpu_arch::xe2) ? 32U : 16U;
- const auto down_group_size = get_group_size(config, config.inter_size);
- // Mirrors FAKE_GROUP_SIZE in moe_3gemm_swiglu_mlp.cl, which also depends on the down group size.
const size_t fake_gs = std::min({gate_up_group_size, down_group_size, size_t{128}});
- OPENVINO_ASSERT(fake_gs >= 2 * sg,
- "MoE GEMV kernel does not support group_size=",
- fake_gs,
+
+ // The kernels have block-read branches for ELEMS_PER_LANE 2, 4 and 8 only.
+ OPENVINO_ASSERT(fake_gs % sg == 0 && one_of(fake_gs / sg, {size_t{2}, size_t{4}, size_t{8}}),
+ "MoE GEMV kernel does not support gate_up_group_size=",
+ gate_up_group_size,
+ ", down_group_size=",
+ down_group_size,
" on this hardware (SUBGROUP_SIZE=",
sg,
- "). Minimum supported group_size is ",
- 2 * sg,
- ". Use a larger quantization group size.");
+ "): min(group sizes, 128)=",
+ fake_gs,
+ " must be 2, 4 or 8 times SUBGROUP_SIZE.");
+ // A chunk must not cross a quantization group, and K must be a whole number of chunks.
+ OPENVINO_ASSERT(gate_up_group_size % fake_gs == 0 && down_group_size % fake_gs == 0 && hidden_size % fake_gs == 0 && inter_size % fake_gs == 0,
+ "MoE GEMV kernel does not support hidden_size=",
+ hidden_size,
+ ", inter_size=",
+ inter_size,
+ ", gate_up_group_size=",
+ gate_up_group_size,
+ ", down_group_size=",
+ down_group_size,
+ ": all of them must be multiples of ",
+ fake_gs,
+ ".");
+ // Implied by the checks above (fake_gs >= 2 * SUBGROUP_SIZE >= 32). The batched GEMV dispatch
+ // {.., SG, N / N_BLOCK} / {1, SG, SUBGROUP_NUM} is uniform only for N a multiple of N_BLOCK * SUBGROUP_NUM.
+ constexpr size_t n_align = N_BLOCK * SUBGROUP_NUM;
+ OPENVINO_ASSERT(hidden_size % n_align == 0 && inter_size % n_align == 0,
+ "MoE GEMV kernel requires hidden_size=",
+ hidden_size,
+ " and inter_size=",
+ inter_size,
+ " to be multiples of ",
+ n_align,
+ ".");
}
void add_common_consts(const RuntimeParams& params, JitConstants& jit) {
@@ -530,7 +559,6 @@ protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
auto jit = KernelGenerator::get_jit_constants(params);
auto desc = params.typed_desc<moe_3gemm_fused_compressed>();
- check_gemv_group_size(params);
add_common_consts(params, jit);
jit.make("GATE_UP_ENABLE", 1);
if (desc->_config.activation_type == ov::op::internal::MOE::Activation_type::GEGLU_TANH) {
@@ -570,7 +598,6 @@ protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
auto jit = KernelGenerator::get_jit_constants(params);
auto desc = params.typed_desc<moe_3gemm_fused_compressed>();
- check_gemv_group_size(params);
add_common_consts(params, jit);
jit.make("DOWN_ENABLE", 1);
if (!_disable_shared_experts && desc->_config.num_shared_expert > 0 &&
@@ -1174,6 +1201,8 @@ public:
add_stage(scatter, params);
// The batched GEMV kernels have no code path for this weight type, so they are not built
if (decode_impl == MoeGemmImpl::batched_gemv) {
+ // Checked here: add_stage() swallows exceptions from kernel generation and drops the stage.
+ check_gemv_shape(params);
add_stage(mlp_gate_up, params);
add_stage(mlp_down, params);
add_stage(mlp_reduce, params);
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp
index bc7a0587e7a..56c6692fb57 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/pa_kv_reorder.cpp
@@ -165,7 +165,11 @@ protected:
if (max_heads_per_wg == 0) {
max_heads_per_wg = 1;
}
+ // Pick the largest divisor of heads_number that fits into one work-group to keep the dispatch uniform
auto wg_heads_number = std::min(heads_number, max_heads_per_wg);
+ while (heads_number % wg_heads_number != 0) {
+ wg_heads_number--;
+ }
wgs.global = {sequences_number, heads_number, subgroup_size};
wgs.local = {1, is_kv_compressed ? 1 : wg_heads_number, subgroup_size};
}};
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_gemv.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_gemv.cpp
index ec3e74da7f5..f8a832eddac 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_gemv.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/fully_connected/fully_connected_kernel_gemv.cpp
@@ -47,6 +47,57 @@ DeviceFeaturesKey FullyConnected_GEMV::get_required_device_features_key(const Pa
return k;
}
+namespace {
+// Weights layout the GEMV kernel runs with (the layout weights are reordered to, or the given blocked layout).
+WeightsLayout get_gemv_weights_layout(const fully_connected_params& fc_params) {
+ auto output_f = get_output_aligned_bf_size(fc_params, false).second;
+
+ WeightsLayout weights_layout = WeightsLayout::os_iyx_osv16;
+ if (is_swiglu_fused(fc_params)) {
+ weights_layout = WeightsLayout::os_is_yx_osv32_isv2;
+ } else if (fc_params.compressed && fc_params.inputs[0].GetDType() == Datatype::F16 &&
+ (fc_params.weights.GetLayout() == WeightsLayout::oiyx || fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2) &&
+ (fc_params.weights.GetDType() == WeightsType::INT4 || fc_params.weights.GetDType() == WeightsType::UINT4) &&
+ is_weight_horizontal(fc_params, output_f)) {
+ // Large N + small K case (horizontal weight) to use osv64_isv2
+ weights_layout = WeightsLayout::os_is_yx_osv64_isv2;
+ } else if (fc_params.compressed && fc_params.inputs[0].GetDType() == Datatype::F16 &&
+ (fc_params.weights.GetDType() == WeightsType::INT4 || fc_params.weights.GetDType() == WeightsType::UINT4) &&
+ (fc_params.weights.GetLayout() == WeightsLayout::oiyx || fc_params.weights.GetLayout() == WeightsLayout::os_iyx_osv16) &&
+ is_weight_vertical(fc_params, output_f)) {
+ // Large K + Small N case (vertical weight) to use osv16
+ weights_layout = WeightsLayout::os_iyx_osv16;
+ } else if (fc_params.compressed &&
+ fc_params.inputs[0].GetDType() == Datatype::F16
+ // ioyx => os_is_yx_osv32_isv2 is not supported yet
+ && (fc_params.weights.GetLayout() == WeightsLayout::oiyx || fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) &&
+ (fc_params.weights.GetDType() == WeightsType::INT4 || fc_params.weights.GetDType() == WeightsType::UINT4)) {
+ weights_layout = WeightsLayout::os_iyx_osv16;
+ }
+
+ if ((fc_params.weights.GetLayout() == WeightsLayout::os_iyx_osv16) || (fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) ||
+ (fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2)) {
+ weights_layout = fc_params.weights.GetLayout();
+ }
+
+ return weights_layout;
+}
+
+// Dispatch: GWS = {OFM / ofm_per_work_item, 1, 16}, LWS = {16, 1, 16}. Shared by Validate and SetDefault.
+constexpr size_t gemv_lws[3] = {16, 1, 16};
+
+size_t get_ofm_per_work_item(WeightsLayout wl) {
+ switch (wl) {
+ case WeightsLayout::os_is_yx_osv32_isv2:
+ return 2;
+ case WeightsLayout::os_is_yx_osv64_isv2:
+ return 4;
+ default:
+ return 1;
+ }
+}
+} // namespace
+
bool FullyConnected_GEMV::Validate(const Params& params) const {
if (!Parent::Validate(params)) {
DO_NOT_USE_THIS_KERNEL(params.layerID);
@@ -77,23 +128,14 @@ bool FullyConnected_GEMV::Validate(const Params& params) const {
DO_NOT_USE_THIS_KERNEL(params.layerID);
}
- auto wl = weights.GetLayout();
- auto wo = weights.OFM().v;
-
- const auto& fc_input = fc_params.inputs[0];
if (is_swiglu_fused(fc_params)) {
DO_NOT_USE_THIS_KERNEL(params.layerID);
}
- if (input_size.first != 0 && fc_input.is_dynamic()) {
- if (input_size.first != 1) {
- DO_NOT_USE_THIS_KERNEL(params.layerID);
- }
- if ((wl != WeightsLayout::os_is_yx_osv32_isv2 || wo % 32 != 0) &&
- (wl != WeightsLayout::os_is_yx_osv64_isv2 || wo % 64 != 0) &&
- (wl != WeightsLayout::os_iyx_osv16 || wo % 16 != 0)) {
- DO_NOT_USE_THIS_KERNEL(params.layerID);
- }
+ // GWS[0] = OFM / ofm_per_work_item must be exact and divisible by LWS[0].
+ const size_t ofm_per_work_item = get_ofm_per_work_item(get_gemv_weights_layout(fc_params));
+ if (weights.OFM().v % (ofm_per_work_item * gemv_lws[0]) != 0) {
+ DO_NOT_USE_THIS_KERNEL(params.layerID);
}
if (input.GetLayout() == DataLayout::bfyx) {
@@ -117,17 +159,8 @@ bool FullyConnected_GEMV::Validate(const Params& params) const {
FullyConnected_GEMV::DispatchData FullyConnected_GEMV::SetDefault(const fully_connected_params& params, int /*autoTuneIndex*/, int /*kernel_number*/) const {
auto dispatchData = Parent::SetDefault(params);
- std::vector<size_t> global = {params.weights.OFM().v, 1, 16};
- if (params.weights.GetLayout() == WeightsLayout::os_iyx_osv16) {
- global[0] = params.weights.OFM().v;
- } else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) {
- global[0] = params.weights.OFM().v / 2;
- } else if (params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2) {
- global[0] = params.weights.OFM().v / 4;
- }
-
- dispatchData.gws = global;
- dispatchData.lws = {16, 1, 16};
+ dispatchData.gws = {params.weights.OFM().v / get_ofm_per_work_item(params.weights.GetLayout()), 1, gemv_lws[2]};
+ dispatchData.lws = {gemv_lws[0], gemv_lws[1], gemv_lws[2]};
return dispatchData;
}
@@ -200,42 +233,7 @@ JitConstants FullyConnected_GEMV::GetJitConstants(const fully_connected_params&
KernelsData FullyConnected_GEMV::GetTunedKernelsDataByIndex(const Params& params, const int autoTuneIndex) const {
const auto& fc_params = static_cast<const fully_connected_params&>(params);
- auto output_f = get_output_aligned_bf_size(fc_params, false).second;
-
- WeightsLayout weights_layout = WeightsLayout::os_iyx_osv16;
- if (is_swiglu_fused(fc_params)) {
- weights_layout = WeightsLayout::os_is_yx_osv32_isv2;
- } else if (fc_params.compressed && fc_params.inputs[0].GetDType() == Datatype::F16 &&
- (fc_params.weights.GetLayout() == WeightsLayout::oiyx ||
- fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2) &&
- (fc_params.weights.GetDType() == WeightsType::INT4 ||
- fc_params.weights.GetDType() == WeightsType::UINT4) &&
- is_weight_horizontal(fc_params, output_f)) {
- // Large N + small K case (horizontal weight) to use osv64_isv2
- weights_layout = WeightsLayout::os_is_yx_osv64_isv2;
- } else if (fc_params.compressed && fc_params.inputs[0].GetDType() == Datatype::F16 &&
- (fc_params.weights.GetDType() == WeightsType::INT4 ||
- fc_params.weights.GetDType() == WeightsType::UINT4) &&
- (fc_params.weights.GetLayout() == WeightsLayout::oiyx ||
- fc_params.weights.GetLayout() == WeightsLayout::os_iyx_osv16) &&
- is_weight_vertical(fc_params, output_f)) {
- // Large K + Small N case (vertical weight) to use osv16
- weights_layout = WeightsLayout::os_iyx_osv16;
- } else if (fc_params.compressed &&
- fc_params.inputs[0].GetDType() == Datatype::F16
- // ioyx => os_is_yx_osv32_isv2 is not supported yet
- && (fc_params.weights.GetLayout() == WeightsLayout::oiyx ||
- fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) &&
- (fc_params.weights.GetDType() == WeightsType::INT4 ||
- fc_params.weights.GetDType() == WeightsType::UINT4)) {
- weights_layout = WeightsLayout::os_iyx_osv16;
- }
-
- if ((fc_params.weights.GetLayout() == WeightsLayout::os_iyx_osv16) ||
- (fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv32_isv2) ||
- (fc_params.weights.GetLayout() == WeightsLayout::os_is_yx_osv64_isv2)) {
- weights_layout = fc_params.weights.GetLayout();
- }
+ WeightsLayout weights_layout = get_gemv_weights_layout(fc_params);
KernelsData kernels_data;
kernels_data = GetCommonKernelsData(params,
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp
index 42d9e219763..c5a11976a64 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp
@@ -7771,3 +7771,77 @@ INSTANTIATE_TEST_SUITE_P(smoke,
fully_connected_onednn_int4_alignment::GetTestCaseName);
} // namespace
#endif
+
+// fully_connected_gpu_gemv has no tail handling for a partial OFM tile (GWS[0] = OFM / ofm_per_work_item, LWS[0] = 16).
+// It must be rejected at Validate for an unaligned OFM, also for the shape-agnostic impl built with a dynamic batch.
+TEST_F(fully_connected_gpu_tests, gemv_rejects_unaligned_ofm_with_dynamic_batch) {
+ tests::random_generator rg(GET_SUITE_NAME);
+ auto& engine = get_test_engine();
+ auto supports_immad = engine.get_device_info().supports_immad;
+ const long int ifm_num = 1024;
+ const long int group_size = 128;
+
+ for (long int ofm_num : {1024L, 1000L}) {
+ SCOPED_TRACE("OFM=" + std::to_string(ofm_num));
+ auto input_mem = engine.allocate_memory({{1, ifm_num}, data_types::f16, format::bfyx});
+ auto weights_mem = engine.allocate_memory({{ofm_num, ifm_num}, data_types::i4, format::bfyx});
+ auto scale_mem = engine.allocate_memory({{ofm_num, ifm_num / group_size}, data_types::f16, format::bfyx});
+ auto dcomp_zp_mem = engine.allocate_memory({{1, 1, 1, 1}, data_types::u8, format::bfyx});
+ set_values<int8_t>(dcomp_zp_mem, {8});
+ set_values(input_mem, rg.generate_random_1d<ov::float16>(ifm_num, -1.0f, 1.0f));
+ set_values(weights_mem, rg.generate_random_1d<uint8_t>(ofm_num * ifm_num / 2, 0, 5));
+ set_values(scale_mem, rg.generate_random_1d<ov::float16>(ofm_num * ifm_num / group_size, -2.0f, 2.0f));
+
+ // Fresh topology per network: program build may modify the primitive descriptors.
+ auto make_topology = [&]() {
+ auto fc_prim = fully_connected("fc_prim", input_info("input"), "weights", "", "scale", supports_immad ? "dcomp_zp" : "", data_types::f16, 2, 2);
+ fc_prim.decompression_zero_point_scalar = 8;
+ return topology(input_layout("input", layout{{-1, ifm_num}, data_types::f16, format::bfyx}),
+ data("weights", weights_mem),
+ data("scale", scale_mem),
+ data("dcomp_zp", dcomp_zp_mem),
+ fc_prim);
+ };
+
+ auto make_config = [&](const std::string& forced_kernel) {
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_user_property(ov::hint::dynamic_quantization_group_size(0));
+ if (!forced_kernel.empty()) {
+ config.set_property(
+ ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"fc_prim", {format::bfyx, forced_kernel, impl_types::ocl}}}));
+ }
+ return config;
+ };
+ auto execute = [&](network& net) {
+ net.set_input_data("input", input_mem);
+ return net.execute().at("fc_prim").get_memory();
+ };
+
+ network ref_net(engine, make_topology(), make_config("fully_connected_gpu_bfyx_ref"));
+ auto ref_mem = execute(ref_net);
+
+ auto kernel_name_of = [](network& net) {
+ auto impl = net.get_primitive("fc_prim")->get_impl();
+ return impl ? impl->get_kernel_name() : std::string("<none>");
+ };
+ auto is_gemv = [](const std::string& kernel_name) {
+ return kernel_name.find("fully_connected_gpu_gemv") != std::string::npos;
+ };
+
+ // With a dynamic batch the shape-agnostic impl is selected at build time, before any enqueue, so this checks
+ // the Validate decision.
+ const bool ofm_aligned = ofm_num % 16 == 0;
+ auto gemv_net = std::make_shared<network>(engine, make_topology(), make_config("fully_connected_gpu_gemv"));
+ const auto shape_agnostic_kernel = kernel_name_of(*gemv_net);
+ EXPECT_EQ(is_gemv(shape_agnostic_kernel), ofm_aligned) << "shape-agnostic kernel " << shape_agnostic_kernel;
+
+ auto net = ofm_aligned ? gemv_net : std::make_shared<network>(engine, make_topology(), make_config(""));
+ auto out_mem = execute(*net);
+ const auto kernel_name = kernel_name_of(*net);
+ EXPECT_EQ(is_gemv(kernel_name), ofm_aligned) << "kernel " << kernel_name;
+
+ compare_outputs<ov::float16>(out_mem, ref_mem);
+ }
+}
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp
index b2d2658dca9..c915179ec0c 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/moe_3gemm_gpu_test.cpp
@@ -2300,3 +2300,90 @@ INSTANTIATE_TEST_SUITE_P(smoke,
Moe3GemmU3TestParams{4, 512, 512, 8, 2, 64, 32, false, false, true},
Moe3GemmU3TestParams{40, 512, 512, 4, 2, 128, 32, true, false, true},
Moe3GemmU3TestParams{1, 512, 512, 8, 2, 128, 32, true, true, true}));
+
+// The batched GEMV kernels walk K in chunks of min(group sizes, 128) and run N in blocks of N_BLOCK * SUBGROUP_NUM (32).
+// Per-channel quantization makes the group size equal to K, so an unaligned hidden_size or inter_size has no correct
+// kernel path and would also enqueue a non-uniform mlp_gate_up / mlp_down work-group. Such shapes must fail to compile.
+class moe_3gemm_unsupported_gemv_shape : public ::testing::TestWithParam<std::tuple<size_t, size_t, bool>> {};
+
+TEST_P(moe_3gemm_unsupported_gemv_shape, per_channel_throws) {
+ const auto [hidden_size, inter_size, is_dynamic] = GetParam();
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad) {
+ GTEST_SKIP() << "No immad support";
+ }
+ const int64_t e = 4, h = static_cast<int64_t>(hidden_size), in = static_cast<int64_t>(inter_size), tokens = 4;
+ auto make_data = [&](data_types dt, ov::PartialShape shape) {
+ return engine.allocate_memory(layout{shape, dt, format::bfyx});
+ };
+
+ topology topology;
+ const auto token_dim = is_dynamic ? ov::Dimension::dynamic() : ov::Dimension(tokens);
+ topology.add(input_layout("hidden_states", layout{ov::PartialShape{1, token_dim, h}, data_types::f16, format::bfyx}));
+ topology.add(input_layout("routing_weights", layout{ov::PartialShape{1, token_dim, e}, data_types::f16, format::bfyx}));
+ topology.add(data("w0_weight", make_data(data_types::u8, {e, in, h, 1})));
+ topology.add(data("w0_scale", make_data(data_types::f16, {e, in, 1})));
+ topology.add(data("w0_zp", make_data(data_types::u8, {e, in, 1})));
+ topology.add(data("w1_weight", make_data(data_types::u8, {e, in, h, 1})));
+ topology.add(data("w1_scale", make_data(data_types::f16, {e, in, 1})));
+ topology.add(data("w1_zp", make_data(data_types::u8, {e, in, 1})));
+ topology.add(data("w2_weight", make_data(data_types::u8, {e, h, in, 1})));
+ topology.add(data("w2_scale", make_data(data_types::f16, {e, h, 1})));
+ topology.add(data("w2_zp", make_data(data_types::u8, {e, h, 1})));
+
+ MoERouterFused::Config router_config;
+ router_config.num_expert = e;
+ router_config.top_k = 2;
+ topology.add(moe_router_fused("router", {input_info("routing_weights")}, router_config));
+
+ cldnn::MOECompressed::Config moe_config;
+ moe_config.hidden_size = hidden_size;
+ moe_config.inter_size = inter_size;
+ moe_config.num_expert = e;
+ moe_config.top_k = 2;
+ moe_config.group_size = std::numeric_limits<size_t>::max(); // per-channel
+ moe_config.out_type = data_types::f16;
+ moe_config.has_zp = true;
+ topology.add(moe_3gemm_fused_compressed("moe",
+ {input_info("hidden_states"),
+ input_info("router", 0),
+ input_info("router", 1),
+ input_info("w0_weight"),
+ input_info("w0_scale"),
+ input_info("w0_zp"),
+ input_info("w1_weight"),
+ input_info("w1_scale"),
+ input_info("w1_zp"),
+ input_info("w2_weight"),
+ input_info("w2_scale"),
+ input_info("w2_zp")},
+ moe_config));
+
+ auto net_config = get_test_default_config(engine);
+ net_config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ if (is_dynamic) {
+ // The model build succeeds for the dynamic shape case, even though there is no corresponding impl.
+ network net(engine, topology, net_config);
+ auto hidden_states_mem = make_data(data_types::f16, {1, tokens, h});
+ auto routing_weights_mem = make_data(data_types::f16, {1, tokens, e});
+ set_values(hidden_states_mem, std::vector<ov::float16>(tokens * h, ov::float16(0.1f)));
+ std::vector<ov::float16> routing_weights(tokens * e);
+ for (size_t i = 0; i < routing_weights.size(); ++i) {
+ routing_weights[i] = ov::float16(static_cast<float>(i % e));
+ }
+ set_values(routing_weights_mem, routing_weights);
+ net.set_input_data("hidden_states", hidden_states_mem);
+ net.set_input_data("routing_weights", routing_weights_mem);
+ // Must throw an explicit compile failure instead of a silent segfault for unsupported shapes.
+ OV_EXPECT_THROW(net.execute(), ov::Exception, ::testing::HasSubstr("MoE GEMV kernel"));
+ } else {
+ OV_EXPECT_THROW(network net(engine, topology, net_config), ov::Exception, ::testing::HasSubstr("MoE GEMV kernel"));
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke,
+ moe_3gemm_unsupported_gemv_shape,
+ ::testing::Values(std::make_tuple(size_t{128}, size_t{1000}, false), // mlp_gate_up N = inter_size
+ std::make_tuple(size_t{128}, size_t{1000}, true), // dynamic tokens
+ std::make_tuple(size_t{1000}, size_t{128}, false), // mlp_down N = hidden_size
+ std::make_tuple(size_t{1000}, size_t{128}, true))); // dynamic tokens
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp
index 8d0fcf720a1..eea8cd3c865 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/pa_kv_reorder_gpu_test.cpp
@@ -12,6 +12,7 @@
#include <vector>
#include "graph/impls/ocl/kernel_selector_helper.h" // check_cm_jit_support
+#include "ocl_v2/primitive_ocl_base.hpp"
#include "test_utils.h"
using namespace cldnn;
@@ -432,6 +433,126 @@ TEST(pa_kv_reorder_gpu, copy_between_blocks_single_sequence) {
value_cache_ref[value_offset(0, 0, 0, 0, kv_heads, v_head_size, block_size)]);
}
+// Non-compressed dispatch is gws = {seq, kv_heads, 16}, lws = {1, min(kv_heads, max_wg / 16), 16}.
+// When kv_heads exceeds max_wg / 16 and is not a multiple of it (e.g. 40 heads with max_wg 512),
+// the head axis is dispatched with a non-uniform work-group.
+TEST(pa_kv_reorder_gpu, many_heads_dispatch_is_uniform) {
+ auto& engine = get_test_engine();
+
+ constexpr size_t subgroup_size = 16;
+ const size_t max_heads_per_wg = engine.get_device_info().max_work_group_size / subgroup_size;
+ // 40 heads (Llama-2-13B) for max_wg 512, 80 heads for max_wg 1024.
+ const size_t kv_heads = max_heads_per_wg + max_heads_per_wg / 4;
+
+ constexpr size_t blocks_num = 2;
+ constexpr size_t k_head_size = 16;
+ constexpr size_t v_head_size = 16;
+ constexpr size_t block_size = cldnn::paged_attention::block_size;
+
+ auto key_cache_layout = layout{ov::PartialShape{blocks_num, static_cast<int64_t>(kv_heads), k_head_size, block_size}, data_types::f16, format::bfyx};
+ auto value_cache_layout = layout{ov::PartialShape{blocks_num, static_cast<int64_t>(kv_heads), block_size, v_head_size}, data_types::f16, format::bfyx};
+ auto block_indices_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+ auto block_update_indices_layout = layout{ov::PartialShape{4}, data_types::i32, format::bfyx};
+ auto block_update_indices_begins_layout = layout{ov::PartialShape{2}, data_types::i32, format::bfyx};
+
+ auto key_cache_mem = engine.allocate_memory(key_cache_layout);
+ auto value_cache_mem = engine.allocate_memory(value_cache_layout);
+ auto block_indices_mem = engine.allocate_memory(block_indices_layout);
+ auto block_indices_begins_mem = engine.allocate_memory(block_indices_begins_layout);
+ auto block_update_indices_mem = engine.allocate_memory(block_update_indices_layout);
+ auto block_update_indices_begins_mem = engine.allocate_memory(block_update_indices_begins_layout);
+
+ // Block 0 holds per-head distinct values (exact in fp16), block 1 is filled with a -1 sentinel,
+ // so a head the kernel never visits keeps the sentinel in its destination slots.
+ const auto src_value = [](size_t head, size_t token, size_t elem) {
+ return ov::float16(static_cast<float>(1 + head * 16 + (token + elem) % 16));
+ };
+ std::vector<ov::float16> key_cache_ref(key_cache_mem->count(), ov::float16(-1.0f));
+ std::vector<ov::float16> value_cache_ref(value_cache_mem->count(), ov::float16(-1.0f));
+ for (size_t h = 0; h < kv_heads; h++) {
+ for (size_t t = 0; t < block_size; t++) {
+ for (size_t k = 0; k < k_head_size; k++) {
+ key_cache_ref[key_offset(0, h, k, t, kv_heads, k_head_size, block_size)] = src_value(h, t, k);
+ }
+ for (size_t v = 0; v < v_head_size; v++) {
+ value_cache_ref[value_offset(0, h, t, v, kv_heads, v_head_size, block_size)] = src_value(h, t, v);
+ }
+ }
+ }
+ set_values(key_cache_mem, key_cache_ref);
+ set_values(value_cache_mem, value_cache_ref);
+
+ set_values<int32_t>(block_indices_mem, {0, 1});
+ set_values<int32_t>(block_indices_begins_mem, {0, 2});
+ set_values<int32_t>(block_update_indices_mem,
+ {
+ 0,
+ 17,
+ 15,
+ 16,
+ });
+ set_values<int32_t>(block_update_indices_begins_mem, {0, 2});
+
+ topology topo;
+ topo.add(mutable_data("key_cache", key_cache_mem));
+ topo.add(mutable_data("value_cache", value_cache_mem));
+ topo.add(input_layout("block_indices", block_indices_layout));
+ topo.add(input_layout("block_indices_begins", block_indices_begins_layout));
+ topo.add(input_layout("block_update_indices", block_update_indices_layout));
+ topo.add(input_layout("block_update_indices_begins", block_update_indices_begins_layout));
+
+ auto pa_reorder = pa_kv_reorder("pa_kv_reorder",
+ {input_info("key_cache"),
+ input_info("value_cache"),
+ input_info("block_indices"),
+ input_info("block_indices_begins"),
+ input_info("block_update_indices"),
+ input_info("block_update_indices_begins")});
+ pa_reorder.kv_heads_num = kv_heads;
+ pa_reorder.adjusted_k_head_size = k_head_size;
+ pa_reorder.adjusted_paged_attention_block_size = block_size;
+ pa_reorder.adjusted_v_head_size = v_head_size;
+ pa_reorder.cache_dt = data_types::f16;
+ pa_reorder.is_kv_compressed = false;
+ topo.add(pa_reorder);
+
+ auto config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ auto network = get_network(engine, topo, config, get_test_stream_ptr(), false);
+
+ network->set_input_data("block_indices", block_indices_mem);
+ network->set_input_data("block_indices_begins", block_indices_begins_mem);
+ network->set_input_data("block_update_indices", block_update_indices_mem);
+ network->set_input_data("block_update_indices_begins", block_update_indices_begins_mem);
+ network->execute();
+ network->get_stream().finish();
+
+ auto impl = dynamic_cast<ov::intel_gpu::ocl::PrimitiveImplOCL*>(network->get_primitive("pa_kv_reorder")->get_impl());
+ ASSERT_NE(impl, nullptr);
+ ASSERT_EQ(impl->_stages.size(), 1u);
+ const auto& wgs = impl->_stages[0]->kd.params.workGroups;
+ for (size_t i = 0; i < wgs.global.size(); i++) {
+ ASSERT_NE(wgs.local[i], 0u) << "axis " << i;
+ EXPECT_EQ(wgs.global[i] % wgs.local[i], 0u) << "axis " << i << ": gws=" << wgs.global[i] << " lws=" << wgs.local[i];
+ }
+
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> key_ptr(key_cache_mem, network->get_stream());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> value_ptr(value_cache_mem, network->get_stream());
+
+ // Updates move src slot 0 -> dst slot 17 (block 1, token 1) and src slot 15 -> dst slot 16 (block 1, token 0).
+ for (size_t h = 0; h < kv_heads; h++) {
+ for (size_t k = 0; k < k_head_size; k++) {
+ ASSERT_EQ(key_ptr[key_offset(1, h, k, 1, kv_heads, k_head_size, block_size)], src_value(h, 0, k)) << "head " << h;
+ ASSERT_EQ(key_ptr[key_offset(1, h, k, 0, kv_heads, k_head_size, block_size)], src_value(h, 15, k)) << "head " << h;
+ }
+ for (size_t v = 0; v < v_head_size; v++) {
+ ASSERT_EQ(value_ptr[value_offset(1, h, 1, v, kv_heads, v_head_size, block_size)], src_value(h, 0, v)) << "head " << h;
+ ASSERT_EQ(value_ptr[value_offset(1, h, 0, v, kv_heads, v_head_size, block_size)], src_value(h, 15, v)) << "head " << h;
+ }
+ }
+}
+
TEST(pa_kv_reorder_gpu, updates_are_scoped_per_sequence) {
auto& engine = get_test_engine();