diff --git a/src/common/transformations/src/transformations/common_optimizations/gelu_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/gelu_fusion.cpp index 718d94f2f57..052001f27a9 100644 --- a/src/common/transformations/src/transformations/common_optimizations/gelu_fusion.cpp +++ b/src/common/transformations/src/transformations/common_optimizations/gelu_fusion.cpp @@ -49,7 +49,7 @@ ov::pass::GeluFusionWithErfOne::GeluFusionWithErfOne() { } bool valid_constant_values = - op::util::has_constant_value(div_const_value, static_cast(M_SQRT2)) && + op::util::has_constant_value(div_const_value, static_cast(M_SQRT2), 0.001f) && op::util::has_constant_value(add_const_value, 1.0f) && op::util::has_constant_value(mul_const_value, 0.5f); @@ -109,7 +109,7 @@ ov::pass::GeluFusionWithErfTwo::GeluFusionWithErfTwo() { } bool valid_constant_values = - op::util::has_constant_value(div_const_value, static_cast(M_SQRT2)) && + op::util::has_constant_value(div_const_value, static_cast(M_SQRT2), 0.001f) && op::util::has_constant_value(add_const_value, 1.0f) && op::util::has_constant_value(mul_const_value, 0.5f); @@ -169,7 +169,7 @@ ov::pass::GeluFusionWithErfThree::GeluFusionWithErfThree() { } bool valid_constant_values = - op::util::has_constant_value(div_const_value, static_cast(M_SQRT2)) && + op::util::has_constant_value(div_const_value, static_cast(M_SQRT2), 0.001f) && op::util::has_constant_value(add_const_value, 1.0f) && op::util::has_constant_value(mul_const_value, 0.5f); diff --git a/src/common/transformations/tests/common_optimizations/gelu_fusion.cpp b/src/common/transformations/tests/common_optimizations/gelu_fusion.cpp index 33fdd06b0e3..ef537569032 100644 --- a/src/common/transformations/tests/common_optimizations/gelu_fusion.cpp +++ b/src/common/transformations/tests/common_optimizations/gelu_fusion.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -50,6 +51,32 @@ TEST_F(TransformationTestsF, GeluFusionPatternOne) { } } +TEST_F(TransformationTestsF, GeluFusionPatternOneF16) { + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + + auto div_const = opset7::Constant::create(element::f16, Shape{1}, {M_SQRT2}); + auto add_const = opset7::Constant::create(element::f16, Shape{1}, {1.0}); + auto mul_const = opset7::Constant::create(element::f16, Shape{1}, {0.5}); + + auto div = std::make_shared(data, div_const); + auto erf = std::make_shared(div); + auto add = std::make_shared(erf, add_const); + auto mul_first = std::make_shared(data, mul_const); + auto mul = std::make_shared(mul_first, add); + + function = std::make_shared(NodeVector{mul}, ParameterVector{data}); + + manager.register_pass(); + } + + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + auto gelu = std::make_shared(data); + function_ref = std::make_shared(NodeVector{gelu}, ParameterVector{data}); + } +} + TEST_F(TransformationTestsF, GeluFusionPatternTwo) { { auto data = std::make_shared(element::f32, Shape{2, 2}); @@ -76,6 +103,32 @@ TEST_F(TransformationTestsF, GeluFusionPatternTwo) { } } +TEST_F(TransformationTestsF, GeluFusionPatternTwoF16) { + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + + auto div_const = opset7::Constant::create(element::f16, Shape{1}, {M_SQRT2}); + auto add_const = opset7::Constant::create(element::f16, Shape{1}, {1.0}); + auto mul_const = opset7::Constant::create(element::f16, Shape{1}, {0.5}); + + auto div = std::make_shared(data, div_const); + auto erf = std::make_shared(div); + auto add = std::make_shared(erf, add_const); + auto mul_first = std::make_shared(data, add); + auto mul = std::make_shared(mul_first, mul_const); + + function = std::make_shared(NodeVector{mul}, ParameterVector{data}); + + manager.register_pass(); + } + + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + auto gelu = std::make_shared(data); + function_ref = std::make_shared(NodeVector{gelu}, ParameterVector{data}); + } +} + TEST_F(TransformationTestsF, GeluFusionPatternThree) { { auto data = std::make_shared(element::f32, Shape{2, 2}); @@ -102,6 +155,32 @@ TEST_F(TransformationTestsF, GeluFusionPatternThree) { } } +TEST_F(TransformationTestsF, GeluFusionPatternThreeF16) { + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + + auto div_const = opset7::Constant::create(element::f16, Shape{1}, {M_SQRT2}); + auto add_const = opset7::Constant::create(element::f16, Shape{1}, {1.0}); + auto mul_const = opset7::Constant::create(element::f16, Shape{1}, {0.5}); + + auto div = std::make_shared(data, div_const); + auto erf = std::make_shared(div); + auto add = std::make_shared(erf, add_const); + auto mul_first = std::make_shared(add, mul_const); + auto mul = std::make_shared(data, mul_first); + + function = std::make_shared(NodeVector{mul}, ParameterVector{data}); + + manager.register_pass(); + } + + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + auto gelu = std::make_shared(data); + function_ref = std::make_shared(NodeVector{gelu}, ParameterVector{data}); + } +} + TEST_F(TransformationTestsF, GeluFusionPatternFour) { { auto data = std::make_shared(element::f32, Shape{2, 2}); @@ -128,6 +207,32 @@ TEST_F(TransformationTestsF, GeluFusionPatternFour) { } } +TEST_F(TransformationTestsF, GeluFusionPatternFourF16) { + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + + auto mul1_const = opset9::Constant::create(element::f16, Shape{1}, {1.0f / M_SQRT2}); + auto add_const = opset9::Constant::create(element::f16, Shape{1}, {0.5f}); + auto mul2_const = opset9::Constant::create(element::f16, Shape{1}, {0.5f}); + + auto mul1 = std::make_shared(data, mul1_const); + auto erf = std::make_shared(mul1); + auto mul2 = std::make_shared(erf, mul2_const); + auto add = std::make_shared(mul2, add_const); + auto mul3 = std::make_shared(data, add); + + function = std::make_shared(NodeVector{mul3}, ParameterVector{data}); + + manager.register_pass(); + } + + { + auto data = std::make_shared(element::f16, Shape{2, 2}); + auto gelu = std::make_shared(data); + function_ref = std::make_shared(NodeVector{gelu}, ParameterVector{data}); + } +} + TEST_F(TransformationTestsF, GeluFusionPatternIncorrectDivConstValue) { { auto data = std::make_shared(element::f32, Shape{2, 2});