ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-openvino / openvino / pass / kv_state_seq_axis.cpp
| 1 | #include "kv_state_seq_axis.h" |
| 2 | |
| 3 | #include <memory> |
| 4 | #include <openvino/core/graph_util.hpp> |
| 5 | #include <openvino/op/assign.hpp> |
| 6 | #include <openvino/op/concat.hpp> |
| 7 | #include <openvino/op/constant.hpp> |
| 8 | #include <openvino/op/read_value.hpp> |
| 9 | #include <openvino/op/transpose.hpp> |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace ov { |
| 13 | namespace frontend { |
| 14 | namespace ggml { |
| 15 | namespace pass { |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | const std::vector<int64_t> & seq_axis_perm() { |
| 20 | // [1, seq, n_heads_kv, head_size] <-> [1, n_heads_kv, seq, head_size] |
| 21 | static const std::vector<int64_t> 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<ov::op::v0::Concat> match_kv_append(const std::shared_ptr<ov::op::v6::Assign> & assign) { |
| 37 | auto concat = ov::as_type_ptr<ov::op::v0::Concat>(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<ov::op::v6::ReadValue>(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<ov::Model> & model) { |
| 54 | std::vector<std::shared_ptr<ov::op::v6::Assign>> assigns; |
| 55 | for (const auto & op : model->get_ops()) { |
| 56 | if (auto assign = ov::as_type_ptr<ov::op::v6::Assign>(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<ov::op::v6::ReadValue>(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<ov::op::v1::Transpose>(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<ov::op::v6::Assign>(node)) { |
| 90 | continue; |
| 91 | } |
| 92 | bool dropped = false; |
| 93 | if (auto * transpose = ov::as_type<ov::op::v1::Transpose>(node)) { |
| 94 | auto order = ov::as_type_ptr<ov::op::v0::Constant>(transpose->get_input_node_shared_ptr(1)); |
| 95 | if (order && order->cast_vector<int64_t>() == 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<ov::op::v1::Transpose>(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 |