Skip to content

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

[GPU] Move MoE Router related code to separate files (#36359)

Vladislav Golubev816d60598f9b
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_router_fused_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_router_fused_opt.cpp
index 8af09306b32..8cc77f357a3 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_router_fused_opt.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_router_fused_opt.cpp
@@ -15,7 +15,7 @@ namespace {
class MoeRouterSoftMaxTopK : public KernelGenerator {
public:
- MoeRouterSoftMaxTopK() : KernelGenerator("moe_3gemm_swiglu_fuse", "softmax_topk") {}
+ MoeRouterSoftMaxTopK() : KernelGenerator("moe_router_fused", "softmax_topk") {}
protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
@@ -41,7 +41,7 @@ protected:
class MoeRouterSigmoidBiasTopK : public KernelGenerator {
public:
- MoeRouterSigmoidBiasTopK() : KernelGenerator("moe_3gemm_swiglu_fuse", "sigmoid_bias_topk") {}
+ MoeRouterSigmoidBiasTopK() : KernelGenerator("moe_router_fused", "sigmoid_bias_topk") {}
protected:
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
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 e533f4b3c14..41be4f3652f 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
@@ -2,165 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
-#if SOFTMAX_TOPK_ENABLE
-
-KERNEL(softmax_topk)(
- const __global MOE_DTYPE* input, // [input_batch, sort_in_num]
- __global uint* output_index, // [input_batch, TOP_K]
- __global MOE_DTYPE* output // [input_batch, TOP_K]
-) {
- // gws [batch, sort_in_num]
- const uint batch = (uint)get_global_id(0);
- const uint sort_index = (uint)get_global_id(1);
- const uint sort_cnt = (uint)get_global_size(1);
-
- input += batch * sort_cnt + sort_index;
-
- uint sort_position = 0;
-
- __local MOE_DTYPE local_input[VALUE_NUM];
- __local MOE_DTYPE local_output[TOP_K];
- __local uint local_index[TOP_K];
-
-#if MOE_DTYPE_SIZE == 2
- MOE_DTYPE in_value = as_half(intel_sub_group_block_read_us((const __global ushort*)(input)));
-#elif MOE_DTYPE_SIZE == 4
- MOE_DTYPE in_value = as_float(intel_sub_group_block_read((const __global uint*)(input)));
-#else
-# error "softmax_topk: unsupported MOE_DTYPE_SIZE"
-#endif
- local_input[sort_index] = in_value;
- barrier(CLK_LOCAL_MEM_FENCE);
-
- __attribute__((opencl_unroll_hint(8)))
- for(uint i = 0; i < sort_index; i++) {
- MOE_DTYPE value = local_input[i];
- if(value >= in_value) {
- sort_position++;
- }
- }
-
- __attribute__((opencl_unroll_hint(8)))
- for(uint i = sort_index; i < sort_cnt; i++) {
- MOE_DTYPE value = local_input[i];
- if(value > in_value) {
- sort_position++;
- }
- }
- if (sort_position < TOP_K) {
- local_output[sort_position] = in_value;
- local_index[sort_position] = sort_index;
- }
- barrier(CLK_LOCAL_MEM_FENCE);
-
- if(sort_position == 0) {
- float softmax_total = 1.0;
- MOE_DTYPE max_v = local_output[0];
- local_output[0] = 1;
- for(uint i = 1; i < TOP_K; i++) {
- local_output[i] = native_exp(local_output[i] - max_v);
- softmax_total += local_output[i];
- }
- output_index += batch * TOP_K;
- output += batch * TOP_K;
-
- for(uint i = 0; i < TOP_K; i++) {
- output[i] = local_output[i]/softmax_total;
- output_index[i] = local_index[i];
- }
- }
-}
-
-#elif SIGMOID_BIAS_TOPK_ENABLE
-
-KERNEL(sigmoid_bias_topk)(
- const __global MOE_DTYPE* input, // routing logits [input_batch, num_experts]
- const __global MOE_DTYPE* bias, // routing bias [1, num_experts] or [num_experts]
- const __global MOE_DTYPE* eps_ptr, // routing epsilon scalar [1]
- __global uint* output_index, // [input_batch, TOP_K]
- __global MOE_DTYPE* output // [input_batch, TOP_K]
-) {
- // gws [batch, num_experts]
- const uint batch = (uint)get_global_id(0);
- const uint sort_index = (uint)get_global_id(1);
- const uint sort_cnt = (uint)get_global_size(1); // num_experts
-
- input += batch * sort_cnt + sort_index;
-
- __local MOE_DTYPE local_sigmoid[VALUE_NUM]; // raw sigmoid values
- __local MOE_DTYPE local_selection[VALUE_NUM]; // sigmoid + bias (for sorting)
- __local MOE_DTYPE local_output[TOP_K];
- __local uint local_index[TOP_K];
-
- // Compute sigmoid
-#if MOE_DTYPE_SIZE == 2
- MOE_DTYPE in_value = as_half(intel_sub_group_block_read_us((const __global ushort*)(input)));
-#elif MOE_DTYPE_SIZE == 4
- MOE_DTYPE in_value = as_float(intel_sub_group_block_read((const __global uint*)(input)));
-#else
-# error "sigmoid_bias_topk: unsupported MOE_DTYPE_SIZE"
-#endif
- MOE_DTYPE sigmoid_val = (MOE_DTYPE)(1.0f / (1.0f + native_exp(-(float)in_value)));
-
- // Add bias for selection (determines which experts are chosen)
- MOE_DTYPE bias_val = bias[sort_index];
- MOE_DTYPE selection_val = sigmoid_val + bias_val;
-
- local_sigmoid[sort_index] = sigmoid_val;
- local_selection[sort_index] = selection_val;
- barrier(CLK_LOCAL_MEM_FENCE);
-
- // Sort by selection_val (sigmoid + bias) to find top-K
- uint sort_position = 0;
- uint actual_topk = (TOP_K < sort_cnt) ? TOP_K : sort_cnt;
-
- __attribute__((opencl_unroll_hint(8)))
- for(uint i = 0; i < sort_index; i++) {
- MOE_DTYPE value = local_selection[i];
- if(value >= selection_val) {
- sort_position++;
- }
- }
-
- __attribute__((opencl_unroll_hint(8)))
- for(uint i = sort_index; i < sort_cnt; i++) {
- MOE_DTYPE value = local_selection[i];
- if(value > selection_val) {
- sort_position++;
- }
- }
-
- // Store raw sigmoid values (NOT sigmoid+bias) for the top-K experts
- if (sort_position < actual_topk) {
- local_output[sort_position] = local_sigmoid[sort_index];
- local_index[sort_position] = sort_index;
- }
- barrier(CLK_LOCAL_MEM_FENCE);
-
- // Normalize: weights / (sum + eps)
- if(sort_position == 0) {
- float sum_weights = 0.0f;
- for(uint i = 0; i < actual_topk; i++) {
- sum_weights += (float)local_output[i];
- }
- sum_weights += (float)eps_ptr[0]; // epsilon to avoid division by zero
-
- output_index += batch * TOP_K;
- output += batch * TOP_K;
-
- for(uint i = 0; i < actual_topk; i++) {
- output[i] = (MOE_DTYPE)((float)local_output[i] / sum_weights);
- output_index[i] = local_index[i];
- }
- // Zero out remaining positions if TOP_K > actual_topk
- for(uint i = actual_topk; i < TOP_K; i++) {
- output[i] = (MOE_DTYPE)0.0f;
- output_index[i] = 0;
- }
- }
-}
-
-#elif GATHER_ENABLE
+#if GATHER_ENABLE
__attribute__((intel_reqd_sub_group_size(SUBGROUP_SIZE)))
KERNEL (gather_2d_ref)(
const __global MOE_DTYPE* src_tok, // input tokens [total_token, hidden_size] - hidden_states_mem_ptr
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_router_fused.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_router_fused.cl
new file mode 100644
index 00000000000..5d1d878b965
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_router_fused.cl
@@ -0,0 +1,163 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#if SOFTMAX_TOPK_ENABLE
+
+KERNEL(softmax_topk)(
+ const __global MOE_DTYPE* input, // [input_batch, sort_in_num]
+ __global uint* output_index, // [input_batch, TOP_K]
+ __global MOE_DTYPE* output // [input_batch, TOP_K]
+) {
+ // gws [batch, sort_in_num]
+ const uint batch = (uint)get_global_id(0);
+ const uint sort_index = (uint)get_global_id(1);
+ const uint sort_cnt = (uint)get_global_size(1);
+
+ input += batch * sort_cnt + sort_index;
+
+ uint sort_position = 0;
+
+ __local MOE_DTYPE local_input[VALUE_NUM];
+ __local MOE_DTYPE local_output[TOP_K];
+ __local uint local_index[TOP_K];
+
+#if MOE_DTYPE_SIZE == 2
+ MOE_DTYPE in_value = as_half(intel_sub_group_block_read_us((const __global ushort*)(input)));
+#elif MOE_DTYPE_SIZE == 4
+ MOE_DTYPE in_value = as_float(intel_sub_group_block_read((const __global uint*)(input)));
+#else
+# error "softmax_topk: unsupported MOE_DTYPE_SIZE"
+#endif
+ local_input[sort_index] = in_value;
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ __attribute__((opencl_unroll_hint(8)))
+ for(uint i = 0; i < sort_index; i++) {
+ MOE_DTYPE value = local_input[i];
+ if(value >= in_value) {
+ sort_position++;
+ }
+ }
+
+ __attribute__((opencl_unroll_hint(8)))
+ for(uint i = sort_index; i < sort_cnt; i++) {
+ MOE_DTYPE value = local_input[i];
+ if(value > in_value) {
+ sort_position++;
+ }
+ }
+ if (sort_position < TOP_K) {
+ local_output[sort_position] = in_value;
+ local_index[sort_position] = sort_index;
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ if(sort_position == 0) {
+ float softmax_total = 1.0;
+ MOE_DTYPE max_v = local_output[0];
+ local_output[0] = 1;
+ for(uint i = 1; i < TOP_K; i++) {
+ local_output[i] = native_exp(local_output[i] - max_v);
+ softmax_total += local_output[i];
+ }
+ output_index += batch * TOP_K;
+ output += batch * TOP_K;
+
+ for(uint i = 0; i < TOP_K; i++) {
+ output[i] = local_output[i]/softmax_total;
+ output_index[i] = local_index[i];
+ }
+ }
+}
+
+#elif SIGMOID_BIAS_TOPK_ENABLE
+
+KERNEL(sigmoid_bias_topk)(
+ const __global MOE_DTYPE* input, // routing logits [input_batch, num_experts]
+ const __global MOE_DTYPE* bias, // routing bias [1, num_experts] or [num_experts]
+ const __global MOE_DTYPE* eps_ptr, // routing epsilon scalar [1]
+ __global uint* output_index, // [input_batch, TOP_K]
+ __global MOE_DTYPE* output // [input_batch, TOP_K]
+) {
+ // gws [batch, num_experts]
+ const uint batch = (uint)get_global_id(0);
+ const uint sort_index = (uint)get_global_id(1);
+ const uint sort_cnt = (uint)get_global_size(1); // num_experts
+
+ input += batch * sort_cnt + sort_index;
+
+ __local MOE_DTYPE local_sigmoid[VALUE_NUM]; // raw sigmoid values
+ __local MOE_DTYPE local_selection[VALUE_NUM]; // sigmoid + bias (for sorting)
+ __local MOE_DTYPE local_output[TOP_K];
+ __local uint local_index[TOP_K];
+
+ // Compute sigmoid
+#if MOE_DTYPE_SIZE == 2
+ MOE_DTYPE in_value = as_half(intel_sub_group_block_read_us((const __global ushort*)(input)));
+#elif MOE_DTYPE_SIZE == 4
+ MOE_DTYPE in_value = as_float(intel_sub_group_block_read((const __global uint*)(input)));
+#else
+# error "sigmoid_bias_topk: unsupported MOE_DTYPE_SIZE"
+#endif
+ MOE_DTYPE sigmoid_val = (MOE_DTYPE)(1.0f / (1.0f + native_exp(-(float)in_value)));
+
+ // Add bias for selection (determines which experts are chosen)
+ MOE_DTYPE bias_val = bias[sort_index];
+ MOE_DTYPE selection_val = sigmoid_val + bias_val;
+
+ local_sigmoid[sort_index] = sigmoid_val;
+ local_selection[sort_index] = selection_val;
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ // Sort by selection_val (sigmoid + bias) to find top-K
+ uint sort_position = 0;
+ uint actual_topk = (TOP_K < sort_cnt) ? TOP_K : sort_cnt;
+
+ __attribute__((opencl_unroll_hint(8)))
+ for(uint i = 0; i < sort_index; i++) {
+ MOE_DTYPE value = local_selection[i];
+ if(value >= selection_val) {
+ sort_position++;
+ }
+ }
+
+ __attribute__((opencl_unroll_hint(8)))
+ for(uint i = sort_index; i < sort_cnt; i++) {
+ MOE_DTYPE value = local_selection[i];
+ if(value > selection_val) {
+ sort_position++;
+ }
+ }
+
+ // Store raw sigmoid values (NOT sigmoid+bias) for the top-K experts
+ if (sort_position < actual_topk) {
+ local_output[sort_position] = local_sigmoid[sort_index];
+ local_index[sort_position] = sort_index;
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ // Normalize: weights / (sum + eps)
+ if(sort_position == 0) {
+ float sum_weights = 0.0f;
+ for(uint i = 0; i < actual_topk; i++) {
+ sum_weights += (float)local_output[i];
+ }
+ sum_weights += (float)eps_ptr[0]; // epsilon to avoid division by zero
+
+ output_index += batch * TOP_K;
+ output += batch * TOP_K;
+
+ for(uint i = 0; i < actual_topk; i++) {
+ output[i] = (MOE_DTYPE)((float)local_output[i] / sum_weights);
+ output_index[i] = local_index[i];
+ }
+ // Zero out remaining positions if TOP_K > actual_topk
+ for(uint i = actual_topk; i < TOP_K; i++) {
+ output[i] = (MOE_DTYPE)0.0f;
+ output_index[i] = 0;
+ }
+ }
+}
+
+#endif
diff --git a/src/plugins/intel_gpu/src/plugin/ops/moe.cpp b/src/plugins/intel_gpu/src/plugin/ops/moe.cpp
index 68598643f30..500f2be8153 100644
--- a/src/plugins/intel_gpu/src/plugin/ops/moe.cpp
+++ b/src/plugins/intel_gpu/src/plugin/ops/moe.cpp
@@ -11,22 +11,12 @@
#include "ov_ops/moe_compressed.hpp"
#include "intel_gpu/plugin/program_builder.hpp"
-#include "intel_gpu/op/moe_router_fused.hpp"
#include "intel_gpu/plugin/common_utils.hpp"
#include "intel_gpu/primitives/moe_3gemm_fused_compressed.hpp"
-#include "intel_gpu/primitives/moe_router_fused.hpp"
#include "intel_gpu/primitives/moe_gemm.hpp"
#include "intel_gpu/primitives/moe_mask_gen.hpp"
#include "openvino/op/constant.hpp"
-namespace ov {
-namespace op {
-namespace internal {
-using MoERouterFused = ov::intel_gpu::op::MoERouterFused;
-} // namespace internal
-} // namespace op
-} // namespace ov
-
namespace ov::intel_gpu {
using namespace cldnn;
@@ -162,16 +152,6 @@ static void CreateMOECompressedOp(ProgramBuilder& p, const std::shared_ptr<ov::o
}
}
-static void CreateMoERouterFusedOp(ProgramBuilder& p, const std::shared_ptr<ov::intel_gpu::op::MoERouterFused>& op) {
- auto inputs = p.GetInputInfo(op);
- const auto& config = op->get_config();
- const size_t expected_inputs = (config.routing_type == ov::intel_gpu::op::MoERouterFused::RoutingType::SIGMOID_BIAS) ? 3 : 1;
- validate_inputs_count(op, {expected_inputs});
- const std::string layerName = layer_type_name_ID(op);
- p.add_primitive(*op, cldnn::moe_router_fused(layerName, inputs, config));
-}
-
REGISTER_FACTORY_IMPL(internal, MOECompressed);
-REGISTER_FACTORY_IMPL(internal, MoERouterFused);
} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/ops/moe_router_fused.cpp b/src/plugins/intel_gpu/src/plugin/ops/moe_router_fused.cpp
new file mode 100644
index 00000000000..efc5a379b4c
--- /dev/null
+++ b/src/plugins/intel_gpu/src/plugin/ops/moe_router_fused.cpp
@@ -0,0 +1,32 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "intel_gpu/op/moe_router_fused.hpp"
+
+#include "intel_gpu/plugin/common_utils.hpp"
+#include "intel_gpu/plugin/program_builder.hpp"
+#include "intel_gpu/primitives/moe_router_fused.hpp"
+
+namespace ov {
+namespace op {
+namespace internal {
+using MoERouterFused = ov::intel_gpu::op::MoERouterFused;
+} // namespace internal
+} // namespace op
+} // namespace ov
+
+namespace ov::intel_gpu {
+using namespace cldnn;
+
+static void CreateMoERouterFusedOp(ProgramBuilder& p, const std::shared_ptr<ov::op::internal::MoERouterFused>& op) {
+ auto inputs = p.GetInputInfo(op);
+ const auto& config = op->get_config();
+ const size_t expected_inputs = (config.routing_type == ov::op::internal::MoERouterFused::RoutingType::SIGMOID_BIAS) ? 3 : 1;
+ validate_inputs_count(op, {expected_inputs});
+ const std::string layerName = layer_type_name_ID(op);
+ p.add_primitive(*op, cldnn::moe_router_fused(layerName, inputs, config));
+}
+
+REGISTER_FACTORY_IMPL(internal, MoERouterFused);
+
+} // namespace ov::intel_gpu