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/pass/kv_state_seq_axis.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 / pass / kv_state_seq_axis.cpp` 114 lines ยท 4497 bytes Raw Blame History 1 #include "kv_state_seq_axis.h" 2 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 12 namespace ov { 13 namespace frontend { 14 namespace ggml { 15 namespace pass { 16 17 namespace { 18 19 const std::vector & seq_axis_perm() { 20 // [1, seq, n_heads_kv, head_size] <-> [1, n_heads_kv, seq, head_size] 21 static const std::vector perm{0, 2, 1, 3}; 22 return perm; 23 } 24 25 // True when the state still has the frontend's stateful KV layout, so the sequence axis 26 // can be moved: rank 4, batch and both head dims static, and seq the only dynamic dim, 27 // at dim 1. Any KV head count is fine. With a single head the rewrite is pure metadata 28 // ([1, seq, 1, head] and [1, 1, seq, head] are the same memory); with several heads it 29 // also drops the reader-side transpose of the whole accumulated state, which is where 30 // most of the gain comes from at depth. 31 bool can_move_seq_axis(const ov::PartialShape & shape) { 32 return shape.rank().is_static() && shape.rank().get_length() == 4 && shape[0].is_static() && 33 shape[1].is_dynamic() && shape[2].is_static() && shape[3].is_static(); 34 } 35 36 std::shared_ptr match_kv_append(const std::shared_ptr & assign) { 37 auto concat = ov::as_type_ptr(assign->get_input_node_shared_ptr(0)); 38 if (!concat || concat->get_input_size() != 2 || concat->get_axis() != 1) { 39 return nullptr; 40 } 41 auto read_value = ov::as_type_ptr(concat->get_input_node_shared_ptr(0)); 42 if (!read_value || read_value->get_variable() != assign->get_variable()) { 43 return nullptr; 44 } 45 if (!can_move_seq_axis(read_value->get_output_partial_shape(0))) { 46 return nullptr; 47 } 48 return concat; 49 } 50 51 } // namespace 52 53 bool KVStateSeqAxis::run_on_model(const std::shared_ptr & model) { 54 std::vector> assigns; 55 for (const auto & op : model->get_ops()) { 56 if (auto assign = ov::as_type_ptr(op)) { 57 assigns.push_back(assign); 58 } 59 } 60 61 bool changed = false; 62 for (const auto & assign : assigns) { 63 auto concat = match_kv_append(assign); 64 if (!concat) { 65 continue; 66 } 67 auto read_value = ov::as_type_ptr(concat->get_input_node_shared_ptr(0)); 68 69 auto variable = read_value->get_variable(); 70 auto info = variable->get_info(); 71 const auto & shape = info.data_shape; 72 info.data_shape = ov::PartialShape{shape[0], shape[2], shape[1], shape[3]}; 73 variable->update(info); 74 read_value->validate_and_infer_types(); 75 76 auto readers = concat->output(0).get_target_inputs(); 77 78 auto new_rows = concat->input_value(1); 79 auto perm_in = ov::op::v0::Constant::create(ov::element::i64, {4}, seq_axis_perm()); 80 concat->set_argument(1, std::make_shared(new_rows, perm_in)); 81 concat->set_axis(2); 82 concat->validate_and_infer_types(); 83 84 // Readers still expect seq at dim 1. A reader that is itself the inverse 85 // Transpose wanted seq at dim 2 all along, so drop it; give anything else the 86 // inverse Transpose so its input is unchanged. 87 for (const auto & reader : readers) { 88 auto * node = reader.get_node(); 89 if (ov::is_type(node)) { 90 continue; 91 } 92 bool dropped = false; 93 if (auto * transpose = ov::as_type(node)) { 94 auto order = ov::as_type_ptr(transpose->get_input_node_shared_ptr(1)); 95 if (order && order->cast_vector() == seq_axis_perm()) { 96 ov::replace_output_update_name(transpose->output(0), concat->output(0)); 97 dropped = true; 98 } 99 } 100 if (!dropped) { 101 auto perm_out = ov::op::v0::Constant::create(ov::element::i64, {4}, seq_axis_perm()); 102 reader.replace_source_output(std::make_shared(concat->output(0), perm_out)); 103 } 104 } 105 changed = true; 106 } 107 108 return changed; 109 } 110 111 } // namespace pass 112 } // namespace ggml 113 } // namespace frontend 114 } // namespace ov