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 / op / flash_attn_ext.cpp
| 1 | #include "../node_context.h" |
| 2 | #include "../op_table.h" |
| 3 | #include "../utils.h" |
| 4 | #include "ggml-openvino/ggml-openvino-extra.h" |
| 5 | |
| 6 | #include <cstddef> |
| 7 | #include <cstdint> |
| 8 | #include <memory> |
| 9 | #include <openvino/op/add.hpp> |
| 10 | #include <openvino/op/broadcast.hpp> |
| 11 | #include <openvino/op/concat.hpp> |
| 12 | #include <openvino/op/constant.hpp> |
| 13 | #include <openvino/op/convert.hpp> |
| 14 | #include <openvino/op/matmul.hpp> |
| 15 | #include <openvino/op/multiply.hpp> |
| 16 | #include <openvino/op/reshape.hpp> |
| 17 | #include <openvino/op/scaled_dot_product_attention.hpp> |
| 18 | #include <openvino/op/slice.hpp> |
| 19 | #include <openvino/op/softmax.hpp> |
| 20 | #include <openvino/op/transpose.hpp> |
| 21 | #include <openvino/op/unsqueeze.hpp> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace ov { |
| 25 | namespace frontend { |
| 26 | namespace ggml { |
| 27 | namespace op { |
| 28 | static ov::Output<ov::Node> reshape_flat_kv(const ov::Output<ov::Node> & kv_flat, |
| 29 | size_t view_offset_bytes, |
| 30 | size_t nb1_bytes, |
| 31 | int64_t n_head, |
| 32 | int64_t head_size, |
| 33 | const ov::Output<ov::Node> & attention_size) { |
| 34 | int64_t n_state = n_head * head_size; |
| 35 | int64_t layer_start_elem = (int64_t) (view_offset_bytes / (nb1_bytes / n_state)); |
| 36 | // Dynamic slice: [layer_start_elem, layer_start_elem + n_kv * n_state) |
| 37 | auto start_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {layer_start_elem}); |
| 38 | auto n_state_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_state}); |
| 39 | // end = start + attention_size * n_state (both static + dynamic) |
| 40 | auto kv_len_elems = std::make_shared<ov::op::v1::Multiply>(attention_size, n_state_c); |
| 41 | auto end_c = std::make_shared<ov::op::v1::Add>(start_c, kv_len_elems); |
| 42 | auto step_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); |
| 43 | auto axis_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {3}); |
| 44 | auto sliced = std::make_shared<ov::op::v8::Slice>(kv_flat, start_c, end_c, step_c, axis_c); |
| 45 | |
| 46 | // KV cache is laid out as {n_kv, n_head, head_size} in memory |
| 47 | // Reshape to {1, n_kv, n_head, head_size}, then transpose to {1, n_head, n_kv, head_size} |
| 48 | // as required by SDPA. |
| 49 | auto one_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {1}); |
| 50 | auto n_head_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {n_head}); |
| 51 | auto head_size_c = ov::op::v0::Constant::create(ov::element::i64, {1}, {head_size}); |
| 52 | // reshape: {n_kv*n_state} -> {1, n_kv, n_head, head_size} |
| 53 | auto new_shape = |
| 54 | std::make_shared<ov::op::v0::Concat>(ov::OutputVector{one_c, attention_size, n_head_c, head_size_c}, 0); |
| 55 | auto reshaped = std::make_shared<ov::op::v1::Reshape>(sliced, new_shape, false); |
| 56 | // transpose: {1, n_kv, n_head, head_size} -> {1, n_head, n_kv, head_size} |
| 57 | auto perm = ov::op::v0::Constant::create(ov::element::i64, {4}, {0, 2, 1, 3}); |
| 58 | auto ret = std::make_shared<ov::op::v1::Transpose>(reshaped, perm); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | OutputVector translate_flash_attn_ext(const NodeContext & context) { |
| 63 | num_inputs_check(context, 3, 4); |
| 64 | const bool has_mask = context.get_input_size() == 4; |
| 65 | auto q_f32 = context.get_input(0); |
| 66 | auto k = context.get_input(1); |
| 67 | auto v = context.get_input(2); |
| 68 | const int op_case = context.get_op_case(); |
| 69 | |
| 70 | if (op_case == 1 || op_case == 2) { |
| 71 | int64_t n_state_head = (int64_t) context.get_view_input_ggml_shape(1, 0)[3]; |
| 72 | int64_t n_head = (int64_t) context.get_view_input_ggml_shape(1, 0)[1]; |
| 73 | size_t nb1 = context.get_view_input_stride(1, 0)[2]; |
| 74 | size_t offset = context.get_view_input_offset(1, 0); |
| 75 | ov::Output<ov::Node> attention_size; |
| 76 | if (op_case == 1) { |
| 77 | attention_size = context.get_input("attention_size"); |
| 78 | } else { |
| 79 | attention_size = context.get_input("attention_size_static"); |
| 80 | } |
| 81 | k = reshape_flat_kv(k, offset, nb1, n_head, n_state_head, attention_size); |
| 82 | v = reshape_flat_kv(v, offset, nb1, n_head, n_state_head, attention_size); |
| 83 | } |
| 84 | |
| 85 | float * params = reinterpret_cast<float *>(context.get_output_op_params()); |
| 86 | float scale = params[0]; |
| 87 | // float max_bias = params[1]; |
| 88 | // float logit_softcap = params[2]; |
| 89 | |
| 90 | auto q = std::make_shared<ov::op::v0::Convert>(q_f32, ov::element::f16); |
| 91 | auto scale_node = std::make_shared<ov::op::v0::Constant>(ov::element::f16, ov::Shape{}, std::vector<float>{scale}); |
| 92 | |
| 93 | ov::Output<ov::Node> res; |
| 94 | |
| 95 | // For stateful |
| 96 | ov::Output<ov::Node> mask; |
| 97 | if (has_mask) { |
| 98 | mask = context.get_input(3); |
| 99 | std::string mask_name = "KQ_mask_sliced"; |
| 100 | if (context.get_input_names()[3].find("swa") != std::string::npos) { |
| 101 | mask_name = "KQ_mask_swa_sliced"; |
| 102 | } |
| 103 | if (context.has_input(mask_name)) { |
| 104 | mask = context.get_input(mask_name); |
| 105 | } |
| 106 | if (mask.get_element_type() != ov::element::f16) { |
| 107 | mask = std::make_shared<ov::op::v0::Convert>(mask, ov::element::f16); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | //auto tile_kv = [&](int64_t num_heads, int64_t num_heads_kv, int64_t head_size, ov::Output<Node> kv) { |
| 112 | // int64_t factor = num_heads / num_heads_kv; |
| 113 | // if (factor > 1 && num_heads_kv > 1) { |
| 114 | auto q_shape = context.get_input_shape(0).to_shape(); |
| 115 | auto k_shape = context.get_input_shape(1).to_shape(); |
| 116 | const int64_t num_heads = q_shape[1]; |
| 117 | const int64_t num_heads_kv = k_shape[1]; |
| 118 | const int64_t head_size = q_shape[3]; |
| 119 | const int64_t factor = num_heads / num_heads_kv; |
| 120 | |
| 121 | // Manual GQA attention: enabled by default on GPU in stateless mode. |
| 122 | // Set GGML_OPENVINO_MANUAL_GQA_ATTN to a positive value (e.g. 1) to force-enable, |
| 123 | // or to 0 to force-disable. Unset falls back to the device-based default. |
| 124 | static const bool manual_gqa_enabled = []() { |
| 125 | const char * env = ggml_openvino_getenv_str("GGML_OPENVINO_MANUAL_GQA_ATTN"); |
| 126 | if (env != nullptr) { |
| 127 | return ggml_openvino_getenv_int("GGML_OPENVINO_MANUAL_GQA_ATTN") > 0; |
| 128 | } |
| 129 | return ggml_openvino_is_gpu(); |
| 130 | }(); |
| 131 | const bool use_manual_gqa_attention = |
| 132 | manual_gqa_enabled && factor > 1 && num_heads_kv > 1 && !context.is_stateful(); |
| 133 | |
| 134 | if (use_manual_gqa_attention) { |
| 135 | // Q, K, V arrive as [B, n_heads(_kv), S, head_size], where B is the active |
| 136 | // batch (n_seq_active) and may be > 1 (llama-perplexity, llama-server -np > 1) |
| 137 | // or dynamic. Reshape to |
| 138 | // K_r: [B, num_heads_kv, 1, S, head_size] |
| 139 | // Q_r: [B, num_heads_kv, factor, S_q, head_size] |
| 140 | // and let MatMul broadcast across the factor dim without materialising |
| 141 | // an expanded K/V. The leading 0 + special_zero=true copies B at runtime, |
| 142 | // so this is correct for B == 1, B > 1, and dynamic B alike. Only the head |
| 143 | // dims and head_size are baked in as literals; the sequence dim stays -1. |
| 144 | auto k_5d_shape = ov::op::v0::Constant::create(ov::element::i64, {5}, |
| 145 | std::vector<int64_t>{0, num_heads_kv, 1, -1, head_size}); |
| 146 | auto v_5d_shape = ov::op::v0::Constant::create(ov::element::i64, {5}, |
| 147 | std::vector<int64_t>{0, num_heads_kv, 1, -1, head_size}); |
| 148 | auto q_5d_shape = ov::op::v0::Constant::create(ov::element::i64, {5}, |
| 149 | std::vector<int64_t>{0, num_heads_kv, factor, -1, head_size}); |
| 150 | |
| 151 | auto k_r = std::make_shared<ov::op::v1::Reshape>(k, k_5d_shape, true); |
| 152 | auto v_r = std::make_shared<ov::op::v1::Reshape>(v, v_5d_shape, true); |
| 153 | auto q_r = std::make_shared<ov::op::v1::Reshape>(q, q_5d_shape, true); |
| 154 | |
| 155 | // QK^T → [B, num_heads_kv, factor, S_q, S_k] |
| 156 | auto qk = std::make_shared<ov::op::v0::MatMul>(q_r, k_r, /*tA=*/false, /*tB=*/true); |
| 157 | auto qk_scaled = std::make_shared<ov::op::v1::Multiply>(qk, scale_node); |
| 158 | |
| 159 | // Mask arrives as [B, 1, S_q, S_k]. Unsqueeze a factor axis at position 2 to |
| 160 | // get [B, 1, 1, S_q, S_k], which NUMPY-broadcasts cleanly against the |
| 161 | // [B, num_heads_kv, factor, S_q, S_k] scores: B==B, then 1→num_heads_kv and |
| 162 | // 1→factor on the head dims. |
| 163 | ov::Output<ov::Node> qk_masked; |
| 164 | if (has_mask) { |
| 165 | auto mask_unsq1 = |
| 166 | std::make_shared<ov::op::v0::Unsqueeze>(mask, ov::op::v0::Constant::create(ov::element::i64, {1}, {2})); |
| 167 | qk_masked = std::make_shared<ov::op::v1::Add>(qk_scaled, mask_unsq1); |
| 168 | } else { |
| 169 | qk_masked = qk_scaled; |
| 170 | } |
| 171 | |
| 172 | auto softmax = std::make_shared<ov::op::v8::Softmax>(qk_masked, /*axis=*/-1); |
| 173 | |
| 174 | // softmax @ V → [B, num_heads_kv, factor, S_q, head_size] |
| 175 | auto attn = std::make_shared<ov::op::v0::MatMul>(softmax, v_r); |
| 176 | |
| 177 | // Reshape back to [B, num_heads, S_q, head_size] (combine num_heads_kv * factor). |
| 178 | // Leading 0 + special_zero=true copies B at runtime. |
| 179 | auto out_4d_shape = |
| 180 | ov::op::v0::Constant::create(ov::element::i64, {4}, std::vector<int64_t>{0, num_heads, -1, head_size}); |
| 181 | auto out_4d = std::make_shared<ov::op::v1::Reshape>(attn, out_4d_shape, true); |
| 182 | |
| 183 | // The standard SDPA path's downstream is Transpose(0,2,1,3) → Convert(f32). |
| 184 | // Replicate it here so callers see the same output layout/dtype. |
| 185 | res = std::make_shared<ov::op::v1::Transpose>( |
| 186 | out_4d, ov::op::v0::Constant::create(ov::element::i64, {4}, {0, 2, 1, 3})); |
| 187 | res = std::make_shared<ov::op::v0::Convert>(res, ov::element::f32); |
| 188 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 189 | } |
| 190 | |
| 191 | // Default path: explicit Broadcast → SDPA. Kept as the fallback because |
| 192 | // (a) it goes through the GPU plugin's micro-SDPA fast path (FlashAttention |
| 193 | // tiles via DPAS), and (b) the manual path above is still being validated. |
| 194 | auto tile_kv = [&](int64_t n_heads, int64_t n_heads_kv, int64_t hs, ov::Output<Node> kv) { |
| 195 | int64_t f = n_heads / n_heads_kv; |
| 196 | if (f > 1 && n_heads_kv > 1) { |
| 197 | ov::Output<ov::Node> kv_broadcast_shape; |
| 198 | ov::Output<ov::Node> kv_unsqueezed; |
| 199 | ov::Output<ov::Node> new_kv_shape; |
| 200 | auto unsqueeze_axes = ov::op::v0::Constant::create(ov::element::i64, Shape{}, {2}); |
| 201 | kv_unsqueezed = std::make_shared<ov::op::v0::Unsqueeze>(kv, unsqueeze_axes); |
| 202 | |
| 203 | kv_broadcast_shape = ov::op::v0::Constant::create(ov::element::i64, {5}, |
| 204 | {(int64_t) 1, (int64_t) 1, f, (int64_t) 1, (int64_t) 1}); |
| 205 | new_kv_shape = |
| 206 | ov::op::v0::Constant::create(ov::element::i64, {4}, {(int64_t) 0, n_heads, (int64_t) -1, hs}); |
| 207 | // ov::element::i64, {5}, {(int64_t) 1, (int64_t) 1, factor, (int64_t) 1, (int64_t) 1}); |
| 208 | //new_kv_shape = |
| 209 | // ov::op::v0::Constant::create(ov::element::i64, {4}, {(int64_t) 0, num_heads, (int64_t) -1, head_size}); |
| 210 | |
| 211 | kv = std::make_shared<ov::op::v3::Broadcast>(kv_unsqueezed, kv_broadcast_shape, |
| 212 | ov::op::BroadcastType::BIDIRECTIONAL); |
| 213 | kv = std::make_shared<ov::op::v1::Reshape>(kv, new_kv_shape, true); |
| 214 | } |
| 215 | return kv; |
| 216 | }; |
| 217 | |
| 218 | //auto q_shape = context.get_input_shape(0).to_shape(); |
| 219 | //auto k_shape = context.get_input_shape(1).to_shape(); |
| 220 | //k = tile_kv(q_shape[1], k_shape[1], q_shape[3], k); |
| 221 | //v = tile_kv(q_shape[1], k_shape[1], q_shape[3], v); |
| 222 | k = tile_kv(num_heads, num_heads_kv, head_size, k); |
| 223 | v = tile_kv(num_heads, num_heads_kv, head_size, v); |
| 224 | |
| 225 | constexpr auto causal = false; |
| 226 | if (has_mask) { |
| 227 | auto sdpa = std::make_shared<ov::op::v13::ScaledDotProductAttention>(q, k, v, mask, scale_node, causal); |
| 228 | res = std::make_shared<ov::op::v1::Transpose>( |
| 229 | sdpa, ov::op::v0::Constant::create(ov::element::i64, {4}, {0, 2, 1, 3})); |
| 230 | } else { |
| 231 | auto sdpa = std::make_shared<ov::op::v13::ScaledDotProductAttention>(q, k, v, scale_node, causal); |
| 232 | res = std::make_shared<ov::op::v1::Transpose>( |
| 233 | sdpa, ov::op::v0::Constant::create(ov::element::i64, {4}, {0, 2, 1, 3})); |
| 234 | } |
| 235 | res = std::make_shared<ov::op::v0::Convert>(res, ov::element::f32); |
| 236 | return rename_outputs_with_suffix({res}, context.get_name()); |
| 237 | } |
| 238 | |
| 239 | } // namespace op |
| 240 | } // namespace ggml |
| 241 | } // namespace frontend |
| 242 | } // namespace ov |