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][MoE] support qwen3-30b-a3b set group_size = -1 as default group_size value (#32724)
River Lifdcf61cdf265
diff --git a/src/plugins/intel_gpu/include/intel_gpu/op/moe_compressed.hpp b/src/plugins/intel_gpu/include/intel_gpu/op/moe_compressed.hppindex 264c93dc441..046e66a3f84 100644--- a/src/plugins/intel_gpu/include/intel_gpu/op/moe_compressed.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/op/moe_compressed.hpp@@ -22,7 +22,9 @@ public:size_t inter_size = 0;size_t num_expert = 0;size_t top_k = 0;- size_t group_size = 0;+ // numeric_limits<size_t>::max() means per_channel compression (single group).+ // other non-zero value means group compression with this given group_size.+ size_t group_size = 0;// In CB, intermediate shapes are expanded to {SeqLen, 1, HiddenSize}// In Non-CB, intermediate shapes are expanded to {Batch, SeqLen, HiddenSize}size_t has_batch_dim = 0;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 a68f1835396..3330bf54b60 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@@ -424,6 +424,15 @@ static void add_common_consts(const RuntimeParams& params, JitConstants& jit) {auto desc = params.typed_desc<moe_3gemm_fused_compressed>();auto& engine = params.prog->get_engine();const auto& info = engine.get_device_info();+ auto gate_up_group_size = desc->_config.group_size;+ auto down_group_size = desc->_config.group_size;+ if (desc->_config.group_size == std::numeric_limits<size_t>::max()) {+ gate_up_group_size = desc->_config.hidden_size;+ down_group_size = desc->_config.inter_size;+ }++ 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;jit.make("MAX_TOPK", desc->_config.top_k);jit.make("EXPERT_NUM", desc->_config.num_expert);jit.make("HIDDEN_SIZE", desc->_config.hidden_size);@@ -431,7 +440,8 @@ static void add_common_consts(const RuntimeParams& params, JitConstants& jit) {jit.make("N_BLOCK", N_BLOCK);jit.make("SUBGROUP_SIZE", info.arch >= gpu_arch::xe2 ? 32 : 16);jit.make("SUBGROUP_NUM", SUBGROUP_NUM);- jit.make("GROUP_SIZE", desc->_config.group_size);+ jit.make("GATE_UP_GROUP_SIZE", gate_up_group_size);+ jit.make("DOWN_GROUP_SIZE", down_group_size);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);}@@ -576,7 +586,8 @@ public:std::vector<std::vector<dnnl_weights>> _dnnl_weights;int _hidden_size;int _intermediate_size;- int _group_size;+ int _gate_up_group_size;+ int _down_group_size;moe_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() {@@ -593,7 +604,16 @@ public:void init(const std::shared_ptr<const moe_3gemm_fused_compressed>& cur_moe) {_hidden_size = static_cast<int>(cur_moe->_config.hidden_size);_intermediate_size = static_cast<int>(cur_moe->_config.inter_size);- _group_size = static_cast<int>(cur_moe->_config.group_size);+ _gate_up_group_size = static_cast<int>(cur_moe->_config.group_size);+ _down_group_size = static_cast<int>(cur_moe->_config.group_size);++ if (cur_moe->_config.group_size == std::numeric_limits<size_t>::max()) {+ _gate_up_group_size = static_cast<int>(cur_moe->_config.hidden_size);+ _down_group_size = static_cast<int>(cur_moe->_config.inter_size);+ }++ GPU_DEBUG_TRACE_DETAIL << "[DEBUG] moe_3gemm_swiglu_opt prefill: group_size=" << cur_moe->_config.group_size+ << ", gate_up_group_size=" << _gate_up_group_size << ", down_group_size=" << _down_group_size << std::endl;}void init_dnnl_weights(const std::shared_ptr<const moe_3gemm_fused_compressed>& cur_moe,@@ -608,13 +628,13 @@ public:auto& dnnl_weights = _dnnl_weights[j];dnnl_weights.resize(3);dnnl_weights[0].ic = _hidden_size;- dnnl_weights[0].ic_group_size = _group_size;+ dnnl_weights[0].ic_group_size = _gate_up_group_size;dnnl_weights[0].oc = _intermediate_size;dnnl_weights[1].ic = _hidden_size;- dnnl_weights[1].ic_group_size = _group_size;+ dnnl_weights[1].ic_group_size = _gate_up_group_size;dnnl_weights[1].oc = _intermediate_size;dnnl_weights[2].ic = _intermediate_size;- dnnl_weights[2].ic_group_size = _group_size;+ dnnl_weights[2].ic_group_size = _down_group_size;dnnl_weights[2].oc = _hidden_size;for (int i = 0; i < 3; i++) {// weight shape: [ic, oc], type: u4@@ -650,7 +670,8 @@ public:cur_moe->_dnnl_weights = _dnnl_weights;cur_moe->_hidden_size = _hidden_size;cur_moe->_intermediate_size = _intermediate_size;- cur_moe->_group_size = _group_size;+ cur_moe->_gate_up_group_size = _gate_up_group_size;+ cur_moe->_down_group_size = _down_group_size;return cur_moe;}@@ -671,8 +692,9 @@ public:internal_buffers.emplace_back(layout_topk_id, true); // 0: topk_idinternal_buffers.emplace_back(layout_topk_weights, true); // 1: topk_weights// fast single batch: scratch.up = up(x) * silu(gate(x)); scratch.y = down(scratch.up) * weight[expert_no]- layout layout_gateup_out(ov::PartialShape{batch, static_cast<int>(config.inter_size)}, data_type, cldnn::format::bfyx);- layout layout_down_out(ov::PartialShape{batch, static_cast<int>(config.hidden_size)}, data_type, cldnn::format::bfyx);+ auto max_batch = (batch == 1 ? max_topk : batch);+ layout layout_gateup_out(ov::PartialShape{max_batch, static_cast<int>(config.inter_size)}, data_type, cldnn::format::bfyx);+ layout layout_down_out(ov::PartialShape{max_batch, static_cast<int>(config.hidden_size)}, data_type, cldnn::format::bfyx);internal_buffers.emplace_back(layout_gateup_out, true); // 2: upinternal_buffers.emplace_back(layout_down_out, true); // 3: y// onednn: scratch.x, scratch.routing_weights = gather(x, ...)@@ -832,7 +854,9 @@ public:return std::make_tuple(mem, layout);}- cldnn::event::ptr exec_single_batch(typed_primitive_inst<moe_3gemm_fused_compressed>& instance, scratch_buffers& scratch) {+ cldnn::event::ptr exec_single_batch(const std::vector<cldnn::event::ptr>& events,+ typed_primitive_inst<moe_3gemm_fused_compressed>& instance,+ scratch_buffers& scratch) {auto cur_moe = instance.get_typed_desc<moe_3gemm_fused_compressed>();int max_topk = static_cast<int>(cur_moe->_config.top_k);@@ -865,8 +889,8 @@ public:{// scratch.up = up(x) * silu(gate(x))- execute_stage(- {},+ auto ret_event = execute_stage(+ events,instance,*mlp_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, hidden_states_mem_ptr},@@ -875,16 +899,16 @@ public:{1, subgroup_size, SUBGROUP_NUM});// scratch.y = down(scratch.up) * weight[expert_no]- execute_stage({},- instance,- *mlp_down,- {batch_mem_ptr, mlp_down_wei_mem, mlp_down_scale_mem, mlp_down_zp_mem, scratch.up, routing_mem_ptr},- {scratch.y},- {static_cast<size_t>(max_topk), subgroup_size, static_cast<size_t>(_hidden_size / N_BLOCK)},- {1, subgroup_size, SUBGROUP_NUM});+ ret_event = execute_stage({ret_event},+ instance,+ *mlp_down,+ {batch_mem_ptr, mlp_down_wei_mem, mlp_down_scale_mem, mlp_down_zp_mem, scratch.up, routing_mem_ptr},+ {scratch.y},+ {static_cast<size_t>(max_topk), subgroup_size, static_cast<size_t>(_hidden_size / N_BLOCK)},+ {1, subgroup_size, SUBGROUP_NUM});// final = sum(scratch.y)- ret = execute_stage({},+ ret = execute_stage({ret_event},instance,*mlp_reduce,{scratch.y},@@ -1014,7 +1038,7 @@ public:// and we can apply optimal kernels against memory bound to improve performance.// It is very important for MoE's second token performance.if (batch == 1) {- return exec_single_batch(instance, scratch);+ return exec_single_batch({topk_event}, instance, scratch);}auto& engine = instance.get_network().get_engine();@@ -1034,7 +1058,7 @@ public:get_expert_mask_from_gpu(config, topk_id_mem, stream, expert_mask);auto& dnn_stream = stream.get_onednn_stream();- cldnn::event::ptr result_event;+ cldnn::event::ptr result_event = nullptr;auto routing_mem_ptr = scratch.topk_weights;auto get_best_lws = [](size_t hidden_size) {@@ -1069,13 +1093,14 @@ public:onednn_kernel& kernel = get_kernel(n_token, static_cast<int>(expert_no), instance);// gather- execute_stage(events,- instance,- *gather,- {hidden_states_mem_ptr, routing_mem_ptr, expert_mask_mem.batch, expert_mask_mem.topk},- {scratch.x, scratch.routing_weights},- {static_cast<size_t>(n_token), static_cast<size_t>(_hidden_size)},- {1, lws_size});+ result_event = execute_stage({result_event},+ instance,+ *gather,+ {hidden_states_mem_ptr, routing_mem_ptr, expert_mask_mem.batch, expert_mask_mem.topk},+ {scratch.x, scratch.routing_weights},+ {static_cast<size_t>(n_token), static_cast<size_t>(_hidden_size)},+ {1, lws_size},+ instance.needs_completion_event());// upkernel.up.forward(dnn_stream,@@ -1098,7 +1123,7 @@ public:convert2dnnl(scratch.y, {static_cast<int>(n_token), _hidden_size}, dnnl::memory::format_tag::ab),convert2dnnl(scratch.routing_weights, {n_token * max_topk}, dnnl::memory::format_tag::a));// index_add- result_event = execute_stage(events,+ result_event = execute_stage({result_event},instance,*scatter,{scratch.y, expert_mask_mem.batch},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 c2a5c0be808..1735c42f30b 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@@ -5,6 +5,9 @@#define unroll_for __attribute__((opencl_unroll_hint)) for+// Fake group size for compatibility and computation performance balance+#define FAKE_GROUP_SIZE 128+#if GATE_UP_ENABLEinline void gemv_n2x(const __global uchar* weight,__global half* scales,@@ -15,7 +18,6 @@ inline void gemv_n2x(const __global uchar* weight,float* xg_sum,const bool silu) {int num_sg = get_num_sub_groups();- int id_sg = get_sub_group_id();int id_local = get_sub_group_local_id();int n_start = get_global_id(2) * N_BLOCK;@@ -24,36 +26,23 @@ inline void gemv_n2x(const __global uchar* weight,const __global uchar* B = weight + n * K / 2;float sum_all0 = 0;float sum_all1 = 0;-#if SZ_LAYOUT == 0__global half* S = scales + n;__global uchar* Z = zps + n / 2;- unroll_for (int gk = 0; gk < K / GROUP_SIZE; gk++, S += N, Z += N / 2) {- half s0 = S[0];- half s1 = S[1];- ushort z = Z[0];+ unroll_for (int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {+ int scale_offset = gk * (FAKE_GROUP_SIZE / GATE_UP_GROUP_SIZE) * N;+ int zp_offset = gk * (FAKE_GROUP_SIZE / GATE_UP_GROUP_SIZE) * N / 2;+ half s0 = S[scale_offset];+ half s1 = S[scale_offset + 1];+ uchar z = Z[zp_offset];half z_hf0 = convert_half(z & 0xf);half z_hf1 = convert_half(z >> 4);-#else- __global half* S = scales + n*K/GROUP_SIZE;- __global uchar* Z = zps + n*K/GROUP_SIZE/2;-- half scale_values = as_half(intel_sub_group_block_read_us((const __global ushort*)S));- uchar zp_values = intel_sub_group_block_read_uc((const __global uchar*)Z);- half zp_even = convert_half(zp_values & 0xF);- half zp_odd = convert_half(zp_values >> 4);- unroll_for (int gk = 0; gk < K / GROUP_SIZE; gk++) {- half s0 = sub_group_broadcast(scale_values, 2*gk + 0);- half s1 = sub_group_broadcast(scale_values, 2*gk + 1);- half z_hf0 = sub_group_broadcast(zp_even, gk);- half z_hf1 = sub_group_broadcast(zp_odd, gk);-#endif#if SUBGROUP_SIZE == 32half2 sum0;half2 sum1;- half4 a = as_half4(intel_sub_group_block_read_us4((const __local ushort*)x2 + gk*GROUP_SIZE));- uchar2 b = intel_sub_group_block_read_uc2((const __global uchar*)B + gk*GROUP_SIZE/2);- uchar2 b2 = intel_sub_group_block_read_uc2((const __global uchar*)(B + (K/2) + gk*GROUP_SIZE/2));+ half4 a = as_half4(intel_sub_group_block_read_us4((const __local ushort*)x2 + gk*FAKE_GROUP_SIZE));+ uchar2 b = intel_sub_group_block_read_uc2((const __global uchar*)B + gk*FAKE_GROUP_SIZE/2);+ uchar2 b2 = intel_sub_group_block_read_uc2((const __global uchar*)(B + (K/2) + gk*FAKE_GROUP_SIZE/2));sum0.s0 = fma(a.s0, (convert_half(b.s0 & 0x0F)), 0);sum0.s1 = fma(a.s1, (convert_half(b.s1 & 0x0F)), 0);@@ -70,9 +59,9 @@ inline void gemv_n2x(const __global uchar* weight,#elsehalf4 sum0;half4 sum1;- half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk*GROUP_SIZE));- uchar4 b = intel_sub_group_block_read_uc4((const __global uchar*)B + gk*GROUP_SIZE/2);- uchar4 b2 = intel_sub_group_block_read_uc4((const __global uchar*)(B + (K/2) + gk*GROUP_SIZE/2));+ half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk * FAKE_GROUP_SIZE));+ uchar4 b = intel_sub_group_block_read_uc4((const __global uchar*)B + gk * FAKE_GROUP_SIZE / 2);+ uchar4 b2 = intel_sub_group_block_read_uc4((const __global uchar*)(B + (K/2) + gk * FAKE_GROUP_SIZE / 2));sum0.s0 = fma(a.s0, (convert_half(b.s0 & 0x0F)), 0);sum0.s1 = fma(a.s1, (convert_half(b.s1 & 0x0F)), 0);@@ -112,7 +101,6 @@ inline void gemv_n2x(const __global uchar* weight,}}}-__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE)))KERNEL (mlp_gate_up)(const __global int* expert_list,@@ -129,8 +117,8 @@ KERNEL (mlp_gate_up)(y += expert_no * INTERMEDIATE_SIZE;const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2;- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE * 2 / GROUP_SIZE;- const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2 / GROUP_SIZE;+ const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE * 2 / GATE_UP_GROUP_SIZE;+ const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2 / GATE_UP_GROUP_SIZE;int expert_id = expert_list[expert_no];// gate, [HIDDEN_SIZE, INTERMEDIATE_SIZE]@@ -145,20 +133,28 @@ KERNEL (mlp_gate_up)(__local half x2[HIDDEN_SIZE];__local float xg_sum[HIDDEN_SIZE/32];++#if GATE_UP_GROUP_SIZE % FAKE_GROUP_SIZE != 0+ if (get_sub_group_id() == 0 && get_sub_group_local_id() == 0) {+ printf("GATE_UP_GROUP_SIZE(%d) must be divisible by FAKE_GROUP_SIZE(%d)", GATE_UP_GROUP_SIZE, FAKE_GROUP_SIZE);+ }+ return;+#endif+//# interleaving x into x2int 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*GROUP_SIZE;- half * px2 = x2 + id_sg*GROUP_SIZE;- unroll_for(int i = id_sg; i < HIDDEN_SIZE/GROUP_SIZE; i += num_sg, px += num_sg*GROUP_SIZE, px2 += num_sg*GROUP_SIZE) {+ half * px = x + 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) {//# quantization groupfloat x_group_sum = 0;- unroll_for(int j = id_local; j < GROUP_SIZE/2; j += SUBGROUP_SIZE) {+ unroll_for(int j = id_local; j < FAKE_GROUP_SIZE/2; j += SUBGROUP_SIZE) {half even = px[2*j + 0];half odd = px[2*j + 1];px2[j] = even;- px2[j + GROUP_SIZE/2] = odd;+ px2[j + FAKE_GROUP_SIZE/2] = odd;x_group_sum += even + odd;}x_group_sum = sub_group_reduce_add(x_group_sum);@@ -188,8 +184,8 @@ KERNEL (mlp_down)(y += expert_no * HIDDEN_SIZE;const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2;- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE * 2 / GROUP_SIZE;- const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2 / GROUP_SIZE;+ const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE * 2 / DOWN_GROUP_SIZE;+ const int expert_zp_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2 / DOWN_GROUP_SIZE;int expert_id = expert_list[expert_no];// down, [INTERMEDIATE_SIZE, HIDDEN_SIZE]@@ -207,16 +203,16 @@ KERNEL (mlp_down)(__local float xg_sum[INTERMEDIATE_SIZE/32];//# interleaving x into x2- __global half * px = x + id_sg*GROUP_SIZE;- __local half * px2 = x2 + id_sg*GROUP_SIZE;- unroll_for(int i = id_sg; i < INTERMEDIATE_SIZE/GROUP_SIZE; i += num_sg, px += num_sg*GROUP_SIZE, px2 += num_sg*GROUP_SIZE) {+ __global half * px = x + id_sg*FAKE_GROUP_SIZE;+ __local half * px2 = x2 + id_sg*FAKE_GROUP_SIZE;+ unroll_for(int i = id_sg; i < INTERMEDIATE_SIZE/FAKE_GROUP_SIZE; i += num_sg, px += num_sg*FAKE_GROUP_SIZE, px2 += num_sg*FAKE_GROUP_SIZE) {//# quantization groupfloat x_group_sum = 0;- unroll_for(int j = id_local; j < GROUP_SIZE/2; j += SUBGROUP_SIZE) {+ unroll_for(int j = id_local; j < FAKE_GROUP_SIZE/2; j += SUBGROUP_SIZE) {half even = px[2*j + 0];half odd = px[2*j + 1];px2[j] = even;- px2[j + GROUP_SIZE/2] = odd;+ px2[j + FAKE_GROUP_SIZE/2] = odd;x_group_sum += even + odd;}x_group_sum = sub_group_reduce_add(x_group_sum);@@ -235,19 +231,21 @@ KERNEL (mlp_down)(__global uchar* Z = zps + n / 2;float sum_all0 = 0;float sum_all1 = 0;- unroll_for (int gk = 0; gk < K / GROUP_SIZE; gk++, S += N, Z += N / 2) {- half s0 = S[0];- half s1 = S[1];- ushort z = Z[0];+ unroll_for (int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {+ int scale_offset = gk * (FAKE_GROUP_SIZE / DOWN_GROUP_SIZE) * N;+ int zp_offset = gk * (FAKE_GROUP_SIZE / DOWN_GROUP_SIZE) * N / 2;+ half s0 = S[scale_offset];+ half s1 = S[scale_offset + 1];+ ushort z = Z[zp_offset];half z_hf0 = convert_half(z & 0xf);half z_hf1 = convert_half(z >> 4);#if SUBGROUP_SIZE == 32half2 sum0;half2 sum1;- half4 a = as_half4(intel_sub_group_block_read_us4((const __local ushort*)x2 + gk*GROUP_SIZE));- uchar2 b = intel_sub_group_block_read_uc2((const __global uchar*)B + gk*GROUP_SIZE/2);- uchar2 b2 = intel_sub_group_block_read_uc2((const __global uchar*)(B + (K/2) + gk*GROUP_SIZE/2));+ half4 a = as_half4(intel_sub_group_block_read_us4((const __local ushort*)x2 + gk*FAKE_GROUP_SIZE));+ uchar2 b = intel_sub_group_block_read_uc2((const __global uchar*)B + gk*FAKE_GROUP_SIZE/2);+ uchar2 b2 = intel_sub_group_block_read_uc2((const __global uchar*)(B + (K/2) + gk*FAKE_GROUP_SIZE/2));sum0.s0 = fma(a.s0, (convert_half(b.s0 & 0x0F)), 0);sum0.s1 = fma(a.s1, (convert_half(b.s1 & 0x0F)), 0);@@ -264,9 +262,9 @@ KERNEL (mlp_down)(#elsehalf4 sum0;half4 sum1;- half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk*GROUP_SIZE));- uchar4 b = intel_sub_group_block_read_uc4((const __global uchar*)B + gk*GROUP_SIZE/2);- uchar4 b2 = intel_sub_group_block_read_uc4((const __global uchar*)(B + (K/2) + gk*GROUP_SIZE/2));+ half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk*FAKE_GROUP_SIZE));+ uchar4 b = intel_sub_group_block_read_uc4((const __global uchar*)B + gk*FAKE_GROUP_SIZE/2);+ uchar4 b2 = intel_sub_group_block_read_uc4((const __global uchar*)(B + (K/2) + gk*FAKE_GROUP_SIZE/2));sum0.s0 = fma(a.s0, (convert_half(b.s0 & 0x0F)), 0);sum0.s1 = fma(a.s1, (convert_half(b.s1 & 0x0F)), 0);diff --git a/src/plugins/intel_gpu/src/plugin/transformations/convert_moe_to_compressed.cpp b/src/plugins/intel_gpu/src/plugin/transformations/convert_moe_to_compressed.cppindex bc1d6d28868..13010a5f6b2 100644--- a/src/plugins/intel_gpu/src/plugin/transformations/convert_moe_to_compressed.cpp+++ b/src/plugins/intel_gpu/src/plugin/transformations/convert_moe_to_compressed.cpp@@ -4,6 +4,7 @@#include "convert_moe_to_compressed.hpp"+#include <limits>#include <memory>#include "intel_gpu/op/moe_compressed.hpp"@@ -21,6 +22,7 @@#include "openvino/op/topk.hpp"#include "openvino/op/transpose.hpp"#include "openvino/op/unsqueeze.hpp"+#include "openvino/pass/pattern/op/optional.hpp"#include "openvino/pass/pattern/op/pattern.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "transformations/rt_info/keep_const_precision.hpp"@@ -89,7 +91,7 @@ namespace ov::intel_gpu {};\\auto gemm3_reshape_const_m_##SUFFIX = wrap_type<ov::op::v0::Constant>();\- auto gemm3_reshape_m_##SUFFIX = wrap_type<ov::op::v1::Reshape>({gemm3_mul_m_##SUFFIX, gemm3_reshape_const_m_##SUFFIX}, gemm3_reshape_ungroup_##SUFFIX);\+ auto gemm3_reshape_m_##SUFFIX = optional<ov::op::v1::Reshape>({gemm3_mul_m_##SUFFIX, gemm3_reshape_const_m_##SUFFIX}, gemm3_reshape_ungroup_##SUFFIX);\\auto gemm3_convert_m_##SUFFIX = wrap_type<ov::op::v0::Convert>({gemm3_reshape_m_##SUFFIX}, type_matches(ov::element::f32));@@ -98,17 +100,20 @@ namespace ov::intel_gpu {auto gemm3_zp_##SUFFIX = pattern_map.at(gemm3_zp_m_##SUFFIX).get_node_shared_ptr();\auto gemm3_scale_shape_##SUFFIX = gemm3_scale_##SUFFIX->get_shape();\gemm3_scale_shape_##SUFFIX.pop_back();\- auto gemm3_reshape_const_##SUFFIX = ov::op::v0::Constant::create(ov::element::i32, \+ auto gemm3_reshape_const_##SUFFIX = ov::op::v0::Constant::create(\+ ov::element::i32, \ov::Shape{ gemm3_scale_shape_##SUFFIX.size() }, \- gemm3_scale_shape_##SUFFIX);\+ gemm3_scale_shape_##SUFFIX);\auto gemm3_scale_reshape_##SUFFIX = std::make_shared<ov::op::v1::Reshape>(gemm3_scale_##SUFFIX, gemm3_reshape_const_##SUFFIX, false);\auto gemm3_zp_reshape_##SUFFIX = std::make_shared<ov::op::v1::Reshape>(gemm3_zp_##SUFFIX, gemm3_reshape_const_##SUFFIX, false);\\std::vector<size_t> gemm3_transpose_order_##SUFFIX(gemm3_scale_reshape_##SUFFIX->get_shape().size());\std::iota(gemm3_transpose_order_##SUFFIX.begin(), gemm3_transpose_order_##SUFFIX.end(), 0);\std::swap(*(gemm3_transpose_order_##SUFFIX.end() - 1), *(gemm3_transpose_order_##SUFFIX.end() - 2));\- auto gemm3_transpose_const_##SUFFIX = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{ gemm3_transpose_order_##SUFFIX.size() }, \- gemm3_transpose_order_##SUFFIX);\+ auto gemm3_transpose_const_##SUFFIX = ov::op::v0::Constant::create(\+ ov::element::i32, \+ ov::Shape{ gemm3_transpose_order_##SUFFIX.size() }, \+ gemm3_transpose_order_##SUFFIX);\auto gemm3_transpose_scale_##SUFFIX = std::make_shared<ov::op::v1::Transpose>(gemm3_scale_reshape_##SUFFIX, gemm3_transpose_const_##SUFFIX);\auto gemm3_transpose_zp_##SUFFIX = std::make_shared<ov::op::v1::Transpose>(gemm3_zp_reshape_##SUFFIX, gemm3_transpose_const_##SUFFIX);@@ -122,12 +127,12 @@ ConvertMOEToMOECompressed::ConvertMOEToMOECompressed(bool is_pa) {auto routing_weights_m = any_input();auto topk_m = any_input();- auto moe_root_gemm3 = wrap_type<ov::op::internal::MOE>(- {hidden_states_m, routing_weights_m, topk_m, gemm3_convert_m_gate, gemm3_convert_m_up, gemm3_convert_m_down},- [](const ov::Output<ov::Node>& output) {- auto moe = ov::as_type_ptr<ov::op::internal::MOE>(output.get_node_shared_ptr());- return moe && moe->get_config().expert_type == ov::op::internal::MOE::Expert_type::GEMM3_SWIGLU;- });+ auto moe_root_gemm3 =+ wrap_type<ov::op::internal::MOE>({hidden_states_m, routing_weights_m, topk_m, gemm3_convert_m_gate, gemm3_convert_m_up, gemm3_convert_m_down},+ [](const ov::Output<ov::Node>& output) {+ auto moe = ov::as_type_ptr<ov::op::internal::MOE>(output.get_node_shared_ptr());+ return moe && moe->get_config().expert_type == ov::op::internal::MOE::Expert_type::GEMM3_SWIGLU;+ });// gemm3 pattern finished// =========================================================================================// gemm2 pattern start@@ -174,35 +179,55 @@ ConvertMOEToMOECompressed::ConvertMOEToMOECompressed(bool is_pa) {return false;}if (moe->get_config().expert_type == ov::op::internal::MOE::Expert_type::GEMM3_SWIGLU) {- MOE_COMPRESSED_WEIGHT_GEMM3(gate);- MOE_COMPRESSED_WEIGHT_GEMM3(up);- MOE_COMPRESSED_WEIGHT_GEMM3(down);+ auto wei_partial_shape = pattern_map.at(gemm3_compressed_weights_m_up).get_partial_shape();+ if (!wei_partial_shape.is_static()) {+ OPENVINO_THROW("Moe weight shape should be static.");+ }+ auto weight_shape = wei_partial_shape.to_shape();+ bool group_compressed = false;+ if (weight_shape.size() == 4) {+ group_compressed = true;+ } else if (weight_shape.size() == 3) {+ group_compressed = false;+ } else {+ OPENVINO_THROW("Moe weight shape must be 3D or 4D.");+ }OutputVector args(12);args[0] = pattern_map.at(hidden_states_m);args[1] = pattern_map.at(routing_weights_m);args[2] = pattern_map.at(topk_m);args[3] = pattern_map.at(gemm3_compressed_weights_m_gate);- args[4] = gemm3_transpose_scale_gate;- args[5] = gemm3_transpose_zp_gate;+ if (group_compressed) {+ MOE_COMPRESSED_WEIGHT_GEMM3(gate);+ args[4] = gemm3_transpose_scale_gate;+ args[5] = gemm3_transpose_zp_gate;+ } else {+ args[4] = pattern_map.at(gemm3_scale_m_gate);+ args[5] = pattern_map.at(gemm3_zp_m_gate);+ }args[6] = pattern_map.at(gemm3_compressed_weights_m_up);- args[7] = gemm3_transpose_scale_up;- args[8] = gemm3_transpose_zp_up;- args[9] = pattern_map.at(gemm3_compressed_weights_m_down);- args[10] = gemm3_transpose_scale_down;- args[11] = gemm3_transpose_zp_down;- ov::intel_gpu::op::MOECompressed::Config config(moe->get_config());- auto wei_partial_shape = pattern_map.at(gemm3_compressed_weights_m_up).get_partial_shape();- if (!wei_partial_shape.is_static()) {- OPENVINO_THROW("Moe weight shape should be static.");+ if (group_compressed) {+ MOE_COMPRESSED_WEIGHT_GEMM3(up);+ args[7] = gemm3_transpose_scale_up;+ args[8] = gemm3_transpose_zp_up;+ } else {+ args[7] = pattern_map.at(gemm3_scale_m_up);+ args[8] = pattern_map.at(gemm3_zp_m_up);}- auto weight_shape = wei_partial_shape.to_shape();- if (weight_shape.size() != 4) {- OPENVINO_THROW("Moe weight shape must be 4D.");+ args[9] = pattern_map.at(gemm3_compressed_weights_m_down);+ if (group_compressed) {+ MOE_COMPRESSED_WEIGHT_GEMM3(down);+ args[10] = gemm3_transpose_scale_down;+ args[11] = gemm3_transpose_zp_down;+ } else {+ args[10] = pattern_map.at(gemm3_scale_m_down);+ args[11] = pattern_map.at(gemm3_zp_m_down);}- config.hidden_size = weight_shape[2] * weight_shape[3];+ ov::intel_gpu::op::MOECompressed::Config config(moe->get_config());+ config.hidden_size = group_compressed ? weight_shape[2] * weight_shape[3] : weight_shape[2];config.inter_size = weight_shape[1];config.num_expert = weight_shape[0];- config.group_size = weight_shape[3];+ config.group_size = group_compressed ? weight_shape[3] : std::numeric_limits<size_t>::max();auto topk_shape = pattern_map.at(topk_m).get_partial_shape();if (!topk_shape[1].is_static()) {OPENVINO_THROW("K dimenion in moe topk input should be static..");