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/permute.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 / permute.cpp` 159 lines ยท 7745 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 17 namespace ov { 18 namespace frontend { 19 namespace ggml { 20 namespace op { 21 22 OutputVector translate_permute(const NodeContext & context) { 23 num_inputs_check(context, 1, 1); 24 25 int op_case = context.get_op_case(); 26 FRONT_END_CHECK_IMPLEMENTED(op_case != 0, "Unsupported PERMUTE case"); 27 // op_case 1 is trivial permute 28 // op_case 2 is to permute Q. It has a preceding VIEW that reshapes Q to restore the sequqence dimension 29 // op_case 3 4 it to permute KV cache in the default layout 30 // op_case 5 6 is to permute V cache when `-fa off`, where v_trans=true 31 32 ov::Output res; 33 ov::Output src; 34 if (op_case == 3 || op_case == 4 || op_case == 5 || op_case == 6) { 35 src = context.get_input(0); 36 } else { 37 src = process_view_input_new(context, 0); 38 } 39 std::vector perm_values{0, 2, 1, 3}; 40 const int32_t * op_params = context.get_output_op_params(); 41 if (op_params != nullptr) { 42 for (size_t input_axis = 0; input_axis < perm_values.size(); ++input_axis) { 43 const size_t output_axis = static_cast(op_params[input_axis]); 44 perm_values[perm_values.size() - 1 - output_axis] = 45 static_cast(perm_values.size() - 1 - input_axis); 46 } 47 } 48 if (op_case == 1 || context.is_stateful()) { 49 // The stateful path carries hidden-state tensors in a rank-3 layout (the 50 // leading batch dim is dropped, e.g. Gemma4's per-layer-embedding path). The 51 // perm above is rank-4; when the actual input is rank-3, drop the batch axis 52 // (perm[0], which is always the identity 0 here) and shift the rest down by 1 53 // so the transpose order matches the input rank. 54 std::vector perm_used = perm_values; 55 const auto & src_ps = src.get_partial_shape(); 56 if (src_ps.rank().is_static() && src_ps.rank().get_length() == 3 && perm_values.size() == 4 && 57 perm_values[0] == 0) { 58 perm_used = {perm_values[1] - 1, perm_values[2] - 1, perm_values[3] - 1}; 59 } 60 auto perm = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{perm_used.size()}, perm_used); 61 res = std::make_shared(src, perm); 62 } else if (op_case == 2) { 63 auto perm = ov::op::v0::Constant::create(ov::element::i64, {4}, perm_values); 64 auto output_shape = context.get_output_shape().to_shape(); 65 auto n_heads = ov::op::v0::Constant::create(ov::element::i64, {1}, {output_shape[1]}); 66 auto head_size = ov::op::v0::Constant::create(ov::element::i64, {1}, {output_shape[3]}); 67 auto n_seq_active = context.has_input("n_seq_active") ? 68 context.get_input("n_seq_active") : 69 ov::op::v0::Constant::create(ov::element::i64, {1}, {output_shape[0]}); 70 auto neg_one = ov::op::v0::Constant::create(ov::element::i64, {1}, {-1}); 71 72 auto new_shape = 73 std::make_shared(ov::OutputVector{n_seq_active, neg_one, n_heads, head_size}, 0); 74 75 // // Alternative 76 // auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 77 // auto new_shape = std::make_shared(ov::OutputVector{n_seq_active, neg_one, zero, zero}, 0); 78 79 auto reshaped = std::make_shared(src, new_shape, true); 80 res = std::make_shared(reshaped, perm); 81 } else { 82 auto perm = ov::op::v0::Constant::create(ov::element::i64, {4}, perm_values); 83 auto cache_shape = src.get_partial_shape(); 84 auto output_shape = context.get_output_shape().to_shape(); 85 int64_t head_size = output_shape[3]; 86 int64_t n_heads = output_shape[1]; 87 if (op_case == 5 || op_case == 6) { 88 head_size = output_shape[2]; 89 n_heads = output_shape[1]; 90 } 91 int64_t ctx_per_seq = cache_shape[2].is_static() ? cache_shape[2].get_length() : -1; 92 int64_t n_seq = cache_shape[1].get_length(); 93 94 Output attention_size; 95 if (!context.has_input("attention_size")) { 96 attention_size = ov::op::v0::Constant::create(ov::element::i64, {1}, {output_shape[2]}); 97 } else if (op_case == 3 || op_case == 5) { 98 attention_size = context.get_input("attention_size"); 99 } else { 100 attention_size = context.get_input("attention_size_swa"); 101 } 102 103 Output seq_active_start; 104 Output seq_active_end; 105 if (context.has_input("seq_active_start")) { 106 seq_active_start = context.get_input("seq_active_start"); 107 seq_active_end = context.get_input("seq_active_end"); 108 } else { 109 int64_t n_seq_active = output_shape[0]; 110 size_t offset = *((size_t *) context.get_input_op_params(0)); 111 int64_t seq_active_start_val = offset / context.get_input_stride(0)[0]; 112 int64_t seq_active_end_val = seq_active_start_val + n_seq_active; 113 seq_active_start = ov::op::v0::Constant::create(ov::element::i64, {1}, {seq_active_start_val}); 114 seq_active_end = ov::op::v0::Constant::create(ov::element::i64, {1}, {seq_active_end_val}); 115 } 116 117 // 1. reshape to [n_seq, ctx_per_seq, n_heads, head_size] (for `-fa off` [n_seq, n_heads, head_size, ctx_per_seq]) 118 // 2. slice out the active sequences 119 // 3. slice out the attention part in each sequence 120 // 4. permute (skip for `-fa off`) 121 auto zero = ov::op::v0::Constant::create(ov::element::i64, {1}, {0}); 122 auto one = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); 123 124 if (op_case == 3 || op_case == 4) { 125 auto src_reshaped = std::make_shared( 126 src, ov::op::v0::Constant::create(ov::element::i64, {4}, {n_seq, ctx_per_seq, n_heads, head_size}), 127 false); 128 ov::Output after_seq_slice; 129 if (n_seq == 1) { 130 after_seq_slice = src_reshaped; 131 } else { 132 after_seq_slice = 133 std::make_shared(src_reshaped, seq_active_start, seq_active_end, one, zero); 134 } 135 auto slice2 = std::make_shared(after_seq_slice, zero, attention_size, one, one); 136 res = std::make_shared(slice2, perm); 137 } else { 138 auto three = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); 139 auto src_reshaped = std::make_shared( 140 src, ov::op::v0::Constant::create(ov::element::i64, {4}, {n_seq, n_heads, head_size, ctx_per_seq}), 141 false); 142 ov::Output after_seq_slice; 143 if (n_seq == 1) { 144 after_seq_slice = src_reshaped; 145 } else { 146 after_seq_slice = 147 std::make_shared(src_reshaped, seq_active_start, seq_active_end, one, zero); 148 } 149 auto slice2 = std::make_shared(after_seq_slice, zero, attention_size, one, three); 150 res = slice2; 151 } 152 } 153 return rename_outputs_with_suffix({res}, context.get_name()); 154 } 155 156 } // namespace op 157 } // namespace ggml 158 } // namespace frontend 159 } // namespace ov