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/cpy.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 / cpy.cpp` 314 lines · 17517 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 #include 23 #include 24 #include 25 #include 26 27 namespace ov { 28 namespace frontend { 29 namespace ggml { 30 namespace op { 31 32 OutputVector translate_cpy(const NodeContext & context) { 33 auto op_case = context.get_op_case(); 34 auto input_shape = context.get_input_shape(0); 35 auto output_shape = context.get_input_shape(1); 36 37 if (op_case == 4) { 38 auto src = process_view_input_new(context, 0); 39 auto base = context.get_input(1); 40 41 int64_t n_elems = 1; 42 for (const auto & dim : context.get_output_shape().to_shape()) { 43 n_elems *= static_cast(dim); 44 } 45 46 const auto output_stride = context.get_output_stride(); 47 const size_t elem_size = output_stride.empty() ? context.get_output_type().size() : output_stride.back(); 48 FRONT_END_OP_CONVERSION_CHECK(elem_size > 0, "CPY conv state view update has invalid element size"); 49 50 const int64_t begin_val = static_cast(context.get_output_op_offset() / elem_size); 51 const int64_t end_val = begin_val + n_elems; 52 53 auto flat_shape = ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector{1, 1, 1, -1}); 54 src = std::make_shared(src, flat_shape, false); 55 if (src.get_element_type() != context.get_output_type()) { 56 src = std::make_shared(src, context.get_output_type()); 57 } 58 59 auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 60 auto begin = ov::op::v0::Constant::create(ov::element::i64, {1}, {begin_val}); 61 auto end = ov::op::v0::Constant::create(ov::element::i64, {1}, {end_val}); 62 auto int_max = ov::op::v0::Constant::create(ov::element::i64, {1}, {INT_MAX}); 63 auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 64 auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); 65 66 auto head_part = std::make_shared(base, zero, begin, one, axis); 67 auto tail_part = std::make_shared(base, end, int_max, one, axis); 68 auto res = std::make_shared(ov::OutputVector{head_part, src, tail_part}, 3); 69 return rename_outputs_with_suffix({res}, context.get_name()); 70 } 71 72 // Recurrent state cache writeback into a slot block of the cache. Where the block starts is a 73 // runtime input, so the cached model works for any kv head and active sequence count. The 74 // result is the full updated cache. 75 // op_case 1: gated-delta-net state, op_case 2: conv state, op_case 3: defrag remainder. 76 if (op_case == 3) { 77 // With -np 1 (and generally whenever there is no defrag remainder) this GET_ROWS gathers 78 // zero rows: nothing to write back, and the cache is unchanged. NPU rejects zero-size 79 // tensors, so short-circuit instead of building a degenerate Slice/Concat chain. 80 bool is_empty = false; 81 if (input_shape.rank().is_static()) { 82 for (const auto & d : input_shape) { 83 if (d.is_static() && d.get_length() == 0) { 84 is_empty = true; 85 break; 86 } 87 } 88 } 89 if (is_empty) { 90 return {context.get_input(1)}; 91 } 92 } 93 // op_case 7/8/9 are the single-slot variants; op_case 10 writes native GDN state into a 94 // multi-slot cache without rollback snapshots. 95 const bool single_slot_assign = op_case >= 7 && op_case <= 9; 96 const bool direct_gdn_state = op_case == 7 || op_case == 10; 97 int writeback_case = op_case; 98 if (op_case == 10) { 99 writeback_case = 1; 100 } else if (single_slot_assign) { 101 writeback_case = op_case - 6; 102 } 103 const std::string slot_begin_name = "rs_slot_begin_" + context.get_name(); 104 const bool slice_assign = writeback_case >= 1 && writeback_case <= 3 && 105 (single_slot_assign || context.has_input(slot_begin_name)); 106 if (slice_assign) { 107 if (single_slot_assign && writeback_case == 3) { 108 return {context.get_input(1)}; 109 } 110 111 const int64_t slot_axis = 2; 112 auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 113 auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 114 auto int_max = ov::op::v0::Constant::create(ov::element::i64, {1}, {INT_MAX}); 115 auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {slot_axis}); 116 auto feature = ov::op::v0::Constant::create(ov::element::i64, {4}, 117 std::vector{1, 1, -1, output_shape[3].get_length()}); 118 119 ov::Output src; 120 ov::Output begin; 121 if (!single_slot_assign) { 122 begin = context.get_input(slot_begin_name); 123 } 124 auto base = context.get_input(1); 125 if (writeback_case == 1) { 126 if (direct_gdn_state) { 127 // Non-rollback GDN publishes state directly as [active_slots, heads, value_dim, 128 // key_dim]. Flatten each active slot before replacing or updating the cache. 129 src = std::make_shared(context.get_input(0), feature, false); 130 } else { 131 // Multi-slot rollback still consumes GGML's packed [attention | state snapshots] 132 // layout. Slice the state block using the runtime source offset. 133 auto src_begin = context.get_input("rs_src_begin_" + context.get_name()); 134 auto state_part = 135 std::make_shared(context.get_input(0), src_begin, int_max, one, axis); 136 src = std::make_shared(state_part, feature, false); 137 } 138 } else if (writeback_case == 2) { 139 // conv_input is [previous conv state | new tokens]; the snapshot is the conv_kernel_size - 1 140 // columns ending at the last *valid* token. Gather (rather than Slice) keeps the output 141 // shape static even though the window start is a runtime value. 142 auto window_size = (int64_t) input_shape[3].get_length(); 143 ov::Output window; 144 auto col_axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); 145 const std::string src_begin_name = "rs_src_begin_" + context.get_name(); 146 if (context.has_input(src_begin_name)) { 147 auto src_begin = context.get_input(src_begin_name); 148 auto src_end = std::make_shared( 149 src_begin, ov::op::v0::Constant::create(ov::element::i64, {1}, {window_size})); 150 window = std::make_shared(context.get_input(0), src_begin, src_end, one, col_axis); 151 } else if (context.has_input("chunk_valid_len")) { 152 std::vector offsets(window_size); 153 std::iota(offsets.begin(), offsets.end(), 0); 154 auto indices = std::make_shared( 155 ov::op::v0::Constant::create(ov::element::i64, {(size_t) window_size}, offsets), 156 context.get_input("chunk_valid_len")); 157 window = std::make_shared(context.get_input(0), indices, col_axis); 158 } else { 159 auto window_begin = ov::op::v0::Constant::create(ov::element::i64, {1}, {-window_size}); 160 window = 161 std::make_shared(context.get_input(0), window_begin, int_max, one, col_axis); 162 } 163 const auto base_shape = base.get_partial_shape(); 164 FRONT_END_OP_CONVERSION_CHECK(base_shape.rank().is_static() && base_shape.rank().get_length() == 4, 165 "CPY conv state cache update requires rank-4 base cache"); 166 FRONT_END_OP_CONVERSION_CHECK(base_shape[3].is_static(), 167 "CPY conv state cache update requires static feature size"); 168 FRONT_END_OP_CONVERSION_CHECK(input_shape.rank().is_static() && input_shape.rank().get_length() == 4 && 169 input_shape[2].is_static() && input_shape[3].is_static(), 170 "CPY conv state cache update requires static source feature view"); 171 172 const int64_t full_feature_size = base_shape[3].get_length(); 173 const int64_t update_feature_size = input_shape[2].get_length() * input_shape[3].get_length(); 174 const auto output_stride = context.get_output_stride(); 175 const size_t elem_size = output_stride.empty() ? context.get_output_type().size() : output_stride.back(); 176 FRONT_END_OP_CONVERSION_CHECK(elem_size > 0, 177 "CPY conv state cache update has invalid element size"); 178 const int64_t feature_begin = static_cast(context.get_output_op_offset() / elem_size) % 179 full_feature_size; 180 const int64_t feature_end = feature_begin + update_feature_size; 181 FRONT_END_OP_CONVERSION_CHECK(feature_begin >= 0 && feature_end <= full_feature_size, 182 "CPY conv state cache update feature range is out of bounds"); 183 184 auto partial_feature = ov::op::v0::Constant::create( 185 ov::element::i64, {4}, std::vector{1, 1, -1, update_feature_size}); 186 src = std::make_shared(window, partial_feature, false); 187 if (src.get_element_type() != context.get_output_type()) { 188 src = std::make_shared(src, context.get_output_type()); 189 } 190 191 if (single_slot_assign) { 192 return rename_outputs_with_suffix({src}, context.get_name()); 193 } 194 195 auto src_len = std::make_shared( 196 std::make_shared(src, ov::element::i64), axis, 197 ov::op::v0::Constant::create(ov::element::i64, {}, {0})); 198 auto slot_end = std::make_shared(begin, src_len); 199 auto active_slots = std::make_shared(base, begin, slot_end, one, axis); 200 201 auto feature_axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); 202 auto feature_begin_node = ov::op::v0::Constant::create(ov::element::i64, {1}, {feature_begin}); 203 auto feature_end_node = ov::op::v0::Constant::create(ov::element::i64, {1}, {feature_end}); 204 auto feature_head = std::make_shared(active_slots, zero, feature_begin_node, one, 205 feature_axis); 206 auto feature_tail = std::make_shared(active_slots, feature_end_node, int_max, one, 207 feature_axis); 208 src = std::make_shared(ov::OutputVector{feature_head, src, feature_tail}, 3); 209 } else { 210 // op_case 3: gathered remainder rows already have the cache slot layout [1, 1, extra, feature] 211 src = context.get_input(0); 212 } 213 214 if (src.get_element_type() != context.get_output_type()) { 215 src = std::make_shared(src, context.get_output_type()); 216 } 217 218 if (single_slot_assign) { 219 return rename_outputs_with_suffix({src}, context.get_name()); 220 } 221 222 auto src_len = 223 std::make_shared(std::make_shared(src, ov::element::i64), axis, 224 ov::op::v0::Constant::create(ov::element::i64, {}, {0})); 225 auto end = std::make_shared(begin, src_len); 226 auto head_part = std::make_shared(base, zero, begin, one, axis); 227 auto tail_part = std::make_shared(base, end, int_max, one, axis); 228 auto res = std::make_shared(ov::OutputVector{head_part, src, tail_part}, slot_axis); 229 return rename_outputs_with_suffix({res}, context.get_name()); 230 } 231 232 auto input = process_view_input_new(context, 0); 233 234 if (op_case == 5 || op_case == 6) { 235 auto input_shape = context.get_input_shape(0); 236 auto output_shape = context.get_output_shape(); 237 auto dst_ggml_shape = context.get_view_input_ggml_shape(1, 0); 238 auto dst_stride = context.get_view_input_stride(1, 0); 239 size_t offset_bytes = context.get_view_input_offset(1, 0); 240 auto n_state = (int64_t) context.get_input_shape(0)[3].get_length(); 241 auto n_state_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_state}); 242 auto kv_buf = context.get_input(1); // shape {1,1,1,N} 243 244 Output token_len_per_seq; 245 Output n_write_dyn; 246 if (context.has_input("token_len_per_seq")) { 247 token_len_per_seq = context.get_input("token_len_per_seq"); 248 n_write_dyn = std::make_shared(token_len_per_seq, n_state_c); 249 } else { 250 n_write_dyn = ov::op::v0::Constant::create(ov::element::i64, {1}, {(int64_t) dst_ggml_shape[3]}); 251 } 252 size_t elem_size = dst_stride[3]; 253 FRONT_END_OP_CONVERSION_CHECK(elem_size > 0, "CPY KV cache view update has invalid element size"); 254 int64_t start_elem = (int64_t) (offset_bytes / elem_size); 255 // op_case 5: decoder self-attention – write offset advances each step. 256 // op_case 6: encoder self-attn or cross-attn – offset fixed at compile time. 257 const bool is_decoder_self_attn = (op_case == 5); 258 auto ones_c = ov::op::v0::Constant::create(ov::element::i64, {3}, std::vector{1, 1, 1}); 259 auto new_shape = std::make_shared(ov::OutputVector{ones_c, n_write_dyn}, 0); 260 261 auto reshaped = std::make_shared(input, new_shape, false); 262 auto data = std::make_shared(reshaped, context.get_output_type()); 263 // Indices [start_elem .. start_elem + n_write) on axis 3 of {1,1,1,N} 264 // For decoder self-attention the write offset advances each step, so compute it 265 // dynamically from the model inputs: start = (attention_size - token_len_per_seq) * n_state. 266 // For encoder self-attn and cross-attn the offset is fixed at graph-compile time. 267 ov::Output start; 268 if (is_decoder_self_attn && context.has_input("attention_size") && context.has_input("token_len_per_seq")) { 269 auto attention_size_in = context.get_input("attention_size"); 270 auto token_len_in = context.get_input("token_len_per_seq"); 271 auto past_tokens = std::make_shared(attention_size_in, token_len_in); 272 auto new_start = std::make_shared(past_tokens, n_state_c); 273 start = std::make_shared( 274 new_start, ov::op::v0::Constant::create(ov::element::i64, {1}, {start_elem})); 275 } else { 276 start = ov::op::v0::Constant::create(ov::element::i64, {1}, {start_elem}); 277 } 278 auto start_squeezed = std::make_shared(start); 279 auto end = std::make_shared(start_squeezed, n_write_dyn); 280 auto end_squeezed = std::make_shared(end); 281 auto step = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 282 auto step_squeezed = std::make_shared(step); 283 auto indices = 284 std::make_shared(start_squeezed, end_squeezed, step_squeezed, ov::element::i64); 285 auto axis = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); 286 287 auto kv_updated = std::make_shared(kv_buf, indices, data, axis); 288 return rename_outputs_with_suffix({kv_updated}, context.get_name()); 289 } 290 291 if (input_shape != output_shape) { 292 auto new_shape = ov::op::v0::Constant::create( 293 ov::element::i64, {static_cast(output_shape.rank().get_length())}, output_shape.to_shape()); 294 input = std::make_shared(input, new_shape, false); 295 } 296 297 ov::Output res; 298 if (context.get_input_type(0) != context.get_output_type()) { 299 res = std::make_shared(input, context.get_output_type()); 300 } else { 301 res = input; 302 } 303 304 if (res.get_node_shared_ptr() == context.get_input(0).get_node_shared_ptr()) { 305 return {res}; 306 } 307 308 return rename_outputs_with_suffix({res}, context.get_name()); 309 } 310 311 } // namespace op 312 } // namespace ggml 313 } // namespace frontend 314 } // namespace ov