interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-openvino / openvino / op / rope.cpp

245 lines · 12329 bytes Raw Blame History
1#include "../node_context.h"
2#include "../op_table.h"
3#include "../utils.h"
4
5#include <cstdint>
6#include <memory>
7#include <openvino/core/node.hpp>
8#include <openvino/core/node_output.hpp>
9#include <openvino/op/add.hpp>
10#include <openvino/op/broadcast.hpp>
11#include <openvino/op/concat.hpp>
12#include <openvino/op/constant.hpp>
13#include <openvino/op/convert.hpp>
14#include <openvino/op/multiply.hpp>
15#include <openvino/op/reshape.hpp>
16#include <openvino/op/slice.hpp>
17#include <openvino/op/split.hpp>
18#include <openvino/op/subtract.hpp>
19#include <openvino/op/unsqueeze.hpp>
20#include <openvino/op/variadic_split.hpp>
21#include <vector>
22
23namespace ov {
24namespace frontend {
25namespace ggml {
26namespace op {
27
28OutputVector translate_rope(const NodeContext & context) {
29 num_inputs_check(context, 2, 3);
30
31 int op_case = context.get_op_case();
32
33 ov::Output<Node> res;
34
35 auto data_node = process_view_input_new(context, 0).get_node_shared_ptr();
36 auto output_shape = context.get_output_shape().to_shape();
37 int32_t * op_params = context.get_output_op_params();
38 const int mode = op_case;
39 const int64_t head_dim = static_cast<int64_t>(output_shape[3]);
40 const int64_t configured_n_dims = static_cast<int64_t>(op_params[1]);
41 const int64_t n_dims = configured_n_dims == 0 ? head_dim : configured_n_dims;
42 const int64_t n_offs = static_cast<int64_t>(op_params[15]);
43
44 constexpr int TYPE_NORMAL = 0;
45 constexpr int TYPE_NEOX = 1;
46 constexpr int TYPE_IMROPE = 2;
47 constexpr int TYPE_VISION = 3;
48 constexpr int TYPE_MROPE = 4;
49
50 Output<Node> cos_theta_node;
51 Output<Node> sin_theta_node;
52 if (context.has_input("rope_cos")) {
53 cos_theta_node = context.get_input("rope_cos");
54 sin_theta_node = context.get_input("rope_sin");
55 } else {
56 std::string cache_key = "rope_sin_cos";
57 for (int i = 0; i < 15; i++) {
58 cache_key += "_" + std::to_string(op_params[i]);
59 }
60 if (context.get_input_size() == 3) {
61 cache_key += "_ff_" + context.get_input_names()[2];
62 }
63 if (context.has_input(cache_key + "_cos")) {
64 cos_theta_node = context.get_input(cache_key + "_cos");
65 sin_theta_node = context.get_input(cache_key + "_sin");
66 } else {
67 auto inp_pos = context.get_input(1).get_node_shared_ptr();
68 std::shared_ptr<ov::Node> rope_freqs_weight;
69 if (context.get_input_size() == 3) {
70 rope_freqs_weight = context.get_input(2).get_node_shared_ptr();
71 }
72 auto sin_cos = make_sin_cos(op_params, inp_pos, rope_freqs_weight, mode, false, head_dim);
73 sin_theta_node = sin_cos.first;
74 cos_theta_node = sin_cos.second;
75 context.put_shared(cache_key + "_cos", cos_theta_node);
76 context.put_shared(cache_key + "_sin", sin_theta_node);
77 }
78 }
79
80 auto output_type = context.get_output_type();
81 if (data_node->get_element_type() != ov::element::f32) {
82 data_node = std::make_shared<ov::op::v0::Convert>(data_node, ov::element::f32);
83 }
84
85 const int64_t total_rope_dims = (mode == TYPE_VISION) ? (2 * n_dims) : n_dims;
86 FRONT_END_OP_CONVERSION_CHECK(n_offs >= 0 && (n_offs % 2 == 0),
87 "ROPE expects non-negative even n_offs");
88 FRONT_END_OP_CONVERSION_CHECK(n_dims > 0 && total_rope_dims + n_offs <= head_dim && (n_dims % 2 == 0),
89 "ROPE expects even n_dims with total_rope_dims + n_offs <= head_dim");
90
91 // RoPEFusionFlux requires rank_equals(4) on x, t_cos and t_sin. The cos/sin
92 // tables are already built rank-4 ([1, S, 1, head_size/2]) for both modes. In
93 // stateful mode the data arrives rank-3 ([S, n_heads, head_size]), so lift it
94 // to rank-4 ([1, S, n_heads, head_size]) here. Stateful RoPE already produced
95 // rank-4 output, so downstream attention is unaffected.
96 if (context.is_stateful()) {
97 const int64_t batch = static_cast<int64_t>(output_shape[0]);
98 auto r4_shape = ov::op::v0::Constant::create(
99 ov::element::i64, {4},
100 std::vector<int64_t>{batch, -1, (int64_t) output_shape[2], (int64_t) output_shape[3]});
101 data_node = std::make_shared<ov::op::v1::Reshape>(data_node, r4_shape, false);
102 }
103 // For TYPE_NORMAL rope (both stateful and stateless) we emit the Flux-style
104 // interleaved pattern below so the GPU plugin's RoPEFusionFlux matcher folds it
105 // into ov::op::internal::RoPE.
106 if (mode == TYPE_NORMAL) {
107 auto axis_last = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
108 auto step_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
109
110 const int64_t batch = static_cast<int64_t>(output_shape[0]);
111 const int64_t n_heads = static_cast<int64_t>(output_shape[2]);
112 const int64_t half = n_dims / 2;
113 auto rot_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs});
114 auto rot_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + n_dims});
115 auto rot_data = std::make_shared<ov::op::v8::Slice>(data_node, rot_start, rot_end, step_one, axis_last);
116
117 auto neg_one_f = ov::op::v0::Constant::create(data_node->get_element_type(), ov::Shape{}, {-1.0f});
118
119 auto paired_shape = ov::op::v0::Constant::create(
120 ov::element::i64, {5}, std::vector<int64_t>{batch, -1, n_heads, half, 2});
121 auto x_paired = std::make_shared<ov::op::v1::Reshape>(rot_data, paired_shape, false);
122
123 auto split_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {-1});
124 auto data_split = std::make_shared<ov::op::v1::Split>(x_paired, split_axis, 2);
125 Output<Node> x0 = data_split->outputs()[0];
126 Output<Node> x1 = data_split->outputs()[1];
127
128 auto x1_neg = std::make_shared<ov::op::v1::Multiply>(x1, neg_one_f);
129 auto x_rotated_paired = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{x1_neg, x0}, -1);
130
131 auto flat_shape =
132 ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{batch, -1, n_heads, n_dims});
133 auto x_rotated =
134 std::make_shared<ov::op::v1::Reshape>(x_rotated_paired, flat_shape, false);
135
136 // Expand cos/sin from [..., n_dims/2] to [..., n_dims] by repeating each
137 // entry twice. Use special_zero on the final Reshape so the seq dim passes
138 // through dynamically. Final rank is 4 to satisfy the matcher's predicate.
139 auto expand_cos_sin = [&](const Output<Node>& cs) {
140 auto cs_unsq = std::make_shared<ov::op::v0::Unsqueeze>(
141 cs, ov::op::v0::Constant::create(ov::element::i64, {1}, {-1}));
142 auto bcast_target = ov::op::v0::Constant::create(
143 ov::element::i64, {5}, std::vector<int64_t>{1, 1, 1, half, 2});
144 auto bcast = std::make_shared<ov::op::v3::Broadcast>(
145 cs_unsq, bcast_target, ov::op::BroadcastType::BIDIRECTIONAL);
146 auto flat = ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{0, 0, 0, n_dims});
147 return std::make_shared<ov::op::v1::Reshape>(bcast, flat, true);
148 };
149 Output<Node> cos_full = expand_cos_sin(cos_theta_node);
150 Output<Node> sin_full = expand_cos_sin(sin_theta_node);
151
152 auto y1 = std::make_shared<ov::op::v1::Multiply>(rot_data, cos_full);
153 auto y2 = std::make_shared<ov::op::v1::Multiply>(x_rotated, sin_full);
154 auto rotated = std::make_shared<ov::op::v1::Add>(y1, y2);
155
156 ov::OutputVector concat_parts;
157 if (n_offs > 0) {
158 auto head_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
159 auto head_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs});
160 auto head = std::make_shared<ov::op::v8::Slice>(data_node, head_start, head_end, step_one, axis_last);
161 concat_parts.push_back(head);
162 }
163 concat_parts.push_back(rotated);
164 if (n_offs + n_dims < head_dim) {
165 auto tail_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + n_dims});
166 auto tail_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {head_dim});
167 auto tail = std::make_shared<ov::op::v8::Slice>(data_node, tail_start, tail_end, step_one, axis_last);
168 concat_parts.push_back(tail);
169 }
170 if (concat_parts.size() == 1) {
171 res = rotated;
172 } else {
173 res = std::make_shared<ov::op::v0::Concat>(concat_parts, -1);
174 }
175 } else if (mode == TYPE_NEOX || mode == TYPE_IMROPE || mode == TYPE_MROPE || mode == TYPE_VISION) {
176 const int64_t half = (mode == TYPE_VISION) ? n_dims : (n_dims / 2);
177 const int64_t rot_dims = 2 * half;
178
179 if (mode != TYPE_NEOX) {
180 auto cos_sin_shape = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4},
181 std::vector<int64_t>{1, -1, 1, half});
182 cos_theta_node = std::make_shared<ov::op::v1::Reshape>(cos_theta_node, cos_sin_shape, true);
183 sin_theta_node = std::make_shared<ov::op::v1::Reshape>(sin_theta_node, cos_sin_shape, true);
184 }
185
186 auto axis_last = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1});
187 auto step_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1});
188
189 Output<Node> rot_data = data_node;
190 if (n_offs > 0 || n_offs + rot_dims < head_dim) {
191 auto rot_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs});
192 auto rot_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + rot_dims});
193 rot_data = std::make_shared<ov::op::v8::Slice>(data_node, rot_start, rot_end, step_one, axis_last);
194 }
195
196 auto neg_one_f = ov::op::v0::Constant::create(data_node->get_element_type(), ov::Shape{}, {-1.0f});
197
198 auto split_axis = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{}, {3});
199 auto split_lengths = ov::op::v0::Constant::create(ov::element::i64, {2}, {half, half});
200 auto data_split = std::make_shared<ov::op::v1::VariadicSplit>(rot_data, split_axis, split_lengths);
201 Output<Node> x1 = data_split->outputs()[0];
202 Output<Node> x2 = data_split->outputs()[1];
203
204 auto x2_neg = std::make_shared<ov::op::v1::Multiply>(x2, neg_one_f);
205 auto x_rotate_half = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{x2_neg, x1}, -1);
206
207 auto cos_full = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{cos_theta_node, cos_theta_node}, -1);
208 auto sin_full = std::make_shared<ov::op::v0::Concat>(ov::OutputVector{sin_theta_node, sin_theta_node}, -1);
209
210 auto y1 = std::make_shared<ov::op::v1::Multiply>(rot_data, cos_full);
211 auto y2 = std::make_shared<ov::op::v1::Multiply>(x_rotate_half, sin_full);
212 auto rotated = std::make_shared<ov::op::v1::Add>(y1, y2);
213
214 ov::OutputVector concat_parts;
215 if (n_offs > 0) {
216 auto head_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {0});
217 auto head_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs});
218 auto head = std::make_shared<ov::op::v8::Slice>(data_node, head_start, head_end, step_one, axis_last);
219 concat_parts.push_back(head);
220 }
221 concat_parts.push_back(rotated);
222 if (n_offs + rot_dims < head_dim) {
223 auto tail_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_offs + rot_dims});
224 auto tail_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {head_dim});
225 auto tail = std::make_shared<ov::op::v8::Slice>(data_node, tail_start, tail_end, step_one, axis_last);
226 concat_parts.push_back(tail);
227 }
228 if (concat_parts.size() == 1) {
229 res = rotated;
230 } else {
231 res = std::make_shared<ov::op::v0::Concat>(concat_parts, -1);
232 }
233 }
234
235 if (res.get_element_type() != output_type) {
236 res = std::make_shared<ov::op::v0::Convert>(res, output_type);
237 }
238
239 return rename_outputs_with_suffix({res}, context.get_name());
240}
241
242} // namespace op
243} // namespace ggml
244} // namespace frontend
245} // namespace ov