Skip to content

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

[GPU] Decode each GEMM of the fused MoE 3-GEMM with its own weight dtype (#39061)

Cyberpunk121016dd7434ba77
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 5c9523357e0..e461056e217 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
@@ -446,10 +446,12 @@ static void check_gemv_group_size(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 sg = (params.get_device_info().arch >= gpu_arch::xe2) ? 32u : 16u;
- const size_t fake_gs = std::min(gate_up_group_size, size_t{128});
+ 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=",
- gate_up_group_size,
+ fake_gs,
" on this hardware (SUBGROUP_SIZE=",
sg,
"). Minimum supported group_size is ",
@@ -486,21 +488,34 @@ static void add_common_consts(const RuntimeParams& params, JitConstants& jit) {
// auto scale_dt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::SCALE_0)).data_type;
// auto zp_dt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::ZP_0)).data_type;
if (weight_dt == ov::element::u4 || weight_dt == ov::element::i4) {
- jit.make("WEIGHT_COMPRESSEION_DT", 0);
jit.make("MOE_WEI_DT", "uchar");
jit.make("MOE_SCALE_DT", "half");
jit.make("MOE_ZP_DT", "uchar");
} else if (weight_dt == ov::element::u8 || weight_dt == ov::element::i8) {
- jit.make("WEIGHT_COMPRESSEION_DT", 1);
jit.make("MOE_WEI_DT", "uchar");
jit.make("MOE_SCALE_DT", "half");
jit.make("MOE_ZP_DT", "uchar");
} else if (weight_dt == ov::element::f16) {
- jit.make("WEIGHT_COMPRESSEION_DT", 2);
jit.make("MOE_WEI_DT", "half");
jit.make("MOE_SCALE_DT", "half"); // not use
jit.make("MOE_ZP_DT", "half"); // not use
}
+
+ // Per-GEMM weight dtype codes: gate, up and down may differ under NNCF mixed precision, and
+ // each is decoded with its own. Codes: 0=u4/i4, 1=u8/i8, 2=f16.
+ const auto moe_weight_code = [](ov::element::Type dt) -> int {
+ if (dt == ov::element::u4 || dt == ov::element::i4)
+ return 0;
+ if (dt == ov::element::f16)
+ return 2;
+ return 1; // u8/i8 (byte) path
+ };
+ const auto gate_wdt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::WEIGHT_0)).data_type;
+ const auto up_wdt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::WEIGHT_1)).data_type;
+ const auto down_wdt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::WEIGHT_2)).data_type;
+ jit.make("GATE_WEIGHT_DT", moe_weight_code(gate_wdt));
+ jit.make("UP_WEIGHT_DT", moe_weight_code(up_wdt));
+ jit.make("DOWN_WEIGHT_DT", moe_weight_code(down_wdt));
}
class MoE3GemmSwigluMLPGateUp : public KernelGenerator {
@@ -2399,12 +2414,17 @@ public:
// Use the model config to determine ZP presence (symmetric vs asymmetric quantization)
bool has_zp = config.has_zp;
- // All the zero points share one dtype, which may differ from the weight dtype (u3 weights have u8 zp)
- const auto zp_dt = has_zp ? convert_data_type(instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::ZP_0))->get_layout().data_type)
- : dnnl::memory::data_type::undef;
+ // The zero point dtype may differ from the weight dtype (u3 weights have u8 zp) and, in a mixed-dtype layer, from GEMM
+ // to GEMM (u4 gate/up with u8 down), so it is taken per GEMM.
+ const std::array<MOE3GemmInputIndex, 3> zp_inputs{MOE3GemmInputIndex::ZP_0, MOE3GemmInputIndex::ZP_1, MOE3GemmInputIndex::ZP_2};
+ std::array<dnnl::memory::data_type, 3> zp_dts{dnnl::memory::data_type::undef, dnnl::memory::data_type::undef, dnnl::memory::data_type::undef};
+ if (has_zp) {
+ for (size_t i = 0; i < zp_inputs.size(); i++) {
+ zp_dts[i] = convert_data_type(instance.input_memory_ptr(static_cast<size_t>(zp_inputs[i]))->get_layout().data_type);
+ }
+ }
// The zero points are constants, so a shared one is read to the host only once
if (has_zp) {
- const std::array<MOE3GemmInputIndex, 3> zp_inputs{MOE3GemmInputIndex::ZP_0, MOE3GemmInputIndex::ZP_1, MOE3GemmInputIndex::ZP_2};
for (size_t i = 0; i < zp_inputs.size(); i++) {
const auto& zp_mem = instance.input_memory_ptr(static_cast<size_t>(zp_inputs[i]));
if (!_grouped_zp_host_scalars[i] && zp_mem->get_layout().count() == 1) {
@@ -2431,17 +2451,17 @@ public:
// per-expert(0) x per-K-group(1) x per-N-channel(2)
attr.set_scales(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 1) | (1 << 2), {group_size, 1}, dnnl::memory::data_type::f16);
if (has_zp && !zp_host_scalar) {
- attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 1) | (1 << 2), {group_size, 1}, zp_dt);
+ attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 1) | (1 << 2), {group_size, 1}, zp_dts[gemm_idx]);
}
} else {
// per-expert(0) x per-N-channel(2), no K-grouping
attr.set_scales(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 2), {}, dnnl::memory::data_type::f16);
if (has_zp && !zp_host_scalar) {
- attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 2), {}, zp_dt);
+ attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 2), {}, zp_dts[gemm_idx]);
}
}
if (zp_host_scalar) {
- attr.set_host_zero_point(DNNL_ARG_WEIGHTS, zp_dt);
+ attr.set_host_zero_point(DNNL_ARG_WEIGHTS, zp_dts[gemm_idx]);
}
// Grouped src/dst: tokens are grouped by expert along axis-0
@@ -2484,21 +2504,21 @@ public:
}
gk->gate_scale_md = make_quant_md(num_experts, K_gu, _gate_up_group_size, N_gu, dnnl::memory::data_type::f16);
if (has_zp) {
- gk->gate_zp_md = make_quant_md(num_experts, K_gu, _gate_up_group_size, N_gu, zp_dt);
+ gk->gate_zp_md = make_quant_md(num_experts, K_gu, _gate_up_group_size, N_gu, zp_dts[0]);
}
gk->up_pd = make_pd(K_gu, N_gu, _gate_up_group_size, uw_dt, 1, dnnl::post_ops());
gk->up_prim = dnnl::matmul(gk->up_pd);
gk->up_scale_md = gk->gate_scale_md;
if (has_zp) {
- gk->up_zp_md = gk->gate_zp_md;
+ gk->up_zp_md = make_quant_md(num_experts, K_gu, _gate_up_group_size, N_gu, zp_dts[1]);
}
gk->down_pd = make_pd(K_d, N_d, _down_group_size, dw_dt, 2, dnnl::post_ops());
gk->down_prim = dnnl::matmul(gk->down_pd);
gk->down_scale_md = make_quant_md(num_experts, K_d, _down_group_size, N_d, dnnl::memory::data_type::f16);
if (has_zp) {
- gk->down_zp_md = make_quant_md(num_experts, K_d, _down_group_size, N_d, zp_dt);
+ gk->down_zp_md = make_quant_md(num_experts, K_d, _down_group_size, N_d, zp_dts[2]);
}
gk->hint_mem = dnnl::memory(dnnl::memory::desc::host_scalar(dnnl::memory::data_type::s32), int32_t{0});
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.hpp
index 8dafaf1e67c..784a566f36e 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_swiglu_opt.hpp
@@ -41,6 +41,14 @@ struct moe_3gemm_swiglu_opt : public ImplementationManager {
return false;
}
+ using Idx = MOE3GemmInputIndex;
+ const auto dt_of = [&](Idx idx) {
+ return ov::element::Type(node.get_input_layout(static_cast<size_t>(idx)).data_type);
+ };
+ const auto is_signed = [](ov::element::Type dt) {
+ return dt == ov::element::i4 || dt == ov::element::i8;
+ };
+
// Only support weight: u4, i4, u8, i8, u3 (u3 prefill runs on OneDNN grouped GEMM, and so does its decode where the
// u3 batched GEMV doesn't apply)
static constexpr std::array supported_wei_type = {
@@ -50,33 +58,40 @@ struct moe_3gemm_swiglu_opt : public ImplementationManager {
ov::element::i8,
ov::element::u3,
};
- const auto& wei_layout = node.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::WEIGHT_0));
- if (!one_of(wei_layout.data_type, supported_wei_type)) {
- return false;
- }
-
// Only support scale: f16
static constexpr std::array supported_scale_type = {
ov::element::f16,
};
- const auto& scale_layout = node.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::SCALE_0));
- if (!one_of(scale_layout.data_type, supported_scale_type)) {
- return false;
- }
-
// Only support zp: u4, i4, u8, i8 (skip check for symmetric quantization where ZP is element::dynamic placeholder)
+ static constexpr std::array supported_zp_type = {
+ ov::element::u4, // asym-quant type
+ ov::element::i4, // sym-quant type
+ ov::element::u8, // asym-quant type
+ ov::element::i8, // sym-quant type
+ };
const auto& config = std::static_pointer_cast<const moe_3gemm_fused_compressed>(node.get_primitive())->_config;
- if (config.has_zp) {
- static constexpr std::array supported_zp_type = {
- ov::element::u4, // asym-quant type
- ov::element::i4, // sym-quant type
- ov::element::u8, // asym-quant type
- ov::element::i8, // sym-quant type
- };
- const auto& zp_layout = node.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::ZP_0));
- if (!one_of(zp_layout.data_type, supported_zp_type)) {
+
+ static constexpr std::array weights = {Idx::WEIGHT_0, Idx::WEIGHT_1, Idx::WEIGHT_2};
+ static constexpr std::array scales = {Idx::SCALE_0, Idx::SCALE_1, Idx::SCALE_2};
+ static constexpr std::array zps = {Idx::ZP_0, Idx::ZP_1, Idx::ZP_2};
+ for (size_t i = 0; i < weights.size(); ++i) {
+ const auto wei_dt = dt_of(weights[i]);
+ if (!one_of(wei_dt, supported_wei_type) || !one_of(dt_of(scales[i]), supported_scale_type)) {
return false;
}
+ // WEIGHT_IS_SIGNED is derived from WEIGHT_0 and applied to all three GEMMs.
+ if (is_signed(wei_dt) != is_signed(dt_of(weights[0]))) {
+ return false;
+ }
+ if (config.has_zp) {
+ const auto zp_dt = dt_of(zps[i]);
+ // The batched GEMV strides zp by the weight's bit width (MOE_EXPERT_ZP_BYTES), so they must match.
+ // u3 never runs it (OneDNN grouped GEMM) and its zp is u8, so it is exempt.
+ const bool zp_width_ok = wei_dt == ov::element::u3 || zp_dt.bitwidth() == wei_dt.bitwidth();
+ if (!one_of(zp_dt, supported_zp_type) || !zp_width_ok) {
+ return false;
+ }
+ }
}
return true;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_mlp.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_mlp.cl
index d37a45c950a..55c2045b9b2 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_mlp.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_mlp.cl
@@ -65,6 +65,12 @@
# define DEQUANT_8BIT(v) convert_half(v)
#endif
+// Per-expert byte strides for compression code DT (0=u4/i4, 1=u8/i8, 2=f16) and group size GS.
+// N*K is INTERMEDIATE_SIZE*HIDDEN_SIZE for all three GEMMs.
+#define MOE_WEI_PROD (INTERMEDIATE_SIZE * HIDDEN_SIZE)
+#define MOE_EXPERT_WEI_BYTES(DT) ((DT) == 0 ? (MOE_WEI_PROD) / 2 : (MOE_WEI_PROD))
+#define MOE_EXPERT_ZP_BYTES(DT, GS) ((DT) == 0 ? (MOE_WEI_PROD) / 2 / (GS) : (MOE_WEI_PROD) / (GS))
+
#if GATE_UP_ENABLE
inline void gate_up_gemv_n2x_u4(const __global uchar* weight,
__global half* scales,
@@ -450,15 +456,11 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(
bool is_shared = false;
# endif
-# if WEIGHT_COMPRESSEION_DT == 0
- const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2;
- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / GATE_UP_GROUP_SIZE;
- const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2 / GATE_UP_GROUP_SIZE;
-# else
- const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE;
- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / GATE_UP_GROUP_SIZE;
- const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / GATE_UP_GROUP_SIZE;
-# endif
+ const int gate_expert_wei_size = MOE_EXPERT_WEI_BYTES(GATE_WEIGHT_DT);
+ const int up_expert_wei_size = MOE_EXPERT_WEI_BYTES(UP_WEIGHT_DT);
+ const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / GATE_UP_GROUP_SIZE; // f16 scale, dtype-independent
+ const int gate_zp_stride = MOE_EXPERT_ZP_BYTES(GATE_WEIGHT_DT, GATE_UP_GROUP_SIZE);
+ const int up_zp_stride = MOE_EXPERT_ZP_BYTES(UP_WEIGHT_DT, GATE_UP_GROUP_SIZE);
int expert_id = 0;
// gate, [HIDDEN_SIZE, INTERMEDIATE_SIZE]
@@ -473,14 +475,14 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(
if (!is_shared) {
expert_id = expert_list[token_idx * MAX_TOPK + expert_slot];
// gate, [HIDDEN_SIZE, INTERMEDIATE_SIZE]
- gate_weight = (__global MOE_WEI_DT*)(gate_weight_addr + expert_id * expert_wei_size);
+ gate_weight = (__global MOE_WEI_DT*)(gate_weight_addr + expert_id * gate_expert_wei_size);
gate_scale = (__global MOE_SCALE_DT*)(gate_scale_addr + expert_id * expert_scale_size);
- gate_zp = (__global MOE_ZP_DT*)(gate_zp_addr + expert_id * expert_zp_size);
+ gate_zp = (__global MOE_ZP_DT*)(gate_zp_addr + expert_id * gate_zp_stride);
// up, [HIDDEN_SIZE, INTERMEDIATE_SIZE]
- up_weight = (__global MOE_WEI_DT*)(up_weight_addr + expert_id * expert_wei_size);
+ up_weight = (__global MOE_WEI_DT*)(up_weight_addr + expert_id * up_expert_wei_size);
up_scale = (__global MOE_SCALE_DT*)(up_scale_addr + expert_id * expert_scale_size);
- up_zp = (__global MOE_ZP_DT*)(up_zp_addr + expert_id * expert_zp_size);
+ up_zp = (__global MOE_ZP_DT*)(up_zp_addr + expert_id * up_zp_stride);
}
# if SHARED_EXPERT_ENABLE
else {
@@ -512,7 +514,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(
__local float shared_gate_partial[SUBGROUP_NUM]; // one slot per subgroup for scalar gate reduction
# endif
-# if WEIGHT_COMPRESSEION_DT == 0
+# if GATE_WEIGHT_DT == 0
//# interleaving x into x2
int id_sg = get_sub_group_id();
int num_sg = get_num_sub_groups();
@@ -604,14 +606,18 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(
// shared_gate_out[token_idx] is consumed by the next kernel (mlp_down) — no barrier needed here.
# endif
-# if WEIGHT_COMPRESSEION_DT == 0
+# if UP_WEIGHT_DT == 0
gate_up_gemv_n2x_u4(up_weight, up_scale, up_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, false);
- gate_up_gemv_n2x_u4(gate_weight, gate_scale, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, true);
-# elif WEIGHT_COMPRESSEION_DT == 1
+# elif UP_WEIGHT_DT == 1
gate_up_gemv_n2x_u8(up_weight, up_scale, up_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, false);
- gate_up_gemv_n2x_u8(gate_weight, gate_scale, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, true);
-# elif WEIGHT_COMPRESSEION_DT == 2
+# elif UP_WEIGHT_DT == 2
gate_up_gemv_n2x_f16(up_weight, up_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, false);
+# endif
+# if GATE_WEIGHT_DT == 0
+ gate_up_gemv_n2x_u4(gate_weight, gate_scale, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, true);
+# elif GATE_WEIGHT_DT == 1
+ gate_up_gemv_n2x_u8(gate_weight, gate_scale, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, true);
+# elif GATE_WEIGHT_DT == 2
gate_up_gemv_n2x_f16(gate_weight, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, true);
# endif
}
@@ -975,15 +981,9 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const
bool is_shared = false;
# endif
-# if WEIGHT_COMPRESSEION_DT == 0
- const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2;
- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / DOWN_GROUP_SIZE;
- const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2 / DOWN_GROUP_SIZE;
-# else
- const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE;
- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / DOWN_GROUP_SIZE;
- const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / DOWN_GROUP_SIZE;
-# endif
+ const int expert_wei_size = MOE_EXPERT_WEI_BYTES(DOWN_WEIGHT_DT);
+ const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / DOWN_GROUP_SIZE; // f16 scale, dtype-independent
+ const int down_zp_stride = MOE_EXPERT_ZP_BYTES(DOWN_WEIGHT_DT, DOWN_GROUP_SIZE);
int expert_id = 0;
// down, [INTERMEDIATE_SIZE, HIDDEN_SIZE]
@@ -996,7 +996,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const
// down, [INTERMEDIATE_SIZE, HIDDEN_SIZE]
weight = (__global MOE_WEI_DT*)(down_weight_addr + expert_id * expert_wei_size);
scales = (__global MOE_SCALE_DT*)(down_scale_addr + expert_id * expert_scale_size);
- zps = (__global MOE_ZP_DT*)(down_zp_addr + expert_id * expert_zp_size);
+ zps = (__global MOE_ZP_DT*)(down_zp_addr + expert_id * down_zp_stride);
}
# if SHARED_EXPERT_ENABLE
else {
@@ -1023,7 +1023,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const
__local float xg_sum[1]; // unused placeholder for function signature
#endif
-# if WEIGHT_COMPRESSEION_DT == 0
+# if DOWN_WEIGHT_DT == 0
//# interleaving x into x2
int id_sg = get_sub_group_id();
int num_sg = get_num_sub_groups();
@@ -1087,11 +1087,11 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const
MOE_DTYPE routing_weight_val = routing_weights[token_idx * MAX_TOPK + expert_slot];
# endif
-# if WEIGHT_COMPRESSEION_DT == 0
+# if DOWN_WEIGHT_DT == 0
down_gemv_n2x_u4(weight, scales, zps, routing_weight_val, y, N, K, x2, xg_sum);
-# elif WEIGHT_COMPRESSEION_DT == 1
+# elif DOWN_WEIGHT_DT == 1
down_gemv_n2x_u8(weight, scales, zps, routing_weight_val, y, N, K, x2, xg_sum);
-# elif WEIGHT_COMPRESSEION_DT == 2
+# elif DOWN_WEIGHT_DT == 2
down_gemv_n2x_f16(weight, routing_weight_val, y, N, K, x2);
# endif
}
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 027cedc642c..b2d2658dca9 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
@@ -1910,6 +1910,192 @@ TEST(moe_3gemm_expert_mask, shape_predictor_overflow_regression) {
ASSERT_EQ(total, actual_tokens * top_k);
}
+// ============================================================================
+// Mixed weight dtypes across the three expert GEMMs.
+//
+// Each GEMM must be decoded with its own dtype. gate and up still have to agree on whether they are
+// u4, because they read one SLM copy of the activations whose layout the gate dtype picks; down
+// stages its own copy and is free.
+//
+// The reference uses the exact dequantized weights, so the tolerance only has to absorb f16
+// accumulation, and a GEMM decoded with the other dtype's stride is far outside it.
+// ============================================================================
+
+struct MoeMixedDtypeParams {
+ size_t seq_len;
+ bool gate_up_u4;
+ bool down_u4;
+};
+
+class moe_3gemm_compressed_gpu_mixed_dtype : public ::testing::TestWithParam<MoeMixedDtypeParams> {
+public:
+ static std::string GetTestCaseName(const testing::TestParamInfo<MoeMixedDtypeParams>& obj) {
+ const auto& p = obj.param;
+ return std::string(p.gate_up_u4 ? "gate_up_u4" : "gate_up_u8") + (p.down_u4 ? "_down_u4" : "_down_u8") + "_seq" +
+ std::to_string(p.seq_len);
+ }
+};
+
+TEST_P(moe_3gemm_compressed_gpu_mixed_dtype, per_gemm_decode_accuracy) {
+ const auto& param = GetParam();
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad) {
+ GTEST_SKIP() << "No immad support";
+ }
+
+ tests::random_generator rg(GET_SUITE_NAME);
+ Moe3GemmConfig config;
+ config.batch_size = 1;
+ config.seq_len = param.seq_len;
+ config.hidden_size = 128;
+ config.inter_size = 256;
+ config.num_experts = 4;
+ config.top_k = 2;
+ config.group_size = 128;
+ // quantize(), pack() and dequantize() all key on config.is_u4, so each dtype gets its own reference.
+ config.is_u4 = param.gate_up_u4;
+ Moe3GemmReference gate_up_ref(config, rg);
+ config.is_u4 = param.down_u4;
+ Moe3GemmReference down_ref(config, rg);
+
+ auto hidden_states = rg.generate_random_1d<ov::float16>(config.batch_size * config.seq_len * config.hidden_size, -1.0f, 1.0f, 1000);
+ auto routing_weights = rg.generate_random_1d<ov::float16>(config.batch_size * config.seq_len * config.num_experts, 0.0f, 1.0f, 1000);
+
+ auto w0_data = rg.generate_random_1d<float>(config.num_experts * config.hidden_size * config.inter_size, -1.0f, 0.0f, 1000);
+ auto w1_data = rg.generate_random_1d<float>(config.num_experts * config.hidden_size * config.inter_size, -1.0f, 0.0f, 1000);
+ auto w2_data = rg.generate_random_1d<float>(config.num_experts * config.inter_size * config.hidden_size, -1.0f, 0.0f, 1000);
+ for (size_t i = 0; i < config.num_experts * config.hidden_size * config.inter_size; ++i) {
+ w0_data[i] /= 7.0f;
+ w1_data[i] /= 11.0f;
+ w2_data[i] /= 7.0f;
+ }
+ auto [w0_q, w0_scale, w0_zp] = gate_up_ref.quantize(w0_data, config.num_experts, config.hidden_size, config.inter_size, config.group_size);
+ auto [w1_q, w1_scale, w1_zp] = gate_up_ref.quantize(w1_data, config.num_experts, config.hidden_size, config.inter_size, config.group_size);
+ auto [w2_q, w2_scale, w2_zp] = down_ref.quantize(w2_data, config.num_experts, config.inter_size, config.hidden_size, config.group_size);
+ auto w0_qdq = gate_up_ref.dequantize(w0_q, w0_scale, w0_zp, config.num_experts, config.hidden_size, config.inter_size, config.group_size);
+ auto w1_qdq = gate_up_ref.dequantize(w1_q, w1_scale, w1_zp, config.num_experts, config.hidden_size, config.inter_size, config.group_size);
+ auto w2_qdq = down_ref.dequantize(w2_q, w2_scale, w2_zp, config.num_experts, config.inter_size, config.hidden_size, config.group_size);
+
+ const int64_t E = static_cast<int64_t>(config.num_experts);
+ const int64_t group_num = static_cast<int64_t>(config.hidden_size / config.group_size);
+ const int64_t group_num2 = static_cast<int64_t>(config.inter_size / config.group_size);
+
+ auto make_weight = [&](Moe3GemmReference& r, const std::vector<uint8_t>& q, int64_t ofm, int64_t G) {
+ auto dt = r.config.is_u4 ? data_types::u4 : data_types::u8;
+ auto mem = engine.allocate_memory({dt, format::bfyx, {E, ofm, static_cast<int64_t>(config.group_size), G}});
+ set_values(mem, r.pack(q));
+ get_test_stream().finish();
+ return mem;
+ };
+ // Logical [E, ofm, G]; physical [E, G, ofm] via byfx for G>1, plain bfyx for per-channel.
+ auto make_scale = [&](const std::vector<ov::float16>& v, int64_t ofm, int64_t G) {
+ auto fmt = G > 1 ? format::byfx : format::bfyx;
+ auto shape = G > 1 ? ov::PartialShape{E, ofm, G, 1} : ov::PartialShape{E, ofm, 1};
+ auto mem = engine.allocate_memory(layout{shape, data_types::f16, fmt});
+ set_values(mem, v);
+ get_test_stream().finish();
+ return mem;
+ };
+ auto make_zp = [&](Moe3GemmReference& r, const std::vector<uint8_t>& v, int64_t ofm, int64_t G) {
+ auto dt = r.config.is_u4 ? data_types::u4 : data_types::u8;
+ auto fmt = G > 1 ? format::byfx : format::bfyx;
+ auto shape = G > 1 ? ov::PartialShape{E, ofm, G, 1} : ov::PartialShape{E, ofm, 1};
+ auto mem = engine.allocate_memory(layout{shape, dt, fmt});
+ set_values(mem, r.pack(v));
+ get_test_stream().finish();
+ return mem;
+ };
+ auto create_f16_tensor_3d = [&](const std::vector<ov::float16>& values, int64_t d0, int64_t d1, int64_t d2) {
+ auto mem = engine.allocate_memory(layout{ov::PartialShape{d0, d1, d2}, data_types::f16, format::bfyx});
+ set_values(mem, values);
+ get_test_stream().finish();
+ return mem;
+ };
+
+ auto hidden_states_mem = create_f16_tensor_3d(hidden_states, config.batch_size, config.seq_len, config.hidden_size);
+ auto routing_weights_mem = create_f16_tensor_3d(routing_weights, config.batch_size, config.seq_len, config.num_experts);
+
+ const int64_t inter = static_cast<int64_t>(config.inter_size);
+ const int64_t hidden = static_cast<int64_t>(config.hidden_size);
+
+ topology topology;
+ topology.add(input_layout("hidden_states", hidden_states_mem->get_layout()));
+ topology.add(input_layout("routing_weights", routing_weights_mem->get_layout()));
+ topology.add(data("w0_weight", make_weight(gate_up_ref, w0_q, inter, group_num)));
+ topology.add(data("w0_scale", make_scale(w0_scale, inter, group_num)));
+ topology.add(data("w0_zp", make_zp(gate_up_ref, w0_zp, inter, group_num)));
+ topology.add(data("w1_weight", make_weight(gate_up_ref, w1_q, inter, group_num)));
+ topology.add(data("w1_scale", make_scale(w1_scale, inter, group_num)));
+ topology.add(data("w1_zp", make_zp(gate_up_ref, w1_zp, inter, group_num)));
+ topology.add(data("w2_weight", make_weight(down_ref, w2_q, hidden, group_num2)));
+ topology.add(data("w2_scale", make_scale(w2_scale, hidden, group_num2)));
+ topology.add(data("w2_zp", make_zp(down_ref, w2_zp, hidden, group_num2)));
+
+ MoERouterFused::Config router_config;
+ router_config.num_expert = config.num_experts;
+ router_config.top_k = config.top_k;
+ router_config.routing_type = MoERouterFused::RoutingType::SOFTMAX;
+ topology.add(moe_router_fused("router", std::vector<input_info>{input_info("routing_weights")}, router_config));
+
+ cldnn::MOECompressed::Config moe_config;
+ moe_config.hidden_size = config.hidden_size;
+ moe_config.inter_size = config.inter_size;
+ moe_config.num_expert = config.num_experts;
+ moe_config.top_k = config.top_k;
+ moe_config.group_size = config.group_size;
+ moe_config.out_type = data_types::f16;
+ moe_config.has_zp = true;
+
+ std::vector<input_info> moe_inputs{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")};
+ topology.add(moe_3gemm_fused_compressed("moe", moe_inputs, moe_config));
+
+ auto net_config = get_test_default_config(engine);
+ net_config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ network network(engine, topology, net_config);
+ network.set_input_data("hidden_states", hidden_states_mem);
+ network.set_input_data("routing_weights", routing_weights_mem);
+
+ auto outputs = network.execute();
+ auto output_prim = outputs.begin()->second.get_memory();
+ get_test_stream().flush();
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> output_ptr(output_prim, get_test_stream());
+
+ auto ref_output = gate_up_ref.run_reference_softmax(hidden_states, routing_weights, w0_qdq, w1_qdq, w2_qdq);
+ const float tolerance = 0.15f;
+ bool all_zero = true;
+ for (size_t i = 0; i < ref_output.size(); ++i) {
+ if (static_cast<float>(output_ptr[i]) != 0.0f)
+ all_zero = false;
+ ASSERT_NEAR(static_cast<float>(output_ptr[i]), static_cast<float>(ref_output[i]), tolerance) << "i = " << i;
+ }
+ ASSERT_FALSE(all_zero) << "output is all zeros - the MoE stage was silently skipped";
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke,
+ moe_3gemm_compressed_gpu_mixed_dtype,
+ ::testing::Values(
+ // Decode GEMV, single token.
+ MoeMixedDtypeParams{1, false, true},
+ MoeMixedDtypeParams{1, true, false},
+ // Batched GEMV (MTP-style token counts).
+ MoeMixedDtypeParams{4, false, true},
+ MoeMixedDtypeParams{16, true, false},
+ // Just above the 32-token batched-GEMV threshold: the prefill GEMM path.
+ MoeMixedDtypeParams{33, false, true},
+ MoeMixedDtypeParams{33, true, false}),
+ moe_3gemm_compressed_gpu_mixed_dtype::GetTestCaseName);
+
// u3 experts: decode on the moe_3gemm_int3 batched GEMV, prefill on OneDNN grouped GEMM
struct Moe3GemmU3TestParams {
size_t seq_len;