Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-openvino/openvino/op/rope.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 23 namespace ov { 24 namespace frontend { 25 namespace ggml { 26 namespace op { 27 28 OutputVector 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 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(output_shape[3]); 40 const int64_t configured_n_dims = static_cast(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(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 cos_theta_node; 51 Output 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 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(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(output_shape[0]); 98 auto r4_shape = ov::op::v0::Constant::create( 99 ov::element::i64, {4}, 100 std::vector{batch, -1, (int64_t) output_shape[2], (int64_t) output_shape[3]}); 101 data_node = std::make_shared(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(output_shape[0]); 111 const int64_t n_heads = static_cast(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(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{batch, -1, n_heads, half, 2}); 121 auto x_paired = std::make_shared(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(x_paired, split_axis, 2); 125 Output x0 = data_split->outputs()[0]; 126 Output x1 = data_split->outputs()[1]; 127 128 auto x1_neg = std::make_shared(x1, neg_one_f); 129 auto x_rotated_paired = std::make_shared(ov::OutputVector{x1_neg, x0}, -1); 130 131 auto flat_shape = 132 ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector{batch, -1, n_heads, n_dims}); 133 auto x_rotated = 134 std::make_shared(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& cs) { 140 auto cs_unsq = std::make_shared( 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{1, 1, 1, half, 2}); 144 auto bcast = std::make_shared( 145 cs_unsq, bcast_target, ov::op::BroadcastType::BIDIRECTIONAL); 146 auto flat = ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector{0, 0, 0, n_dims}); 147 return std::make_shared(bcast, flat, true); 148 }; 149 Output cos_full = expand_cos_sin(cos_theta_node); 150 Output sin_full = expand_cos_sin(sin_theta_node); 151 152 auto y1 = std::make_shared(rot_data, cos_full); 153 auto y2 = std::make_shared(x_rotated, sin_full); 154 auto rotated = std::make_shared(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(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(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(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::element::i64, ov::Shape{4}, 181 std::vector{1, -1, 1, half}); 182 cos_theta_node = std::make_shared(cos_theta_node, cos_sin_shape, true); 183 sin_theta_node = std::make_shared(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 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(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(rot_data, split_axis, split_lengths); 201 Output x1 = data_split->outputs()[0]; 202 Output x2 = data_split->outputs()[1]; 203 204 auto x2_neg = std::make_shared(x2, neg_one_f); 205 auto x_rotate_half = std::make_shared(ov::OutputVector{x2_neg, x1}, -1); 206 207 auto cos_full = std::make_shared(ov::OutputVector{cos_theta_node, cos_theta_node}, -1); 208 auto sin_full = std::make_shared(ov::OutputVector{sin_theta_node, sin_theta_node}, -1); 209 210 auto y1 = std::make_shared(rot_data, cos_full); 211 auto y2 = std::make_shared(x_rotate_half, sin_full); 212 auto rotated = std::make_shared(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(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(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(concat_parts, -1); 232 } 233 } 234 235 if (res.get_element_type() != output_type) { 236 res = std::make_shared(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