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/ggml-decoder.h # 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 / ggml-decoder.h` 507 lines ยท 23870 bytes Raw Blame History 1 #pragma once 2 3 #include "ggml-backend-impl.h" 4 #include "ggml-backend.h" 5 #include "ggml.h" 6 #include "openvino/decoder.h" 7 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 18 struct ModelParams { 19 int ctx = -1; 20 int ctx_per_seq = -1; 21 int ctx_per_seq_swa = -1; 22 int n_seq = 1; 23 int n_heads_kv = -1; 24 // Per-layer KV head count. gemma-4 12B interleaves 8 x 256 sliding layers with 1 x 512 25 // full-attention layers, so no single scalar describes every layer. Keyed by layer, not by 26 // layer TYPE, because the SWA classification depends on the context size (extents tie at a 27 // small -c) while the head count does not. 28 std::map n_heads_kv_per_layer; 29 int head_size = -1; 30 int state_size = -1; // for SSM molels, eg qwen35 31 int32_t rope_params[16] = {}; 32 int n_rs_slots = -1; 33 bool has_rs_rollback = false; 34 bool mixed_rope_params = false; 35 bool is_cacheless_attn = false; 36 std::vector swa_layers; 37 // The sliding-window mask tensor, identified in compute_llm_params() by grouping attention 38 // layers on the mask they consume. Only used to tell the two masks apart when naming OV 39 // parameters -- both carry the same tensor name. Null when the graph has a single mask. 40 const ggml_tensor * swa_mask = nullptr; 41 42 std::vector kv_names; 43 size_t kv_buffer_ctx_id = 0; 44 45 bool same_rope_params(const ModelParams & other) const { 46 return mixed_rope_params == other.mixed_rope_params && 47 memcmp(rope_params, other.rope_params, sizeof(int32_t) * 16) == 0; 48 } 49 50 bool can_reuse_dynamically(const ModelParams & other) const { 51 return same_rope_params(other) && n_rs_slots == other.n_rs_slots && 52 has_rs_rollback == other.has_rs_rollback; 53 } 54 55 bool can_reuse_statically(const ModelParams & other) const { 56 return same_rope_params(other) && ctx == other.ctx && n_rs_slots == other.n_rs_slots && 57 has_rs_rollback == other.has_rs_rollback; 58 } 59 60 bool kv_buffer_changed(const ModelParams & other) const { return kv_buffer_ctx_id != other.kv_buffer_ctx_id; } 61 }; 62 63 struct ComputeParams { 64 int n_seq_active = 1; 65 int seq_active_start = 0; 66 int attention_size = -1; 67 int attention_size_swa = -1; 68 int attention_size_static = -1; // encoder/cross-attn KV fill level (whisper) 69 // Sliding window width, read back from the band of ggml's own SWA mask. ggml never passes 70 // n_swa down to a backend, but fill_mask() bakes it into the mask contents, so the widest 71 // unmasked row recovers it. Shorter than n_swa while the sequence is still short, which is 72 // harmless: every causal pair is inside the window then anyway. 73 int swa_window = -1; 74 int input_len = -1; 75 int token_len_per_seq = -1; 76 int past_kv_len = -1; 77 int output_len = 1; 78 79 int cache_rs_reset_idx = -1; 80 int cache_rs_reset_len = -1; 81 // SSM/DeltaNet models otionally clear cache_r and cache_s of certain slots in the cgraph 82 // 3: [ 18432, 4, 1, 1] RESHAPE cache_r_l0 (reshaped) 83 // [ 18432, 4, 1, 1] 0: NONE cache_r_l0 84 // 4: [ 18432, 1, 1, 1] VIEW cache_r_l0 (reshaped) (view) 85 // [ 18432, 4, 1, 1] 0: RESHAPE cache_r_l0 (reshaped) 86 // 5: [ 18432, 1, 1, 1] SCALE cache_r_l0 (reshaped) (view) (view) 87 // [ 18432, 1, 1, 1] 0: VIEW cache_r_l0 (reshaped) (view) 88 89 int s_copy_active_slot_len = -1; 90 // SSM/DeltaNet models otionally reorder slots of state cache, to make the active slots contiguous 91 // leaf_5 is the inp->s_copy in llama-graph.cpp, eg if there are 8 slots in total and slot 3 and 7 92 // are active in the current batch, leaf_5 will be [3, 7, 5, 6, 4] 93 // 6: [ 2, 1, 1, 1] VIEW (view) 94 // [ 2, 1, 1, 1] 0: NONE leaf_5 95 // 7: [ 18432, 2, 1, 1] GET_ROWS conv_states-0 96 // [ 18432, 4, 1, 1] 0: RESHAPE cache_r_l0 (reshaped) 97 // [ 2, 1, 1, 1] 1: VIEW (view) 98 // 8: [ 0, 1, 1, 1] VIEW (view) 99 // [ 2, 1, 1, 1] 0: NONE leaf_5 100 // 9: [ 18432, 0, 1, 1] GET_ROWS node_9 101 // [ 18432, 4, 1, 1] 0: RESHAPE cache_r_l0 (reshaped) 102 // [ 0, 1, 1, 1] 1: VIEW (view) 103 // 10: [ 18432, 0, 1, 1] VIEW cache_r_l0 (view) 104 // [ 18432, 4, 1, 1] 0: NONE cache_r_l0 105 // 11: [ 18432, 0, 1, 1] CPY cache_r_l0 (view) (copy of ) 106 // [ 18432, 0, 1, 1] 0: GET_ROWS node_9 107 // [ 18432, 0, 1, 1] 1: VIEW cache_r_l0 (view) 108 109 struct RsWriteback { 110 int slot_begin = 0; // first cache slot written by the CPY 111 int src_begin = -1; // first source column copied by a conv-state CPY 112 }; 113 114 std::map rs_writebacks; 115 // Destination slot offset of each state cache writeback CPY node, keyed by node name. It 116 // changes with the batch (kv head, active sequence count) and, with rollback enabled 117 // (cparams.n_rs_seq > 0), the conv state is written back once per snapshot slot. Passed to the 118 // cached model as a runtime input. Dynamic models also receive the source-side offset; static 119 // models use a fixed end-anchored offset in the translator. 120 }; 121 122 // defined below; declared here because GgmlOvDecoder uses it inline 123 std::optional extract_layer_from_name(const std::string & name); 124 125 // detects the MoE expert-plane-sum ADD chain (see definition); used by supports_op too 126 bool is_moe_expert_sum_add(const ggml_tensor * node); 127 128 class GgmlOvDecoder : public ov::frontend::ggml::GgmlDecoder { 129 public: 130 static std::string get_tensor_name(const ggml_cgraph * cgraph, const ggml_tensor * tensor); 131 struct NodeInfo { 132 ggml_tensor * node; 133 std::string node_name; 134 std::string node_op_type; 135 std::map node_inputs; 136 std::map>> node_inputs_views; 137 std::vector node_inputs_names; 138 int node_op_case = 0; 139 void * data_addr; 140 }; 141 142 // Graph decoder 143 GgmlOvDecoder(ggml_cgraph * cgraph, 144 ModelParams & model_params, 145 ComputeParams & compute_params, 146 std::map> & model_weights, 147 bool is_static, 148 bool is_stateful = false, 149 bool model_is_splitted = false, 150 bool is_prefill = false, 151 int prefill_chunk_size = 256); 152 153 // Naive graph decoder 154 GgmlOvDecoder(ggml_cgraph * cgraph, std::map> & model_weights); 155 156 virtual ov::Any get_attribute(const std::string & name) const override { 157 return nullptr; 158 GGML_UNUSED(name); 159 } 160 161 virtual ov::PartialShape get_input_shape(int node_idx, const std::string & name) const override; 162 163 virtual std::vector get_input_stride(int node_idx, const std::string & name) const override; 164 165 virtual size_t get_view_input_size(int node_idx, const std::string & name) const override; 166 167 virtual size_t get_view_input_offset(int node_idx, const std::string & name, size_t view_index) const override; 168 169 virtual size_t get_view_input_src_offset(int node_idx, const std::string & name, size_t view_index) const override; 170 171 virtual std::vector get_view_input_stride(int node_idx, 172 const std::string & name, 173 size_t view_index) const override; 174 175 virtual std::vector get_view_input_src_stride(int node_idx, 176 const std::string & name, 177 size_t view_index) const override; 178 179 virtual ov::Shape get_view_input_ggml_shape(int node_idx, 180 const std::string & name, 181 size_t view_index) const override; 182 183 virtual ov::Shape get_view_input_src_ggml_shape(int node_idx, 184 const std::string & name, 185 size_t view_index) const override; 186 187 virtual ov::PartialShape get_view_input_ov_shape(int node_idx, 188 const std::string & name, 189 size_t view_index) const override; 190 191 virtual ov::PartialShape get_view_input_src_ov_shape(int node_idx, 192 const std::string & name, 193 size_t view_index) const override; 194 195 virtual std::string get_view_input_name(int node_idx, const std::string & name, size_t view_index) const override; 196 197 virtual std::string get_view_input_src_name(int node_idx, 198 const std::string & name, 199 size_t view_index) const override; 200 201 virtual ov::element::Type get_input_type(int node_idx, const std::string & name) const override; 202 203 virtual size_t get_input_size() const override; 204 205 virtual size_t get_input_size(int node_idx) const override; 206 207 virtual void get_input_node(size_t input_port_idx, 208 std::string & producer_name, 209 std::string & producer_output_port_name, 210 size_t & producer_output_port_index) const override { 211 GGML_UNUSED(input_port_idx); 212 GGML_UNUSED(producer_name); 213 GGML_UNUSED(producer_output_port_name); 214 GGML_UNUSED(producer_output_port_index); 215 } 216 217 virtual std::vector get_input_names(int node_idx) const override; 218 219 virtual ov::PartialShape get_output_shape(int node_idx) const override; 220 221 virtual ov::element::Type get_output_type(int node_idx) const override; 222 223 virtual std::vector get_output_stride(int node_idx) const override; 224 225 virtual int32_t * get_input_op_params(int node_idx, const std::string & name) const override; 226 227 virtual int32_t * get_output_op_params(int node_idx) const override; 228 229 virtual size_t get_output_op_offset(int node_idx) const override; 230 231 virtual std::vector get_output_names(int node_idx) const override; 232 233 virtual std::string get_inplace_op_src(int node_idx) const override; 234 235 virtual bool is_view_like_alias_of(int node_idx, const std::string & view_src_name) const override; 236 237 virtual const std::string & get_op_type() const override; 238 239 virtual const std::string & get_op_type(int node_idx) const override; 240 241 virtual const std::string & get_op_name() const override; 242 243 virtual const std::string & get_op_name(int node_idx) const override; 244 245 virtual int32_t get_op_dynamic_dim(int node_idx) const override; 246 247 virtual void visit_subgraph( 248 std::function, int node_idx)> node_visitor) const override; 249 250 ggml_tensor * get_input_ggml_tensor(const std::string & name) const { return m_inputs.at(name); } 251 252 virtual int get_op_case(int node_idx) const override { return m_node_info_list[node_idx].node_op_case; } 253 254 virtual const std::map & get_model_inputs() const override { 255 return m_model_inputs; 256 } 257 258 virtual const std::map & get_model_extra_inputs() const override { 259 return m_model_extra_inputs; 260 } 261 262 virtual const std::map> & get_model_weights() const override { 263 return m_model_weights; 264 } 265 266 virtual std::set get_model_output_names() const override { return m_model_output_names; } 267 268 const std::map & get_model_outputs() const { return m_model_outputs; } 269 270 virtual int get_ctx_size() const { return m_model_params.ctx; } 271 272 virtual int get_ctx_per_seq() const { return m_model_params.ctx_per_seq; } 273 274 virtual int get_ctx_per_seq_swa() const { return m_model_params.ctx_per_seq_swa; } 275 276 virtual int get_n_seq() const { return m_model_params.n_seq; } 277 278 virtual int is_swa_layer(int layer) const override { 279 return std::find(m_model_params.swa_layers.begin(), m_model_params.swa_layers.end(), layer) != 280 m_model_params.swa_layers.end(); 281 } 282 283 // KV head count for one layer. Sliding and full layers can differ (gemma-4 12B), so callers 284 // that reinterpret a KV buffer must use this and not the model-level n_heads_kv. 285 int get_n_heads_kv_for_layer(int layer) const { 286 auto it = m_model_params.n_heads_kv_per_layer.find(layer); 287 return it != m_model_params.n_heads_kv_per_layer.end() ? it->second : m_model_params.n_heads_kv; 288 } 289 290 // Same, for a KV cache tensor: its layer comes from the leaf name (cache_k_l). 291 int get_n_heads_kv_for_tensor(const ggml_tensor * kv_tensor) const { 292 if (auto layer = extract_layer_from_name(std::string(kv_tensor->name)); layer.has_value()) { 293 return get_n_heads_kv_for_layer(layer.value()); 294 } 295 return m_model_params.n_heads_kv; 296 } 297 298 int get_past_kv_len() const { return m_compute_params.past_kv_len; } 299 300 int get_input_len() const { return m_compute_params.input_len; } 301 302 virtual int32_t * get_rope_params() const override { return const_cast(m_model_params.rope_params); } 303 304 virtual bool has_mixed_rope_params() const override { return m_model_params.mixed_rope_params; } 305 306 virtual int get_ssm_state_size() const override { return m_model_params.state_size; } 307 308 virtual std::map get_kv_param_res_names() const override; 309 310 virtual bool is_static() const override { return m_is_static; } 311 312 virtual bool is_stateful() const override { return m_is_stateful; } 313 314 int get_static_n_tokens() const { return m_is_prefill ? m_prefill_chunk_size : 1; } 315 316 virtual bool is_splited_model() const override { return m_model_is_splitted; } 317 318 ov::PartialShape get_graph_input_shape(const ggml_tensor * op, 319 const ggml_tensor * input, 320 int dynamic_dim_index = -1) const; 321 322 static void dump_cgraph(const ggml_cgraph * cgraph, std::string & filename); 323 324 static std::shared_ptr create_weight_node(ggml_tensor * tensor, bool naive = false); 325 326 static std::map> create_weight_nodes(ggml_cgraph * cgraph, 327 bool naive = false); 328 329 // Collect just the set of weight-tensor names referenced by the graph, without 330 // building (or requantizing) any OV weight nodes. Used by topology checks like 331 // is_model_splitted that only need name membership. 332 static std::set collect_weight_names(ggml_cgraph * cgraph); 333 334 const ggml_tensor * get_tensor_used_op(const ggml_tensor * tensor) const; 335 336 const ggml_tensor * get_tensor_from_name(const std::string & name) const; 337 338 void clear_model_weights() { m_model_weights.clear(); } 339 340 static std::pair compute_llm_params(ggml_cgraph * cgraph, bool is_static); 341 342 ModelParams get_model_params() const { return m_model_params; } 343 344 ComputeParams get_compute_params() const { return m_compute_params; } 345 346 void set_model_params(const ModelParams & model_params) { m_model_params = model_params; } 347 348 void set_compute_params(const ComputeParams & compute_params) { m_compute_params = compute_params; } 349 350 bool m_is_static = false; 351 bool m_is_stateful = false; 352 bool m_is_prefill = false; 353 bool m_naive = false; 354 int m_prefill_chunk_size = 0; 355 bool m_model_is_splitted = false; // label the cgraph is splited or not 356 357 static ov::Shape get_shape(const ggml_tensor * tensor); 358 static std::vector get_stride(const ggml_tensor * tensor); 359 static ov::element::Type get_ov_type(const ggml_tensor * tensor); 360 static std::string compute_op_type(const ggml_tensor * node); 361 void add_extra_inputs(); 362 363 void update_io(ggml_cgraph * cgraph); 364 bool is_bound_to(const ggml_cgraph * cgraph) const; 365 366 static bool is_inp_tok(const ggml_tensor * tensor, const ggml_tensor * op) { 367 return op->op == GGML_OP_GET_ROWS && tensor == op->src[1] && op->src[0]->op == GGML_OP_NONE; 368 } 369 370 static bool is_inp_pos(const ggml_tensor * tensor, const ggml_tensor * op) { 371 return op->op == GGML_OP_ROPE && tensor == op->src[1]; 372 } 373 374 // IMROPE and VISION pack 4 stacked position planes (t/h/w/e) into inp_pos, each of length 375 // n_tokens; other modes carry a single position per token. 376 static int get_inp_pos_n_planes(const ggml_tensor * op) { 377 const int mode = op->op_params[2]; 378 return (mode == GGML_ROPE_TYPE_IMROPE || mode == GGML_ROPE_TYPE_VISION || (mode & GGML_ROPE_TYPE_MROPE)) ? 4 : 1; 379 } 380 381 static bool is_inp_emb(const ggml_tensor * tensor, const ggml_tensor * op) { 382 return tensor->op == GGML_OP_GET_ROWS && op->op == GGML_OP_RMS_NORM; 383 } 384 385 static bool is_inp_mask(const ggml_tensor * tensor, const ggml_tensor * op) { 386 return op->op == GGML_OP_CPY || (op->op == GGML_OP_FLASH_ATTN_EXT && tensor == op->src[3]) || 387 (op->op == GGML_OP_SOFT_MAX && tensor == op->src[1]); 388 } 389 390 static bool is_inp_mean(const ggml_tensor * tensor, const ggml_tensor * op) { 391 return op->op == GGML_OP_MUL_MAT && tensor == op->src[1] && tensor->op == GGML_OP_NONE && 392 (tensor->flags & GGML_TENSOR_FLAG_INPUT) && tensor->type == GGML_TYPE_F32 && 393 op->src[0] != nullptr && op->src[0]->op != GGML_OP_NONE; 394 } 395 396 // per-token embedding scale [1, n_tokens] (inp->scale_rows in llama-graph.cpp) 397 static bool is_inp_scale_rows(const ggml_tensor * tensor, const ggml_tensor * op) { 398 return op->op == GGML_OP_MUL && tensor == op->src[1] && strcmp(tensor->name, "inp_scale_rows") == 0; 399 } 400 401 static bool is_rope_freqs_weight(const ggml_tensor * tensor, const ggml_tensor * op) { 402 return op->op == GGML_OP_ROPE && tensor == op->src[2]; 403 } 404 405 inline static bool is_recurrent_cache(const ggml_tensor * tensor) { 406 return tensor != nullptr && (strncmp(tensor->name, "cache_r_l", strlen("cache_r_l")) == 0 || 407 strncmp(tensor->name, "cache_s_l", strlen("cache_s_l")) == 0 || 408 strncmp(tensor->name, "cache_ple_r_l", strlen("cache_ple_r_l")) == 0); 409 } 410 411 inline static bool is_cache(const ggml_tensor * tensor, const ggml_tensor * op) { 412 return is_recurrent_cache(tensor) || is_kvcache(tensor, op); 413 } 414 415 inline static bool is_kvcache(const ggml_tensor * tensor, const ggml_tensor * op) { 416 if (tensor == nullptr || is_recurrent_cache(tensor)) { 417 return false; 418 } 419 return (tensor->buffer != nullptr && tensor->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY) || 420 (op != nullptr && op->op == GGML_OP_SET_ROWS && op->src[2] == tensor); 421 } 422 423 inline static bool is_conv_state_writeback(const ggml_tensor * node) { 424 return node->op == GGML_OP_CPY && node->view_src != nullptr && is_recurrent_cache(node->view_src) && 425 node->src[0] != nullptr && node->src[0]->op == GGML_OP_VIEW && node->src[0]->src[0] != nullptr && 426 node->src[0]->src[0]->op == GGML_OP_CONCAT && node->src[1] != nullptr && 427 node->src[1]->op == GGML_OP_VIEW && node->src[1]->view_src == node->view_src; 428 } 429 430 static bool is_kv_idx(const ggml_tensor * tensor, const ggml_tensor * op) { 431 return op->op == GGML_OP_SET_ROWS && op->src[1] == tensor; 432 } 433 434 bool is_swa_mask(const ggml_tensor * tensor) const { 435 return m_model_params.swa_mask != nullptr && tensor == m_model_params.swa_mask; 436 } 437 438 static bool is_output_idx(const ggml_tensor * tensor, const ggml_tensor * op) { 439 return op->op == GGML_OP_GET_ROWS && tensor == op->src[1] && op->src[0]->op != GGML_OP_NONE && 440 op->src[1]->op == GGML_OP_NONE; 441 } 442 443 // the state permutation index input used in SSM/DeltaNet models (inp->s_copy in llama-graph.cpp) 444 static bool is_inp_s_copy(const ggml_tensor * tensor, const ggml_tensor * op) { 445 return op->op == GGML_OP_GET_ROWS && tensor == op->src[1] && 446 op->src[0]->buffer->usage == GGML_BACKEND_BUFFER_USAGE_ANY; 447 } 448 449 std::string get_graph_input_ov_name(const ggml_tensor * tensor, const ggml_tensor * op) const { 450 if (is_inp_pos(tensor, op)) { 451 return "inp_pos"; 452 } 453 if (is_inp_emb(tensor, op)) { 454 return "embd"; 455 } 456 if (is_inp_mask(tensor, op)) { 457 // Give the two attention masks distinct OV parameter names. 458 // 459 // An interleaved-SWA model builds one full-attention mask and one sliding-window mask, 460 // but build_attn_inp_kq_mask() names them identically, so keying a parameter off 461 // tensor->name alone makes the second mask OVERWRITE the first in m_model_inputs: both 462 // attention types then read a single parameter, and the windowed layers silently run 463 // against an unbanded mask. Disambiguate using the SWA layer set computed in 464 // compute_llm_params(), which classifies by mask tensor identity rather than by name. 465 // 466 // When no SWA layer was found there is only one mask in play, so the plain name is 467 // correct and no _swa parameter is created. 468 if (m_model_params.swa_layers.empty()) { 469 return "self_kq_mask"; 470 } 471 return is_swa_mask(tensor) ? "self_kq_mask_swa" : "self_kq_mask"; 472 } 473 return tensor->name; 474 } 475 476 private: 477 void set_input_output(); 478 int compute_op_case(const ggml_tensor * node) const; 479 bool node_is_used_as_src(const int node_idx); 480 void compute_model_inputs(); 481 void compute_model_outputs(); 482 483 // True if tensor is the inp->s_copy index leaf gathered by a recurrent state cache GET_ROWS 484 // (possibly through a VIEW), so it gets a dynamic [1,1,1,-1] graph-input shape. 485 bool is_s_copy_leaf(const ggml_tensor * tensor) const; 486 487 // Infer and propagate dynamic-dimension indices for all tensors in the GGML graph. 488 void compute_node_dynamic_dims(); 489 490 void validate_cgraph() const; 491 492 ggml_cgraph * m_cgraph = nullptr; 493 std::map m_inputs; 494 495 std::map m_model_inputs; 496 std::map m_model_extra_inputs; 497 std::map> m_model_weights; 498 std::map m_model_outputs; 499 std::set m_model_output_names; 500 std::vector m_node_info_list; 501 std::map m_node_dynamic_dims; 502 503 ModelParams m_model_params; 504 ComputeParams m_compute_params; 505 }; 506 507 void print_tensor_address_map(const ggml_cgraph * cgraph);