Skip to content

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

[GPU] enable post_proc for moe micro_gemm (#33723)

River Li0ffa0defc715
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_base.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_base.hpp
index 4e2837d595d..ed011addca1 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_base.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_base.hpp
@@ -26,6 +26,8 @@ namespace ov::intel_gpu::ocl {
#define MOE_INTERNAL_BUFFER_TOKEN_IDX_PER_EXPERT 12 // token idx per expert
#define MOE_INTERNAL_BUFFER_ACTUAL_USED_EXPERT_NUM 13 // num_actual_used_experts
+#define ENABLE_MOE_MICRO_GEMM_POST_PROC_SILU_MUL 1
+
enum class MoE3GemmMicroKernelType : uint8_t { MLP_GATE = 0, MLP_UP = 1, MLP_DOWN = 2 };
enum class MOE3GemmInputIndex : uint8_t {
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 88ffb084a87..a10771fd136 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
@@ -119,6 +119,10 @@ JitConstants MoE3GemmMicroGenerator::get_jit_constants(const kernel_impl_params&
if (slm_size > 0)
jit.make("USE_SLM", 1);
+ const bool enable_silu_mul = ENABLE_MOE_MICRO_GEMM_POST_PROC_SILU_MUL;
+ if (enable_silu_mul && m_type == MoE3GemmMicroKernelType::MLP_GATE)
+ jit.make("POST_PROC_SILU_MUL", 1);
+
return jit;
}
@@ -187,6 +191,7 @@ void MoE3GemmMicroGenerator::init_microkernels(const kernel_impl_params& params,
const auto& zp_layout = params.get_input_layout(zp_idx);
MoE3GemmMicroGenerator::GemmCacheKey key;
+ key.type = type;
key.weight_shape = weight_layout.get_shape();
key.weight_dt = weight_layout.data_type;
key.scale_shape = scale_layout.get_shape();
@@ -365,7 +370,12 @@ Arguments MoE3GemmMicroGenerator::get_arguments_desc(const kernel_impl_params& p
case MoE3GemmMicroKernelType::MLP_GATE:
args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_GATE_UP_INPUT}); // gather input tensor
args.push_back({ArgumentDescriptor::Types::INPUT, static_cast<int>(MOE3GemmInputIndex::WEIGHT_0)});
- args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_GATE_OUTPUT}); // gate output
+ args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_GATE_OUTPUT}); // gate output
+ {
+ const bool enable_silu_mul = ENABLE_MOE_MICRO_GEMM_POST_PROC_SILU_MUL;
+ if (enable_silu_mul)
+ args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_UP_OUTPUT});
+ }
args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_ACTIVATED_EXPERT_IDS}); // experts_ids
args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_TOKEN_START_OFFSET_PER_EXPERT}); // input_offset_per_expert
args.push_back({ArgumentDescriptor::Types::INTERNAL_BUFFER, MOE_INTERNAL_BUFFER_TOKEN_LEN_PER_ACTIVATED_EXPERT}); // n_array - token len
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.hpp
index 72ef9583ecc..5185c095014 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_3gemm_gen_micro.hpp
@@ -79,6 +79,7 @@ public:
static std::mutex mtx;
struct GemmCacheKey {
+ MoE3GemmMicroKernelType type;
ov::Shape weight_shape;
ov::element::Type weight_dt;
@@ -89,8 +90,8 @@ public:
ov::element::Type zp_dt;
bool operator==(const GemmCacheKey& other) const {
- return weight_shape == other.weight_shape && weight_dt == other.weight_dt && scale_shape == other.scale_shape && scale_dt == other.scale_dt &&
- zp_shape == other.zp_shape && zp_dt == other.zp_dt;
+ return type == other.type && weight_shape == other.weight_shape && weight_dt == other.weight_dt && scale_shape == other.scale_shape &&
+ scale_dt == other.scale_dt && zp_shape == other.zp_shape && zp_dt == other.zp_dt;
}
};
@@ -108,6 +109,7 @@ public:
}
};
+ hash_combine(h, static_cast<size_t>(k.type));
hash_shape(k.weight_shape);
hash_shape(k.scale_shape);
hash_shape(k.zp_shape);
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 4b0b1233ed4..ed6c1dac520 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
@@ -1433,6 +1433,7 @@ public:
// 8: wei_zp
// output:
// 0: up/gate output, shape = [token_len * expert_topK, hidden_size]
+ // Note: If POST_PROC_SILU_MUL is enabled, silu_mul result will be involved in micro_gemm_gate, don't change kernel executor order.
{
# if DEBUG_MOE_LOG
GPU_DEBUG_TRACE_DETAIL << "\nstep 3: moe_gemm for up and gate" << std::endl;
@@ -1447,7 +1448,10 @@ public:
// 1: gate [token_len * expert_topK, hidden_size]
// output
// 0: gate_up [token_len * expert_topK, hidden_size]
- {
+ // Note: If POST_PROC_SILU_MUL is disabled, single silu_mul kernel will be submmited.
+ // Otherwise, silu_mul has been involved in micro_gemm_gate kernel, skip here.
+ const bool enable_silu_mul = ENABLE_MOE_MICRO_GEMM_POST_PROC_SILU_MUL;
+ if (!enable_silu_mul) {
auto token_size = token_num * max_topk;
# if DEBUG_MOE_LOG
GPU_DEBUG_TRACE_DETAIL << "\nstep 4: prefill_swiglu token_size=" << token_size << ", hidden_size=" << _intermediate_size << std::endl;
@@ -1457,8 +1461,8 @@ public:
*prefill_swiglu,
{intermediates_memories[MOE_INTERNAL_BUFFER_UP_OUTPUT], intermediates_memories[MOE_INTERNAL_BUFFER_GATE_OUTPUT]},
{intermediates_memories[MOE_INTERNAL_BUFFER_GATE_OUTPUT]},
- {static_cast<size_t>(token_size), static_cast<size_t>(_intermediate_size), 1},
- {1, subgroup_size, 1});
+ {static_cast<size_t>(_intermediate_size), static_cast<size_t>(token_size), 1},
+ {subgroup_size, 1, 1});
}
// step 5: moe_gemm for down
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl
index 8a8e89bd276..ad0abdda0c9 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_3gemm_swiglu_fuse.cl
@@ -133,27 +133,32 @@ KERNEL (index_add_)(const __global MOE_DTYPE* src_tok,
#elif PREFILL_SWIGLU_ENABLE
#define SWISH_BETA 1.0f
+#define ACC_DTYPE float
__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE)))
KERNEL(swiglu_ref) (
- const __global MOE_DTYPE* up, // [token_len * expert_topK, hidden_size]
+ const __global MOE_DTYPE* up, // [token_len * expert_topK, inter_size]
const __global MOE_DTYPE* gate,
- __global MOE_DTYPE* output // [token_len * expert_topK, hidden_size]
+ __global MOE_DTYPE* output // [token_len * expert_topK, inter_size]
) {
- const uint token_idx = get_global_id(0);
- const uint n_offset = get_global_id(1);
+ const uint token_idx = get_global_id(1);
+ const uint n_offset = get_global_id(0);
+ // gws = {_intermediate_size, token_cnt, 1}
+ // lws = {subgroup_size, 1, 1};
- const uint offset = token_idx * INTERMEDIA_SIZE + n_offset;
#if MOE_DTYPE_SIZE == 2
- half up_value = as_half(intel_sub_group_block_read_us((const __global ushort *)(up + offset)));
- half gate_value = as_half(intel_sub_group_block_read_us((const __global ushort *)(gate + offset)));
- half value = gate_value / (1.0 + native_exp(-SWISH_BETA * gate_value));
- MOE_DTYPE result = value * up_value;
+ const uint sg_id = get_sub_group_local_id();
+ const uint offset = token_idx * INTERMEDIA_SIZE + n_offset - sg_id;
+ ACC_DTYPE up_value = as_half(intel_sub_group_block_read_us((const __global ushort *)(up + offset)));
+ ACC_DTYPE gate_value = as_half(intel_sub_group_block_read_us((const __global ushort *)(gate + offset)));
+ ACC_DTYPE value = gate_value / (1.0f + native_exp(-SWISH_BETA * gate_value));
+ half result = value * up_value;
intel_sub_group_block_write_us((__global ushort *)(output + offset), as_ushort(result));
#else
- MOE_DTYPE gate_value = gate[offset];
- MOE_DTYPE up_value = up[offset];
- half value = gate_value / (1.0 + native_exp(-SWISH_BETA * gate_value));
- MOE_DTYPE result = value * up_value;
+ const uint offset = token_idx * INTERMEDIA_SIZE + n_offset;
+ ACC_DTYPE gate_value = gate[offset];
+ ACC_DTYPE up_value = up[offset];
+ ACC_DTYPE value = gate_value / (1.0f + native_exp(-SWISH_BETA * gate_value));
+ ACC_DTYPE result = value * up_value;
output[offset] = result;
#endif
}
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_gemm.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_gemm.cl
index e27dbb57a0a..35577e5d2ce 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_gemm.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_gemm.cl
@@ -23,6 +23,8 @@ DECLARE_2D_TILE(ugemm_moe_c_type_half, half, SUBGROUP_SIZE, ugemm_moe_c_type_blo
DECLARE_2D_TILE_COPY_REBLOCK(ugemm_moe_c_type, SUBGROUP_SIZE, ugemm_moe_c_type_block0, ugemm_moe_c_type_block1, ugemm_moe_c_type_nblock0, ugemm_moe_c_type_nblock1,
ugemm_moe_c_type_half, SUBGROUP_SIZE, ugemm_moe_c_type_block0, ugemm_moe_c_type_block1, ugemm_moe_c_type_nblock0, ugemm_moe_c_type_nblock1)
+#define unroll_for __attribute__((opencl_unroll_hint)) for
+
__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE)))
KERNEL(moe_gemm)(OPTIONAL_SHAPE_INFO_ARG
const global INPUT0_TYPE *input_ptr,
@@ -32,8 +34,11 @@ KERNEL(moe_gemm)(OPTIONAL_SHAPE_INFO_ARG
const global INPUT1_TYPE *weight_ptr,
#endif
global OUTPUT_TYPE *out_ptr,
+#ifdef POST_PROC_SILU_MUL
+ const global OUTPUT_TYPE *post_op_input,
+#endif
const global INPUT2_TYPE *experts_ids,
- const global INPUT3_TYPE * input_offset_per_expert,
+ const global INPUT3_TYPE *input_offset_per_expert,
const global INPUT4_TYPE *n_array,
int m, int k
#ifdef BIAS_DT
@@ -50,7 +55,7 @@ KERNEL(moe_gemm)(OPTIONAL_SHAPE_INFO_ARG
#endif
) {
uint batch = get_group_id(2);
- int input_offset = input_offset_per_expert[batch];
+ int input_offset = sub_group_broadcast(input_offset_per_expert[batch], 0);
#ifdef IS_GENERATE
if (INPUT_SEQ_LEN > 1) {
@@ -60,21 +65,25 @@ KERNEL(moe_gemm)(OPTIONAL_SHAPE_INFO_ARG
}
#endif
out_ptr += input_offset * OUTPUT_STRIDE;
- weight_ptr += experts_ids[batch] * EXPERT_STRIDE;
+#ifdef POST_PROC_SILU_MUL
+ post_op_input += input_offset * OUTPUT_STRIDE;
+#endif
+ INPUT2_TYPE expert_id = sub_group_broadcast(experts_ids[batch], 0);
+ weight_ptr += expert_id * EXPERT_STRIDE;
int ld_input = k;
#ifdef WEIGHT_COMPRESSED_INT4
- weight_scales += experts_ids[batch] * m * NUM_GROUPS;
+ weight_scales += expert_id * m * NUM_GROUPS;
#ifdef WEIGHT_ZP_DT
#ifdef WEIGHT_COMPRESSED_ZP_INT4
- weight_zps += experts_ids[batch] * m * NUM_GROUPS / 2;
+ weight_zps += expert_id * m * NUM_GROUPS / 2;
#else
- weight_zps += experts_ids[batch] * m * NUM_GROUPS;
+ weight_zps += expert_id * m * NUM_GROUPS;
#endif
#endif
#endif
int ld_weight = k;
- int cur_n_tokens = n_array[batch];
+ int cur_n_tokens = sub_group_broadcast(n_array[batch], 0);
uint sg_i = sub_group_broadcast(get_local_id(0)/SUBGROUP_SIZE, 0);
uint sg_j = sub_group_broadcast(get_local_id(1), 0);
@@ -125,7 +134,7 @@ KERNEL(moe_gemm)(OPTIONAL_SHAPE_INFO_ARG
tile_copy_reblock(c_tile, &c_tile_half);
#ifdef BIAS_DT
- bias_ptr += (experts_ids[batch] * BIAS_STRIDE);
+ bias_ptr += (expert_id * BIAS_STRIDE);
int sglid = get_sub_group_local_id();
const int br = ugemm_moe_c_type_block0;
const int nbr = ugemm_moe_c_type_nblock0;
@@ -136,11 +145,42 @@ KERNEL(moe_gemm)(OPTIONAL_SHAPE_INFO_ARG
if (sg_j0 + j < cur_n_tokens) {
for (int i0 = 0; i0 < br * nbr; i0 += sg) {
int i = i0 + sglid;
- if (sg_i0 + i < m)
+ if (sg_i0 + i < m) {
c_tile_half.x[i0 / br + nbr * (j / bc)][(i0 % br)/sg + (j % bc) * (br / sg)] += bias_ptr[sg_i0 + i];
+ }
+ }
+ }
+ }
+#endif
+
+#ifdef POST_PROC_SILU_MUL
+ {
+ int sglid = get_sub_group_local_id();
+ const int br = ugemm_moe_c_type_block0;
+ const int nbr = ugemm_moe_c_type_nblock0;
+ const int bc = ugemm_moe_c_type_block1;
+ const int nbc = ugemm_moe_c_type_nblock1;
+ int sg = SUBGROUP_SIZE;
+
+ const global OUTPUT_TYPE* post_op_base = post_op_input + sg_j0 * m + sg_i0;
+ unroll_for (int j = 0; j < bc * nbc; j++) {
+ if (sg_j0 + j < cur_n_tokens) {
+ const global OUTPUT_TYPE* post_op_row = post_op_base + j * m;
+ unroll_for (int i0 = 0; i0 < br * nbr; i0 += sg) {
+ int i = i0 + sglid;
+ if (sg_i0 + i < m) {
+ float post_val = post_op_row[i];
+ int reg_idx_i = (i0 / br) + nbr * (j / bc);
+ int reg_idx_j = (i0 % br)/sg + (j % bc) * (br / sg);
+ float val = c_tile_half.x[reg_idx_i][reg_idx_j];
+ float res = post_val * (val / (1.0f + native_exp(-val)));
+ c_tile_half.x[reg_idx_i][reg_idx_j] = res;
+ }
+ }
}
}
}
#endif
+
tile_store(c_tile_half, out_ptr, m, cur_n_tokens, sg_i0, sg_j0);
}