Skip to content

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

[GPU] qwen3 moe support int8 weights compression (#33188)

River Li5ab80acea3ee
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp
index 458849e96e7..14730bf5b88 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.cpp
@@ -52,12 +52,23 @@ JitConstants MoE3GemmMicroGenerator::get_jit_constants(const kernel_impl_params&
jit.make("SUBGROUP_SIZE", subgroup_size);
jit.make("OUTPUT_TYPE", to_ocl_type(data_types::f16)); // output
jit.make("INPUT0_TYPE", to_ocl_type(data_types::f16)); // input: f16
+
+ GPU_DEBUG_TRACE_DETAIL << "\t m_wei_idx: " << m_wei_idx << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "\t m_wei_idx.get_shape(): " << weight_layout.to_short_string() << std::endl;
+ const auto& weight_shape = weight_layout.get_shape();
+ // weight layout: u4/u8:bfyx:4x3072x8x128:nopad
+ size_t expert_stride = weight_shape.size() == 4 ? (weight_shape[1] * weight_shape[2] * weight_shape[3]) : (weight_shape[1] * weight_shape[2]);
if (weight_layout.data_type == ov::element::u4 || weight_layout.data_type == ov::element::i4) {
jit.make("INPUT1_TYPE", to_ocl_type(data_types::u8)); // weight: u4/i4
jit.make("WEIGHT_COMPRESSED_INT4", 1);
+ jit.make("EXPERT_STRIDE", expert_stride / 2);
+ GPU_DEBUG_TRACE_DETAIL << "\t expert_stride: " << expert_stride / 2 << std::endl;
} else {
jit.make("INPUT1_TYPE", to_ocl_type(weight_layout.data_type)); // weight type
jit.make("WEIGHT_COMPRESSED_INT4", 0);
+ ov::element::Type dt = weight_layout.data_type;
+ jit.make("EXPERT_STRIDE", expert_stride * dt.size());
+ GPU_DEBUG_TRACE_DETAIL << "\t expert_stride: " << expert_stride * dt.size() << std::endl;
}
jit.make("INPUT2_TYPE", to_ocl_type(data_types::i32)); // experts_ids: i32
jit.make("INPUT3_TYPE", to_ocl_type(data_types::i32)); // input_offset_per_expert: i32
@@ -76,14 +87,6 @@ JitConstants MoE3GemmMicroGenerator::get_jit_constants(const kernel_impl_params&
jit.make("INPUT_SEQ_LEN", 4); // prefill not use it
jit.make("SCALE_ZP_NO_TRANSPOSE", 1);
- GPU_DEBUG_TRACE_DETAIL << "\t m_wei_idx: " << m_wei_idx << std::endl;
- GPU_DEBUG_TRACE_DETAIL << "\t m_wei_idx.get_shape(): " << weight_layout.to_short_string() << std::endl;
- const auto& weight_shape = weight_layout.get_shape();
- // weight layout: u4:bfyx:4x3072x8x128:nopad
- size_t expert_stride = weight_shape.size() == 4 ? (weight_shape[1] * weight_shape[2] * weight_shape[3]) : (weight_shape[1] * weight_shape[2]);
- jit.make("EXPERT_STRIDE", expert_stride / 2);
- GPU_DEBUG_TRACE_DETAIL << "\t expert_stride: " << expert_stride / 2 << std::endl;
-
GPU_DEBUG_TRACE_DETAIL << "\t m_scale_idx: " << m_scale_idx << std::endl;
GPU_DEBUG_TRACE_DETAIL << "\t m_scale_idx.get_shape(): " << scale_layout.to_short_string() << std::endl;
if (cfg.weight_group_size > 0) {
@@ -147,21 +150,32 @@ void MoE3GemmMicroGenerator::init_microkernels(const kernel_impl_params& params,
std::lock_guard<std::mutex> l(mtx);
int wei_idx, scale_idx, zp_idx;
+ auto desc = params.typed_desc<moe_3gemm_fused_compressed>();
+ size_t group_size = desc->_config.group_size;
switch (type) {
case MoE3GemmMicroKernelType::MLP_GATE:
wei_idx = static_cast<int>(MOE3GemmInputIndex::WEIGHT_0);
scale_idx = static_cast<int>(MOE3GemmInputIndex::SCALE_0);
zp_idx = static_cast<int>(MOE3GemmInputIndex::ZP_0);
+ if (group_size == std::numeric_limits<size_t>::max()) {
+ group_size = desc->_config.hidden_size;
+ }
break;
case MoE3GemmMicroKernelType::MLP_UP:
wei_idx = static_cast<int>(MOE3GemmInputIndex::WEIGHT_1);
scale_idx = static_cast<int>(MOE3GemmInputIndex::SCALE_1);
zp_idx = static_cast<int>(MOE3GemmInputIndex::ZP_1);
+ if (group_size == std::numeric_limits<size_t>::max()) {
+ group_size = desc->_config.hidden_size;
+ }
break;
case MoE3GemmMicroKernelType::MLP_DOWN:
wei_idx = static_cast<int>(MOE3GemmInputIndex::WEIGHT_2);
scale_idx = static_cast<int>(MOE3GemmInputIndex::SCALE_2);
zp_idx = static_cast<int>(MOE3GemmInputIndex::ZP_2);
+ if (group_size == std::numeric_limits<size_t>::max()) {
+ group_size = desc->_config.inter_size;
+ }
break;
default:
OPENVINO_THROW("Unsupported MoE3GemmMicroKernelType");
@@ -201,8 +215,6 @@ void MoE3GemmMicroGenerator::init_microkernels(const kernel_impl_params& params,
GPU_DEBUG_TRACE_DETAIL << "MoE3GemmMicroGenerator::init_microkernels: " << std::endl;
GPU_DEBUG_TRACE_DETAIL << "\t m = " << m << ", n = " << n << ", k = " << k << std::endl;
-
- size_t group_size = weight_shape.size() == 4 ? weight_shape[3] : weight_shape[2];
GPU_DEBUG_TRACE_DETAIL << "\t weight group size: " << group_size << "\n";
micro::GEMMProblem problem_moe;
@@ -297,7 +309,19 @@ DispatchDataFunc MoE3GemmMicroGenerator::get_dispatch_data_func() const {
GPU_DEBUG_TRACE_DETAIL << "\t experts_weight_layout: " << experts_weight_layout.to_short_string() << std::endl;
// has_batch_dim indicates whether the input tensor has batch dimension
- size_t n = input_layout.get_shape().size() == 3 ? input_layout.get_shape()[1] : input_layout.get_shape()[0];
+ size_t n = input_layout.get_shape()[0];
+ switch (input_layout.get_shape().size()) {
+ case 2:
+ n = input_layout.get_shape()[0];
+ break;
+ case 3:
+ case 4:
+ n = input_layout.get_shape()[0] * input_layout.get_shape()[1];
+ break;
+ default:
+ OPENVINO_THROW("Unsupported input tensor shape size: ", input_layout.get_shape().size());
+ }
+
auto cur_moe = params.typed_desc<moe_3gemm_fused_compressed>();
const auto& config = cur_moe->_config;
n = n * config.top_k;
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 c76efacbbb7..2e80c3986d7 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
@@ -429,6 +429,15 @@ protected:
}
};
+static size_t get_seq_len(cldnn::layout& layout) {
+ auto shape = layout.get_shape();
+ size_t seq_len = static_cast<size_t>(shape[0]);
+ if (shape.size() == 4) {
+ seq_len = static_cast<size_t>(shape[0] * shape[1]);
+ }
+ return seq_len;
+}
+
static size_t get_vec_size(const RuntimeParams& params) {
const auto& input = params.get_input_layout(0);
size_t vec_size = 1;
@@ -646,6 +655,26 @@ static void add_common_consts(const RuntimeParams& params, JitConstants& jit) {
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);
+
+ ov::element::Type weight_dt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::WEIGHT_0)).data_type;
+ // 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
+ }
}
class MoE3GemmSwigluMLPGateUp : public KernelGenerator {
@@ -655,9 +684,9 @@ public:
protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
auto jit = KernelGenerator::get_jit_constants(params);
- auto desc = params.typed_desc<moe_3gemm_fused_compressed>();
add_common_consts(params, jit);
jit.make("GATE_UP_ENABLE", 1);
+
return jit;
}
@@ -678,7 +707,6 @@ public:
protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
auto jit = KernelGenerator::get_jit_constants(params);
- auto desc = params.typed_desc<moe_3gemm_fused_compressed>();
add_common_consts(params, jit);
jit.make("DOWN_ENABLE", 1);
return jit;
@@ -701,7 +729,6 @@ public:
protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
auto jit = KernelGenerator::get_jit_constants(params);
- auto desc = params.typed_desc<moe_3gemm_fused_compressed>();
add_common_consts(params, jit);
jit.make("REDUCE_ENABLE", 1);
return jit;
@@ -806,7 +833,6 @@ public:
if (m_rt_params == nullptr) {
m_rt_params = std::make_unique<MoE3GemmRuntimeParams>();
}
- // auto rtp = static_cast<MoE3GemmRuntimeParams*>(m_rt_params.get());
init(node.as<moe_3gemm_fused_compressed>().get_primitive());
auto use_micro_gemm_prefill_str = std::getenv("MOE_USE_MICRO_GEMM_PREFILL");
@@ -838,6 +864,12 @@ public:
<< ", arch=" << static_cast<int>(info.arch) << std::endl;
}
+ // Remove this limitation once micro_gemm kernels has supported i8/u8 weights.
+ const auto& weight_dt = params.get_input_layout(static_cast<size_t>(MOE3GemmInputIndex::WEIGHT_0)).data_type;
+ if (weight_dt != data_types::u4 && use_micro_gemm_prefill) {
+ use_micro_gemm_prefill = false;
+ }
+
// Don't change the order of stages
add_stage(softmax_topk, params);
add_stage(gather, params);
@@ -877,6 +909,19 @@ public:
return;
init(cur_moe);
+ auto get_bytes_count = [](int64_t size, const cldnn::layout& layout) {
+ ov::element::Type dt = layout.data_type;
+ switch (layout.data_type) {
+ case ov::element::u4:
+ case ov::element::i4:
+ return size / 2;
+ break;
+ default:
+ return size * static_cast<int64_t>(dt.size());
+ break;
+ }
+ };
+
_dnnl_weights.resize(cur_moe->_config.num_expert);
for (size_t j = 0; j < cur_moe->_config.num_expert; j++) {
auto& dnnl_weights = _dnnl_weights[j];
@@ -891,20 +936,22 @@ public:
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
- int64_t wei_offset = j * dnnl_weights[i].ic * dnnl_weights[i].oc / 2;
+ // weight shape: [ic, oc], type: u4/i8
+ int64_t wei_offset = j * get_bytes_count(dnnl_weights[i].ic * dnnl_weights[i].oc, moe_fusion_wei_addr.weight[i]->get_layout());
dnnl_weights[i].weight =
convert2dnnl(moe_fusion_wei_addr.weight[i], {dnnl_weights[i].ic, dnnl_weights[i].oc}, dnnl::memory::format_tag::ba, wei_offset);
// scale shape: [ic / ic_group_size, oc], type: f16
- int64_t scale_offset = j * dnnl_weights[i].ic * dnnl_weights[i].oc / dnnl_weights[i].ic_group_size * 2;
+ int64_t scale_offset =
+ j * get_bytes_count(dnnl_weights[i].ic * dnnl_weights[i].oc / dnnl_weights[i].ic_group_size, moe_fusion_wei_addr.scale[i]->get_layout());
dnnl_weights[i].scale = convert2dnnl(moe_fusion_wei_addr.scale[i],
{dnnl_weights[i].ic / dnnl_weights[i].ic_group_size, dnnl_weights[i].oc},
dnnl::memory::format_tag::ab,
scale_offset);
- // zp shape: [ic / ic_group_size, oc], type: u4
- int64_t zp_offset = j * dnnl_weights[i].ic * dnnl_weights[i].oc / dnnl_weights[i].ic_group_size / 2;
+ // zp shape: [ic / ic_group_size, oc], type: u4/i8
+ int64_t zp_offset =
+ j * get_bytes_count(dnnl_weights[i].ic * dnnl_weights[i].oc / dnnl_weights[i].ic_group_size, moe_fusion_wei_addr.zp[i]->get_layout());
dnnl_weights[i].zp = convert2dnnl(moe_fusion_wei_addr.zp[i],
{dnnl_weights[i].ic / dnnl_weights[i].ic_group_size, dnnl_weights[i].oc},
dnnl::memory::format_tag::ab,
@@ -936,18 +983,18 @@ public:
size_t max_topk = static_cast<size_t>(config.top_k);
size_t expert_num = static_cast<size_t>(config.num_expert);
auto hidden_states_layout = params.input_layouts[0];
- auto batch = static_cast<size_t>(hidden_states_layout.get_shape()[0]);
+ auto token_num = get_seq_len(hidden_states_layout);
auto data_type = hidden_states_layout.data_type;
std::vector<BufferDescriptor> internal_buffers;
// softmax+topk
- layout layout_topk_id(ov::Shape{batch, max_topk}, data_types::u32, cldnn::format::bfyx);
- layout layout_topk_weights(ov::Shape{batch, max_topk}, data_type, cldnn::format::bfyx);
+ layout layout_topk_id(ov::Shape{token_num, max_topk}, data_types::u32, cldnn::format::bfyx);
+ layout layout_topk_weights(ov::Shape{token_num, max_topk}, data_type, cldnn::format::bfyx);
internal_buffers.emplace_back(layout_topk_id, true); // 0: topk_id
internal_buffers.emplace_back(layout_topk_weights, true); // 1: topk_weights
- // To support micro_gemm, prefill need to allocate max_topk * batch for input data of micro_gemm
- auto max_batch = max_topk * batch;
+ // To support micro_gemm, prefill need to allocate max_topk * token_num for input data of micro_gemm
+ auto max_batch = max_topk * token_num;
layout layout_gateup_out(ov::Shape{max_batch, static_cast<size_t>(config.inter_size)}, data_type, cldnn::format::bfyx);
layout layout_down_out(ov::Shape{max_batch, static_cast<size_t>(config.hidden_size)}, data_type, cldnn::format::bfyx);
internal_buffers.emplace_back(layout_gateup_out, true); // 2: up output
@@ -957,22 +1004,22 @@ public:
// scratch.gate = gate(scratch.x) * scratch.up
// scratch.y = down(scratch.gate) * routing_weights
internal_buffers.emplace_back(layout_down_out, true); // 4: up/gate input, scratch.x has same layout with down output
- layout routing_layout(ov::Shape{batch * max_topk}, data_type, cldnn::format::bfyx);
+ layout routing_layout(ov::Shape{token_num * max_topk}, data_type, cldnn::format::bfyx);
internal_buffers.emplace_back(routing_layout, true); // 5: routing_weights
internal_buffers.emplace_back(layout_gateup_out, true); // 6: gate output, scratch.gate has same layout with up
// expert masks for gpu
- layout index_layout(ov::Shape{expert_num, batch}, ov::element::i32, cldnn::format::bfyx);
+ layout index_layout(ov::Shape{expert_num, token_num}, ov::element::i32, cldnn::format::bfyx);
internal_buffers.emplace_back(index_layout, true); // 7: expert_mask_batch
internal_buffers.emplace_back(index_layout, true); // 8: expert_mask_topk
GPU_DEBUG_TRACE_DETAIL << "[DEBUG] get_internal_buffer_descs(): use_micro_gemm_prefill=" << use_micro_gemm_prefill << std::endl;
// for micro_gemm
- if (use_micro_gemm_prefill && batch > 1) {
- layout layout_micro_gemm(ov::Shape{expert_num, batch}, ov::element::i32, cldnn::format::bfyx);
+ if (use_micro_gemm_prefill && token_num > 1) {
+ layout layout_micro_gemm(ov::Shape{expert_num, token_num}, ov::element::i32, cldnn::format::bfyx);
internal_buffers.emplace_back(layout_micro_gemm, true); // 9: experts_ids for each activated expert
internal_buffers.emplace_back(layout_micro_gemm, true); // 10: token start offset idx (input gather tokens) for each activated expert
internal_buffers.emplace_back(layout_micro_gemm, true); // 11: token len (input gather tokens) for each activated expert
- layout layout_token_idx(ov::Shape{batch * max_topk}, ov::element::i32, cldnn::format::bfyx);
+ layout layout_token_idx(ov::Shape{token_num * max_topk}, ov::element::i32, cldnn::format::bfyx);
internal_buffers.emplace_back(layout_token_idx, true); // 12: token idx per expert
layout layout_actual_used_expert_num(ov::Shape{1}, ov::element::i32, cldnn::format::bfyx);
internal_buffers.emplace_back(layout_actual_used_expert_num, false); // 13: actual_used_expert_num
@@ -980,14 +1027,14 @@ public:
return internal_buffers;
}
- void prepare_internal_buffers(typed_primitive_inst<moe_3gemm_fused_compressed>& instance, scratch_buffers& scratch, size_t batch) {
+ void prepare_internal_buffers(typed_primitive_inst<moe_3gemm_fused_compressed>& instance, scratch_buffers& scratch, size_t token_num) {
const auto& intermediates_memories = instance.get_intermediates_memories();
auto& engine = instance.get_network().get_engine();
scratch.topk_id = intermediates_memories[MOE_INTERNAL_BUFFER_TOPK_IDX];
scratch.topk_weights = intermediates_memories[MOE_INTERNAL_BUFFER_TOPK_WEIGHTS];
scratch.up = intermediates_memories[MOE_INTERNAL_BUFFER_UP_OUTPUT];
scratch.y = intermediates_memories[MOE_INTERNAL_BUFFER_DOWN_OUTPUT];
- if (batch > 1) {
+ if (token_num > 1) {
scratch.x = intermediates_memories[MOE_INTERNAL_BUFFER_GATE_UP_INPUT];
scratch.routing_weights = intermediates_memories[MOE_INTERNAL_BUFFER_ROUTING_WEIGHTS];
scratch.gate = intermediates_memories[MOE_INTERNAL_BUFFER_GATE_OUTPUT];
@@ -995,11 +1042,11 @@ public:
int expert_num = static_cast<int>(config.num_expert);
scratch.expert_masks.resize(expert_num);
for (int i = 0; i < expert_num; i++) {
- auto mask_layout = cldnn::layout({static_cast<int>(batch)}, cldnn::data_types::i32, cldnn::format::get_default_format(1));
+ auto mask_layout = cldnn::layout({static_cast<int>(token_num)}, cldnn::data_types::i32, cldnn::format::get_default_format(1));
scratch.expert_masks[i].batch =
- engine.create_subbuffer(*intermediates_memories[MOE_INTERNAL_BUFFER_EXPERT_MASK_BATCH], mask_layout, i * batch * sizeof(int32_t));
+ engine.create_subbuffer(*intermediates_memories[MOE_INTERNAL_BUFFER_EXPERT_MASK_BATCH], mask_layout, i * token_num * sizeof(int32_t));
scratch.expert_masks[i].topk =
- engine.create_subbuffer(*intermediates_memories[MOE_INTERNAL_BUFFER_EXPERT_MASK_TOPK], mask_layout, i * batch * sizeof(int32_t));
+ engine.create_subbuffer(*intermediates_memories[MOE_INTERNAL_BUFFER_EXPERT_MASK_TOPK], mask_layout, i * token_num * sizeof(int32_t));
}
}
@@ -1020,7 +1067,7 @@ public:
}
void get_expert_mask_from_gpu(const MOE3GemmFusedCompressed::Config& config, memory::ptr mem, stream& stream, expert_mask_cpu& expert_mask) {
- // shape: [batch, topk]
+ // shape: [token_num, topk]
auto layout = mem->get_layout();
const auto& shape = layout.get_shape();
@@ -1036,6 +1083,8 @@ public:
OPENVINO_THROW("get_expert_mask_from_memory not support padding");
}
+ GPU_DEBUG_TRACE_DETAIL << "[DEBUG] get_expert_mask_from_gpu: layout=" << layout.to_short_string() << ", max_expert_num=" << max_expert_num
+ << ", max_topk=" << max_topk << ", max_tokens=" << max_tokens << std::endl;
std::vector<int32_t> buf(max_topk * max_tokens);
mem->copy_to(stream, buf.data(), 0, 0, buf.size() * sizeof(int32_t), true);
@@ -1076,15 +1125,8 @@ public:
void copy_expert_mask_to_gpu(stream& stream, const expert_mask_cpu& expert_mask, size_t expert_no, expert_mask_gpu& expert_mask_mem) {
auto size = expert_mask.batch[expert_no].size() * sizeof(int);
-
- {
- mem_lock<int32_t, mem_lock_type::write> lock_data{expert_mask_mem.batch, stream};
- memcpy(lock_data.data(), expert_mask.batch[expert_no].data(), size);
- }
- {
- mem_lock<int32_t, mem_lock_type::write> lock_data{expert_mask_mem.topk, stream};
- memcpy(lock_data.data(), expert_mask.topk[expert_no].data(), size);
- }
+ expert_mask_mem.batch->copy_from(stream, expert_mask.batch[expert_no].data(), 0, 0, size, true);
+ expert_mask_mem.topk->copy_from(stream, expert_mask.topk[expert_no].data(), 0, 0, size, true);
}
cldnn::event::ptr execute_stage(const std::vector<cldnn::event::ptr>& events,
@@ -1152,7 +1194,8 @@ public:
return std::make_tuple(mem, layout);
}
- cldnn::event::ptr exec_single_batch(const std::vector<cldnn::event::ptr>& events,
+ bool print_wei = false;
+ cldnn::event::ptr exec_single_token(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>();
@@ -1231,7 +1274,8 @@ public:
auto batch_mem_ptr = scratch.topk_id;
auto [hidden_states_mem_ptr, hidden_states_layout] = get_input_info(instance, static_cast<size_t>(MOE3GemmInputIndex::HIDDEN_STATES));
auto routing_mem_ptr = scratch.topk_weights;
- auto input_shape = instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::HIDDEN_STATES))->get_layout().get_shape();
+ auto input_layout = instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::HIDDEN_STATES))->get_layout();
+ auto token_num = get_seq_len(input_layout);
_hidden_size = static_cast<int>(cur_moe->_config.hidden_size);
_intermediate_size = static_cast<int>(cur_moe->_config.inter_size);
@@ -1255,7 +1299,7 @@ public:
// mask 3: expert id, shape = [activated_expert_num]
// mask 4: actual activated expert num, shape = [1]
if (use_gpu_mask_gen) {
- auto token_size = input_shape[0];
+ auto token_size = token_num;
ret_event = execute_stage(events,
instance,
*prefill_mask_gen,
@@ -1275,15 +1319,14 @@ public:
cldnn::mem_lock<int32_t, mem_lock_type::read> num_actual_experts_lock(intermediates_memories[MOE_INTERNAL_BUFFER_ACTUAL_USED_EXPERT_NUM], stream);
rtp->num_actually_used_experts = num_actual_experts_lock[0];
# if DEBUG_MOE_LOG
- std::cout << "Step 1: mask gen by gpu, num_actually_used_experts = " << rtp->num_actually_used_experts << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "Step 1: mask gen by gpu, num_actually_used_experts = " << rtp->num_actually_used_experts << std::endl;
# endif
-
} else {
ret_event = events.empty() ? nullptr : events[0];
expert_mask_cpu expert_mask_cpu;
get_expert_mask_from_gpu(config, batch_mem_ptr, stream, expert_mask_cpu);
- auto token_size = input_shape[0];
+ auto token_size = token_num;
auto max_topk = static_cast<int>(cur_moe->_config.top_k);
std::vector<int32_t> tokens_per_expert_cpu(token_size * max_topk, -1);
std::vector<int32_t> tokens_lens_per_expert_cpu(num_total_experts, -1);
@@ -1318,33 +1361,33 @@ public:
# if DEBUG_MOE_LOG
{
- std::cout << "\nstep 1: prefill_mask num_actually_used_experts=" << num_actually_used_experts << std::endl;
- std::cout << "expert_id[" << num_actually_used_experts << "]: = ";
+ GPU_DEBUG_TRACE_DETAIL << "\nstep 1: prefill_mask num_actually_used_experts=" << num_actually_used_experts << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "expert_id[" << num_actually_used_experts << "]: = ";
for (int i = 0; i < num_actually_used_experts; i++) {
- std::cout << experts_id_cpu[i] << ", ";
+ GPU_DEBUG_TRACE_DETAIL << experts_id_cpu[i] << ", ";
}
- std::cout << std::endl;
- std::cout << "experts_info_start_idx[" << num_actually_used_experts << "]: = ";
+ GPU_DEBUG_TRACE_DETAIL << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "experts_info_start_idx[" << num_actually_used_experts << "]: = ";
for (int i = 0; i < num_actually_used_experts; i++) {
- std::cout << experts_info_start_idx_cpu[i] << ", ";
+ GPU_DEBUG_TRACE_DETAIL << experts_info_start_idx_cpu[i] << ", ";
}
- std::cout << std::endl;
- std::cout << "tokens_len_per_expert[" << num_actually_used_experts << "]: = ";
+ GPU_DEBUG_TRACE_DETAIL << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "tokens_len_per_expert[" << num_actually_used_experts << "]: = ";
for (int i = 0; i < num_actually_used_experts; i++) {
- std::cout << tokens_lens_per_expert_cpu[i] << ", ";
+ GPU_DEBUG_TRACE_DETAIL << tokens_lens_per_expert_cpu[i] << ", ";
}
- std::cout << std::endl;
- std::cout << "tokens_per_expert[" << num_actually_used_experts << "]:" << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "tokens_per_expert[" << num_actually_used_experts << "]:" << std::endl;
int token_idx = 0;
for (int i = 0; i < num_actually_used_experts; i++) {
- std::cout << "\texpert[" << i << "]: = ";
+ GPU_DEBUG_TRACE_DETAIL << "\texpert[" << i << "]: = ";
for (int j = 0; j < tokens_lens_per_expert_cpu[i]; j++) {
- std::cout << tokens_per_expert_cpu[token_idx + j] << ", ";
+ GPU_DEBUG_TRACE_DETAIL << tokens_per_expert_cpu[token_idx + j] << ", ";
}
token_idx += tokens_lens_per_expert_cpu[i];
- std::cout << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << std::endl;
}
- std::cout << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << std::endl;
}
# endif
}
@@ -1363,9 +1406,9 @@ public:
auto token_per_expert = intermediates_memories[MOE_INTERNAL_BUFFER_TOKEN_IDX_PER_EXPERT]->get_layout().get_shape()[0];
# if DEBUG_MOE_LOG
- std::cout << "\nstep 2: prefill_gather local_threads_count=" << local_threads_count << ", batches_per_thread=" << batches_per_thread
- << ", unaligned_elements=" << unaligned_elements << ", token_per_expert=" << token_per_expert << ", block_size = " << block_size
- << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "\nstep 2: prefill_gather local_threads_count=" << local_threads_count << ", batches_per_thread=" << batches_per_thread
+ << ", unaligned_elements=" << unaligned_elements << ", token_per_expert=" << token_per_expert
+ << ", block_size = " << block_size << std::endl;
# endif
ret_event = execute_stage({ret_event},
instance,
@@ -1392,7 +1435,7 @@ public:
// 0: up/gate output, shape = [token_len * expert_topK, hidden_size]
{
# if DEBUG_MOE_LOG
- std::cout << "\nstep 3: moe_gemm for up and gate" << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "\nstep 3: moe_gemm for up and gate" << std::endl;
# endif
ret_event = PrimitiveImplOCL::execute_stage({ret_event}, instance, micro_gemm_up);
ret_event = PrimitiveImplOCL::execute_stage({ret_event}, instance, micro_gemm_gate);
@@ -1405,9 +1448,9 @@ public:
// output
// 0: gate_up [token_len * expert_topK, hidden_size]
{
- auto token_size = input_shape[0] * max_topk;
+ auto token_size = token_num * max_topk;
# if DEBUG_MOE_LOG
- std::cout << "\nstep 4: prefill_swiglu token_size=" << token_size << ", hidden_size=" << _intermediate_size << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "\nstep 4: prefill_swiglu token_size=" << token_size << ", hidden_size=" << _intermediate_size << std::endl;
# endif
ret_event = execute_stage({ret_event},
instance,
@@ -1433,7 +1476,7 @@ public:
// 0: down output, shape = [token_len * expert_topK, hidden_size]
{
# if DEBUG_MOE_LOG
- std::cout << "\nstep 5: moe_gemm for down" << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "\nstep 5: moe_gemm for down" << std::endl;
# endif
ret_event = PrimitiveImplOCL::execute_stage({ret_event}, instance, micro_gemm_down);
}
@@ -1450,12 +1493,12 @@ public:
// output:
// 0: final hidden states, shape = [token_len, hidden_size]
{
- auto token_size = input_shape[0];
+ auto token_size = token_num;
auto [local_threads_count, batches_per_thread, _] = calc_thread_count(const_cast<RuntimeParams&>(*instance.get_impl_params()), 4, _hidden_size);
# if DEBUG_MOE_LOG
- std::cout << "\nstep 6: prefill_scatter_reduce token_size=" << token_size << ", local_threads_count=" << local_threads_count
- << ", num_actually_used_experts = " << num_actually_used_experts << std::endl;
+ GPU_DEBUG_TRACE_DETAIL << "\nstep 6: prefill_scatter_reduce token_size=" << token_size << ", local_threads_count=" << local_threads_count
+ << ", num_actually_used_experts = " << num_actually_used_experts << std::endl;
# endif
ret_event = execute_stage({ret_event},
@@ -1566,6 +1609,20 @@ public:
return *_kernels.get(key);
}
+ // inputs 0 is hidden_states, inputs 1 is router_logits[num_tokens, NUM_EXPERTS=128]
+ // extra step Softmax_TopK is fused to give topk-id & router_weights
+ //
+ // scratch.topk_id, scratch.full_router_weights = Softmax_TopK(router_logits)
+ //
+ // generate expert_mask from topk-id
+ // expert_mask.batch[i][j] : j'th token index for i'th expert
+ // expert_mask.topk[i][j] : topk-output offset for j'th token for i'th expert, used to get weights
+ // expert_mask.pred_flag[i]: bool, if expert i can be skipped
+ //
+ // scratch.x, scratch.routing_weights = gather(hidden_states, scratch.full_router_weights, expert_mask.batch, expert_mask.topk)
+ // scratch.y = MLP(scratch.x, .gate/up/down) * scratch.routing_weights
+ // scatter(final_hidden, scratch.y, expert_mask.batch)
+ //
cldnn::event::ptr exec_prefill_onednn(const std::vector<cldnn::event::ptr>& events,
cldnn::stream& stream,
typed_primitive_inst<moe_3gemm_fused_compressed>& instance,
@@ -1578,7 +1635,6 @@ public:
auto [hidden_states_mem_ptr, hidden_states_layout] = get_input_info(instance, static_cast<size_t>(MOE3GemmInputIndex::HIDDEN_STATES));
auto& engine = instance.get_network().get_engine();
init_dnnl_weights(cur_moe, engine, scratch.moe_fusion_wei_addr);
- // auto final_hidden_states_layout = instance.get_output_layout(0);
auto routing_mem_ptr = scratch.topk_weights;
auto final_hidden_states_mem_ptr = instance.output_memory_ptr(0);
@@ -1661,20 +1717,6 @@ public:
return result_event;
}
- // inputs 0 is hidden_states, inputs 1 is router_logits[num_tokens, NUM_EXPERTS=128]
- // extra step Softmax_TopK is fused to give topk-id & router_weights
- //
- // scratch.topk_id, scratch.full_router_weights = Softmax_TopK(router_logits)
- //
- // generate expert_mask from topk-id
- // expert_mask.batch[i][j] : j'th token index for i'th expert
- // expert_mask.topk[i][j] : topk-output offset for j'th token for i'th expert, used to get weights
- // expert_mask.pred_flag[i]: bool, if expert i can be skipped
- //
- // scratch.x, scratch.routing_weights = gather(hidden_states, scratch.full_router_weights, expert_mask.batch, expert_mask.topk)
- // scratch.y = MLP(scratch.x, .gate/up/down) * scratch.routing_weights
- // scatter(final_hidden, scratch.y, expert_mask.batch)
- //
cldnn::event::ptr execute(const std::vector<cldnn::event::ptr>& events, cldnn::primitive_inst& ins) override {
OV_ITT_SCOPED_TASK(ov::intel_gpu::itt::domains::intel_gpu_plugin, openvino::itt::handle("moe_3gemm_swiglu_opt_impl::execute"));
auto& instance = reinterpret_cast<typed_primitive_inst<moe_3gemm_fused_compressed>&>(ins);
@@ -1685,10 +1727,9 @@ public:
cldnn::event::ptr ret_env = nullptr;
auto [hidden_states_mem_ptr, hidden_states_layout] = get_input_info(instance, static_cast<size_t>(MOE3GemmInputIndex::HIDDEN_STATES));
- auto batch = static_cast<int>(hidden_states_layout.get_shape()[0]);
-
+ size_t token_num = get_seq_len(hidden_states_layout);
scratch_buffers scratch;
- prepare_internal_buffers(instance, scratch, batch);
+ prepare_internal_buffers(instance, scratch, token_num);
// softmax+topk
auto lws_size = config.num_expert;
@@ -1697,14 +1738,14 @@ public:
*softmax_topk,
{instance.input_memory_ptr(static_cast<size_t>(MOE3GemmInputIndex::ROUTING_WEIGHTS))},
{scratch.topk_id, scratch.topk_weights},
- {static_cast<size_t>(batch), lws_size},
+ {static_cast<size_t>(token_num), lws_size},
{1, lws_size},
instance.needs_completion_event());
// 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 (batch == 1) {
- return exec_single_batch({topk_event}, instance, scratch);
+ if (token_num == 1) {
+ return exec_single_token({topk_event}, instance, scratch);
}
// onednn path will accumulate to the output
@@ -1718,7 +1759,7 @@ public:
topk_event->wait();
}
- GPU_DEBUG_TRACE_DETAIL << "\nMoE3GemmFusedCompressed exec(): batch=" << batch << ", max_topk=" << static_cast<int>(config.top_k)
+ GPU_DEBUG_TRACE_DETAIL << "\nMoE3GemmFusedCompressed exec(): token_num=" << token_num << ", max_topk=" << static_cast<int>(config.top_k)
<< ", use_micro_gemm_prefill=" << use_micro_gemm_prefill << std::endl;
update_rt_params(instance);
if (use_micro_gemm_prefill) {
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 9ca04854bb9..2fe76184317 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
@@ -40,9 +40,11 @@ struct moe_3gemm_swiglu_opt : public ImplementationManager {
return false;
}
- // Only support weight: u4
+ // Only support weight: u4, i8, u8
static constexpr std::array supported_wei_type = {
ov::element::u4,
+ ov::element::u8,
+ ov::element::i8,
};
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)) {
@@ -58,9 +60,10 @@ struct moe_3gemm_swiglu_opt : public ImplementationManager {
return false;
}
- // Only support zp: u4
+ // Only support zp: u4, u8
static constexpr std::array supported_zp_type = {
ov::element::u4,
+ ov::element::u8,
};
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)) {
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 4ed1006c539..d29dfe7cfbc 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
@@ -9,26 +9,27 @@
#define FAKE_GROUP_SIZE 128
#if GATE_UP_ENABLE
-inline void gemv_n2x(const __global uchar* weight,
- __global half* scales,
- __global uchar* zps,
- const __global half* x,
- __global half* y, int N, int K,
- half* x2,
- float* xg_sum,
- const bool silu) {
+inline void gate_up_gemv_n2x_u4(const __global uchar* weight,
+ __global half* scales,
+ __global uchar* zps,
+ __global half* y,
+ int N,
+ int K,
+ half* x2,
+ float* xg_sum,
+ const bool silu) {
int num_sg = get_num_sub_groups();
int id_local = get_sub_group_local_id();
int n_start = get_global_id(2) * N_BLOCK;
int n_end = n_start + N_BLOCK;
- unroll_for (int n = n_start; n < n_end; n+=2) {
+ unroll_for(int n = n_start; n < n_end; n += 2) {
const __global uchar* B = weight + n * K / 2;
float sum_all0 = 0;
float sum_all1 = 0;
__global half* S = scales + n;
__global uchar* Z = zps + n / 2;
- unroll_for (int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {
+ 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];
@@ -37,12 +38,12 @@ inline void gemv_n2x(const __global uchar* weight,
half z_hf0 = convert_half(z & 0xf);
half z_hf1 = convert_half(z >> 4);
-#if SUBGROUP_SIZE == 32
+# if SUBGROUP_SIZE == 32
half2 sum0;
half2 sum1;
- 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));
+ 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);
@@ -56,12 +57,12 @@ inline void gemv_n2x(const __global uchar* weight,
sum_all0 += (sum0[0] + sum0[1] - xg_sum[gk] * z_hf0) * s0;
sum_all1 += (sum1[0] + sum1[1] - xg_sum[gk] * z_hf1) * s1;
-#else
+# else
half4 sum0;
half4 sum1;
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));
+ 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);
@@ -85,7 +86,7 @@ inline void gemv_n2x(const __global uchar* weight,
sum_all0 += (sum0[0] + sum0[1] + sum0[2] + sum0[3] - xg_sum[gk] * z_hf0) * s0;
sum_all1 += (sum1[0] + sum1[1] + sum1[2] + sum1[3] - xg_sum[gk] * z_hf1) * s1;
-#endif
+# endif
}
sum_all0 = sub_group_reduce_add(sum_all0);
@@ -93,68 +94,244 @@ inline void gemv_n2x(const __global uchar* weight,
if (id_local == 0) {
if (silu) {
y[n] *= sum_all0 / (1 + exp(-sum_all0));
- y[n+1] *= sum_all1 / (1 + exp(-sum_all1));
+ y[n + 1] *= sum_all1 / (1 + exp(-sum_all1));
} else {
y[n] = sum_all0;
- y[n+1] = sum_all1;
+ y[n + 1] = sum_all1;
}
}
}
}
-__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE)))
-KERNEL (mlp_gate_up)(
- const __global int* expert_list,
- const __global uchar* gate_weight_addr,
- const __global uchar* gate_scale_addr,
- const __global uchar* gate_zp_addr,
- const __global uchar* up_weight_addr,
- const __global uchar* up_scale_addr,
- const __global uchar* up_zp_addr,
- __global MOE_DTYPE* x, // [1, HIDDEN_SIZE]
- __global MOE_DTYPE* y) { // [MAX_TOPK, INTERMEDIATE_SIZE]
+
+inline void gate_up_gemv_n2x_u8(const __global uchar* weight,
+ __global half* scales,
+ __global uchar* zps,
+ __global half* y,
+ int N,
+ int K,
+ half* x2,
+ float* xg_sum,
+ const bool silu) {
+ int num_sg = get_num_sub_groups();
+ int id_local = get_sub_group_local_id();
+
+ int n_start = get_global_id(2) * N_BLOCK;
+ int n_end = n_start + N_BLOCK;
+ unroll_for(int n = n_start; n < n_end; n += 2) {
+ const __global uchar* B = weight + n * K;
+ float sum_all0 = 0;
+ float sum_all1 = 0;
+ __global half* S = scales + n;
+ __global uchar* Z = zps + n;
+ 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;
+ half s0 = S[scale_offset];
+ half s1 = S[scale_offset + 1];
+ half z0 = convert_half(Z[zp_offset]);
+ half z1 = convert_half(Z[zp_offset + 1]);
+
+# if SUBGROUP_SIZE == 32
+ float2 sum0;
+ float2 sum1;
+ half4 a = as_half4(intel_sub_group_block_read_us4((const __local ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ uchar4 b = intel_sub_group_block_read_uc4((const __global uchar*)B + gk * FAKE_GROUP_SIZE);
+ uchar4 b2 = intel_sub_group_block_read_uc4((const __global uchar*)(B + K + gk * FAKE_GROUP_SIZE));
+
+ sum0.s0 = fma((float)a.s0, (float)(convert_half(b.s0)), 0.0f);
+ sum0.s1 = fma((float)a.s1, (float)(convert_half(b.s1)), 0.0f);
+ sum0.s0 = fma((float)a.s2, (float)(convert_half(b.s2)), sum0.s0);
+ sum0.s1 = fma((float)a.s3, (float)(convert_half(b.s3)), sum0.s1);
+
+ sum1.s0 = fma((float)a.s0, (float)(convert_half(b2.s0)), 0.0f);
+ sum1.s1 = fma((float)a.s1, (float)(convert_half(b2.s1)), 0.0f);
+ sum1.s0 = fma((float)a.s2, (float)(convert_half(b2.s2)), sum1.s0);
+ sum1.s1 = fma((float)a.s3, (float)(convert_half(b2.s3)), sum1.s1);
+
+ sum_all0 += (sum0[0] + sum0[1] - xg_sum[gk] * z0) * s0;
+ sum_all1 += (sum1[0] + sum1[1] - xg_sum[gk] * z1) * s1;
+# else
+ float4 sum0;
+ float4 sum1;
+ half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ uchar8 b = intel_sub_group_block_read_uc8((const __global uchar*)B + gk * FAKE_GROUP_SIZE);
+ uchar8 b2 = intel_sub_group_block_read_uc8((const __global uchar*)(B + K + gk * FAKE_GROUP_SIZE));
+
+ sum0.s0 = fma((float)a.s0, (float)(convert_half(b.s0)), 0.0f);
+ sum0.s1 = fma((float)a.s1, (float)(convert_half(b.s1)), 0.0f);
+ sum0.s2 = fma((float)a.s2, (float)(convert_half(b.s2)), 0.0f);
+ sum0.s3 = fma((float)a.s3, (float)(convert_half(b.s3)), 0.0f);
+
+ sum0.s0 = fma((float)a.s4, (float)(convert_half(b.s4)), sum0.s0);
+ sum0.s1 = fma((float)a.s5, (float)(convert_half(b.s5)), sum0.s1);
+ sum0.s2 = fma((float)a.s6, (float)(convert_half(b.s6)), sum0.s2);
+ sum0.s3 = fma((float)a.s7, (float)(convert_half(b.s7)), sum0.s3);
+
+ sum1.s0 = fma((float)a.s0, (float)(convert_half(b2.s0)), 0.0f);
+ sum1.s1 = fma((float)a.s1, (float)(convert_half(b2.s1)), 0.0f);
+ sum1.s2 = fma((float)a.s2, (float)(convert_half(b2.s2)), 0.0f);
+ sum1.s3 = fma((float)a.s3, (float)(convert_half(b2.s3)), 0.0f);
+
+ sum1.s0 = fma((float)a.s4, (float)(convert_half(b2.s4)), sum1.s0);
+ sum1.s1 = fma((float)a.s5, (float)(convert_half(b2.s5)), sum1.s1);
+ sum1.s2 = fma((float)a.s6, (float)(convert_half(b2.s6)), sum1.s2);
+ sum1.s3 = fma((float)a.s7, (float)(convert_half(b2.s7)), sum1.s3);
+
+ sum_all0 += (sum0[0] + sum0[1] + sum0[2] + sum0[3] - xg_sum[gk] * z0) * s0;
+ sum_all1 += (sum1[0] + sum1[1] + sum1[2] + sum1[3] - xg_sum[gk] * z1) * s1;
+# endif
+ }
+
+ sum_all0 = sub_group_reduce_add(sum_all0);
+ sum_all1 = sub_group_reduce_add(sum_all1);
+ if (id_local == 0) {
+ if (silu) {
+ y[n] *= sum_all0 / (1 + exp(-sum_all0));
+ y[n + 1] *= sum_all1 / (1 + exp(-sum_all1));
+ } else {
+ y[n] = sum_all0;
+ y[n + 1] = sum_all1;
+ }
+ }
+ }
+}
+
+inline void gate_up_gemv_n2x_f16(const __global half* weight, __global half* y, int N, int K, half* x2, const bool silu) {
+ int num_sg = get_num_sub_groups();
+ int id_local = get_sub_group_local_id();
+
+ int n_start = get_global_id(2) * N_BLOCK;
+ int n_end = n_start + N_BLOCK;
+ unroll_for(int n = n_start; n < n_end; n += 2) {
+ const __global half* B = weight + n * K;
+ float sum_all0 = 0;
+ float sum_all1 = 0;
+ unroll_for(int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {
+# if SUBGROUP_SIZE == 32
+ half2 sum0;
+ half2 sum1;
+ half4 a = as_half4(intel_sub_group_block_read_us4((const __global ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ half4 b = as_half4(intel_sub_group_block_read_us4((const __global ushort*)B + gk * FAKE_GROUP_SIZE));
+ half4 b2 = as_half4(intel_sub_group_block_read_us4((const __global ushort*)B + K + gk * FAKE_GROUP_SIZE));
+
+ sum0.s0 = fma(a.s0, b.s0, 0);
+ sum0.s1 = fma(a.s1, b.s1, 0);
+ sum0.s0 = fma(a.s2, b.s2, sum0.s0);
+ sum0.s1 = fma(a.s3, b.s3, sum0.s1);
+
+ sum1.s0 = fma(a.s0, b2.s0, 0);
+ sum1.s1 = fma(a.s1, b2.s1, 0);
+ sum1.s0 = fma(a.s2, b2.s2, sum1.s0);
+ sum1.s1 = fma(a.s3, b2.s3, sum1.s1);
+
+ sum_all0 += sum0[0] + sum0[1];
+ sum_all1 += sum1[0] + sum1[1];
+# else
+ half4 sum0;
+ half4 sum1;
+ half8 a = as_half8(intel_sub_group_block_read_us8((const __global ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ half8 b = as_half8(intel_sub_group_block_read_us8((const __global ushort*)B + gk * FAKE_GROUP_SIZE));
+ half8 b2= as_half8(intel_sub_group_block_read_us8((const __global ushort*)(B + K + gk * FAKE_GROUP_SIZE)));
+
+ sum0.s0 = fma(a.s0, b.s0, 0);
+ sum0.s1 = fma(a.s1, b.s1, 0);
+ sum0.s2 = fma(a.s2, b.s2, 0);
+ sum0.s3 = fma(a.s3, b.s3, 0);
+
+ sum0.s0 = fma(a.s4, b.s4, sum0.s0);
+ sum0.s1 = fma(a.s5, b.s5, sum0.s1);
+ sum0.s2 = fma(a.s6, b.s6, sum0.s2);
+ sum0.s3 = fma(a.s7, b.s7, sum0.s3);
+
+ sum1.s0 = fma(a.s0, b2.s0, 0);
+ sum1.s1 = fma(a.s1, b2.s1, 0);
+ sum1.s2 = fma(a.s2, b2.s2, 0);
+ sum1.s3 = fma(a.s3, b2.s3, 0);
+
+ sum1.s0 = fma(a.s4, b2.s4, sum1.s0);
+ sum1.s1 = fma(a.s5, b2.s5, sum1.s1);
+ sum1.s2 = fma(a.s6, b2.s6, sum1.s2);
+ sum1.s3 = fma(a.s7, b2.s7, sum1.s3);
+
+ sum_all0 += sum0[0] + sum0[1] + sum0[2] + sum0[3] ;
+ sum_all1 += sum1[0] + sum1[1] + sum1[2] + sum1[3];
+# endif
+ }
+
+ sum_all0 = sub_group_reduce_add(sum_all0);
+ sum_all1 = sub_group_reduce_add(sum_all1);
+ if (id_local == 0) {
+ if (silu) {
+ y[n] *= sum_all0 / (1 + exp(-sum_all0));
+ y[n + 1] *= sum_all1 / (1 + exp(-sum_all1));
+ } else {
+ y[n] = sum_all0;
+ y[n + 1] = sum_all1;
+ }
+ }
+ }
+}
+
+__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_gate_up)(const __global int* expert_list,
+ const __global MOE_WEI_DT* gate_weight_addr,
+ const __global MOE_SCALE_DT* gate_scale_addr,
+ const __global MOE_ZP_DT* gate_zp_addr,
+ const __global MOE_WEI_DT* up_weight_addr,
+ const __global MOE_SCALE_DT* up_scale_addr,
+ const __global MOE_ZP_DT* up_zp_addr,
+ __global MOE_DTYPE* x, // [1, HIDDEN_SIZE]
+ __global MOE_DTYPE* y) { // [MAX_TOPK, 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;
+# if WEIGHT_COMPRESSEION_DT == 0
const int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2;
- const int expert_scale_size = INTERMEDIATE_SIZE * HIDDEN_SIZE * 2 / GATE_UP_GROUP_SIZE;
+ 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
+
int expert_id = expert_list[expert_no];
// gate, [HIDDEN_SIZE, INTERMEDIATE_SIZE]
- __global uchar* gate_weight = (__global uchar*)(gate_weight_addr + expert_id * expert_wei_size);
- __global half* gate_scale = (__global half*)(gate_scale_addr + expert_id * expert_scale_size);
- __global uchar* gate_zp = (__global uchar*)(gate_zp_addr + expert_id * expert_zp_size);
+ __global MOE_WEI_DT* gate_weight = (__global MOE_WEI_DT*)(gate_weight_addr + expert_id * expert_wei_size);
+ __global MOE_SCALE_DT* gate_scale = (__global MOE_SCALE_DT*)(gate_scale_addr + expert_id * expert_scale_size);
+ __global MOE_ZP_DT* gate_zp = (__global MOE_ZP_DT*)(gate_zp_addr + expert_id * expert_zp_size);
// up, [HIDDEN_SIZE, INTERMEDIATE_SIZE]
- __global uchar* up_weight = (__global uchar*)(up_weight_addr + expert_id * expert_wei_size);
- __global half* up_scale = (__global half*)(up_scale_addr + expert_id * expert_scale_size);
- __global uchar* up_zp = (__global uchar*)(up_zp_addr + expert_id * expert_zp_size);
-
- __local half x2[HIDDEN_SIZE];
- __local float xg_sum[HIDDEN_SIZE/32];
+ __global MOE_WEI_DT* up_weight = (__global MOE_WEI_DT*)(up_weight_addr + expert_id * expert_wei_size);
+ __global MOE_SCALE_DT* up_scale = (__global MOE_SCALE_DT*)(up_scale_addr + expert_id * expert_scale_size);
+ __global MOE_ZP_DT* up_zp = (__global MOE_ZP_DT*)(up_zp_addr + expert_id * expert_zp_size);
-#if GATE_UP_GROUP_SIZE % FAKE_GROUP_SIZE != 0
+# 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
+# endif
+ __local half x2[HIDDEN_SIZE];
+ __local float xg_sum[HIDDEN_SIZE / FAKE_GROUP_SIZE];
+
+# if WEIGHT_COMPRESSEION_DT == 0
//# interleaving x into x2
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 * 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) {
+ 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 group
float x_group_sum = 0;
- 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];
+ 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 + FAKE_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);
@@ -162,76 +339,65 @@ KERNEL (mlp_gate_up)(
xg_sum[i] = x_group_sum / SUBGROUP_SIZE;
}
}
- barrier(CLK_LOCAL_MEM_FENCE);
-
- gemv_n2x(up_weight, up_scale, up_zp, x, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, false);
- gemv_n2x(gate_weight, gate_scale, gate_zp, x, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, xg_sum, true);
-}
-
-#elif DOWN_ENABLE
-__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE)))
-KERNEL (mlp_down)(
- const __global int* expert_list,
- const __global uchar* down_weight_addr,
- const __global uchar* down_scale_addr,
- const __global uchar* down_zp_addr,
- 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 int expert_wei_size = INTERMEDIATE_SIZE * HIDDEN_SIZE / 2;
- 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]
- __global uchar* weight = (__global uchar*)(down_weight_addr + expert_id * expert_wei_size);
- __global half* scales = (__global half*)(down_scale_addr + expert_id * expert_scale_size);
- __global uchar* zps = (__global uchar*)(down_zp_addr + expert_id * expert_zp_size);
-
- int N = HIDDEN_SIZE;
- int K = INTERMEDIATE_SIZE;
- int num_sg = get_num_sub_groups();
+# else
+ //# load x into slm
int id_sg = get_sub_group_id();
+ int num_sg = get_num_sub_groups();
int id_local = get_sub_group_local_id();
-
- __local half x2[INTERMEDIATE_SIZE];
- __local float xg_sum[INTERMEDIATE_SIZE/32];
-
- //# interleaving x into x2
- __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) {
+ 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 group
float x_group_sum = 0;
- 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 + FAKE_GROUP_SIZE/2] = odd;
- x_group_sum += even + odd;
+ unroll_for(int j = id_local; j < FAKE_GROUP_SIZE; j += SUBGROUP_SIZE) {
+ half value = px[j];
+ px2[j] = value;
+ x_group_sum += value;
}
x_group_sum = sub_group_reduce_add(x_group_sum);
if (id_local == 0) {
xg_sum[i] = x_group_sum / SUBGROUP_SIZE;
}
}
+# endif
+
barrier(CLK_LOCAL_MEM_FENCE);
+# if WEIGHT_COMPRESSEION_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
+ 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
+ gate_up_gemv_n2x_f16(up_weight, up_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, false);
+ gate_up_gemv_n2x_f16(gate_weight, gate_zp, y, INTERMEDIATE_SIZE, HIDDEN_SIZE, x2, true);
+# endif
+}
+
+#elif DOWN_ENABLE
+
+inline void down_gemv_n2x_u4(const __global uchar* weight,
+ __global half* scales,
+ __global uchar* zps,
+ __global MOE_DTYPE* routing_weights,
+ __global half* y,
+ int N,
+ int K,
+ half* x2,
+ float* xg_sum) {
+ int id_local = get_sub_group_local_id();
+ int expert_no = get_global_id(0);
int n_start = get_global_id(2) * N_BLOCK;
int n_end = n_start + N_BLOCK;
- unroll_for (int n = n_start; n < n_end; n+=2) {
+ unroll_for(int n = n_start; n < n_end; n += 2) {
const __global uchar* B = weight + n * K / 2;
__global half* S = scales + n;
__global uchar* Z = zps + n / 2;
float sum_all0 = 0;
float sum_all1 = 0;
- unroll_for (int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {
+ 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];
@@ -240,12 +406,12 @@ KERNEL (mlp_down)(
half z_hf0 = convert_half(z & 0xf);
half z_hf1 = convert_half(z >> 4);
-#if SUBGROUP_SIZE == 32
+# if SUBGROUP_SIZE == 32
half2 sum0;
half2 sum1;
- 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));
+ 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);
@@ -259,12 +425,12 @@ KERNEL (mlp_down)(
sum_all0 += (sum0[0] + sum0[1] - xg_sum[gk] * z_hf0) * s0;
sum_all1 += (sum1[0] + sum1[1] - xg_sum[gk] * z_hf1) * s1;
-#else
+# else
half4 sum0;
half4 sum1;
- 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));
+ 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);
@@ -288,26 +454,271 @@ KERNEL (mlp_down)(
sum_all0 += (sum0[0] + sum0[1] + sum0[2] + sum0[3] - xg_sum[gk] * z_hf0) * s0;
sum_all1 += (sum1[0] + sum1[1] + sum1[2] + sum1[3] - xg_sum[gk] * z_hf1) * s1;
-#endif
+# endif
+ }
+ sum_all0 = sub_group_reduce_add(sum_all0);
+ sum_all1 = sub_group_reduce_add(sum_all1);
+ if (id_local == 0) {
+ y[n] = sum_all0 * routing_weights[expert_no];
+ y[n + 1] = sum_all1 * routing_weights[expert_no];
+ }
+ }
+}
+
+inline void down_gemv_n2x_u8(const __global uchar* weight,
+ __global half* scales,
+ __global uchar* zps,
+ __global MOE_DTYPE* routing_weights,
+ __global half* y,
+ int N,
+ int K,
+ half* x2,
+ float* xg_sum) {
+ int id_local = get_sub_group_local_id();
+ int expert_no = get_global_id(0);
+ int n_start = get_global_id(2) * N_BLOCK;
+ int n_end = n_start + N_BLOCK;
+
+ unroll_for(int n = n_start; n < n_end; n += 2) {
+ const __global uchar* B = weight + n * K;
+ __global half* S = scales + n;
+ __global uchar* Z = zps + n;
+ float sum_all0 = 0;
+ float sum_all1 = 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;
+ half s0 = S[scale_offset];
+ half s1 = S[scale_offset + 1];
+ half z0 = convert_half(Z[zp_offset]);
+ half z1 = convert_half(Z[zp_offset + 1]);
+
+# if SUBGROUP_SIZE == 32
+ float2 sum0;
+ float2 sum1;
+ half4 a = as_half4(intel_sub_group_block_read_us4((const __local ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ uchar4 b = intel_sub_group_block_read_uc4((const __global uchar*)B + gk * FAKE_GROUP_SIZE);
+ uchar4 b2 = intel_sub_group_block_read_uc4((const __global uchar*)B + K + gk * FAKE_GROUP_SIZE);
+
+ sum0.s0 = fma((float)a.s0, (float)(convert_half(b.s0)), 0.0f);
+ sum0.s1 = fma((float)a.s1, (float)(convert_half(b.s1)), 0.0f);
+ sum0.s0 = fma((float)a.s2, (float)(convert_half(b.s2)), sum0.s0);
+ sum0.s1 = fma((float)a.s3, (float)(convert_half(b.s3)), sum0.s1);
+
+ sum1.s0 = fma((float)a.s0, (float)(convert_half(b2.s0)), 0.0f);
+ sum1.s1 = fma((float)a.s1, (float)(convert_half(b2.s1)), 0.0f);
+ sum1.s0 = fma((float)a.s2, (float)(convert_half(b2.s2)), sum1.s0);
+ sum1.s1 = fma((float)a.s3, (float)(convert_half(b2.s3)), sum1.s1);
+
+ sum_all0 += (sum0[0] + sum0[1] - xg_sum[gk] * z0) * s0;
+ sum_all1 += (sum1[0] + sum1[1] - xg_sum[gk] * z1) * s1;
+# else
+ float4 sum0;
+ float4 sum1;
+ half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ uchar8 b = intel_sub_group_block_read_uc8((const __global uchar*)B + gk * FAKE_GROUP_SIZE);
+ uchar8 b2 = intel_sub_group_block_read_uc8((const __global uchar*)(B + K + gk * FAKE_GROUP_SIZE));
+
+ sum0.s0 = fma((float)a.s0, (float)(convert_half(b.s0)), 0.0f);
+ sum0.s1 = fma((float)a.s1, (float)(convert_half(b.s1)), 0.0f);
+ sum0.s2 = fma((float)a.s2, (float)(convert_half(b.s2)), 0.0f);
+ sum0.s3 = fma((float)a.s3, (float)(convert_half(b.s3)), 0.0f);
+
+ sum0.s0 = fma((float)a.s4, (float)(convert_half(b.s4)), sum0.s0);
+ sum0.s1 = fma((float)a.s5, (float)(convert_half(b.s5)), sum0.s1);
+ sum0.s2 = fma((float)a.s6, (float)(convert_half(b.s6)), sum0.s2);
+ sum0.s3 = fma((float)a.s7, (float)(convert_half(b.s7)), sum0.s3);
+
+ sum1.s0 = fma((float)a.s0, (float)(convert_half(b2.s0)), 0.0f);
+ sum1.s1 = fma((float)a.s1, (float)(convert_half(b2.s1)), 0.0f);
+ sum1.s2 = fma((float)a.s2, (float)(convert_half(b2.s2)), 0.0f);
+ sum1.s3 = fma((float)a.s3, (float)(convert_half(b2.s3)), 0.0f);
+
+ sum1.s0 = fma((float)a.s4, (float)(convert_half(b2.s4)), sum1.s0);
+ sum1.s1 = fma((float)a.s5, (float)(convert_half(b2.s5)), sum1.s1);
+ sum1.s2 = fma((float)a.s6, (float)(convert_half(b2.s6)), sum1.s2);
+ sum1.s3 = fma((float)a.s7, (float)(convert_half(b2.s7)), sum1.s3);
+
+ sum_all0 += (sum0[0] + sum0[1] + sum0[2] + sum0[3] - xg_sum[gk] * z0) * s0;
+ sum_all1 += (sum1[0] + sum1[1] + sum1[2] + sum1[3] - xg_sum[gk] * z1) * s1;
+# endif
+ }
+ sum_all0 = sub_group_reduce_add(sum_all0);
+ sum_all1 = sub_group_reduce_add(sum_all1);
+ if (id_local == 0) {
+ y[n] = sum_all0 * routing_weights[expert_no];
+ y[n + 1] = sum_all1 * routing_weights[expert_no];
+ }
+ }
+}
+
+inline void down_gemv_n2x_f16(const __global half* weight, __global MOE_DTYPE* routing_weights, __global half* y, int N, int K, half* x2) {
+ int id_local = get_sub_group_local_id();
+ int expert_no = get_global_id(0);
+ int n_start = get_global_id(2) * N_BLOCK;
+ int n_end = n_start + N_BLOCK;
+
+ unroll_for(int n = n_start; n < n_end; n += 2) {
+ const __global half* B = weight + n * K;
+ float sum_all0 = 0;
+ float sum_all1 = 0;
+ unroll_for(int gk = 0; gk < K / FAKE_GROUP_SIZE; gk++) {
+
+# if SUBGROUP_SIZE == 32
+ half2 sum0;
+ half2 sum1;
+ half4 a = as_half4(intel_sub_group_block_read_us4((const __global ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ half4 b = as_half4(intel_sub_group_block_read_us4((const __global ushort*)B + gk * FAKE_GROUP_SIZE));
+ half4 b2 = as_half4(intel_sub_group_block_read_us4((const __global ushort*)B + K + gk * FAKE_GROUP_SIZE));
+
+ sum0.s0 = fma(a.s0, b.s0, 0);
+ sum0.s1 = fma(a.s1, b.s1, 0);
+ sum0.s0 = fma(a.s2, b.s2, sum0.s0);
+ sum0.s1 = fma(a.s3, b.s3, sum0.s1);
+
+ sum1.s0 = fma(a.s0, b2.s0, 0);
+ sum1.s1 = fma(a.s1, b2.s1, 0);
+ sum1.s0 = fma(a.s2, b2.s2, sum1.s0);
+ sum1.s1 = fma(a.s3, b2.s3, sum1.s1);
+
+ sum_all0 += sum0[0] + sum0[1];
+ sum_all1 += sum1[0] + sum1[1];
+# else
+ half4 sum0;
+ half4 sum1;
+ half8 a = as_half8(intel_sub_group_block_read_us8((const __local ushort*)x2 + gk * FAKE_GROUP_SIZE));
+ half8 b = as_half8(intel_sub_group_block_read_us8((const __global ushort*)B + gk * FAKE_GROUP_SIZE));
+ half8 b2 = as_half8(intel_sub_group_block_read_us8((const __global ushort*)B + K + gk * FAKE_GROUP_SIZE));
+
+ sum0.s0 = fma(a.s0, b.s0, 0);
+ sum0.s1 = fma(a.s1, b.s1, 0);
+ sum0.s2 = fma(a.s2, b.s2, 0);
+ sum0.s3 = fma(a.s3, b.s3, 0);
+
+ sum0.s0 = fma(a.s4, b.s4, sum0.s0);
+ sum0.s1 = fma(a.s5, b.s5, sum0.s1);
+ sum0.s2 = fma(a.s6, b.s6, sum0.s2);
+ sum0.s3 = fma(a.s7, b.s7, sum0.s3);
+
+ sum1.s0 = fma(a.s0, b2.s0, 0);
+ sum1.s1 = fma(a.s1, b2.s1, 0);
+ sum1.s2 = fma(a.s2, b2.s2, 0);
+ sum1.s3 = fma(a.s3, b2.s3, 0);
+
+ sum1.s0 = fma(a.s4, b2.s4, sum1.s0);
+ sum1.s1 = fma(a.s5, b2.s5, sum1.s1);
+ sum1.s2 = fma(a.s6, b2.s6, sum1.s2);
+ sum1.s3 = fma(a.s7, b2.s7, sum1.s3);
+
+ sum_all0 += sum0[0] + sum0[1] + sum0[2] + sum0[3];
+ sum_all1 += sum1[0] + sum1[1] + sum1[2] + sum1[3];
+# endif
}
sum_all0 = sub_group_reduce_add(sum_all0);
sum_all1 = sub_group_reduce_add(sum_all1);
if (id_local == 0) {
y[n] = sum_all0 * routing_weights[expert_no];
- y[n+1] = sum_all1 * routing_weights[expert_no];
+ y[n + 1] = sum_all1 * routing_weights[expert_no];
}
}
}
+__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE))) KERNEL(mlp_down)(const __global int* expert_list,
+ const __global MOE_WEI_DT* down_weight_addr,
+ const __global MOE_SCALE_DT* down_scale_addr,
+ const __global MOE_ZP_DT* down_zp_addr,
+ 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;
+
+# 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
+ int expert_id = expert_list[expert_no];
+
+ // down, [INTERMEDIATE_SIZE, HIDDEN_SIZE]
+ __global MOE_WEI_DT* weight = (__global MOE_WEI_DT*)(down_weight_addr + expert_id * expert_wei_size);
+ __global MOE_SCALE_DT* scales = (__global MOE_SCALE_DT*)(down_scale_addr + expert_id * expert_scale_size);
+ __global MOE_ZP_DT* zps = (__global MOE_ZP_DT*)(down_zp_addr + expert_id * expert_zp_size);
+
+ int N = HIDDEN_SIZE;
+ int K = INTERMEDIATE_SIZE;
+
+ __local half x2[INTERMEDIATE_SIZE];
+ __local float xg_sum[INTERMEDIATE_SIZE / FAKE_GROUP_SIZE];
+
+# if WEIGHT_COMPRESSEION_DT == 0
+ //# interleaving x into x2
+ 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* 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 group
+ float x_group_sum = 0;
+ 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 + FAKE_GROUP_SIZE / 2] = odd;
+ x_group_sum += even + odd;
+ }
+ x_group_sum = sub_group_reduce_add(x_group_sum);
+ if (id_local == 0) {
+ xg_sum[i] = x_group_sum / SUBGROUP_SIZE;
+ }
+ }
+# else
+ //# load x into slm
+ 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* 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 group
+ float x_group_sum = 0;
+ unroll_for(int j = id_local; j < FAKE_GROUP_SIZE; j += SUBGROUP_SIZE) {
+ half value = px[j];
+ px2[j] = value;
+ x_group_sum += value;
+ }
+ x_group_sum = sub_group_reduce_add(x_group_sum);
+ if (id_local == 0) {
+ xg_sum[i] = x_group_sum / SUBGROUP_SIZE;
+ }
+ }
+# endif
+
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+# if WEIGHT_COMPRESSEION_DT == 0
+ down_gemv_n2x_u4(weight, scales, zps, routing_weights, y, N, K, x2, xg_sum);
+# elif WEIGHT_COMPRESSEION_DT == 1
+ down_gemv_n2x_u8(weight, scales, zps, routing_weights, y, N, K, x2, xg_sum);
+# elif WEIGHT_COMPRESSEION_DT == 2
+ down_gemv_n2x_f16(weight, routing_weights, y, N, K, x2);
+# endif
+}
+
#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, // [MAX_TOPK, HIDDEN_SIZE]
+ __global MOE_DTYPE* y) { // [1, HIDDEN_SIZE]
int n = get_global_id(1);
half sum[MAX_TOPK] = {0};
- __attribute__((opencl_unroll_hint(MAX_TOPK)))
- for (int i = 0; i < MAX_TOPK; i++) {
- sum[i] = as_half(intel_sub_group_block_read_us((const __global ushort*)(x + i*HIDDEN_SIZE + n)));
+ __attribute__((opencl_unroll_hint(MAX_TOPK))) for (int i = 0; i < MAX_TOPK; i++) {
+ sum[i] = as_half(intel_sub_group_block_read_us((const __global ushort*)(x + i * HIDDEN_SIZE + n)));
}
for (int i = 1; i < MAX_TOPK; i++) {
sum[0] += sum[i];
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.cpp
index a238010b394..a50ac0781dd 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
@@ -74,8 +74,8 @@ namespace ov::intel_gpu {
mul_m_##SUFFIX, mul2_m_##SUFFIX, convert_mul_m_##SUFFIX});
#define MOE_COMPRESSED_WEIGHT_GEMM3_PATTERN(SUFFIX)\
- auto gemm3_compressed_weights_m_##SUFFIX = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));\
- auto gemm3_zp_m_##SUFFIX = wrap_type<ov::op::v0::Constant>(type_matches(ov::element::u4));\
+ auto gemm3_compressed_weights_m_##SUFFIX = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4,ov::element::u8}));\
+ auto gemm3_zp_m_##SUFFIX = wrap_type<ov::op::v0::Constant>(type_matches_any({ov::element::u4, ov::element::u8}));\
\
auto gemm3_weight_convert_m_##SUFFIX = wrap_type<ov::op::v0::Convert>({gemm3_compressed_weights_m_##SUFFIX}, type_matches(ov::element::f16));\
auto gemm3_zp_convert_m_##SUFFIX = wrap_type<ov::op::v0::Convert>({gemm3_zp_m_##SUFFIX}, type_matches(ov::element::f16));\
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 2594261e30a..2d4cc89b2a7 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
@@ -1,25 +1,384 @@
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
+#include <algorithm>
+#include <cmath>
#include <intel_gpu/primitives/data.hpp>
#include <intel_gpu/primitives/fully_connected.hpp>
#include <intel_gpu/primitives/moe_3gemm_fused_compressed.hpp>
#include <intel_gpu/primitives/reorder.hpp>
+#include <intel_gpu/runtime/engine.hpp>
+#include <intel_gpu/runtime/layout.hpp>
+#include <intel_gpu/runtime/memory.hpp>
#include <iostream>
#include <numeric>
-#include "random_generator.hpp"
-#include "test_utils.h"
+#include "../../common/random_generator.hpp"
+#include "../test_utils/test_utils.h"
#include "moe_3gemm_test_data.h"
using namespace cldnn;
using namespace ::tests;
-TEST(moe_3gemm_compressed_gpu, moe_accuracy_test) {
+struct Moe3GemmConfig {
+ size_t batch_size;
+ size_t seq_len;
+ size_t hidden_size;
+ size_t inter_size;
+ size_t num_experts;
+ size_t top_k;
+ size_t group_size;
+ bool is_u4;
+};
+
+struct Moe3GemmReference {
+ Moe3GemmConfig config;
+ tests::random_generator& rg;
+
+ Moe3GemmReference(Moe3GemmConfig config, tests::random_generator& rg) : config(config), rg(rg) {}
+
+ std::vector<uint8_t> pack(const std::vector<uint8_t>& unpacked) {
+ if (!config.is_u4)
+ return unpacked;
+
+ std::vector<uint8_t> packed;
+ packed.reserve((unpacked.size() + 1) / 2);
+ for (size_t i = 0; i < unpacked.size(); i += 2) {
+ uint8_t val1 = unpacked[i] & 0xF;
+ uint8_t val2 = (i + 1 < unpacked.size()) ? (unpacked[i + 1] & 0xF) : 0;
+ packed.push_back(val1 | (val2 << 4));
+ }
+ return packed;
+ }
+
+ std::tuple<std::vector<uint8_t>, std::vector<ov::float16>, std::vector<uint8_t>> quantize(const std::vector<float>& data,
+ size_t expert_num,
+ size_t rows,
+ size_t cols,
+ size_t group_size) {
+ std::vector<uint8_t> q_data(expert_num * rows * cols);
+ std::vector<ov::float16> scales;
+ std::vector<uint8_t> zps;
+
+ size_t num_groups_per_row = rows / group_size;
+ size_t num_groups = cols * num_groups_per_row;
+ float max_range = config.is_u4 ? 7.0f : 255.0f;
+ float zp_max_range = config.is_u4 ? 127.0f : 255.0f;
+
+ scales.resize(expert_num * num_groups);
+ zps.resize(expert_num * num_groups);
+
+ for (size_t e = 0; e < expert_num; ++e) {
+ size_t offset = e * rows * cols;
+ for (size_t c = 0; c < cols; ++c) {
+ for (size_t g = 0; g < num_groups_per_row; ++g) {
+ float min_val = std::numeric_limits<float>::max();
+ float max_val = std::numeric_limits<float>::lowest();
+
+ size_t group_start = g * group_size;
+ for (size_t i = 0; i < group_size; ++i) {
+ float val = data[offset + (group_start + i) * cols + c];
+ if (val < min_val)
+ min_val = val;
+ if (val > max_val)
+ max_val = val;
+ }
+
+ float scale = (max_val - min_val) / max_range;
+ if (scale < 1e-5f)
+ scale = 1e-5f;
+ float zp = -min_val / scale;
+
+ uint8_t zp_val = static_cast<uint8_t>(std::round(std::max(0.0f, std::min(zp_max_range, zp))));
+
+ // Scales: [expert_num, num_groups_per_row, cols](Row-Major)
+ scales[e * num_groups + g * cols + c] = static_cast<ov::float16>(scale);
+ // ZPs: [expert_num, num_groups_per_row, cols](Row-Major)
+ zps[e * num_groups + g * cols + c] = zp_val;
+
+ for (size_t i = 0; i < group_size; ++i) {
+ float val = data[offset + (group_start + i) * cols + c];
+ float q_val = val / scale + zp_val;
+ uint8_t q = static_cast<uint8_t>(std::round(std::max(0.0f, std::min(max_range, q_val))));
+
+ // Store at [expert_num, cols, row] (Cols-Major)
+ size_t r = group_start + i;
+ q_data[offset + c * rows + r] = q;
+ }
+ }
+ }
+ }
+ return {q_data, scales, zps};
+ }
+
+ std::vector<ov::float16> run_reference(const std::vector<ov::float16>& hidden_states,
+ const std::vector<ov::float16>& routing_weights,
+ const std::vector<float>& w0_data,
+ const std::vector<float>& w1_data,
+ const std::vector<float>& w2_data) {
+ size_t batch_size = config.batch_size;
+ size_t seq_len = config.seq_len;
+ size_t hidden_size = config.hidden_size;
+ size_t inter_size = config.inter_size;
+ size_t num_experts = config.num_experts;
+ size_t top_k = config.top_k;
+
+ std::vector<ov::float16> output(batch_size * seq_len * hidden_size, 0);
+
+ for (size_t b = 0; b < batch_size; ++b) {
+ for (size_t s = 0; s < seq_len; ++s) {
+ // Do softmax for routing weights
+ std::vector<float> logits(num_experts);
+ float max_val = std::numeric_limits<float>::lowest();
+ for (size_t e = 0; e < num_experts; ++e) {
+ logits[e] = static_cast<float>(routing_weights[b * seq_len * num_experts + s * num_experts + e]);
+ if (logits[e] > max_val)
+ max_val = logits[e];
+ }
+
+ float sum_exp = 0.0f;
+ for (size_t e = 0; e < num_experts; ++e) {
+ logits[e] = std::exp(logits[e] - max_val);
+ sum_exp += logits[e];
+ }
+
+ // Get routing weights and top-k
+ std::vector<std::pair<float, size_t>> expert_weights;
+ for (size_t e = 0; e < num_experts; ++e) {
+ expert_weights.push_back({logits[e] / sum_exp, e});
+ }
+ // Sort descending
+ std::partial_sort(expert_weights.begin(),
+ expert_weights.begin() + top_k,
+ expert_weights.end(),
+ [](const std::pair<float, size_t>& a, const std::pair<float, size_t>& b) {
+ return a.first > b.first;
+ });
+
+ // Normalize weights
+ float sum_weights = 0.0f;
+ for (size_t k = 0; k < top_k; ++k) {
+ sum_weights += expert_weights[k].first;
+ }
+
+ for (size_t k = 0; k < top_k; ++k) {
+ size_t expert_idx = expert_weights[k].second;
+ float weight = expert_weights[k].first / sum_weights;
+
+ std::vector<float> gate(inter_size);
+ std::vector<float> up(inter_size);
+
+ // Compute gate and up
+ for (size_t i = 0; i < inter_size; ++i) {
+ float gate_val = 0.0f;
+ float up_val = 0.0f;
+ for (size_t j = 0; j < hidden_size; ++j) {
+ float x_val = static_cast<float>(hidden_states[b * seq_len * hidden_size + s * hidden_size + j]);
+
+ // w0_data is Row-Major: [num_experts, hidden_size, inter_size]
+ size_t w0_cols = inter_size;
+ size_t w0_r = expert_idx * hidden_size + j;
+ float w0_val = w0_data[w0_r * w0_cols + i];
+ gate_val += x_val * w0_val;
+
+ // w1_data is Row-Major: [num_experts, hidden_size, inter_size]
+ size_t w1_cols = inter_size;
+ size_t w1_r = expert_idx * hidden_size + j;
+ float w1_val = w1_data[w1_r * w1_cols + i];
+ up_val += x_val * w1_val;
+ }
+ gate[i] = gate_val;
+ up[i] = up_val;
+ }
+
+ // SwiGLU
+ std::vector<float> act(inter_size);
+ for (size_t i = 0; i < inter_size; ++i) {
+ float silu = gate[i] / (1.0f + std::exp(-gate[i]));
+ act[i] = silu * up[i];
+ }
+
+ // Compute out = act @ w2
+ for (size_t j = 0; j < hidden_size; ++j) {
+ float out_val = 0.0f;
+ for (size_t i = 0; i < inter_size; ++i) {
+ // w2_data is Row-Major: [num_experts, inter_size, hidden_size]
+ size_t w2_cols = hidden_size;
+ size_t w2_r = expert_idx * inter_size + i;
+ float w2_val = w2_data[w2_r * w2_cols + j];
+
+ out_val += act[i] * w2_val;
+ }
+ output[b * seq_len * hidden_size + s * hidden_size + j] += static_cast<ov::float16>(out_val * weight);
+ }
+ }
+ }
+ }
+ return output;
+ }
+};
+
+struct Moe3GemmTestParams {
+ size_t seq_len;
+ bool is_u4;
+ size_t hidden_size;
+ size_t inter_size;
+ size_t num_experts;
+ size_t top_k;
+ size_t group_size;
+};
+
+class moe_3gemm_compressed_gpu_random : public ::testing::TestWithParam<Moe3GemmTestParams> {};
+
+TEST_P(moe_3gemm_compressed_gpu_random, moe_accuracy_test_random) {
+ auto param = GetParam();
auto& engine = get_test_engine();
if (!engine.get_device_info().supports_immad) {
return;
}
+ tests::random_generator rg(GET_SUITE_NAME);
+ Moe3GemmConfig config;
+ config.batch_size = 1;
+ config.seq_len = param.seq_len;
+ config.hidden_size = param.hidden_size;
+ config.inter_size = param.inter_size;
+ config.num_experts = param.num_experts;
+ config.top_k = param.top_k;
+ config.group_size = param.group_size;
+ config.is_u4 = param.is_u4;
+
+ Moe3GemmReference ref(config, rg);
+
+ // Generate random data
+ 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] = ref.quantize(w0_data, config.num_experts, config.hidden_size, config.inter_size, config.group_size);
+ auto [w1_q, w1_scale, w1_zp] = ref.quantize(w1_data, config.num_experts, config.hidden_size, config.inter_size, config.group_size);
+ auto [w2_q, w2_scale, w2_zp] = ref.quantize(w2_data, config.num_experts, config.inter_size, config.hidden_size, config.group_size);
+
+ auto w0_q_packed = ref.pack(w0_q);
+ auto w0_zp_packed = ref.pack(w0_zp);
+ auto w1_q_packed = ref.pack(w1_q);
+ auto w1_zp_packed = ref.pack(w1_zp);
+ auto w2_q_packed = ref.pack(w2_q);
+ auto w2_zp_packed = ref.pack(w2_zp);
+
+ // Create tensors
+ auto create_weight_tensor = [&](const std::vector<uint8_t>& values, int64_t b, int64_t f, int64_t y, int64_t x) {
+ auto dt = config.is_u4 ? data_types::u4 : data_types::u8;
+ auto mem = engine.allocate_memory({dt, format::bfyx, {b, f, y, x}});
+ set_values(mem, values);
+ get_test_stream().finish();
+ return mem;
+ };
+
+ auto create_zp_tensor = [&](const std::vector<uint8_t>& values, int64_t b, int64_t f, int64_t y, int64_t x) {
+ auto dt = config.is_u4 ? data_types::u4 : data_types::u8;
+ auto mem = engine.allocate_memory({dt, format::bfyx, {b, f, y, x}});
+ set_values(mem, values);
+ get_test_stream().finish();
+ return mem;
+ };
+
+ auto create_f16_tensor = [&](const std::vector<ov::float16>& values, int64_t b, int64_t f, int64_t y, int64_t x) {
+ auto mem = engine.allocate_memory({data_types::f16, format::bfyx, {b, f, y, x}});
+ set_values(mem, values);
+ get_test_stream().finish();
+ return mem;
+ };
+
+ auto hidden_states_mem = create_f16_tensor(hidden_states, config.batch_size, config.seq_len, config.hidden_size, 1);
+ auto routing_weights_mem = create_f16_tensor(routing_weights, config.batch_size, config.seq_len, config.num_experts, 1);
+
+ size_t group_num = config.hidden_size / config.group_size;
+ size_t group_num2 = config.inter_size / config.group_size;
+
+ 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 = create_f16_tensor(w0_scale, config.num_experts, group_num, 1, config.inter_size);
+ auto w0_zp_mem = create_zp_tensor(w0_zp_packed, config.num_experts, group_num, 1, config.inter_size);
+
+ 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 = create_f16_tensor(w1_scale, config.num_experts, group_num, 1, config.inter_size);
+ auto w1_zp_mem = create_zp_tensor(w1_zp_packed, config.num_experts, group_num, 1, config.inter_size);
+
+ 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 = create_f16_tensor(w2_scale, config.num_experts, group_num2, 1, config.hidden_size);
+ auto w2_zp_mem = create_zp_tensor(w2_zp_packed, config.num_experts, group_num2, 1, config.hidden_size);
+
+ // Build topology
+ 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", w0_weight_mem));
+ topology.add(data("w0_scale", w0_scale_mem));
+ topology.add(data("w0_zp", w0_zp_mem));
+ topology.add(data("w1_weight", w1_weight_mem));
+ topology.add(data("w1_scale", w1_scale_mem));
+ topology.add(data("w1_zp", w1_zp_mem));
+ topology.add(data("w2_weight", w2_weight_mem));
+ topology.add(data("w2_scale", w2_scale_mem));
+ topology.add(data("w2_zp", w2_zp_mem));
+
+ cldnn::MOE3GemmFusedCompressed::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;
+
+ auto moe_prim = moe_3gemm_fused_compressed("moe_3gemm_fused_compressed",
+ {input_info("hidden_states"),
+ input_info("routing_weights"),
+ input_info("w0_weight"),
+ input_info("w0_scale"),
+ input_info("w0_zp"),
+ input_info("w1_weight"),
+ input_info("w1_scale"),
+ input_info("w1_zp"),
+ input_info("w2_weight"),
+ input_info("w2_scale"),
+ input_info("w2_zp")},
+ moe_config);
+
+ topology.add(moe_prim);
+
+ network network(engine, topology, get_test_default_config(engine));
+ 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 = ref.run_reference(hidden_states, routing_weights, w0_data, w1_data, w2_data);
+ for (size_t i = 0; i < ref_output.size(); ++i) {
+ ASSERT_NEAR(static_cast<float>(output_ptr[i]), static_cast<float>(ref_output[i]), 0.1f);
+ }
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke,
+ moe_3gemm_compressed_gpu_random,
+ ::testing::Values(Moe3GemmTestParams{1, true, 128, 256, 4, 2, 128},
+ Moe3GemmTestParams{16, true, 128, 256, 4, 2, 128},
+ Moe3GemmTestParams{1, false, 128, 256, 4, 2, 128},
+ Moe3GemmTestParams{16, false, 128, 256, 4, 2, 128}));
+
+TEST(moe_3gemm_compressed_gpu, moe_accuracy_test_u4) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad) {
+ return;
+ }
const size_t batch_size = 1;
const size_t seq_len = 1;
@@ -65,7 +424,7 @@ TEST(moe_3gemm_compressed_gpu, moe_accuracy_test) {
// Input 7: w1_scale [num_experts, inter_size, group_num, 1]
auto w1_scale = create_f16_tensor(w1_scale_data, num_experts, inter_size, group_num, 1);
// Input 8: w1_zp [num_experts, inter_size, group_num, 1]
- auto w1_zp = create_u4_tensor(w1_zp_data, num_experts, inter_size, group_num, group_size);
+ auto w1_zp = create_u4_tensor(w1_zp_data, num_experts, inter_size, group_num, 1);
// Input 9: w2_weight [num_experts, hidden_size, group_num, group_size]
auto w2_weight = create_u4_tensor(w2_weights_data, num_experts, hidden_size, group_num2, group_size);
@@ -105,18 +464,18 @@ TEST(moe_3gemm_compressed_gpu, moe_accuracy_test) {
// Create MOECompressed primitive
auto moe_prim = moe_3gemm_fused_compressed("moe_3gemm_fused_compressed",
- {input_info("hidden_states"),
- input_info("routing_weights"),
- 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")},
- config);
+ {input_info("hidden_states"),
+ input_info("routing_weights"),
+ 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")},
+ config);
topology.add(moe_prim);