Skip to content

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

[GPU] ConvertMOEToMOECompressed: keep precisions consistency after MOECompressed output precision enforcement (#34295)

Vladislav Golubev8becd09579a9
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 a50ac0781dd..1dc49a611b7 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
@@ -235,10 +235,18 @@ ConvertMOEToMOECompressed::ConvertMOEToMOECompressed(bool is_pa) {
config.top_k = topk_shape[1].get_length();
config.out_type = ov::element::f16;
config.has_batch_dim = is_pa ? 0 : 1;
- auto moe_compressed = std::make_shared<ov::intel_gpu::op::MOECompressed>(args, config);
-
+ std::shared_ptr<ov::Node> moe_compressed = std::make_shared<ov::intel_gpu::op::MOECompressed>(args, config);
moe_compressed->set_friendly_name(moe->get_friendly_name());
ov::copy_runtime_info(moe, moe_compressed);
+
+ // Since f16 precision is forced for MOECompressed output, we may need to insert Convert after MOECompressed
+ // in order not to break the model semantic. This Convert will be most likely optimized at ConvertPrecision stage
+ if (moe->get_output_element_type(0) != moe_compressed->get_output_element_type(0)) {
+ moe_compressed->set_friendly_name(moe_compressed->get_friendly_name() + "/MOECompressed");
+ moe_compressed = std::make_shared<ov::op::v0::Convert>(moe_compressed, moe->get_output_element_type(0));
+ moe_compressed->set_friendly_name(moe->get_friendly_name());
+ ov::copy_runtime_info(moe, moe_compressed);
+ }
ov::replace_node(moe, moe_compressed);
} else if (moe->get_config().expert_type == ov::op::internal::MOE::Expert_type::GEMM2_BIAS_SWIGLU_CLAMP) {
OutputVector args;
diff --git a/src/plugins/intel_gpu/tests/unit/transformations/convert_moe_to_compressed_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/convert_moe_to_compressed_test.cpp
index 68fad583e00..b9ed4a9a382 100644
--- a/src/plugins/intel_gpu/tests/unit/transformations/convert_moe_to_compressed_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/transformations/convert_moe_to_compressed_test.cpp
@@ -27,12 +27,23 @@ using namespace ov::intel_gpu;
namespace ov {
namespace test {
namespace intel_gpu {
-TEST_F(TransformationTestsF, ConvertMOEToMOE3GemmCompressedTest) {
+class ConvertMOEToMOE3GemmCompressedTest : public TransformationTestsF, public WithParamInterface<ov::element::Type> {
+public:
+ static std::string get_test_case_name(testing::TestParamInfo<ov::element::Type> obj) {
+ ov::element::Type element_type = obj.param;
+ std::ostringstream result;
+ result << "ElementType=" << element_type.get_type_name();
+ return result.str();
+ }
+};
+
+TEST_P(ConvertMOEToMOE3GemmCompressedTest, GEMM3_SwiGLU) {
disable_rt_info_check();
+ const auto& data_type = GetParam();
{
// tokens:32, hidden_size:2048, iter_size:768, experts:128, topk:8
- auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{32, 2048});
- auto routing_weights = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{128, 1, 32, 1});
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(data_type, Shape{32, 2048});
+ auto routing_weights = std::make_shared<ov::op::v0::Parameter>(data_type, Shape{128, 1, 32, 1});
auto routing_idx = std::make_shared<ov::op::v0::Parameter>(element::i32, Shape{32, 8});
// Gate projection
@@ -84,8 +95,8 @@ TEST_F(TransformationTestsF, ConvertMOEToMOE3GemmCompressedTest) {
}
{
// Inputs
- auto hidden_states = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{32, 2048});
- auto routing_weights = std::make_shared<ov::op::v0::Parameter>(element::f16, Shape{128, 1, 32, 1});
+ auto hidden_states = std::make_shared<ov::op::v0::Parameter>(data_type, Shape{32, 2048});
+ auto routing_weights = std::make_shared<ov::op::v0::Parameter>(data_type, Shape{128, 1, 32, 1});
auto routing_idx = std::make_shared<ov::op::v0::Parameter>(element::i32, Shape{32, 8});
// Gate and up projection
@@ -126,14 +137,22 @@ TEST_F(TransformationTestsF, ConvertMOEToMOE3GemmCompressedTest) {
config.top_k = 8;
config.group_size = 128;
config.out_type = ov::element::f16;
- auto moe_compressed = std::make_shared<ov::intel_gpu::op::MOECompressed>(
+ std::shared_ptr<ov::Node> moe_compressed = std::make_shared<ov::intel_gpu::op::MOECompressed>(
ov::OutputVector{hidden_states, routing_weights, routing_idx,
wei_gate, scale_transpose_gate, zp_transpose_gate,
wei_up, scale_transpose_up, zp_transpose_up,
wei_down, scale_transpose_down, zp_transpose_down}, config);
+ if (config.out_type != data_type) {
+ moe_compressed = std::make_shared<ov::op::v0::Convert>(moe_compressed, data_type);
+ }
model_ref = std::make_shared<ov::Model>(moe_compressed, ov::ParameterVector{hidden_states, routing_weights, routing_idx});
}
}
+
+INSTANTIATE_TEST_SUITE_P(smoke,
+ ConvertMOEToMOE3GemmCompressedTest,
+ Values(element::f16, element::f32),
+ ConvertMOEToMOE3GemmCompressedTest::get_test_case_name);
} // namespace intel_gpu
} // namespace test
} // namespace ov