OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Calculate rope in f32 for gpt-oss (#33297)
Eddy Kim0d6d50c02f29
diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cl b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.clindex e583fbfa057..babd10391bb 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cl+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cl@@ -331,10 +331,17 @@ KERNEL(rope_opt)const __global INPUT3_TYPE* gather,#endif__global OUTPUT_TYPE* output) {+#ifdef REVERSED_GWS+ const uint b = get_global_id(2);+ const uint h = get_global_id(1);+ const uint p = ((uint)get_global_id(0) * VEC_SIZE) / HALF_ROTARY_NDIMS;+ const uint r = ((uint)get_global_id(0) * VEC_SIZE) % HALF_ROTARY_NDIMS;+#elseconst uint b = get_global_id(0);const uint h = get_global_id(1);const uint p = ((uint)get_global_id(2) * VEC_SIZE) / HALF_ROTARY_NDIMS;const uint r = ((uint)get_global_id(2) * VEC_SIZE) % HALF_ROTARY_NDIMS;+#endif#if ENABLE_TRANSPOSEuint input_idx = INPUT0_GET_INDEX(b, p, h, 0);@@ -405,13 +412,14 @@ uint cos_sin_p = p;uint output_idx = OUTPUT_GET_INDEX(b, h, p, 0);#if VEC_SIZE == 1- INPUT0_TYPE in1 = input[input_idx + r];- INPUT0_TYPE in2 = input[input_idx + HALF_ROTARY_NDIMS + r];+ ACCUMULATOR_TYPE in1 = TO_ACCUMULATOR_TYPE(input[input_idx + r]);+ ACCUMULATOR_TYPE in2 = TO_ACCUMULATOR_TYPE(input[input_idx + HALF_ROTARY_NDIMS + r]);- output[output_idx + r] = cos[cos_idx + r] * in1 - sin[sin_idx + r] * in2;+ ACCUMULATOR_TYPE res = cos[cos_idx + r] * in1 - sin[sin_idx + r] * in2;+ output[output_idx + r] = TO_OUTPUT_TYPE(res);- output[output_idx + HALF_ROTARY_NDIMS + r] =- cos[cos_idx + COS_SIN_TABLE_OFFSET + r] * in2 + sin[sin_idx + COS_SIN_TABLE_OFFSET + r] * in1;+ res = cos[cos_idx + COS_SIN_TABLE_OFFSET + r] * in2 + sin[sin_idx + COS_SIN_TABLE_OFFSET + r] * in1;+ output[output_idx + HALF_ROTARY_NDIMS + r] = TO_OUTPUT_TYPE(res);#elseINPUT_VEC_TYPE in1 = *(INPUT_VEC_TYPE*)(input + input_idx + r);INPUT_VEC_TYPE in2 = *(INPUT_VEC_TYPE*)(input + input_idx + HALF_ROTARY_NDIMS + r);diff --git a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cppindex 3d084b1540b..6e425fb5bd4 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl_v2/rope_opt.cpp@@ -7,6 +7,7 @@#include "common_utils/jitter.hpp"#include "intel_gpu/primitives/rope.hpp"#include "primitive_ocl_base.hpp"+#include "utils/jitter.hpp"#include "utils/kernel_generator.hpp"namespace ov::intel_gpu::ocl {@@ -14,6 +15,7 @@ namespace {size_t get_vec_size(const RuntimeParams& params) {const auto& input = params.get_input_layout(0);+ const auto& input1 = params.get_input_layout(1);auto desc = params.typed_desc<rope>();size_t vec_size = 1;switch (input.data_type) {@@ -31,6 +33,11 @@ size_t get_vec_size(const RuntimeParams& params) {vec_size = 1;}+ // Some models use f32 precision for input1 (cos) and input2 (sin) for better accuracy.+ // If input0 is not f32, we set vec_size as 1 for simple type conversion.+ if (input1.data_type == ov::element::f32 && input.data_type != input1.data_type)+ vec_size = 1;+if (desc->config.is_qwen) {auto count = desc->config.head_cnt * std::max(desc->config.rotary_ndims / 2ul, desc->config.head_size - desc->config.rotary_ndims);if (count % vec_size != 0) {@@ -93,8 +100,16 @@ protected:jit.make("RotateInterleaved", true);} else {jit.make("RotateHalf", true);+ if (get_vec_size(params) == 1) {+ jit.make("REVERSED_GWS", true);+ }}jit.make("VEC_SIZE", get_vec_size(params));+ if (params.get_input_layout(0).data_type != params.get_input_layout(1).data_type) {+ jit.add(make_type_jit_constants("ACCUMULATOR", params.get_input_layout(1).data_type));+ } else {+ jit.add(make_type_jit_constants("ACCUMULATOR", params.get_input_layout(0).data_type));+ }return jit;}@@ -157,9 +172,38 @@ protected:if (cfg.support_3d_rope) {wgs.global = {b, f, cfg.rotary_ndims / 2ul / vec_size};}+ // reverse gws when RotateHalf and vec_size is one+ if (!desc->config.is_interleaved && vec_size == 1) {+ size_t tmp = wgs.global[0];+ wgs.global[0] = wgs.global[2];+ wgs.global[2] = tmp;+ }}- wgs.local = ov::intel_gpu::get_optimal_lws(wgs.global, params.get_device_info(), in_l.format, out_l.format, dims_by_gws);+ // We need to set the 1st local workgroup size as large as possible for better performance.+ if (vec_size == 1) {+ auto get_max_lws = [](size_t gws, size_t max_workgroup_size) -> size_t {+ size_t val = 1;+ size_t lws = 1;+ while (((val + 1) <= max_workgroup_size) && (gws >= (val + 1))) {+ val += 1;+ if (gws % val == 0)+ lws = val;+ }+ return lws;+ };++ size_t max_workgroup_size = static_cast<size_t>(params.get_device_info().max_work_group_size);++ wgs.local = {1, 1, 1};+ wgs.local[0] = get_max_lws(wgs.global[0], max_workgroup_size);+ max_workgroup_size /= wgs.local[0];+ wgs.local[1] = get_max_lws(wgs.global[1], max_workgroup_size);+ max_workgroup_size /= wgs.local[1];+ wgs.local[2] = get_max_lws(wgs.global[2], max_workgroup_size);+ } else {+ wgs.local = ov::intel_gpu::get_optimal_lws(wgs.global, params.get_device_info(), in_l.format, out_l.format, dims_by_gws);+ }}}};}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.cppindex d26fe941c22..8d3fc7535a1 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@@ -29,6 +29,7 @@#include "openvino/op/scaled_dot_product_attention.hpp"#include "openvino/op/variadic_split.hpp"#include "ov_ops/rms.hpp"+#include "ov_ops/rotary_positional_embeddings.hpp"#include "openvino/pass/pattern/op/or.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"#include "transformations/utils/utils.hpp"@@ -251,7 +252,6 @@ IncreasePositionIdsPrecisionForLtxVideo::IncreasePositionIdsPrecisionForLtxVideothis->register_matcher(m, callback);}-// TODO : To have a fused rope kernel for this patternIncreasePositionIdsPrecisionForGPTOSS::IncreasePositionIdsPrecisionForGPTOSS() {using namespace ov::pass::pattern;using ov::pass::pattern::op::Or;@@ -294,58 +294,26 @@ IncreasePositionIdsPrecisionForGPTOSS::IncreasePositionIdsPrecisionForGPTOSS() {auto reshape_mul_sin_scale_ = wrap_type<ov::op::v1::Reshape>({mul_sin_scale, any_input()});auto reshape_mul_sin_scale = std::make_shared<Or>(OutputVector{unsqueeze_mul_sin_scale_, reshape_mul_sin_scale_});- auto mul_qk_sin = wrap_type<ov::op::v1::Multiply>({any_input()/*second_half*/, reshape_mul_sin_scale});-auto unsqueeze_mul_cos_scale_ = wrap_type<ov::op::v0::Unsqueeze>({mul_cos_scale, any_input()});auto reshape_mul_cos_scale_ = wrap_type<ov::op::v1::Reshape>({mul_cos_scale, any_input()});auto reshape_mul_cos_scale = std::make_shared<Or>(OutputVector{unsqueeze_mul_cos_scale_, reshape_mul_cos_scale_});- auto mul_qk_cos = wrap_type<ov::op::v1::Multiply>({any_input()/* first_half*/, reshape_mul_cos_scale});-- auto qk_half_mul1 = wrap_type<ov::op::v1::Multiply>({any_input(), any_input()});- auto qk_half_mul2_1 = wrap_type<ov::op::v1::Multiply>({qk_half_mul1, any_input()});- auto qk_half_mul2_2 = wrap_type<ov::op::v1::Multiply>({any_input(), qk_half_mul1});- auto qk_half_mul2 = std::make_shared<Or>(OutputVector{qk_half_mul2_1, qk_half_mul2_2});- auto qk_half_first_1 = wrap_type<ov::op::v1::Add>({mul_qk_cos, qk_half_mul2});- auto qk_half_first_2 = wrap_type<ov::op::v1::Add>({qk_half_mul2, mul_qk_cos});- auto qk_half_first = std::make_shared<Or>(OutputVector{qk_half_first_1, qk_half_first_2});- auto qk_half_mul4 = wrap_type<ov::op::v1::Multiply>({any_input(), any_input()});- auto qk_half_second_1 = wrap_type<ov::op::v1::Add>({mul_qk_sin, qk_half_mul4});- auto qk_half_second_2 = wrap_type<ov::op::v1::Add>({qk_half_mul4, mul_qk_sin});- auto qk_half_second = std::make_shared<Or>(OutputVector{qk_half_second_1, qk_half_second_2});-- auto concat_qk_1 = wrap_type<ov::op::v0::Concat>({qk_half_second, qk_half_first});- auto concat_qk_2 = wrap_type<ov::op::v0::Concat>({qk_half_first, qk_half_second});- auto concat_qk = std::make_shared<ov::pass::pattern::op::Or>(OutputVector{concat_qk_1, concat_qk_2});+ auto rope_qk = wrap_type<ov::op::internal::RoPE>({any_input(), reshape_mul_cos_scale, reshape_mul_sin_scale});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_qk_1) > 0) {- matched = true;- } else if (pattern_map.count(concat_qk_2) > 0) {- matched = true;- }- if (!matched || transformation_callback(concat_qk))++ if (transformation_callback(rope_qk))return false;- std::shared_ptr<ov::op::v0::Concat> output_concat_node;- if (pattern_map.count(concat_qk_1) > 0) {- output_concat_node = ov::as_type_ptr<ov::op::v0::Concat>(pattern_map.at(concat_qk_1).get_node_shared_ptr());- } else if (pattern_map.count(concat_qk_2) > 0) {- output_concat_node = ov::as_type_ptr<ov::op::v0::Concat>(pattern_map.at(concat_qk_2).get_node_shared_ptr());- }+ auto rope_node = ov::as_type_ptr<ov::op::internal::RoPE>(pattern_map.at(rope_qk).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_qk_sin).get_node_shared_ptr());- auto mul_node4 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(mul_qk_cos).get_node_shared_ptr());- auto mul_node5 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(qk_half_mul1).get_node_shared_ptr());- auto mul_node6 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(qk_half_mul2).get_node_shared_ptr());- auto mul_node8 = ov::as_type_ptr<ov::op::v1::Multiply>(pattern_map.at(qk_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);+ const auto original_et = rope_node->get_output_element_type(0);if (original_et == desired_et)return false;@@ -353,16 +321,11 @@ IncreasePositionIdsPrecisionForGPTOSS::IncreasePositionIdsPrecisionForGPTOSS() {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_qk, "IncreasePositionIdsPrecisionForGPTOSS");+ auto m = std::make_shared<ov::pass::pattern::Matcher>(rope_qk, "IncreasePositionIdsPrecisionForGPTOSS");this->register_matcher(m, callback);}diff --git a/src/plugins/intel_gpu/tests/unit/transformations/increase_position_ids_precision_test.cpp b/src/plugins/intel_gpu/tests/unit/transformations/increase_position_ids_precision_test.cppindex d7356654c4f..fa10b8a5297 100644--- a/src/plugins/intel_gpu/tests/unit/transformations/increase_position_ids_precision_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/transformations/increase_position_ids_precision_test.cpp@@ -723,4 +723,94 @@ TEST_F(TransformationTestsF, IncreasePositionIdsPrecisionForQwen25VL) {model_ref = std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{position_ids, input_2});}comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);-}\ No newline at end of file+}++TEST_F(TransformationTestsF, IncreasePositionIdsPrecisionForGPTOSS) {+ {+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{ 3, -1, -1 });+ auto input_convert = std::make_shared<ov::op::v0::Convert>(position_ids, ov::element::i32);+ auto input_unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(input_convert,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, std::vector<int64_t>{0}));+ auto convert_2 = std::make_shared<ov::op::v0::Convert>(input_unsqueeze, ov::element::f16);++ auto shape_of = std::make_shared<ov::op::v3::ShapeOf>(input_convert, ov::element::i32);+ auto gather_0 = std::make_shared<ov::op::v8::Gather>(shape_of,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{1}),+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, std::vector<int64_t>{0}));+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{1}),+ gather_0,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{64}),+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{1})}, 0);+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1, 64, 1}), concat);+ auto matmul = std::make_shared<ov::op::v0::MatMul>(broadcast, convert_2);+ auto transpose = std::make_shared<ov::op::v1::Transpose>(matmul,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<int64_t>{0, 1, 3, 2}));++ auto cos = std::make_shared<ov::op::v0::Cos>(transpose);+ auto sin = std::make_shared<ov::op::v0::Sin>(transpose);++ auto mul_cos_scale = std::make_shared<ov::op::v1::Multiply>(cos,+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1}));+ auto unsqueeze_cos = std::make_shared<ov::op::v0::Unsqueeze>(mul_cos_scale,+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{}, std::vector<int64_t>{0}));++ auto mul_sin_scale = std::make_shared<ov::op::v1::Multiply>(sin,+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1}));+ auto unsqueeze_sin = std::make_shared<ov::op::v0::Unsqueeze>(mul_sin_scale,+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{}, std::vector<int64_t>{0}));++ auto input_2 = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{ -1, -1, 28, 128});+ auto rope = std::make_shared<ov::op::internal::RoPE>(ov::OutputVector{input_2, unsqueeze_cos, unsqueeze_sin},+ ov::op::internal::RoPE::Config());++ model = std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{position_ids, input_2});+ manager.register_pass<IncreasePositionIdsPrecisionForGPTOSS>();+ }+ {+ auto position_ids = std::make_shared<ov::op::v0::Parameter>(ov::element::i64, ov::PartialShape{ 3, -1, -1 });+ auto input_convert = std::make_shared<ov::op::v0::Convert>(position_ids, ov::element::i32);+ auto input_unsqueeze = std::make_shared<ov::op::v0::Unsqueeze>(input_convert,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, std::vector<int64_t>{0}));+ auto convert_2 = std::make_shared<ov::op::v0::Convert>(input_unsqueeze, ov::element::f32);++ auto shape_of = std::make_shared<ov::op::v3::ShapeOf>(input_convert, ov::element::i32);+ auto gather_0 = std::make_shared<ov::op::v8::Gather>(shape_of,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{1}),+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{}, std::vector<int64_t>{0}));+ auto concat = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{1}),+ gather_0,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{64}),+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int64_t>{1})}, 0);+ auto broadcast = std::make_shared<ov::op::v3::Broadcast>(+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1, 1, 64, 1}), concat);+ auto convert_broadcast = std::make_shared<ov::op::v0::Convert>(broadcast, ov::element::f32);+ auto matmul = std::make_shared<ov::op::v0::MatMul>(convert_broadcast, convert_2);+ auto transpose = std::make_shared<ov::op::v1::Transpose>(matmul,+ std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{4}, std::vector<int64_t>{0, 1, 3, 2}));++ auto cos = std::make_shared<ov::op::v0::Cos>(transpose);+ auto sin = std::make_shared<ov::op::v0::Sin>(transpose);++ auto mul_cos_scale = std::make_shared<ov::op::v1::Multiply>(+ std::make_shared<ov::op::v0::Convert>(cos, ov::element::f32),+ std::make_shared<ov::op::v0::Convert>(std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1}), ov::element::f32));+ auto unsqueeze_cos = std::make_shared<ov::op::v0::Unsqueeze>(mul_cos_scale,+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{}, std::vector<int64_t>{0}));++ auto mul_sin_scale = std::make_shared<ov::op::v1::Multiply>(+ std::make_shared<ov::op::v0::Convert>(sin, ov::element::f32),+ std::make_shared<ov::op::v0::Convert>(std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{1}), ov::element::f32));+ auto unsqueeze_sin = std::make_shared<ov::op::v0::Unsqueeze>(mul_sin_scale,+ std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{}, std::vector<int64_t>{0}));++ auto input_2 = std::make_shared<ov::op::v0::Parameter>(ov::element::f16, ov::PartialShape{ -1, -1, 28, 128});+ auto rope = std::make_shared<ov::op::internal::RoPE>(ov::OutputVector{input_2, unsqueeze_cos, unsqueeze_sin},+ ov::op::internal::RoPE::Config());++ model_ref = std::make_shared<ov::Model>(ov::OutputVector{rope}, ov::ParameterVector{position_ids, input_2});+ }+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);+}