OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Optimize performance for moe with speculative decoding (#35901)
River Lie01939759b2a
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.cppindex fc8b06fa654..c78b2098857 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@@ -86,6 +86,7 @@ protected:jit.make("SOFTMAX_TOPK_ENABLE", 1);jit.make("TOP_K", desc->_config.top_k);jit.make("VALUE_NUM", desc->_config.num_expert);+ jit.make("SHARED_EXPERT_ENABLE", desc->_config.num_shared_expert > 0 ? 1 : 0);jit.make("MOE_DTYPE", params.get_input_layout(0).data_type == ov::element::f16 ? "half" : "float");jit.make("MOE_DTYPE_SIZE", params.get_input_layout(0).data_type == ov::element::f16 ? 2 : 4);return jit;@@ -113,6 +114,7 @@ protected:jit.make("SIGMOID_BIAS_TOPK_ENABLE", 1);jit.make("TOP_K", desc->_config.top_k);jit.make("VALUE_NUM", desc->_config.num_expert);+ jit.make("SHARED_EXPERT_ENABLE", desc->_config.num_shared_expert > 0 ? 1 : 0);jit.make("MOE_DTYPE", params.get_input_layout(0).data_type == ov::element::f16 ? "half" : "float");jit.make("MOE_DTYPE_SIZE", params.get_input_layout(0).data_type == ov::element::f16 ? 2 : 4);return jit;@@ -430,6 +432,22 @@ static void add_common_consts(const RuntimeParams& params, JitConstants& jit) {GPU_DEBUG_TRACE_DETAIL << "[DEBUG] moe_3gemm_swiglu_opt: group_size=" << desc->_config.group_size << ", gate_up_group_size=" << gate_up_group_size<< ", down_group_size=" << down_group_size << std::endl;++ // 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.+ {+ const size_t sg = (info.arch >= gpu_arch::xe2) ? 32u : 16u;+ const size_t fake_gs = std::min(gate_up_group_size, size_t{128});+ OPENVINO_ASSERT(fake_gs >= 2 * sg,+ "MoE GEMV kernel does not support group_size=",+ gate_up_group_size,+ " on this hardware (SUBGROUP_SIZE=",+ sg,+ "). Minimum supported group_size is ",+ 2 * sg,+ ". Use a larger quantization group size.");+ }+jit.make("MAX_TOPK", desc->_config.top_k);jit.make("EXPERT_NUM", desc->_config.num_expert);jit.make("HIDDEN_SIZE", desc->_config.hidden_size);@@ -679,6 +697,7 @@ public:bool use_micro_gemm_prefill = false;bool use_gpu_mask_gen_prefill = false;bool use_grouped_gemm_prefill = false;+ size_t batched_gemv_threshold = 32; // token_num <= threshold uses batched GEMV pathmoe_3gemm_swiglu_opt_impl() : PrimitiveImplOCL(moe_3gemm_swiglu_opt::get_type_info_static()) {}moe_3gemm_swiglu_opt_impl(const program_node& node, const RuntimeParams& params) : moe_3gemm_swiglu_opt_impl() {@@ -738,6 +757,15 @@ public:GPU_DEBUG_TRACE_DETAIL << "[DEBUG] moe_3gemm_swiglu_opt_impl(): use_grouped_gemm_prefill=" << use_grouped_gemm_prefill << std::endl;+ auto batched_gemv_threshold_str = std::getenv("MOE_BATCHED_GEMV_THRESHOLD");+ if (batched_gemv_threshold_str) {+ batched_gemv_threshold = std::stoul(batched_gemv_threshold_str);+ if (batched_gemv_threshold <= 0) {+ batched_gemv_threshold = 1;+ }+ GPU_DEBUG_TRACE_DETAIL << "MOE_BATCHED_GEMV_THRESHOLD = " << batched_gemv_threshold << std::endl;+ }+// Don't change the order of stagesauto routing_type = node.as<moe_3gemm_fused_compressed>().get_primitive()->_config.routing_type;if (routing_type == ov::op::internal::MOECompressed::RoutingType::SOFTMAX) {@@ -1019,6 +1047,7 @@ public:cur_moe->use_micro_gemm_prefill = use_micro_gemm_prefill;cur_moe->use_gpu_mask_gen_prefill = use_gpu_mask_gen_prefill;cur_moe->use_grouped_gemm_prefill = use_grouped_gemm_prefill;+ cur_moe->batched_gemv_threshold = batched_gemv_threshold;cur_moe->_activation_type = _activation_type;return cur_moe;}@@ -1130,6 +1159,15 @@ public:scratch.moe_fusion_wei_addr.scale[2] = instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::SCALE_2));scratch.moe_fusion_wei_addr.zp[2] = instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::ZP_2));+ // For symmetric quantization (has_zp=false), ZP inputs are element::dynamic placeholders+ // with zero-count layout. Use scale memory as a dummy to avoid null pointer issues.+ const auto& config = instance.get_typed_desc<moe_3gemm_fused_compressed>()->_config;+ if (!config.has_zp) {+ scratch.moe_fusion_wei_addr.zp[0] = scratch.moe_fusion_wei_addr.scale[0];+ scratch.moe_fusion_wei_addr.zp[1] = scratch.moe_fusion_wei_addr.scale[1];+ scratch.moe_fusion_wei_addr.zp[2] = scratch.moe_fusion_wei_addr.scale[2];+ }+// shared expertsize_t dep_count = instance.dependencies().size();if (dep_count >= static_cast<size_t>(MOE3GemmInputIndex::SHARED_GATE_GATE_WEIGHT) + 1) {@@ -1150,6 +1188,13 @@ public:// Scalar Gate - f16scratch.moe_fusion_wei_addr.shared_weight[3] = instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::SHARED_GATE_GATE_WEIGHT));++ // For symmetric quantization, shared expert ZPs are also element::dynamic placeholders+ if (!config.has_zp) {+ scratch.moe_fusion_wei_addr.shared_zp[0] = scratch.moe_fusion_wei_addr.shared_scale[0];+ scratch.moe_fusion_wei_addr.shared_zp[1] = scratch.moe_fusion_wei_addr.shared_scale[1];+ scratch.moe_fusion_wei_addr.shared_zp[2] = scratch.moe_fusion_wei_addr.shared_scale[2];+ }}}@@ -1283,9 +1328,13 @@ public:return std::make_tuple(mem, layout);}- cldnn::event::ptr exec_single_token(const std::vector<cldnn::event::ptr>& events,+ // Batched GEMV path: handles token_num >= 1 with optimized GEMV kernels.+ // Each workgroup processes one (token, expert) pair. Avoids gather/scatter/CPU-sync overhead of prefill paths.+ // Supports shared expert: EXPERTS_PER_TOKEN = MAX_TOPK + 1 when shared expert is enabled.+ cldnn::event::ptr exec_batched_gemv(const std::vector<cldnn::event::ptr>& events,typed_primitive_inst<moe_3gemm_fused_compressed>& instance,- scratch_buffers& scratch) {+ scratch_buffers& scratch,+ size_t token_num) {auto cur_moe = instance.get_typed_desc<moe_3gemm_fused_compressed>();int max_topk = static_cast<int>(cur_moe->_config.top_k);@@ -1339,8 +1388,12 @@ public:scratch.moe_fusion_wei_addr.shared_scale[2],scratch.moe_fusion_wei_addr.shared_zp[2]};}++ GPU_DEBUG_TRACE_DETAIL << "\nexec_batched_gemv(): token_num=" << token_num << ", max_topk=" << max_topk << ", has_shared=" << _has_shared_expert+ << std::endl;+{- // scratch.up = up(x) * silu(gate(x))+ // scratch.up = up(x) * silu(gate(x)) for all (token, expert) pairsstd::vector<memory::ptr> args_gate_up ={batch_mem_ptr, mlp_gate_wei_mem, mlp_gate_scale_mem, mlp_gate_zp_mem, mlp_up_wei_mem, mlp_up_scale_mem, mlp_up_zp_mem};if (_has_shared_expert)@@ -1352,10 +1405,10 @@ public:*stage_gate_up,args_gate_up,{scratch.up},- {compute_experts, subgroup_size, static_cast<size_t>(_intermediate_size / N_BLOCK)},+ {token_num * compute_experts, subgroup_size, static_cast<size_t>(_intermediate_size / N_BLOCK)},{1, subgroup_size, SUBGROUP_NUM});- // scratch.y = down(scratch.up) * weight[expert_no]+ // scratch.y = down(scratch.up) * routing_weight for all (token, expert) pairsstd::vector<memory::ptr> args_down = {batch_mem_ptr, mlp_down_wei_mem, mlp_down_scale_mem, mlp_down_zp_mem};if (_has_shared_expert)args_down.insert(args_down.end(), extra_args_down.begin(), extra_args_down.end());@@ -1366,16 +1419,16 @@ public:*stage_down,args_down,{scratch.y},- {compute_experts, subgroup_size, static_cast<size_t>(_hidden_size / N_BLOCK)},+ {token_num * compute_experts, subgroup_size, static_cast<size_t>(_hidden_size / N_BLOCK)},{1, subgroup_size, SUBGROUP_NUM});- // final = sum(scratch.y)+ // Per-token reduction: final[t] = sum(scratch.y[t * REDUCE_COUNT .. (t+1) * REDUCE_COUNT - 1])ret = execute_stage({ret_event},instance,*stage_reduce,{scratch.y},{final_hidden_states_mem_ptr},- {static_cast<size_t>(1), static_cast<size_t>(_hidden_size)},+ {token_num, static_cast<size_t>(_hidden_size)},{1, std::min(max_work_group_size, size_t{1024})},instance.needs_completion_event());}@@ -2261,10 +2314,10 @@ public:OPENVINO_THROW("Unsupported routing type ", static_cast<int>(config.routing_type));}- // Single token is a special case, we don't need to do gather/scatter,- // and we can apply optimal kernels against memory bound to improve performance.- if (token_num == 1) {- return exec_single_token({topk_event}, instance, scratch);+ // Batched GEMV: for small token counts (including single token, MTP/speculative decoding),+ // use optimized GEMV kernels with batch dimension. Avoids gather/scatter overhead.+ if (token_num <= batched_gemv_threshold) {+ return exec_batched_gemv({topk_event}, instance, scratch, token_num);}auto final_hidden_states_mem_ptr = instance.output_memory_ptr(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.hppindex 6cb7f5884d4..e527bdb4855 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@@ -9,6 +9,7 @@#include "intel_gpu/primitives/activation.hpp"#include "intel_gpu/primitives/eltwise.hpp"+#include "intel_gpu/primitives/moe_3gemm_fused_compressed.hpp"#include "moe_3gemm_base.hpp"#include "program_node.h"#include "registry/implementation_manager.hpp"@@ -61,16 +62,19 @@ struct moe_3gemm_swiglu_opt : public ImplementationManager {return false;}- // Only support zp: u4, i4, u8, i8- 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)) {- return false;+ // Only support zp: u4, i4, u8, i8 (skip check for symmetric quantization where ZP is element::dynamic placeholder)+ 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)) {+ return false;+ }}return true;diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.clindex e533f4b3c14..58ad0fd52da 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl@@ -62,7 +62,9 @@ KERNEL(softmax_topk)(softmax_total += local_output[i];}output_index += batch * TOP_K;- output += batch * TOP_K;+ // Use EXPERTS_PER_TOKEN stride so batched GEMV reads multi-token weights correctly.+ // For shared expert: EXPERTS_PER_TOKEN=TOP_K+1. For non-shared: TOP_K (no change).+ output += batch * (TOP_K + SHARED_EXPERT_ENABLE);for(uint i = 0; i < TOP_K; i++) {output[i] = local_output[i]/softmax_total;@@ -146,7 +148,7 @@ KERNEL(sigmoid_bias_topk)(sum_weights += (float)eps_ptr[0]; // epsilon to avoid division by zerooutput_index += batch * TOP_K;- output += batch * TOP_K;+ output += batch * (TOP_K + SHARED_EXPERT_ENABLE);for(uint i = 0; i < actual_topk; i++) {output[i] = (MOE_DTYPE)((float)local_output[i] / sum_weights);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.clindex f6138b8125d..b7d526eb891 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@@ -35,6 +35,14 @@// 4 (e.g. SG=32 + FAKE=128 or SG=16 + FAKE=64), and 8 (SG=16 + FAKE=128).#define ELEMS_PER_LANE (FAKE_GROUP_SIZE / SUBGROUP_SIZE)+// Experts per token: MAX_TOPK for non-shared, MAX_TOPK+1 with shared expert.+// Used by batched GEMV to decompose flat workgroup ID into (token_idx, expert_slot).+#if SHARED_EXPERT_ENABLE+#define EXPERTS_PER_TOKEN (MAX_TOPK + 1)+#else+#define EXPERTS_PER_TOKEN MAX_TOPK+#endif+// Gate activation: SwiGLU (Swish, default), GeGLU-Tanh, or GeGLU-ERF.// GATE_ACT_GELU_ERF takes precedence over GATE_ACT_GELU_TANH when both are set.#ifdef GATE_ACT_GELU_ERF@@ -448,15 +456,19 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(const __global half* shared_gate_gate_weight, // [HIDDEN_SIZE] (assuming no scale/zp for now, or pre-dequantized)__global MOE_DTYPE* routing_weights, // Input routing weights, will append shared gate result at end# endif- __global MOE_DTYPE* x, // [1, HIDDEN_SIZE]- __global MOE_DTYPE* y) { // [MAX_TOPK (+1 if shared), INTERMEDIATE_SIZE]- // global: [expert, SUBGROUP_SIZE, N//N_BLOCK],[1, SUBGROUP_SIZE, SUBGROUP_NUM]- int expert_no = get_global_id(0);- y += expert_no * INTERMEDIATE_SIZE;+ __global MOE_DTYPE* x, // [token_num, HIDDEN_SIZE]+ __global MOE_DTYPE* y) { // [token_num * EXPERTS_PER_TOKEN, INTERMEDIATE_SIZE]+ // global: [token_num*EXPERTS_PER_TOKEN, SUBGROUP_SIZE, N//N_BLOCK],[1, SUBGROUP_SIZE, SUBGROUP_NUM]+ // Batched GEMV: each workgroup handles one (token, expert) pair.+ // For single token (token_num=1), flat_id == expert_slot and token_idx == 0.+ int flat_id = get_global_id(0);+ int token_idx = flat_id / EXPERTS_PER_TOKEN;+ int expert_slot = flat_id % EXPERTS_PER_TOKEN;+ y += flat_id * INTERMEDIATE_SIZE;// Check if we are processing the Shared Expert# if SHARED_EXPERT_ENABLE- bool is_shared = (expert_no == MAX_TOPK); // Assuming global size was increased by 1+ bool is_shared = (expert_slot == MAX_TOPK);# elsebool is_shared = false;# endif@@ -471,7 +483,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / GATE_UP_GROUP_SIZE;# endif- int expert_id = expert_list[expert_no];+ int expert_id = 0;// gate, [HIDDEN_SIZE, INTERMEDIATE_SIZE]__global MOE_WEI_DT* gate_weight;__global MOE_SCALE_DT* gate_scale;@@ -482,7 +494,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(__global MOE_ZP_DT* up_zp;if (!is_shared) {- expert_id = expert_list[expert_no];+ 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_scale = (__global MOE_SCALE_DT*)(gate_scale_addr + expert_id * expert_scale_size);@@ -528,7 +540,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(int id_sg = get_sub_group_id();int num_sg = get_num_sub_groups();int id_local = get_sub_group_local_id();- half* px = x + id_sg * FAKE_GROUP_SIZE;+ half* px = x + token_idx * HIDDEN_SIZE + id_sg * FAKE_GROUP_SIZE;half* px2 = x2 + id_sg * FAKE_GROUP_SIZE;unroll_for(int i = id_sg; i < HIDDEN_SIZE / FAKE_GROUP_SIZE; i += num_sg, px += num_sg * FAKE_GROUP_SIZE, px2 += num_sg * FAKE_GROUP_SIZE) {#if HAS_ZP@@ -555,7 +567,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(int id_sg = get_sub_group_id();int num_sg = get_num_sub_groups();int id_local = get_sub_group_local_id();- half* px = x + id_sg * FAKE_GROUP_SIZE;+ half* px = x + token_idx * HIDDEN_SIZE + id_sg * FAKE_GROUP_SIZE;half* px2 = x2 + id_sg * FAKE_GROUP_SIZE;unroll_for(int i = id_sg; i < HIDDEN_SIZE / FAKE_GROUP_SIZE; i += num_sg, px += num_sg * FAKE_GROUP_SIZE, px2 += num_sg * FAKE_GROUP_SIZE) {#if HAS_ZP@@ -591,7 +603,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(int thread_id = id_sg * SUBGROUP_SIZE + id_local;int total_threads = num_sg * SUBGROUP_SIZE;for (int i = thread_id; i < HIDDEN_SIZE; i += total_threads) {- gate_val += (float)x[i] * (float)shared_gate_gate_weight[i];+ gate_val += (float)x[token_idx * HIDDEN_SIZE + i] * (float)shared_gate_gate_weight[i];}// Step 2: intra-subgroup reduction.@@ -608,7 +620,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(float final_val = (id_local < num_sg) ? shared_gate_partial[id_local] : 0.0f;final_val = sub_group_reduce_add(final_val);if (id_local == 0) {- routing_weights[MAX_TOPK] = (MOE_DTYPE)(1.0f / (1.0f + exp(-final_val)));+ routing_weights[token_idx * EXPERTS_PER_TOKEN + MAX_TOPK] = (MOE_DTYPE)(1.0f / (1.0f + exp(-final_val)));}}}@@ -970,16 +982,18 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(constconst __global MOE_SCALE_DT* shared_down_scale,const __global MOE_ZP_DT* shared_down_zp,# endif- const __global MOE_DTYPE* x, // [MAX_TOPK, INTERMEDIATE_SIZE]- __global MOE_DTYPE* routing_weights, // [MAX_TOPK]- __global MOE_DTYPE* y) { // [MAX_TOPK, HIDDEN_SIZE]- // global: [expert, SUBGROUP_SIZE, N//N_BLOCK],[1, SUBGROUP_SIZE, SUBGROUP_NUM]- int expert_no = get_global_id(0);- x += expert_no * INTERMEDIATE_SIZE;- y += expert_no * HIDDEN_SIZE;+ const __global MOE_DTYPE* x, // [token_num * EXPERTS_PER_TOKEN, INTERMEDIATE_SIZE]+ __global MOE_DTYPE* routing_weights, // [token_num * EXPERTS_PER_TOKEN]+ __global MOE_DTYPE* y) { // [token_num * EXPERTS_PER_TOKEN, HIDDEN_SIZE]+ // global: [token_num*EXPERTS_PER_TOKEN, SUBGROUP_SIZE, N//N_BLOCK],[1, SUBGROUP_SIZE, SUBGROUP_NUM]+ int flat_id = get_global_id(0);+ int token_idx = flat_id / EXPERTS_PER_TOKEN;+ int expert_slot = flat_id % EXPERTS_PER_TOKEN;+ x += flat_id * INTERMEDIATE_SIZE;+ y += flat_id * HIDDEN_SIZE;# if SHARED_EXPERT_ENABLE- bool is_shared = (expert_no == MAX_TOPK);+ bool is_shared = (expert_slot == MAX_TOPK);# elsebool is_shared = false;# endif@@ -993,7 +1007,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(constconst int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / DOWN_GROUP_SIZE;const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / DOWN_GROUP_SIZE;# endif- int expert_id = expert_list[expert_no];+ int expert_id = 0;// down, [INTERMEDIATE_SIZE, HIDDEN_SIZE]__global MOE_WEI_DT* weight;@@ -1001,7 +1015,7 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const__global MOE_ZP_DT* zps;if (!is_shared) {- expert_id = expert_list[expert_no];+ expert_id = expert_list[token_idx * MAX_TOPK + expert_slot];// 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);@@ -1098,8 +1112,10 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const}#else-__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_reduce)(const __global MOE_DTYPE* x, // [MAX_TOPK, HIDDEN_SIZE]- __global MOE_DTYPE* y) { // [1, HIDDEN_SIZE]+__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_reduce)(const __global MOE_DTYPE* x, // [token_num * REDUCE_COUNT, HIDDEN_SIZE]+ __global MOE_DTYPE* y) { // [token_num, HIDDEN_SIZE]+ // gws={token_num, HIDDEN_SIZE}, lws={1, min(max_wgs, 1024)}+ int token_idx = get_global_id(0);int n = get_global_id(1);# if SHARED_EXPERT_ENABLE# define REDUCE_COUNT (MAX_TOPK + 1)@@ -1108,11 +1124,11 @@ __attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_reduce)(con# endifhalf sum[REDUCE_COUNT] = {0};__attribute__((opencl_unroll_hint(REDUCE_COUNT))) for (int i = 0; i < REDUCE_COUNT; i++) {- sum[i] = as_half(intel_sub_group_block_read_us((const __global ushort*)(x + i * HIDDEN_SIZE + n)));+ sum[i] = as_half(intel_sub_group_block_read_us((const __global ushort*)(x + (token_idx * REDUCE_COUNT + i) * HIDDEN_SIZE + n)));}for (int i = 1; i < REDUCE_COUNT; i++) {sum[0] += sum[i];}- intel_sub_group_block_write_us((__global ushort*)(y + n), as_ushort(sum[0]));+ intel_sub_group_block_write_us((__global ushort*)(y + token_idx * HIDDEN_SIZE + n), as_ushort(sum[0]));}#endifdiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cppindex 0cb4dc777db..56ed0f607de 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/utils/jitter.cpp@@ -320,6 +320,21 @@ JitConstants make_type_jit_constants(const std::string& name, const ov::element:type_size = "4";is_fp = true;break;+ case ov::element::dynamic:+ type = "uchar";+ max_val = "UCHAR_MAX";+ min_val = "0";+ val_one = "(uchar) 1";+ val_zero = "(uchar) 0";+ to_type = "convert_uchar(v)";+ to_type_sat = "convert_uchar_sat(v)";+ as_type = "as_uchar(v)";+ max_func = "max";+ min_func = "min";+ abs_func = "abs";+ type_size = "1";+ is_fp = false;+ break;default:OPENVINO_THROW("[GPU] Jitter: unsupported data type: ", value);}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.cppindex 112fd3f81d2..c7063eeec79 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@@ -596,6 +596,27 @@ INSTANTIATE_TEST_SUITE_P(smoke_sub128_group_size,Moe3GemmTestParams{1, true, 256, 512, 4, 2, 64},Moe3GemmTestParams{1, false, 256, 512, 4, 2, 64})));+// Batched GEMV: MTP/speculative decoding scenarios where token_num is 2-16.+// These small batch sizes exercise the batched GEMV path (exec_batched_gemv)+// which avoids gather/scatter/CPU-sync overhead of the prefill GEMM path.+INSTANTIATE_TEST_SUITE_P(smoke_batched_gemv_mtp,+ moe_3gemm_compressed_gpu_random,+ ::testing::Combine(::testing::Values(cldnn::MOE3GemmFusedCompressed::RoutingType::SOFTMAX,+ cldnn::MOE3GemmFusedCompressed::RoutingType::SIGMOID_BIAS),+ ::testing::Values(Moe3GemmTestParams{2, true, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{4, true, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{8, true, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{2, false, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{4, false, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{8, false, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{2, true, 256, 512, 4, 2, 256},+ Moe3GemmTestParams{4, true, 256, 512, 4, 2, 256},+ Moe3GemmTestParams{2, false, 256, 512, 4, 2, 256},+ Moe3GemmTestParams{4, false, 256, 512, 4, 2, 256},+ // Sub-128 group_size batched GEMV coverage.+ Moe3GemmTestParams{2, true, 128, 256, 4, 2, 64},+ Moe3GemmTestParams{2, false, 128, 256, 4, 2, 64})));+class moe_3gemm_compressed_gpu_u4 : public ::testing::TestWithParam<cldnn::MOE3GemmFusedCompressed::RoutingType> {};class moe_3gemm_compressed_gpu_shared_random : public ::testing::TestWithParam<Moe3GemmTestParams> {};@@ -865,6 +886,24 @@ INSTANTIATE_TEST_SUITE_P(smoke,Moe3GemmTestParams{1, false, 128, 256, 4, 2, 64},Moe3GemmTestParams{1, true, 256, 512, 4, 2, 64}));+// Batched GEMV with shared expert: MTP/speculative decoding scenarios with shared expert enabled.+// Exercises the batched GEMV path with EXPERTS_PER_TOKEN = MAX_TOPK + 1.+INSTANTIATE_TEST_SUITE_P(smoke_batched_gemv_mtp_shared,+ moe_3gemm_compressed_gpu_shared_random,+ ::testing::Values(Moe3GemmTestParams{2, true, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{4, true, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{8, true, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{2, false, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{4, false, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{8, false, 128, 256, 4, 2, 128},+ Moe3GemmTestParams{2, true, 256, 512, 4, 2, 256},+ Moe3GemmTestParams{4, true, 256, 512, 4, 2, 256},+ Moe3GemmTestParams{2, false, 256, 512, 4, 2, 256},+ Moe3GemmTestParams{4, false, 256, 512, 4, 2, 256},+ // Sub-128 group_size batched GEMV coverage.+ Moe3GemmTestParams{2, true, 128, 256, 4, 2, 64},+ Moe3GemmTestParams{2, false, 128, 256, 4, 2, 64}));+TEST_P(moe_3gemm_compressed_gpu_u4, moe_accuracy_test_u4) {auto routing_type = GetParam();auto& engine = get_test_engine();@@ -1064,7 +1103,6 @@ TEST_P(moe_3gemm_compressed_gpu_symmetric_random, moe_accuracy_test_symmetric) {// Create tensorsauto weight_dt = config.is_u4 ? data_types::i4 : data_types::i8;- auto zp_dt = config.is_u4 ? data_types::u4 : data_types::u8;auto create_weight_tensor = [&](const std::vector<uint8_t>& values, int64_t b, int64_t f, int64_t y, int64_t x) {auto mem = engine.allocate_memory({weight_dt, format::bfyx, {b, f, y, x}});@@ -1073,15 +1111,11 @@ TEST_P(moe_3gemm_compressed_gpu_symmetric_random, moe_accuracy_test_symmetric) {return mem;};- // Symmetric: ZP zero-filled. Layout matches make_scale.- auto create_zero_zp = [&](int64_t E, 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, zp_dt, fmt});- auto lock = cldnn::mem_lock<uint8_t, cldnn::mem_lock_type::write>(mem, get_test_stream());- std::memset(lock.data(), 0, lock.size());- get_test_stream().finish();- return mem;+ // Symmetric: ZP is element::dynamic placeholder (matches real model where the transformation+ // creates Constant(element::dynamic, Shape{0}) for absent ZP in symmetric quantization).+ auto create_dynamic_zp_placeholder = [&]() {+ auto one_byte_mem = engine.allocate_memory(cldnn::layout(ov::PartialShape{1}, data_types::u8, format::bfyx), false);+ return engine.reinterpret_buffer(*one_byte_mem, cldnn::layout(ov::PartialShape{0}, data_types::dynamic, format::bfyx));};auto make_scale_sym = [&](const std::vector<ov::float16>& v, int64_t E, int64_t ofm, int64_t G) {auto fmt = G > 1 ? format::byfx : format::bfyx;@@ -1117,15 +1151,15 @@ TEST_P(moe_3gemm_compressed_gpu_symmetric_random, moe_accuracy_test_symmetric) {auto w0_weight_mem = create_weight_tensor(w0_q_packed, config.num_experts, config.inter_size, config.group_size, group_num);auto w0_scale_mem = make_scale_sym(w0_scale, config.num_experts, config.inter_size, group_num);- auto w0_zp_mem = create_zero_zp(config.num_experts, config.inter_size, group_num);+ auto w0_zp_mem = create_dynamic_zp_placeholder();auto w1_weight_mem = create_weight_tensor(w1_q_packed, config.num_experts, config.inter_size, config.group_size, group_num);auto w1_scale_mem = make_scale_sym(w1_scale, config.num_experts, config.inter_size, group_num);- auto w1_zp_mem = create_zero_zp(config.num_experts, config.inter_size, group_num);+ auto w1_zp_mem = create_dynamic_zp_placeholder();auto w2_weight_mem = create_weight_tensor(w2_q_packed, config.num_experts, config.hidden_size, config.group_size, group_num2);auto w2_scale_mem = make_scale_sym(w2_scale, config.num_experts, config.hidden_size, group_num2);- auto w2_zp_mem = create_zero_zp(config.num_experts, config.hidden_size, group_num2);+ auto w2_zp_mem = create_dynamic_zp_placeholder();// Build topologytopology topology;@@ -1279,7 +1313,6 @@ TEST_P(moe_3gemm_compressed_gpu_shared_symmetric_random, moe_accuracy_test_share// Create tensors with signed weight typesauto weight_dt = config.is_u4 ? data_types::i4 : data_types::i8;- auto zp_dt = config.is_u4 ? data_types::u4 : data_types::u8;auto create_weight_tensor = [&](const std::vector<uint8_t>& values, int64_t b, int64_t f, int64_t y, int64_t x) {auto mem = engine.allocate_memory({weight_dt, format::bfyx, {b, f, y, x}});@@ -1288,14 +1321,10 @@ TEST_P(moe_3gemm_compressed_gpu_shared_symmetric_random, moe_accuracy_test_sharereturn mem;};- auto create_zero_zp = [&](int64_t E, 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, zp_dt, fmt});- auto lock = cldnn::mem_lock<uint8_t, cldnn::mem_lock_type::write>(mem, get_test_stream());- std::memset(lock.data(), 0, lock.size());- get_test_stream().finish();- return mem;+ // Symmetric: ZP is element::dynamic placeholder (matches real model).+ auto create_dynamic_zp_placeholder = [&]() {+ auto one_byte_mem = engine.allocate_memory(cldnn::layout(ov::PartialShape{1}, data_types::u8, format::bfyx), false);+ return engine.reinterpret_buffer(*one_byte_mem, cldnn::layout(ov::PartialShape{0}, data_types::dynamic, format::bfyx));};auto make_scale_sym = [&](const std::vector<ov::float16>& v, int64_t E, int64_t ofm, int64_t G) {auto fmt = G > 1 ? format::byfx : format::bfyx;@@ -1332,28 +1361,28 @@ TEST_P(moe_3gemm_compressed_gpu_shared_symmetric_random, moe_accuracy_test_share// Expert Weightsauto w0_weight_mem = create_weight_tensor(w0_q_packed, config.num_experts, config.inter_size, config.group_size, group_num);auto w0_scale_mem = make_scale_sym(w0_scale, config.num_experts, config.inter_size, group_num);- auto w0_zp_mem = create_zero_zp(config.num_experts, config.inter_size, group_num);+ auto w0_zp_mem = create_dynamic_zp_placeholder();auto w1_weight_mem = create_weight_tensor(w1_q_packed, config.num_experts, config.inter_size, config.group_size, group_num);auto w1_scale_mem = make_scale_sym(w1_scale, config.num_experts, config.inter_size, group_num);- auto w1_zp_mem = create_zero_zp(config.num_experts, config.inter_size, group_num);+ auto w1_zp_mem = create_dynamic_zp_placeholder();auto w2_weight_mem = create_weight_tensor(w2_q_packed, config.num_experts, config.hidden_size, config.group_size, group_num2);auto w2_scale_mem = make_scale_sym(w2_scale, config.num_experts, config.hidden_size, group_num2);- auto w2_zp_mem = create_zero_zp(config.num_experts, config.hidden_size, group_num2);+ auto w2_zp_mem = create_dynamic_zp_placeholder();// Shared Expert Weightsauto s_gate_weight_mem = create_weight_tensor(s_gate_q_packed, 1, config.inter_size, config.group_size, group_num);auto s_gate_scale_mem = make_scale_sym(s_gate_scale, 1, config.inter_size, group_num);- auto s_gate_zp_mem = create_zero_zp(1, config.inter_size, group_num);+ auto s_gate_zp_mem = create_dynamic_zp_placeholder();auto s_up_weight_mem = create_weight_tensor(s_up_q_packed, 1, config.inter_size, config.group_size, group_num);auto s_up_scale_mem = make_scale_sym(s_up_scale, 1, config.inter_size, group_num);- auto s_up_zp_mem = create_zero_zp(1, config.inter_size, group_num);+ auto s_up_zp_mem = create_dynamic_zp_placeholder();auto s_down_weight_mem = create_weight_tensor(s_down_q_packed, 1, config.hidden_size, config.group_size, group_num2);auto s_down_scale_mem = make_scale_sym(s_down_scale, 1, config.hidden_size, group_num2);- auto s_down_zp_mem = create_zero_zp(1, config.hidden_size, group_num2);+ auto s_down_zp_mem = create_dynamic_zp_placeholder();auto s_gate_scalar_mem = create_scalar_gate_tensor(s_gate_scalar_data);@@ -1446,4 +1475,4 @@ INSTANTIATE_TEST_SUITE_P(smoke,// Sub-128 group_size for symmetric shared expert.Moe3GemmTestParams{1, true, 128, 256, 4, 2, 64, true},Moe3GemmTestParams{1, false, 128, 256, 4, 2, 64, true},- Moe3GemmTestParams{1, true, 256, 512, 4, 2, 64, true}));\ No newline at end of file+ Moe3GemmTestParams{1, true, 256, 512, 4, 2, 64, true}));