Skip to content

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

[GPU] Fix MoE results with u4 weights and u8 zero points (#39959)

Andrew Kwangwoong Park71c68e795f9e
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/expert_gemm_compute.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/expert_gemm_compute.cl
index 8d8212ed623..6ebdc8697dd 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/expert_gemm_compute.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/expert_gemm_compute.cl
@@ -61,7 +61,7 @@ inline bool expert_gemm_compute(
#ifdef WEIGHT_COMPRESSED_INT4
weight_scales += expert_id * m_gemm * NUM_GROUPS;
# ifdef WEIGHT_ZP_DT
-# ifdef WEIGHT_COMPRESSED_ZP_INT4
+# if WEIGHT_COMPRESSED_ZP_INT4
weight_zps += expert_id * m_gemm * NUM_GROUPS / 2;
# else
weight_zps += expert_id * m_gemm * NUM_GROUPS;
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul.cpp
index 72bb3095338..f61ea33e0b9 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul.cpp
@@ -301,6 +301,8 @@ private:
auto a_dt = convert_data_type(input_layout.data_type);
auto out_dt = convert_data_type(output_layout.data_type);
auto w_dt = convert_data_type(weights_layout.data_type);
+ // Zero points can be wider than the weights (e.g. u4 weights with u8 zero points).
+ auto zp_dt = desc->has_zp ? convert_data_type(impl_params.input_layouts[gather_matmul::BGMInputIdx::WEIGHT_ZP].data_type) : w_dt;
const dnnl::memory::dim num_experts = static_cast<dnnl::memory::dim>(wshape[0]);
const dnnl::memory::dim N = static_cast<dnnl::memory::dim>(wshape[1]);
@@ -343,13 +345,13 @@ private:
// per-expert(0) × per-K-group(1) × per-N-channel(2)
attr.set_scales(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 1) | (1 << 2), {weight_group_size, 1}, dnnl::memory::data_type::f16);
if (desc->has_zp) {
- attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 1) | (1 << 2), {weight_group_size, 1}, w_dt);
+ attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 1) | (1 << 2), {weight_group_size, 1}, zp_dt);
}
} else {
// per-expert(0) × per-N-channel(2), no K-grouping
attr.set_scales(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 2), {}, dnnl::memory::data_type::f16);
if (desc->has_zp) {
- attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 2), {}, w_dt);
+ attr.set_zero_points(DNNL_ARG_WEIGHTS, (1 << 0) | (1 << 2), {}, zp_dt);
}
}
}
@@ -384,12 +386,12 @@ private:
const dnnl::memory::dim num_k_groups = K / weight_group_size;
gk->scale_md = dnnl::memory::desc({num_experts, num_k_groups, N}, s_dt, dnnl::memory::format_tag::abc);
if (gk->has_zp) {
- gk->zp_md = dnnl::memory::desc({num_experts, num_k_groups, N}, w_dt, dnnl::memory::format_tag::abc);
+ gk->zp_md = dnnl::memory::desc({num_experts, num_k_groups, N}, zp_dt, dnnl::memory::format_tag::abc);
}
} else {
gk->scale_md = dnnl::memory::desc({num_experts, N}, s_dt, dnnl::memory::format_tag::ab);
if (gk->has_zp) {
- gk->zp_md = dnnl::memory::desc({num_experts, N}, w_dt, dnnl::memory::format_tag::ab);
+ gk->zp_md = dnnl::memory::desc({num_experts, N}, zp_dt, dnnl::memory::format_tag::ab);
}
}
}
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp
index 4705049423f..3345585c499 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gather_matmul_gen_micro.cpp
@@ -138,7 +138,8 @@ JitConstants GatherMatmulMicroGenerator::get_jit_constants(const kernel_impl_par
jit.make("WEIGHT_COMPRESSED_ZP_INT4", 1);
jit.make("WEIGHT_ZP_DT", to_ocl_type(data_types::u8));
} else {
- jit.make("WEIGHT_ZP_DT", to_ocl_type(data_types::f16));
+ // Must match the micro-GEMM ZP type: expert_gemm_compute.cl strides the per-expert ZP pointer with it.
+ jit.make("WEIGHT_ZP_DT", to_ocl_type(zp_layout.data_type));
}
}
} else {
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp
index 4c7789c9f08..553e842ddb8 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/gather_matmul/gathermatmul_batched_gemm_gen.cpp
@@ -92,7 +92,8 @@ JitConstants GatherMatmulBatchedGemmGenerator::get_jit_constants(const kernel_im
jit.make("WEIGHT_COMPRESSED_ZP_INT4", 1);
jit.make("WEIGHT_ZP_DT", to_ocl_type(data_types::u8));
} else {
- jit.make("WEIGHT_ZP_DT", to_ocl_type(data_types::f16));
+ // Must match the micro-GEMM ZP type: expert_gemm_compute.cl strides the per-expert ZP pointer with it.
+ jit.make("WEIGHT_ZP_DT", to_ocl_type(zp_layout.data_type));
}
}
} else {
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 d75eee5b04a..cdd029208d5 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
@@ -551,6 +551,18 @@ void add_common_consts(const RuntimeParams& params, JitConstants& jit) {
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));
+
+ // Zero points are packed only when they are 4-bit themselves: GGUF Q4_1/Q4_K pair u4 weights with u8 zero points.
+ const auto zp_int4 = [&](MOE3GemmInputIndex zp_idx) {
+ if (!desc->_config.has_zp) {
+ return false;
+ }
+ const auto zp_dt = params.get_input_layout(static_cast<size_t>(zp_idx)).data_type;
+ return zp_dt == ov::element::u4 || zp_dt == ov::element::i4;
+ };
+ jit.make("GATE_ZP_INT4", zp_int4(MOE3GemmInputIndex::ZP_0) ? 1 : 0);
+ jit.make("UP_ZP_INT4", zp_int4(MOE3GemmInputIndex::ZP_1) ? 1 : 0);
+ jit.make("DOWN_ZP_INT4", zp_int4(MOE3GemmInputIndex::ZP_2) ? 1 : 0);
}
class MoE3GemmSwigluMLPGateUp : public KernelGenerator {
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 784a566f36e..e226ef6f05f 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
@@ -85,9 +85,11 @@ struct moe_3gemm_swiglu_opt : public ImplementationManager {
}
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.
+ // The batched GEMV reads 4-bit zero points packed and 8-bit ones unpacked, but only the 4-bit GEMV
+ // takes both: 4-bit weights accept 4-bit or u8 zero points (GGUF Q4_1/Q4_K), 8-bit weights 8-bit ones.
// 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();
+ const bool zp_width_ok =
+ wei_dt == ov::element::u3 || zp_dt.bitwidth() == wei_dt.bitwidth() || (wei_dt.bitwidth() == 4 && zp_dt == ov::element::u8);
if (!one_of(zp_dt, supported_zp_type) || !zp_width_ok) {
return false;
}
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 55c2045b9b2..993a6ff3e9f 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
@@ -69,7 +69,8 @@
// 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))
+// Zero points follow their own dtype (ZP_INT4: packed u4/i4), e.g. GGUF Q4_1/Q4_K pair u4 weights with u8 zero points.
+#define MOE_EXPERT_ZP_BYTES(ZP_INT4, GS) ((ZP_INT4) ? (MOE_WEI_PROD) / 2 / (GS) : (MOE_WEI_PROD) / (GS))
#if GATE_UP_ENABLE
inline void gate_up_gemv_n2x_u4(const __global uchar* weight,
@@ -80,7 +81,8 @@ inline void gate_up_gemv_n2x_u4(const __global uchar* weight,
int K,
half* x2,
float* xg_sum,
- const bool silu) {
+ const bool silu,
+ const bool zp_int4) {
int num_sg = get_num_sub_groups();
int id_local = get_sub_group_local_id();
@@ -92,17 +94,23 @@ inline void gate_up_gemv_n2x_u4(const __global uchar* weight,
float sum_all1 = 0;
__global half* S = scales + n;
#if HAS_ZP
- __global uchar* Z = zps + n / 2;
+ __global uchar* Z = zps + (zp_int4 ? n / 2 : n);
#endif
unroll_for(int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {
int scale_offset = (gk * FAKE_GROUP_SIZE / GATE_UP_GROUP_SIZE) * N;
half s0 = S[scale_offset];
half s1 = S[scale_offset + 1];
#if HAS_ZP
- int zp_offset = (gk * FAKE_GROUP_SIZE / GATE_UP_GROUP_SIZE) * N / 2;
- uchar z = Z[zp_offset];
- half z_hf0 = convert_half(z & 0xf);
- half z_hf1 = convert_half(z >> 4);
+ int zp_offset = (gk * FAKE_GROUP_SIZE / GATE_UP_GROUP_SIZE) * N;
+ half z_hf0, z_hf1;
+ if (zp_int4) {
+ uchar z = Z[zp_offset / 2];
+ z_hf0 = convert_half(z & 0xf);
+ z_hf1 = convert_half(z >> 4);
+ } else {
+ z_hf0 = convert_half(Z[zp_offset]);
+ z_hf1 = convert_half(Z[zp_offset + 1]);
+ }
#endif
# if ELEMS_PER_LANE == 4
@@ -459,8 +467,8 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(
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);
+ const int gate_zp_stride = MOE_EXPERT_ZP_BYTES(GATE_ZP_INT4, GATE_UP_GROUP_SIZE);
+ const int up_zp_stride = MOE_EXPERT_ZP_BYTES(UP_ZP_INT4, GATE_UP_GROUP_SIZE);
int expert_id = 0;
// gate, [HIDDEN_SIZE, INTERMEDIATE_SIZE]
@@ -607,14 +615,14 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(
# endif
# 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(up_weight, up_scale, up_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, false, UP_ZP_INT4);
# 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);
# 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);
+ gate_up_gemv_n2x_u4(gate_weight, gate_scale, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, true, GATE_ZP_INT4);
# 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
@@ -632,7 +640,8 @@ inline void down_gemv_n2x_u4(const __global uchar* weight,
int N,
int K,
half* x2,
- float* xg_sum) {
+ float* xg_sum,
+ const bool zp_int4) {
int id_local = get_sub_group_local_id();
int n_start = get_global_id(2) * N_BLOCK;
int n_end = n_start + N_BLOCK;
@@ -641,7 +650,7 @@ inline void down_gemv_n2x_u4(const __global uchar* weight,
const __global uchar* B = weight + n * K / 2;
__global half* S = scales + n;
#if HAS_ZP
- __global uchar* Z = zps + n / 2;
+ __global uchar* Z = zps + (zp_int4 ? n / 2 : n);
#endif
float sum_all0 = 0;
float sum_all1 = 0;
@@ -650,10 +659,16 @@ inline void down_gemv_n2x_u4(const __global uchar* weight,
half s0 = S[scale_offset];
half s1 = S[scale_offset + 1];
#if HAS_ZP
- int zp_offset = (gk * FAKE_GROUP_SIZE / DOWN_GROUP_SIZE) * N / 2;
- ushort z = Z[zp_offset];
- half z_hf0 = convert_half(z & 0xf);
- half z_hf1 = convert_half(z >> 4);
+ int zp_offset = (gk * FAKE_GROUP_SIZE / DOWN_GROUP_SIZE) * N;
+ half z_hf0, z_hf1;
+ if (zp_int4) {
+ ushort z = Z[zp_offset / 2];
+ z_hf0 = convert_half(z & 0xf);
+ z_hf1 = convert_half(z >> 4);
+ } else {
+ z_hf0 = convert_half(Z[zp_offset]);
+ z_hf1 = convert_half(Z[zp_offset + 1]);
+ }
#endif
# if ELEMS_PER_LANE == 4
@@ -983,7 +998,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const
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);
+ const int down_zp_stride = MOE_EXPERT_ZP_BYTES(DOWN_ZP_INT4, DOWN_GROUP_SIZE);
int expert_id = 0;
// down, [INTERMEDIATE_SIZE, HIDDEN_SIZE]
@@ -1088,7 +1103,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const
# endif
# if DOWN_WEIGHT_DT == 0
- down_gemv_n2x_u4(weight, scales, zps, routing_weight_val, y, N, K, x2, xg_sum);
+ down_gemv_n2x_u4(weight, scales, zps, routing_weight_val, y, N, K, x2, xg_sum, DOWN_ZP_INT4);
# elif DOWN_WEIGHT_DT == 1
down_gemv_n2x_u8(weight, scales, zps, routing_weight_val, y, N, K, x2, xg_sum);
# elif DOWN_WEIGHT_DT == 2
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/keep_moe_3gemm_const_precision.cpp b/src/plugins/intel_gpu/src/plugin/transformations/keep_moe_3gemm_const_precision.cpp
index 1ae62e4e698..3a7152ba2aa 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/keep_moe_3gemm_const_precision.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/keep_moe_3gemm_const_precision.cpp
@@ -17,20 +17,21 @@ namespace ov::intel_gpu {
using namespace ov::pass::pattern;
KeepMOE3GemmConstPrecision::KeepMOE3GemmConstPrecision() {
- auto wei_0_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto wei_1_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto wei_2_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto zp_0_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto zp_1_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto zp_2_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
+ auto wei_0_m = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::i4}));
+ auto wei_1_m = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::i4}));
+ auto wei_2_m = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::i4}));
+ // Zero points may be wider than the weights (u8 for GGUF Q4_1/Q4_K) or a dynamic placeholder when symmetric.
+ auto zp_0_m = wrap_type<ov::op::v0::Constant>();
+ auto zp_1_m = wrap_type<ov::op::v0::Constant>();
+ auto zp_2_m = wrap_type<ov::op::v0::Constant>();
// Shared expert weights
- auto sh_gate_wei_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto sh_gate_zp_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto sh_up_wei_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto sh_up_zp_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto sh_down_wei_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
- auto sh_down_zp_m = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));
+ auto sh_gate_wei_m = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::i4}));
+ auto sh_gate_zp_m = wrap_type<ov::op::v0::Constant>();
+ auto sh_up_wei_m = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::i4}));
+ auto sh_up_zp_m = wrap_type<ov::op::v0::Constant>();
+ auto sh_down_wei_m = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::i4}));
+ auto sh_down_zp_m = wrap_type<ov::op::v0::Constant>();
// Without shared expert: 12 inputs [hs, routing, topk, w0, s0, zp0, w1, s1, zp1, w2, s2, zp2]
auto moe_no_shared_m = wrap_type<ov::op::internal::MOECompressed>(
@@ -60,19 +61,30 @@ KeepMOE3GemmConstPrecision::KeepMOE3GemmConstPrecision() {
ov::enable_keep_const_precision(pattern_map.at(pattern_node).get_node_shared_ptr());
}
};
+ // ConvertPrecision unpacks only 4-bit constants, so wider zero points need no protection.
+ auto enable_zp_if_4bit = [&](const std::shared_ptr<Node>& pattern_node) {
+ if (pattern_map.count(pattern_node) == 0) {
+ return;
+ }
+ const auto node = pattern_map.at(pattern_node).get_node_shared_ptr();
+ const auto et = node->get_output_element_type(0);
+ if (et == ov::element::u4 || et == ov::element::i4) {
+ ov::enable_keep_const_precision(node);
+ }
+ };
enable_if_present(wei_0_m);
enable_if_present(wei_1_m);
enable_if_present(wei_2_m);
- enable_if_present(zp_0_m);
- enable_if_present(zp_1_m);
- enable_if_present(zp_2_m);
+ enable_zp_if_4bit(zp_0_m);
+ enable_zp_if_4bit(zp_1_m);
+ enable_zp_if_4bit(zp_2_m);
enable_if_present(sh_gate_wei_m);
- enable_if_present(sh_gate_zp_m);
+ enable_zp_if_4bit(sh_gate_zp_m);
enable_if_present(sh_up_wei_m);
- enable_if_present(sh_up_zp_m);
+ enable_zp_if_4bit(sh_up_zp_m);
enable_if_present(sh_down_wei_m);
- enable_if_present(sh_down_zp_m);
+ enable_zp_if_4bit(sh_down_zp_m);
return true;
};
diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/moe.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/moe.cpp
index ee595c585a7..7ac4d5ba559 100644
--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/moe.cpp
+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/dynamic/moe.cpp
@@ -8,6 +8,11 @@
#include "common_test_utils/node_builders/moe_builders.hpp"
#include "common_test_utils/ov_tensor_utils.hpp"
#include "common_test_utils/test_assertions.hpp"
+#include "openvino/core/graph_util.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/subtract.hpp"
#include "openvino/pass/serialize.hpp"
#include "openvino/runtime/intel_gpu/properties.hpp"
#include "shared_test_classes/base/ov_subgraph.hpp"
@@ -713,4 +718,160 @@ INSTANTIATE_TEST_SUITE_P(smoke_MoE3GemmUncompressed,
::testing::Values(false)), // use_weight_decompression
MoECompressedFusionTest::getTestCaseName);
+void set_env_var(const char* name, const char* value) {
+#ifdef _WIN32
+ _putenv_s(name, value);
+#else
+ ::setenv(name, value, 1);
+#endif
+}
+
+void unset_env_var(const char* name) {
+#ifdef _WIN32
+ _putenv_s(name, "");
+#else
+ ::unsetenv(name);
+#endif
+}
+
+// GGUF Q4_1/Q4_K store integer zero points as u8 next to u4 weights; kernels must not assume packed u4 zero points.
+// opt_in_prefill selects the non-default prefill kernels: fused MoE micro-GEMM (Xe2+) or GatherMatmul oneDNN grouped matmul.
+class MoECompressedZeroPointTest : public MoECompressedFusionTest {
+protected:
+ MoECompressedZeroPointTest(ov::element::Type zero_point_type, bool opt_in_prefill) : zero_point_type(zero_point_type), opt_in_prefill(opt_in_prefill) {}
+
+ void SetUp() override {
+ MoECompressedFusionTest::SetUp();
+ if (IsSkipped() || HasFailure()) {
+ return;
+ }
+ if (opt_in_prefill) {
+ const bool force_gather_matmul = std::get<11>(GetParam());
+ prefill_env_var = force_gather_matmul ? "GATHER_MATMUL_USE_ONEDNN_PREFILL" : "OV_GPU_MOE_USE_GROUPED_GEMM_PREFILL";
+ set_env_var(prefill_env_var, force_gather_matmul ? "1" : "0");
+ }
+ if (zero_point_type != ov::element::u8) {
+ return;
+ }
+ size_t widened_zero_points = 0;
+ for (const auto& op : function->get_ordered_ops()) {
+ if (!ov::is_type<ov::op::v1::Subtract>(op)) {
+ continue;
+ }
+ auto zp_node = op->get_input_node_shared_ptr(1);
+ while (ov::is_type<ov::op::v1::Reshape>(zp_node) || ov::is_type<ov::op::v0::Convert>(zp_node)) {
+ zp_node = zp_node->get_input_node_shared_ptr(0);
+ }
+ const auto zp = ov::as_type_ptr<ov::op::v0::Constant>(zp_node);
+ if (zp && zp->get_element_type() == ov::element::u4) {
+ ov::replace_node(zp, std::make_shared<ov::op::v0::Constant>(ov::element::u8, zp->get_shape(), zp->cast_vector<uint8_t>()));
+ ++widened_zero_points;
+ }
+ }
+ ASSERT_GT(widened_zero_points, 0u) << "No u4 zero point found to convert to u8";
+ }
+
+ void TearDown() override {
+ MoECompressedFusionTest::TearDown();
+ if (prefill_env_var) {
+ unset_env_var(prefill_env_var);
+ }
+ }
+
+private:
+ ov::element::Type zero_point_type;
+ bool opt_in_prefill;
+ const char* prefill_env_var = nullptr;
+};
+
+class MoECompressedU8ZeroPointTest : public MoECompressedZeroPointTest {
+public:
+ MoECompressedU8ZeroPointTest() : MoECompressedZeroPointTest(ov::element::u8, false) {}
+};
+
+class MoECompressedU4ZeroPointOptInPrefillTest : public MoECompressedZeroPointTest {
+public:
+ MoECompressedU4ZeroPointOptInPrefillTest() : MoECompressedZeroPointTest(ov::element::u4, true) {}
+};
+
+class MoECompressedU8ZeroPointOptInPrefillTest : public MoECompressedZeroPointTest {
+public:
+ MoECompressedU8ZeroPointOptInPrefillTest() : MoECompressedZeroPointTest(ov::element::u8, true) {}
+};
+
+TEST_P(MoECompressedU8ZeroPointTest, Inference) {
+ run();
+}
+
+TEST_P(MoECompressedU4ZeroPointOptInPrefillTest, Inference) {
+ run();
+}
+
+TEST_P(MoECompressedU8ZeroPointOptInPrefillTest, Inference) {
+ run();
+}
+
+// Token counts cover the fused batched GEMV and the GatherMatmul single-token, multi-token and batched kernels.
+INSTANTIATE_TEST_SUITE_P(smoke_MoECompressedU8ZeroPoint,
+ MoECompressedU8ZeroPointTest,
+ ::testing::Combine(::testing::ValuesIn(moe_params_smoke),
+ ::testing::Values(MoePatternType::GEMM3, MoePatternType::GEMM2),
+ ::testing::Values(MoERoutingType::SOFTMAX),
+ ::testing::Values(ov::element::u4), // weights_precision
+ ::testing::Values(ov::element::f16), // decompression_precision
+ ::testing::Values(ov::element::f16), // scale_precision
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(true), // reshape_on_decompression
+ ::testing::Values(128),
+ ::testing::Values(size_t{0}), // gate_idx
+ ::testing::Values(false, true), // force_gather_matmul
+ ::testing::Values(MoEActivationType::SWISH),
+ ::testing::Values(false), // use_per_expert_scale
+ ::testing::Values(false), // use_layernorm_multiply
+ ::testing::Values(true)), // use_weight_decompression
+ MoECompressedU8ZeroPointTest::getTestCaseName);
+
+// Prefill above the fused batched GEMV (32 tokens) and the GatherMatmul batched kernel (16 tokens) thresholds.
+const std::vector<MoeTestShapeParams> moe_params_prefill = {
+ {
+ {{-1, -1, 256}, {{1, 40, 256}, {1, 64, 256}, {1, 1, 256}}}, // prefill + decode
+ 4, // topk
+ 8, // number_of_experts
+ 512 // intermediate_size
+ },
+};
+
+const auto moe_zero_point_prefill_params = ::testing::Combine(::testing::ValuesIn(moe_params_prefill),
+ ::testing::Values(MoePatternType::GEMM3),
+ ::testing::Values(MoERoutingType::SOFTMAX),
+ ::testing::Values(ov::element::u4), // weights_precision
+ ::testing::Values(ov::element::f16), // decompression_precision
+ ::testing::Values(ov::element::f16), // scale_precision
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(ov::test::utils::DecompressionType::full),
+ ::testing::Values(true), // reshape_on_decompression
+ ::testing::Values(128),
+ ::testing::Values(size_t{0}), // gate_idx unused for GEMM3
+ ::testing::Values(false, true), // force_gather_matmul
+ ::testing::Values(MoEActivationType::SWISH),
+ ::testing::Values(false), // use_per_expert_scale
+ ::testing::Values(false), // use_layernorm_multiply
+ ::testing::Values(true)); // use_weight_decompression
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoECompressedU8ZeroPointPrefill,
+ MoECompressedU8ZeroPointTest,
+ moe_zero_point_prefill_params,
+ MoECompressedU8ZeroPointTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoECompressedU4ZeroPointOptInPrefill,
+ MoECompressedU4ZeroPointOptInPrefillTest,
+ moe_zero_point_prefill_params,
+ MoECompressedU4ZeroPointOptInPrefillTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(smoke_MoECompressedU8ZeroPointOptInPrefill,
+ MoECompressedU8ZeroPointOptInPrefillTest,
+ moe_zero_point_prefill_params,
+ MoECompressedU8ZeroPointOptInPrefillTest::getTestCaseName);
+
} // namespace
diff --git a/src/plugins/intel_gpu/tests/unit/sources.cmake b/src/plugins/intel_gpu/tests/unit/sources.cmake
index 7255d491d94..9fe3e29bdcb 100644
--- a/src/plugins/intel_gpu/tests/unit/sources.cmake
+++ b/src/plugins/intel_gpu/tests/unit/sources.cmake
@@ -352,6 +352,7 @@ set(GPU_UNIT_TESTS_SRCS
${CMAKE_CURRENT_LIST_DIR}/transformations/increase_precision_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/indirect_kv_cache_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/keep_gqa_kv_scale_precision_test.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/transformations/keep_moe_3gemm_const_precision_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/keep_xattention_threshold_precision_test.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/kv_cache_compression.cpp
${CMAKE_CURRENT_LIST_DIR}/transformations/kv_cache_fusion_test.cpp
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/keep_moe_3gemm_const_precision_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/keep_moe_3gemm_const_precision_test.cpp
new file mode 100644
index 00000000000..54408a2d4a6
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/unit/transformations/keep_moe_3gemm_const_precision_test.cpp
@@ -0,0 +1,124 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "plugin/transformations/keep_moe_3gemm_const_precision.hpp"
+
+#include <gtest/gtest.h>
+
+#include <array>
+
+#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/pass/manager.hpp"
+#include "ov_ops/moe_compressed.hpp"
+#include "transformations/rt_info/keep_const_precision.hpp"
+
+using namespace testing;
+using namespace ov::intel_gpu;
+
+namespace {
+
+using ov::op::internal::MOECompressed;
+
+constexpr size_t num_experts = 2;
+constexpr size_t hidden_size = 64;
+constexpr size_t inter_size = 32;
+constexpr size_t group_size = 32;
+
+// MOECompressed GEMM3 inputs: gate/up/down weights at 3/6/9, their zero points at 5/8/11.
+constexpr std::array<size_t, 3> weight_inputs{3, 6, 9};
+constexpr std::array<size_t, 3> zp_inputs{5, 8, 11};
+
+// An element::dynamic zero-point type builds the symmetric placeholder (has_zp=false).
+std::shared_ptr<ov::Model> make_moe_model(ov::element::Type weight_type, ov::element::Type zp_type, bool mark_weights = false, bool mark_zero_points = false) {
+ const bool has_zp = zp_type != ov::element::dynamic;
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{4, hidden_size});
+ auto routing_weights = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::Shape{4, 1});
+ auto topk_indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::Shape{4, 1});
+
+ ov::OutputVector args{hidden_states, routing_weights, topk_indices};
+ // {ofm, K} of gate, up and down.
+ for (const auto& [ofm, k] : {std::pair{inter_size, hidden_size}, std::pair{inter_size, hidden_size}, std::pair{hidden_size, inter_size}}) {
+ const size_t groups = k / group_size;
+ auto weights = ov::op::v0::Constant::create(weight_type, ov::Shape{num_experts, ofm, groups, group_size}, {1});
+ auto scale = ov::op::v0::Constant::create(ov::element::f16, ov::Shape{num_experts, ofm, groups}, {0.01f});
+ auto zp = has_zp ? ov::op::v0::Constant::create(zp_type, ov::Shape{num_experts, ofm, groups}, {1})
+ : ov::op::v0::Constant::create(ov::element::dynamic, ov::Shape{0}, {});
+ if (mark_weights) {
+ ov::enable_keep_const_precision(weights);
+ }
+ if (mark_zero_points) {
+ ov::enable_keep_const_precision(zp);
+ }
+ args.insert(args.end(), {weights, scale, zp});
+ }
+
+ MOECompressed::Config config;
+ config.expert_type = ov::op::internal::MOE::Expert_type::GEMM3_SWIGLU;
+ config.hidden_size = hidden_size;
+ config.inter_size = inter_size;
+ config.num_expert = num_experts;
+ config.group_size = group_size;
+ config.top_k = 1;
+ config.has_zp = has_zp;
+ config.out_type = ov::element::f16;
+ auto moe = std::make_shared<MOECompressed>(args, config);
+ return std::make_shared<ov::Model>(moe->outputs(), ov::ParameterVector{hidden_states, routing_weights, topk_indices});
+}
+
+std::shared_ptr<ov::Node> moe_input(const std::shared_ptr<ov::Model>& model, size_t index) {
+ return model->get_results().at(0)->get_input_node_shared_ptr(0)->get_input_node_shared_ptr(index);
+}
+
+// The fixture catches a missing mark but not an extra one, so Constants that must stay unmarked are checked explicitly.
+class KeepMOE3GemmConstPrecisionTest : public TransformationTestsF {
+protected:
+ void SetUp() override {
+ TransformationTestsF::SetUp();
+ manager.register_pass<KeepMOE3GemmConstPrecision>();
+ }
+};
+
+void run_pass_and_expect_unmarked(const std::shared_ptr<ov::Model>& model, const std::array<size_t, 3>& inputs) {
+ ov::pass::Manager manager;
+ manager.register_pass<KeepMOE3GemmConstPrecision>();
+ manager.run_passes(model);
+
+ for (const auto index : inputs) {
+ EXPECT_FALSE(ov::is_keep_const_precision(moe_input(model, index))) << "MOECompressed input " << index;
+ }
+}
+
+} // namespace
+
+// GGUF Q4_1/Q4_K import: u4 weights with u8 zero points.
+TEST_F(KeepMOE3GemmConstPrecisionTest, U4WeightsWithU8ZeroPoints) {
+ model = make_moe_model(ov::element::u4, ov::element::u8);
+ model_ref = make_moe_model(ov::element::u4, ov::element::u8, /*mark_weights=*/true);
+}
+
+TEST_F(KeepMOE3GemmConstPrecisionTest, U4WeightsWithU4ZeroPoints) {
+ model = make_moe_model(ov::element::u4, ov::element::u4);
+ model_ref = make_moe_model(ov::element::u4, ov::element::u4, /*mark_weights=*/true, /*mark_zero_points=*/true);
+}
+
+TEST_F(KeepMOE3GemmConstPrecisionTest, SymmetricI4Weights) {
+ model = make_moe_model(ov::element::i4, ov::element::dynamic);
+ model_ref = make_moe_model(ov::element::i4, ov::element::dynamic, /*mark_weights=*/true);
+}
+
+TEST(KeepMOE3GemmConstPrecisionUnmarkedTest, U8ZeroPoints) {
+ run_pass_and_expect_unmarked(make_moe_model(ov::element::u4, ov::element::u8), zp_inputs);
+}
+
+TEST(KeepMOE3GemmConstPrecisionUnmarkedTest, SymmetricZeroPointPlaceholders) {
+ run_pass_and_expect_unmarked(make_moe_model(ov::element::i4, ov::element::dynamic), zp_inputs);
+}
+
+// 8-bit weights are never unpacked, so the pass leaves them alone.
+TEST(KeepMOE3GemmConstPrecisionUnmarkedTest, U8Weights) {
+ run_pass_and_expect_unmarked(make_moe_model(ov::element::u8, ov::element::u8), weight_inputs);
+}