Skip to content

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

[GPU] Fix scatter reduce randomness and Use f32 for RoPE subgraph (#32790)

Taylor Yeonbok Lee4eb08a2a829d
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_base.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_base.hpp
new file mode 100644
index 00000000000..721d04df4ad
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_base.hpp
@@ -0,0 +1,20 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <memory>
+#include <utility>
+
+#include "program_node.h"
+#include "registry/implementation_manager.hpp"
+
+using namespace cldnn; // TODO: Remove once namespaces are aligned
+
+namespace ov::intel_gpu::ocl {
+
+struct MoeScatterReductionBase : public ImplementationManager {
+ explicit MoeScatterReductionBase(shape_types shape_type, ValidateFunc vf = nullptr) : ImplementationManager(impl_types::ocl, shape_type, std::move(vf)) {}
+};
+} // namespace ov::intel_gpu::ocl
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_opt.cpp
similarity index 62%
rename from src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction.cpp
rename to src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_opt.cpp
index 105ef397358..2c69cbf26d2 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction.cpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_opt.cpp
@@ -1,7 +1,7 @@
// Copyright (C) 2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
-#include "moe_scatter_reduction.hpp"
+#include "moe_scatter_reduction_opt.hpp"
#include "../common_utils/dispatch_utils.hpp"
#include "../common_utils/jitter.hpp"
@@ -12,73 +12,44 @@
namespace ov::intel_gpu::ocl {
namespace {
-class MoeScatterReductionRefGenerator : public KernelGenerator {
+using namespace ov::intel_gpu::ocl;
+class MoeScatterReductionOptGenerator : public KernelGenerator {
public:
- MoeScatterReductionRefGenerator() : KernelGenerator("moe_scatter_reduction_ref") {}
+ MoeScatterReductionOptGenerator() : KernelGenerator("moe_scatter_reduction_opt") {}
protected:
- static size_t GetBlockSize(const RuntimeParams& params) {
- const auto& input = params.get_input_layout(0);
- size_t vec_size = 1;
- switch (input.data_type) {
- case ov::element::i8:
- case ov::element::u8:
- vec_size = 16;
- break;
- case ov::element::f16:
- vec_size = 8;
- break;
- case ov::element::f32:
- case ov::element::i32:
- vec_size = 4;
- break;
- case ov::element::i64:
- vec_size = 2;
- break;
- default:
- vec_size = 1;
- break;
- }
- return vec_size;
- }
-
static auto calc_thread_count(RuntimeParams& params, const size_t vector_size, const size_t hidden_size) {
auto max_wgs = params.get_program().get_engine().get_device_info().max_work_group_size;
const uint64_t threads_needed = (hidden_size + vector_size - 1) / vector_size;
size_t local_threads_needed = std::min(threads_needed, max_wgs);
size_t batches_per_thread = 1;
- size_t unaligned_elements = 0;
if (threads_needed <= max_wgs) {
batches_per_thread = 1;
- unaligned_elements = hidden_size % vector_size;
} else {
batches_per_thread = (threads_needed + max_wgs - 1) / max_wgs;
auto new_block_size = batches_per_thread * vector_size;
- unaligned_elements = hidden_size % new_block_size;
local_threads_needed = hidden_size / new_block_size;
auto partialblock = (hidden_size % new_block_size != 0) ? 1 : 0;
local_threads_needed += static_cast<size_t>(partialblock);
}
- return std::tuple{local_threads_needed, batches_per_thread, unaligned_elements};
+ return std::tuple{local_threads_needed, batches_per_thread};
}
[[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
auto jit = KernelGenerator::get_jit_constants(params);
auto in_l = params.input_layouts[0];
auto hidden_size = extract_channel(ChannelName::Y, in_l);
- auto block_size = GetBlockSize(params);
- auto [local_threads_count, batches_per_thread, unaligned_elements] = calc_thread_count(const_cast<RuntimeParams&>(params), block_size, hidden_size);
+ auto [local_threads_count, batches_per_thread] = calc_thread_count(const_cast<RuntimeParams&>(params), MoeScatterReductionOpt::block_size, hidden_size);
const auto& desc = params.typed_desc<moe_scatter_reduction>();
jit.make("ACTIVE_EXPERTS", desc->num_active_experts_per_token);
jit.make("HIDDEN_SIZE", hidden_size);
- jit.make("VEC_BLK_SIZE", block_size);
+ jit.make("VEC_BLK_SIZE", MoeScatterReductionOpt::block_size);
jit.make("BATCHES_PER_THREAD", batches_per_thread);
- jit.make("UNALIGNED_ELEMENTS", unaligned_elements);
return jit;
}
@@ -106,9 +77,8 @@ protected:
if (!params.is_dynamic()) {
size_t hidden_size = params.input_layouts[0].get_shape().back();
- auto block_size = GetBlockSize(params);
- auto [local_threads_count, batches_per_thread, unaligned_elements] =
- calc_thread_count(const_cast<RuntimeParams&>(params), block_size, hidden_size);
+ auto [local_threads_count, batches_per_thread] =
+ calc_thread_count(const_cast<RuntimeParams&>(params), MoeScatterReductionOpt::block_size, hidden_size);
auto num_tokens = extract_channel(ChannelName::BATCH, params.input_layouts[1]);
@@ -119,30 +89,29 @@ protected:
}
};
-class MoeScatterReductionRefImpl : public PrimitiveImplOCL {
+class MoeScatterReductionOptImpl : public PrimitiveImplOCL {
public:
- DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::MoeScatterReductionRefImpl)
+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::MoeScatterReductionOptImpl)
- Stage::Ptr moe_scatter_reduction = make_stage<MoeScatterReductionRefGenerator>();
+ Stage::Ptr moe_scatter_reduction = make_stage<MoeScatterReductionOptGenerator>();
- MoeScatterReductionRefImpl() : PrimitiveImplOCL(MoeScatterReductionRef::get_type_info_static()) {}
- MoeScatterReductionRefImpl(const program_node& node, const RuntimeParams& params) : MoeScatterReductionRefImpl() {
+ MoeScatterReductionOptImpl() : PrimitiveImplOCL(MoeScatterReductionOpt::get_type_info_static()) {}
+ MoeScatterReductionOptImpl(const program_node& node, const RuntimeParams& params) : MoeScatterReductionOptImpl() {
add_stage(moe_scatter_reduction, params);
}
[[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {
- return make_deep_copy<MoeScatterReductionRefImpl>(this);
+ return make_deep_copy<MoeScatterReductionOptImpl>(this);
}
};
} // namespace
-std::unique_ptr<primitive_impl> MoeScatterReductionRef::create_impl(const program_node& node, const RuntimeParams& params) const {
+std::unique_ptr<primitive_impl> MoeScatterReductionOpt::create_impl(const program_node& node, const RuntimeParams& params) const {
assert(node.is_type<moe_scatter_reduction>());
- return std::make_unique<MoeScatterReductionRefImpl>(node, params);
+ return std::make_unique<MoeScatterReductionOptImpl>(node, params);
}
} // namespace ov::intel_gpu::ocl
-BIND_BINARY_BUFFER_WITH_TYPE(cldnn::moe_scatter_reduction)
-BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::MoeScatterReductionRefImpl)
+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::MoeScatterReductionOptImpl)
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_opt.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_opt.hpp
new file mode 100644
index 00000000000..5d6a174588f
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_opt.hpp
@@ -0,0 +1,53 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <memory>
+#include <utility>
+
+#include "moe_scatter_reduction_base.hpp"
+#include "program_node.h"
+#include "registry/implementation_manager.hpp"
+
+using namespace cldnn; // TODO: Remove once namespaces are aligned
+namespace ov::intel_gpu::ocl {
+struct MoeScatterReductionOpt : public MoeScatterReductionBase {
+ OV_GPU_PRIMITIVE_IMPL("ocl::moe_scatter_reduction::opt")
+ explicit MoeScatterReductionOpt(shape_types shape_type, ValidateFunc vf = nullptr) : MoeScatterReductionBase(shape_type, std::move(vf)) {}
+ std::unique_ptr<primitive_impl> create_impl(const program_node& node, const RuntimeParams& params) const override;
+ [[nodiscard]] bool validate_impl(const program_node& node) const override {
+ static constexpr std::array supported_fmts = {
+ format::bfyx,
+ };
+
+ static constexpr std::array supported_types = {
+ ov::element::f16,
+ ov::element::i32,
+ };
+
+ const auto& in0_layout = node.get_input_layout(0);
+ const auto& out_layout = node.get_output_layout(0);
+ const auto& input_pshapes = in0_layout.get_partial_shape();
+
+ if (input_pshapes.rank().get_length() != 3 || input_pshapes[2].is_dynamic()) {
+ return false;
+ }
+
+ if (input_pshapes[2].get_length() % block_size != 0) {
+ return false;
+ }
+ if (!one_of(in0_layout.format, supported_fmts) || !one_of(out_layout.format, supported_fmts)) {
+ return false;
+ }
+
+ if (!one_of(in0_layout.data_type, supported_types) || !one_of(out_layout.data_type, supported_types)) {
+ return false;
+ }
+
+ return true;
+ }
+ static constexpr size_t block_size = 4;
+};
+} // namespace ov::intel_gpu::ocl
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_ref.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_ref.cpp
new file mode 100644
index 00000000000..2dbd4f7de94
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_ref.cpp
@@ -0,0 +1,89 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+#include "moe_scatter_reduction_ref.hpp"
+
+#include "../common_utils/dispatch_utils.hpp"
+#include "../common_utils/jitter.hpp"
+#include "../primitive_ocl_base.hpp"
+#include "../utils/kernel_generator.hpp"
+#include "intel_gpu/primitives/moe_scatter_reduction.hpp"
+
+namespace ov::intel_gpu::ocl {
+namespace {
+
+using namespace ov::intel_gpu::ocl;
+class MoeScatterReductionRefGenerator : public KernelGenerator {
+public:
+ MoeScatterReductionRefGenerator() : KernelGenerator("moe_scatter_reduction_ref") {}
+
+protected:
+ [[nodiscard]] JitConstants get_jit_constants(const RuntimeParams& params) const override {
+ auto jit = KernelGenerator::get_jit_constants(params);
+ auto in_l = params.input_layouts[0];
+ auto hidden_size = extract_channel(ChannelName::Y, in_l);
+
+ const auto& desc = params.typed_desc<moe_scatter_reduction>();
+
+ jit.make("ACTIVE_EXPERTS", desc->num_active_experts_per_token);
+ jit.make("HIDDEN_SIZE", hidden_size);
+ return jit;
+ }
+
+ Arguments get_arguments_desc(const RuntimeParams& params) const override {
+ Arguments args;
+ if (params.is_dynamic()) {
+ args.push_back({ArgumentDescriptor::Types::SHAPE_INFO, 0});
+ }
+
+ uint32_t num_of_inputs = 7;
+
+ for (uint32_t i = 0; i < num_of_inputs; i++) {
+ args.push_back({ArgumentDescriptor::Types::INPUT, i});
+ }
+
+ args.push_back({ArgumentDescriptor::Types::OUTPUT, 0});
+
+ return args;
+ }
+
+ [[nodiscard]] DispatchDataFunc get_dispatch_data_func() const override {
+ return DispatchDataFunc{[](const RuntimeParams& params, KernelData& kd, ImplRuntimeParams* rt_params) {
+ auto& wgs = kd.params.workGroups;
+
+ if (!params.is_dynamic()) {
+ auto num_tokens = extract_channel(ChannelName::BATCH, params.input_layouts[1]);
+ wgs.global = {num_tokens, 1, 1};
+ wgs.local = {1, 1, 1};
+ }
+ }};
+ }
+};
+
+class MoeScatterReductionRefImpl : public PrimitiveImplOCL {
+public:
+ DECLARE_OBJECT_TYPE_SERIALIZATION(ov::intel_gpu::ocl::MoeScatterReductionRefImpl)
+
+ Stage::Ptr moe_scatter_reduction = make_stage<MoeScatterReductionRefGenerator>();
+
+ MoeScatterReductionRefImpl() : PrimitiveImplOCL(MoeScatterReductionRef::get_type_info_static()) {}
+ MoeScatterReductionRefImpl(const program_node& node, const RuntimeParams& params) : MoeScatterReductionRefImpl() {
+ add_stage(moe_scatter_reduction, params);
+ }
+
+ [[nodiscard]] std::unique_ptr<primitive_impl> clone() const override {
+ return make_deep_copy<MoeScatterReductionRefImpl>(this);
+ }
+};
+
+} // namespace
+
+std::unique_ptr<primitive_impl> MoeScatterReductionRef::create_impl(const program_node& node, const RuntimeParams& params) const {
+ assert(node.is_type<moe_scatter_reduction>());
+ return std::make_unique<MoeScatterReductionRefImpl>(node, params);
+}
+
+} // namespace ov::intel_gpu::ocl
+
+BIND_BINARY_BUFFER_WITH_TYPE(cldnn::moe_scatter_reduction)
+BIND_BINARY_BUFFER_WITH_TYPE(ov::intel_gpu::ocl::MoeScatterReductionRefImpl)
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_ref.hpp
similarity index 79%
rename from src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction.hpp
rename to src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_ref.hpp
index 64af22a039a..5bcc838c1ef 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction.hpp
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe/moe_scatter_reduction_ref.hpp
@@ -7,17 +7,16 @@
#include <memory>
#include <utility>
+#include "moe_scatter_reduction_base.hpp"
#include "program_node.h"
#include "registry/implementation_manager.hpp"
using namespace cldnn; // TODO: Remove once namespaces are aligned
-
namespace ov::intel_gpu::ocl {
-
-struct MoeScatterReductionRef : public ImplementationManager {
- OV_GPU_PRIMITIVE_IMPL("ocl::moe_scatter_reduction")
- explicit MoeScatterReductionRef(shape_types shape_type, ValidateFunc vf = nullptr) : ImplementationManager(impl_types::ocl, shape_type, std::move(vf)) {}
- [[nodiscard]] std::unique_ptr<primitive_impl> create_impl(const program_node& node, const RuntimeParams& params) const override;
+struct MoeScatterReductionRef : public MoeScatterReductionBase {
+ OV_GPU_PRIMITIVE_IMPL("ocl::moe_scatter_reduction::ref")
+ explicit MoeScatterReductionRef(shape_types shape_type, ValidateFunc vf = nullptr) : MoeScatterReductionBase(shape_type, std::move(vf)) {}
+ std::unique_ptr<primitive_impl> create_impl(const program_node& node, const RuntimeParams& params) const override;
[[nodiscard]] bool validate_impl(const program_node& node) const override {
static constexpr std::array supported_fmts = {
format::bfyx,
@@ -51,5 +50,4 @@ struct MoeScatterReductionRef : public ImplementationManager {
return true;
}
};
-
} // namespace ov::intel_gpu::ocl
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_opt.cl
new file mode 100644
index 00000000000..f3e96aa9058
--- /dev/null
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_opt.cl
@@ -0,0 +1,80 @@
+// Copyright (C) 2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "include/batch_headers/common.cl"
+#include "include/fetch_utils.cl"
+
+#define VLOAD CAT(vload, VEC_BLK_SIZE)
+#define VSTORE CAT(vstore, VEC_BLK_SIZE)
+#define INPUT_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_BLK_SIZE)
+#define OUTPUT_VEC_TYPE MAKE_VECTOR_TYPE(OUTPUT_TYPE, VEC_BLK_SIZE)
+
+KERNEL(moe_scatter_reduction_ref)(
+ OPTIONAL_SHAPE_INFO_ARG
+ const __global INPUT0_TYPE* input,
+ const __global INPUT1_TYPE* experts_per_token,
+ const __global INPUT2_TYPE* expert_weights,
+ const __global INPUT3_TYPE* tokens_per_expert,
+ const __global INPUT4_TYPE* experts_start_offset,
+ const __global INPUT5_TYPE* tokens_len_per_expert,
+ const __global INPUT6_TYPE* experts_ids,
+ __global OUTPUT_TYPE* output
+)
+{
+ const uint token_group_id = (uint)get_group_id(0);
+ const uint threads_index = (uint)get_local_id(0);
+
+ OUTPUT_VEC_TYPE output_vec[BATCHES_PER_THREAD];
+ // start_offset_idx[i] = n : info for i-th expert in this thread is in the nth slot of the mask
+ __local uint start_offset_index[ACTIVE_EXPERTS];
+ __local uint input_offset;
+
+ if (threads_index < ACTIVE_EXPERTS) {
+ INPUT1_TYPE expert_id = experts_per_token[token_group_id * ACTIVE_EXPERTS + threads_index];
+ for (int i = 0; i < INPUT6_BATCH_NUM; i++) {
+ if (experts_ids[i] == expert_id) {
+ start_offset_index[threads_index] = i;
+ break;
+ }
+ }
+ }
+
+ if (threads_index == 0)
+ input_offset = 0;
+
+ barrier(CLK_LOCAL_MEM_FENCE);
+
+ uint dest_index = token_group_id * HIDDEN_SIZE;
+ uint output_pos = dest_index + threads_index * VEC_BLK_SIZE * BATCHES_PER_THREAD;
+
+ for (uint i = 0; i < BATCHES_PER_THREAD; i++) {
+ output_vec[i] = TO_OUTPUT_TYPE(0);
+ }
+
+ for (uint i = 0; i < ACTIVE_EXPERTS; i++) {
+ INPUT1_TYPE expert_id = experts_per_token[token_group_id * ACTIVE_EXPERTS + i];
+ INPUT2_TYPE expert_weight = expert_weights[token_group_id * ACTIVE_EXPERTS + i];
+ INPUT5_TYPE token_len = tokens_len_per_expert[start_offset_index[i]];
+ INPUT4_TYPE expert_offset = experts_start_offset[start_offset_index[i]];
+
+ for (uint tid = threads_index; tid < token_len; tid += get_local_size(0)) {
+ if (tokens_per_expert[expert_offset + tid] == token_group_id) {
+ input_offset = expert_offset + tid;
+ break;
+ }
+ }
+ barrier(CLK_LOCAL_MEM_FENCE);
+ for (uint j = 0; j < BATCHES_PER_THREAD; j++) {
+ const uint input_pos = input_offset * HIDDEN_SIZE + j * VEC_BLK_SIZE + threads_index * VEC_BLK_SIZE * BATCHES_PER_THREAD;
+ INPUT_VEC_TYPE input_data = VLOAD(0, &input[input_pos]);
+ input_data *= expert_weight;
+ output_vec[j] += input_data;
+ }
+ }
+
+ for (uint v = 0; v < BATCHES_PER_THREAD; v++) {
+ const uint out_pos = output_pos + v * VEC_BLK_SIZE;
+ VSTORE(output_vec[v], 0, &output[out_pos]);
+ }
+}
\ No newline at end of file
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_ref.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_ref.cl
index 6e0ec365777..bc324005293 100644
--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_ref.cl
+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/moe_scatter_reduction_ref.cl
@@ -5,11 +5,6 @@
#include "include/batch_headers/common.cl"
#include "include/fetch_utils.cl"
-#define VLOAD CAT(vload, VEC_BLK_SIZE)
-#define VSTORE CAT(vstore, VEC_BLK_SIZE)
-#define INPUT_VEC_TYPE MAKE_VECTOR_TYPE(INPUT0_TYPE, VEC_BLK_SIZE)
-#define OUTPUT_VEC_TYPE MAKE_VECTOR_TYPE(OUTPUT_TYPE, VEC_BLK_SIZE)
-
KERNEL(moe_scatter_reduction_ref)(
OPTIONAL_SHAPE_INFO_ARG
const __global INPUT0_TYPE* input,
@@ -22,94 +17,34 @@ KERNEL(moe_scatter_reduction_ref)(
__global OUTPUT_TYPE* output
)
{
- const uint token_group_id = (uint)get_group_id(0);
- const uint threads_index = (uint)get_local_id(0);
-
- OUTPUT_VEC_TYPE output_vec[BATCHES_PER_THREAD];
- // start_offset_idx[i] = n : info for i-th expert in this thread is in the nth slot of the mask
- __local uint start_offset_index[ACTIVE_EXPERTS];
- __local uint input_offset;
-
- if (threads_index < ACTIVE_EXPERTS) {
- INPUT1_TYPE expert_id = experts_per_token[token_group_id * ACTIVE_EXPERTS + threads_index];
- for (int i = 0; i < INPUT6_BATCH_NUM; i++) {
+ uint token_id = (uint)get_global_id(0);
+ uint output_base_idx = token_id * HIDDEN_SIZE;
+ for (int e_iter = 0; e_iter < ACTIVE_EXPERTS; ++e_iter) {
+ INPUT1_TYPE expert_id = experts_per_token[token_id * ACTIVE_EXPERTS + e_iter];
+ INPUT2_TYPE weight = expert_weights[token_id * ACTIVE_EXPERTS + e_iter];
+ int idx = 0;
+ for (int i = 0; i < INPUT6_BATCH_NUM; ++i) {
if (experts_ids[i] == expert_id) {
- start_offset_index[threads_index] = i;
- break;
- }
- }
- }
-
- if (threads_index == 0)
- input_offset = 0;
-
- barrier(CLK_LOCAL_MEM_FENCE);
-
-#if UNALIGNED_ELEMENTS > 0
- OUTPUT_TYPE output_scalar[UNALIGNED_ELEMENTS];
-#endif
-
- uint dest_index = token_group_id * HIDDEN_SIZE;
- uint output_pos = dest_index + threads_index * VEC_BLK_SIZE * BATCHES_PER_THREAD;
-
- for (uint i = 0; i < BATCHES_PER_THREAD; i++) {
- output_vec[i] = TO_OUTPUT_TYPE(0);
- }
-
-#if UNALIGNED_ELEMENTS > 0
- for (uint i = 0; i < UNALIGNED_ELEMENTS; i++) {
- output_scalar[i] = TO_OUTPUT_TYPE(0);
- }
-#endif
-
- for (uint i = 0; i < ACTIVE_EXPERTS; i++) {
- INPUT1_TYPE expert_id = experts_per_token[token_group_id * ACTIVE_EXPERTS + i];
- INPUT2_TYPE expert_weight = expert_weights[token_group_id * ACTIVE_EXPERTS + i];
- INPUT5_TYPE token_len = tokens_len_per_expert[start_offset_index[i]];
- INPUT4_TYPE expert_offset = experts_start_offset[start_offset_index[i]];
-
- for (uint tid = threads_index; tid < token_len; tid += get_local_size(0)) {
- if (tokens_per_expert[expert_offset + tid] == token_group_id) {
- input_offset = expert_offset + tid;
+ idx = i;
break;
}
}
- barrier(CLK_LOCAL_MEM_FENCE);
- for (uint j = 0; j < BATCHES_PER_THREAD; j++) {
- const uint input_pos = input_offset * HIDDEN_SIZE + j * VEC_BLK_SIZE + threads_index * VEC_BLK_SIZE * BATCHES_PER_THREAD;
-
-#if UNALIGNED_ELEMENTS > 0
- if ((threads_index == get_local_size(0) - 1) && (j == 0)) {
- uint input_pos_unaligned = input_pos;
- for (uint k = 0; k < UNALIGNED_ELEMENTS; k++) {
- output_scalar[k] += input[input_pos_unaligned] * expert_weight;
- input_pos_unaligned++;
- }
- } else {
-#endif
- INPUT_VEC_TYPE input_data = VLOAD(0, &input[input_pos]);
- input_data *= expert_weight;
- output_vec[j] += input_data;
-#if UNALIGNED_ELEMENTS > 0
+ uint exp_offset_start = experts_start_offset[idx];
+ uint input_len = tokens_len_per_expert[idx];
+ uint input_offset = 0;
+ for (uint t = 0; t < input_len; ++t) {
+ if (tokens_per_expert[exp_offset_start + t] == token_id) {
+ input_offset = exp_offset_start + t;
+ break;
}
-#endif
- }
- }
-
-#if UNALIGNED_ELEMENTS > 0
- if ((threads_index == get_local_size(0) - 1)) {
- uint output_pos_unaligned = output_pos;
- for (uint s = 0; s < UNALIGNED_ELEMENTS; s++) {
- output[output_pos_unaligned] = output_scalar[s];
- output_pos_unaligned++;
}
- } else {
-#endif
- for (uint v = 0; v < BATCHES_PER_THREAD; v++) {
- const uint out_pos = output_pos + v * VEC_BLK_SIZE;
- VSTORE(output_vec[v], 0, &output[out_pos]);
+ uint in_pos = input_offset * HIDDEN_SIZE;
+ uint out_pos = token_id * HIDDEN_SIZE;
+ for (uint h = 0; h < HIDDEN_SIZE; h++) {
+ if (e_iter == 0)
+ output[out_pos + h] = input[in_pos + h] * weight;
+ else
+ output[out_pos + h] += input[in_pos + h] * weight;
}
-#if UNALIGNED_ELEMENTS > 0
}
-#endif
}
diff --git a/src/plugins/intel_gpu/src/graph/registry/moe_scatter_reduction_impls.cpp b/src/plugins/intel_gpu/src/graph/registry/moe_scatter_reduction_impls.cpp
index 535e9564ed5..dc39c3227a2 100644
--- a/src/plugins/intel_gpu/src/graph/registry/moe_scatter_reduction_impls.cpp
+++ b/src/plugins/intel_gpu/src/graph/registry/moe_scatter_reduction_impls.cpp
@@ -7,7 +7,8 @@
#include "primitive_inst.h"
#if OV_GPU_WITH_OCL
- #include "impls/ocl_v2/moe/moe_scatter_reduction.hpp"
+ #include "impls/ocl_v2/moe/moe_scatter_reduction_ref.hpp"
+ #include "impls/ocl_v2/moe/moe_scatter_reduction_opt.hpp"
#endif
namespace ov {
@@ -17,6 +18,8 @@ using namespace cldnn;
const std::vector<std::shared_ptr<cldnn::ImplementationManager>>& Registry<moe_scatter_reduction>::get_implementations() {
static const std::vector<std::shared_ptr<ImplementationManager>> impls = {
+ OV_GPU_CREATE_INSTANCE_OCL(ocl::MoeScatterReductionOpt, shape_types::dynamic_shape)
+ OV_GPU_CREATE_INSTANCE_OCL(ocl::MoeScatterReductionOpt, shape_types::static_shape)
OV_GPU_CREATE_INSTANCE_OCL(ocl::MoeScatterReductionRef, shape_types::dynamic_shape)
OV_GPU_CREATE_INSTANCE_OCL(ocl::MoeScatterReductionRef, shape_types::static_shape)
};
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp
index c82c726d0e1..487b0ce3884 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.cpp
@@ -9,6 +9,7 @@
#include "openvino/core/rt_info.hpp"
#include "openvino/op/constant.hpp"
+#include "openvino/op/matmul.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/cos.hpp"
#include "openvino/op/sin.hpp"
@@ -17,6 +18,7 @@
#include "openvino/op/gelu.hpp"
#include "openvino/op/reshape.hpp"
#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
#include "openvino/op/transpose.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "openvino/op/add.hpp"
@@ -170,6 +172,106 @@ IncreasePositionIdsPrecisionForLtxVideo::IncreasePositionIdsPrecisionForLtxVideo
this->register_matcher(m, callback);
}
+IncreasePositionIdsPrecisionForGPTOSS::IncreasePositionIdsPrecisionForGPTOSS() {
+ using namespace ov::pass::pattern;
+ using ov::pass::pattern::op::Or;
+
+ auto broadcast_freq = wrap_type<ov::op::v3::Broadcast>({any_input(), any_input()});
+
+ auto convert_pos_id_to_i32 = wrap_type<ov::op::v0::Convert>({any_input()});
+ auto unsqueeze_pos_id = wrap_type<ov::op::v0::Unsqueeze>({convert_pos_id_to_i32, any_input()});
+ auto convert_pos_id_to_f16 = wrap_type<ov::op::v0::Convert>({unsqueeze_pos_id});
+
+ auto convert_broadcast_freq = wrap_type<ov::op::v0::Convert>({broadcast_freq});
+
+ auto broadcast_freq_ = std::make_shared<Or>(OutputVector{broadcast_freq, convert_broadcast_freq});
+
+ auto matmul_freq_pos_id = wrap_type<ov::op::v0::MatMul>({broadcast_freq_, convert_pos_id_to_f16});
+ auto transpose = wrap_type<ov::op::v1::Transpose>({matmul_freq_pos_id, any_input()});
+
+ auto sin = wrap_type<ov::op::v0::Sin>({transpose});
+ auto sin_convert = wrap_type<ov::op::v0::Convert>({sin});
+ auto sin_ = std::make_shared<Or>(OutputVector{sin, sin_convert});
+
+ auto cos = wrap_type<ov::op::v0::Cos>({transpose});
+ auto cos_convert = wrap_type<ov::op::v0::Convert>({cos});
+ auto cos_ = std::make_shared<Or>(OutputVector{cos, cos_convert});
+
+ auto scale_const_sin = wrap_type<ov::op::v0::Constant>();
+ auto scale_const_sin_convert = wrap_type<ov::op::v0::Convert>({scale_const_sin});
+ auto scale_const_sin_ = std::make_shared<Or>(OutputVector{scale_const_sin, scale_const_sin_convert});
+ auto mul_sin_scale = wrap_type<ov::op::v1::Multiply>({sin_, scale_const_sin_});
+
+ auto scale_const_cos = wrap_type<ov::op::v0::Constant>();
+ auto scale_const_cos_convert = wrap_type<ov::op::v0::Convert>({scale_const_cos});
+ auto scale_const_cos_ = std::make_shared<Or>(OutputVector{scale_const_cos, scale_const_cos_convert});
+ auto mul_cos_scale = wrap_type<ov::op::v1::Multiply>({cos_, scale_const_cos_});
+
+ auto unsqueeze_mul_sin_scale = wrap_type<ov::op::v0::Unsqueeze>({mul_sin_scale, any_input()});
+ auto mul_q_sin = wrap_type<ov::op::v1::Multiply>({any_input()/* q_second_half*/, unsqueeze_mul_sin_scale});
+
+ auto unsqueeze_mul_cos_scale = wrap_type<ov::op::v0::Unsqueeze>({mul_cos_scale, any_input()});
+ auto mul_q_cos = wrap_type<ov::op::v1::Multiply>({any_input()/* q_second_half*/, unsqueeze_mul_cos_scale});
+
+ auto q_half_mul1 = wrap_type<ov::op::v1::Multiply>({any_input(), any_input()});
+ auto q_half_mul2 = wrap_type<ov::op::v1::Multiply>({q_half_mul1, any_input()});
+ auto q_half_first = wrap_type<ov::op::v1::Add>({mul_q_cos, q_half_mul2});
+
+ auto q_half_mul4 = wrap_type<ov::op::v1::Multiply>({any_input(), any_input()});
+ auto q_half_second = wrap_type<ov::op::v1::Add>({mul_q_sin, q_half_mul4});
+
+ auto concat_q_1 = wrap_type<ov::op::v0::Concat>({q_half_second, q_half_first});
+ auto concat_q_2 = wrap_type<ov::op::v0::Concat>({q_half_first, q_half_second});
+ auto concat_q = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{concat_q_1, concat_q_2});
+
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
+ const auto& pattern_map = m.get_pattern_value_map();
+ bool matched = false;
+ if (pattern_map.count(concat_q_1) > 0) {
+ matched = true;
+ } else if (pattern_map.count(concat_q_2) > 0) {
+ matched = true;
+ }
+ if (!matched || transformation_callback(concat_q))
+ return false;
+
+ std::shared_ptr<ov::op::v0::Concat> output_concat_node;
+ if (pattern_map.count(concat_q_1) > 0) {
+ output_concat_node = ov::as_type_ptr<ov::op::v0::Concat>(pattern_map.at(concat_q_1).get_node_shared_ptr());
+ } else if (pattern_map.count(concat_q_2) > 0) {
+ output_concat_node = ov::as_type_ptr<ov::op::v0::Concat>(pattern_map.at(concat_q_2).get_node_shared_ptr());
+ }
+ auto matmul_node = ov::as_type_ptr<ov::op::v0::MatMul>(pattern_map.at(matmul_freq_pos_id).get_node_shared_ptr());
+ auto mul_node1 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(mul_sin_scale).get_node_shared_ptr());
+ auto mul_node2 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(mul_cos_scale).get_node_shared_ptr());
+ auto mul_node3 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(mul_q_sin).get_node_shared_ptr());
+ auto mul_node4 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(mul_q_cos).get_node_shared_ptr());
+ auto mul_node5 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(q_half_mul1).get_node_shared_ptr());
+ auto mul_node6 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(q_half_mul2).get_node_shared_ptr());
+ auto mul_node8 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(q_half_mul4).get_node_shared_ptr());
+
+ const auto desired_et = ov::element::f32;
+ const auto original_et = output_concat_node->get_output_element_type(0);
+ if (original_et == desired_et)
+ return false;
+
+ size_t idx = 0;
+ insert_converts_before_if_needed(matmul_node, desired_et, idx);
+ insert_converts_before_if_needed(mul_node1, desired_et, idx);
+ insert_converts_before_if_needed(mul_node2, desired_et, idx);
+ insert_converts_before_if_needed(mul_node3, desired_et, idx);
+ insert_converts_before_if_needed(mul_node4, desired_et, idx);
+ insert_converts_before_if_needed(mul_node5, desired_et, idx);
+ insert_converts_before_if_needed(mul_node6, desired_et, idx);
+ insert_converts_before_if_needed(mul_node8, desired_et, idx);
+ insert_converts_after_if_needed(output_concat_node, original_et, idx);
+ return true;
+ };
+
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(concat_q, "IncreasePositionIdsPrecisionForGPTOSS");
+ this->register_matcher(m, callback);
+}
+
bool IncreasePositionIdsPrecisionForRoPE::insert_converts_before_if_needed(const std::shared_ptr<ov::Node>& node,
const ov::element::Type desired_et, size_t& input_idx) {
bool is_changed = false;
@@ -225,6 +327,38 @@ bool IncreasePositionIdsPrecision::run_on_model(const std::shared_ptr<ov::Model>
auto symbolic_ctx_manager = symbolic_optimizations.get_manager();
symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForRoPE>();
symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForLtxVideo>();
+ symbolic_ctx_manager->register_pass<IncreasePositionIdsPrecisionForGPTOSS>();
return symbolic_optimizations.run_on_model(model);
}
+
+DisableFP16ComForGPTOSSROPEPattern::DisableFP16ComForGPTOSSROPEPattern() {
+ using namespace ov::pass::pattern;
+ using ov::pass::pattern::op::Or;
+
+ // for gpt-oss pattern
+ auto freq_const = wrap_type<ov::op::v0::Constant>();
+ auto broadcast_freq = wrap_type<ov::op::v3::Broadcast>({freq_const, any_input()});
+
+ // position_id
+ auto unsqueeze_pos_id = wrap_type<ov::op::v0::Unsqueeze>({any_input(), any_input()});
+ auto convert_pos_id_to_f16 = wrap_type<ov::op::v0::Convert>({unsqueeze_pos_id});
+
+ auto matmul_freq_pos_id = wrap_type<ov::op::v0::MatMul>({broadcast_freq, convert_pos_id_to_f16});
+ auto transpose = wrap_type<ov::op::v1::Transpose>({matmul_freq_pos_id, any_input()});
+ auto sin = wrap_type<ov::op::v0::Sin>({transpose});
+
+ ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
+ const auto& pattern_map = m.get_pattern_value_map();
+
+ auto sin_node = ov::as_type_ptr<ov::op::v0::Sin>(pattern_map.at(sin).get_node_shared_ptr());
+ if (!sin_node || transformation_callback(sin_node))
+ return false;
+ auto freq_const_node = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(freq_const).get_node_shared_ptr());
+ ov::disable_fp16_compression(freq_const_node);
+ return true;
+ };
+
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(sin, "DisableFP16ComForGPTOSSROPEPattern");
+ this->register_matcher(m, callback);
+}
} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp
index bc7a2230b8b..64c6a5e88db 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations/increase_position_ids_precision.hpp
@@ -23,6 +23,13 @@ public:
IncreasePositionIdsPrecisionForLtxVideo();
};
+class IncreasePositionIdsPrecisionForGPTOSS : public IncreasePositionIdsPrecisionForRoPE {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("IncreasePositionIdsPrecisionForGPTOSS");
+ IncreasePositionIdsPrecisionForGPTOSS();
+};
+
+
/**
* @brief This pass adds additional convert nodes on the position_ids input branch (around MatMul or Multiply operation),
* targeting to improve runtime rotary position embeddings calculation for FP16 models.
@@ -36,4 +43,9 @@ public:
bool run_on_model(const std::shared_ptr<ov::Model>& model) override;
};
+class DisableFP16ComForGPTOSSROPEPattern: public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("DisableFP16ComForROPEPattern");
+ DisableFP16ComForGPTOSSROPEPattern();
+};
} // namespace ov::intel_gpu
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index f323034d6b8..b583751399f 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -539,7 +539,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
});
manager.register_pass<ov::pass::RMSFusion>(false, true);
manager.register_pass<DisableFP16CompForGemma3RMSPattern>();
-
+ manager.register_pass<DisableFP16ComForGPTOSSROPEPattern>();
const bool keep_precision_sensitive_in_fp32_1 = true;
const bool convert_input_output_precision = false;
const bool store_original_precision_as_rt_attribute = true;