char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
511f9c137
char/aliceai-t5-moe
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 <cstdint> 9#include <cstring> 10#include <map> 11#include <memory> 12#include <openvino/core/partial_shape.hpp> 13#include <optional> 14#include <set> 15#include <string> 16#include <vector> 17 18struct ModelParams { 19int ctx = -1 ; 20int ctx_per_seq = -1 ; 21int ctx_per_seq_swa = -1 ; 22int n_seq = 1 ; 23int n_heads_kv = -1 ; 24int head_size = -1 ; 25int state_size = -1 ;// for SSM molels, eg qwen35 26int32_t rope_params [15 ]; 27bool mixed_rope_params = false; 28std ::vector < int > swa_layers ; 29 30std ::vector < std ::string > kv_names ; 31size_t kv_buffer_ctx_id = 0 ; 32 33bool same_rope_params (const ModelParams & other )const { 34return mixed_rope_params == other .mixed_rope_params && 35memcmp (rope_params ,other .rope_params ,sizeof (int32_t )* 15 )== 0 ; 36 } 37 38bool can_reuse_dynamically (const ModelParams & other )const {return same_rope_params (other ); } 39 40bool can_reuse_statically (const ModelParams & other )const {return same_rope_params (other )&& ctx == other .ctx ; } 41 42bool kv_buffer_changed (const ModelParams & other )const {return kv_buffer_ctx_id != other .kv_buffer_ctx_id ; } 43}; 44 45struct ComputeParams { 46int n_seq_active = 1 ; 47int seq_active_start = 0 ; 48int attention_size = -1 ; 49int attention_size_swa = -1 ; 50int attention_size_static = -1 ;// encoder/cross-attn KV fill level (whisper) 51int input_len = -1 ; 52int token_len_per_seq = -1 ; 53int past_kv_len = -1 ; 54int output_len = 1 ; 55 56int cache_rs_reset_idx = -1 ; 57int cache_rs_reset_len = -1 ; 58// SSM/DeltaNet models otionally clear cache_r and cache_s of certain slots in the cgraph 59// 3: [ 18432, 4, 1, 1] RESHAPE cache_r_l0 (reshaped) 60// [ 18432, 4, 1, 1] 0: NONE cache_r_l0 61// 4: [ 18432, 1, 1, 1] VIEW cache_r_l0 (reshaped) (view) 62// [ 18432, 4, 1, 1] 0: RESHAPE cache_r_l0 (reshaped) 63// 5: [ 18432, 1, 1, 1] SCALE cache_r_l0 (reshaped) (view) (view) 64// [ 18432, 1, 1, 1] 0: VIEW cache_r_l0 (reshaped) (view) 65 66int s_copy_active_slot_len = -1 ; 67// SSM/DeltaNet models otionally reorder slots of state cache, to make the active slots contiguous 68// leaf_5 is the inp->s_copy in llama-graph.cpp, eg if there are 8 slots in total and slot 3 and 7 69// are active in the current batch, leaf_5 will be [3, 7, 5, 6, 4] 70// 6: [ 2, 1, 1, 1] VIEW (view) 71// [ 2, 1, 1, 1] 0: NONE leaf_5 72// 7: [ 18432, 2, 1, 1] GET_ROWS conv_states-0 73// [ 18432, 4, 1, 1] 0: RESHAPE cache_r_l0 (reshaped) 74// [ 2, 1, 1, 1] 1: VIEW (view) 75// 8: [ 0, 1, 1, 1] VIEW (view) 76// [ 2, 1, 1, 1] 0: NONE leaf_5 77// 9: [ 18432, 0, 1, 1] GET_ROWS node_9 78// [ 18432, 4, 1, 1] 0: RESHAPE cache_r_l0 (reshaped) 79// [ 0, 1, 1, 1] 1: VIEW (view) 80// 10: [ 18432, 0, 1, 1] VIEW cache_r_l0 (view) 81// [ 18432, 4, 1, 1] 0: NONE cache_r_l0 82// 11: [ 18432, 0, 1, 1] CPY cache_r_l0 (view) (copy of ) 83// [ 18432, 0, 1, 1] 0: GET_ROWS node_9 84// [ 18432, 0, 1, 1] 1: VIEW cache_r_l0 (view) 85 86struct RsWriteback { 87int slot_begin = 0 ;// first cache slot written by the CPY 88int src_begin = 0 ;// first source row or column copied by the CPY 89 }; 90 91std ::map < std ::string ,RsWriteback > rs_writebacks ; 92// Destination slot offset of each state cache writeback CPY node, keyed by node name. It 93// changes with the batch (kv head, active sequence count) and, with rollback enabled 94// (cparams.n_rs_seq > 0), the conv state is written back once per snapshot slot. Passed to the 95// cached model as a runtime input. Dynamic models also receive the source-side offset; static 96// models use a fixed end-anchored offset in the translator. 97}; 98 99class GgmlOvDecoder :public ov ::frontend ::ggml ::GgmlDecoder { 100public : 101struct NodeInfo { 102ggml_tensor * node ; 103std ::string node_name ; 104std ::string node_op_type ; 105std ::map < std ::string ,ggml_tensor *> node_inputs ; 106std ::map < std ::string ,std ::vector < std ::pair < std ::string ,ggml_tensor * >>> node_inputs_views ; 107std ::vector < std ::string > node_inputs_names ; 108int node_op_case = 0 ; 109void * data_addr ; 110 }; 111 112// Graph decoder 113GgmlOvDecoder (ggml_cgraph * cgraph, 114ModelParams & model_params, 115ComputeParams & compute_params, 116std::map < std::string, std::shared_ptr < ov::Node>> & model_weights, 117bool is_static, 118bool is_stateful = false, 119bool model_is_splitted = false, 120bool is_prefill = false, 121int prefill_chunk_size = 256 ); 122 123// Naive graph decoder 124GgmlOvDecoder (ggml_cgraph * cgraph, std::map < std::string, std::shared_ptr < ov::Node>> & model_weights); 125 126virtual ov::Any get_attribute ( const std ::string & name) const override { 127return nullptr ; 128GGML_UNUSED (name); 129} 130 131virtual ov::PartialShape get_input_shape ( int node_idx, const std ::string & name) const override; 132 133virtual std::vector < size_t > get_input_stride ( int node_idx, const std ::string & name) const override; 134 135virtual size_t get_view_input_size ( int node_idx, const std ::string & name) const override; 136 137virtual size_t get_view_input_offset ( int node_idx, const std ::string & name, size_t view_index) const override; 138 139virtual size_t get_view_input_src_offset ( int node_idx, const std ::string & name, size_t view_index) const override; 140 141virtual std::vector < size_t > get_view_input_stride ( int node_idx, 142const std ::string & name, 143size_t view_index) const override; 144 145virtual std::vector < size_t > get_view_input_src_stride ( int node_idx, 146const std ::string & name, 147size_t view_index) const override; 148 149virtual ov::Shape get_view_input_ggml_shape ( int node_idx, 150const std ::string & name, 151size_t view_index) const override; 152 153virtual ov::Shape get_view_input_src_ggml_shape ( int node_idx, 154const std ::string & name, 155size_t view_index) const override; 156 157virtual ov::PartialShape get_view_input_ov_shape ( int node_idx, 158const std ::string & name, 159size_t view_index) const override; 160 161virtual ov::PartialShape get_view_input_src_ov_shape ( int node_idx, 162const std ::string & name, 163size_t view_index) const override; 164 165virtual std::string get_view_input_name ( int node_idx, const std ::string & name, size_t view_index) const override; 166 167virtual std::string get_view_input_src_name ( int node_idx, 168const std ::string & name, 169size_t view_index) const override; 170 171virtual ov::element::Type get_input_type ( int node_idx, const std ::string & name) const override; 172 173virtual size_t get_input_size () const override ; 174 175virtual size_t get_input_size ( int node_idx) const override; 176 177virtual void get_input_node ( size_t input_port_idx, 178std ::string & producer_name, 179std ::string & producer_output_port_name, 180size_t & producer_output_port_index) const override { 181GGML_UNUSED ( input_port_idx ); 182GGML_UNUSED (producer_name); 183GGML_UNUSED (producer_output_port_name); 184GGML_UNUSED (producer_output_port_index); 185} 186 187virtual std::vector < std::string > get_input_names ( int node_idx) const override; 188 189virtual ov::PartialShape get_output_shape ( int node_idx) const override; 190 191virtual ov::element::Type get_output_type ( int node_idx) const override; 192 193virtual std::vector < size_t > get_output_stride ( int node_idx) const override; 194 195virtual int32_t * get_input_op_params ( int node_idx, const std ::string & name) const override; 196 197virtual int32_t * get_output_op_params ( int node_idx) const override; 198 199virtual size_t get_output_op_offset ( int node_idx) const override; 200 201virtual std::vector < std::string > get_output_names ( int node_idx) const override; 202 203virtual std::string get_inplace_op_src ( int node_idx) const override; 204 205virtual bool is_view_like_alias_of ( int node_idx, const std ::string & view_src_name) const override; 206 207virtual const std::string & get_op_type () const override; 208 209virtual const std::string & get_op_type ( int node_idx) const override; 210 211virtual const std::string & get_op_name () const override; 212 213virtual const std::string & get_op_name ( int node_idx) const override; 214 215virtual int32_t get_op_dynamic_dim ( int node_idx) const override; 216 217virtual void visit_subgraph ( 218std ::function < void ( std ::shared_ptr < GgmlDecoder > , int node_idx) > node_visitor) const override; 219 220ggml_tensor * get_input_ggml_tensor ( const std ::string & name) const { return m_inputs. at ( name ); } 221 222virtual int get_op_case ( int node_idx) const override { return m_node_info_list[node_idx]. node_op_case ; } 223 224virtual const std::map < std::string, ov::frontend::ggml::ModelInputInfo > & get_model_inputs () const override { 225return m_model_inputs; 226} 227 228virtual const std::map < std::string, ov::frontend::ggml::ModelExtraInputInfo > & get_model_extra_inputs () const override { 229return m_model_extra_inputs; 230} 231 232virtual const std::map < std::string, std::shared_ptr < ov::Node>> & get_model_weights () const override { 233return m_model_weights; 234} 235 236virtual std::set < std::string > get_model_output_names () const override { return m_model_output_names; } 237 238const std ::map < std::string, ggml_tensor *> & get_model_outputs () const { return m_model_outputs; } 239 240virtual int get_ctx_size () const { return m_model_params. ctx ; } 241 242virtual int get_ctx_per_seq () const { return m_model_params. ctx_per_seq ; } 243 244virtual int get_ctx_per_seq_swa () const { return m_model_params. ctx_per_seq_swa ; } 245 246virtual int get_n_seq () const { return m_model_params. n_seq ; } 247 248virtual int is_swa_layer ( int layer) const override { 249return std:: find (m_model_params. swa_layers . begin (), m_model_params. swa_layers . end (), layer) != 250m_model_params. swa_layers . end (); 251} 252 253int get_past_kv_len () const { return m_compute_params. past_kv_len ; } 254 255int get_input_len () const { return m_compute_params. input_len ; } 256 257virtual int32_t * get_rope_params () const override { return const_cast < int32_t *> (m_model_params. rope_params ); } 258 259virtual bool has_mixed_rope_params () const override { return m_model_params. mixed_rope_params ; } 260 261virtual int get_ssm_state_size () const override { return m_model_params. state_size ; } 262 263virtual std::map < std::string, std::string > get_kv_param_res_names () const override; 264 265virtual bool is_static () const override { return m_is_static; } 266 267virtual bool is_stateful () const override { return m_is_stateful; } 268 269int get_static_n_tokens () const { return m_is_prefill ? m_prefill_chunk_size : 1 ; } 270 271virtual bool is_splited_model () const override { return m_model_is_splitted; } 272 273ov :: PartialShape get_graph_input_shape ( const ggml_tensor * op, 274const ggml_tensor * input, 275int dynamic_dim_index = -1 ) const ; 276 277static void dump_cgraph ( const ggml_cgraph * cgraph, std ::string & filename); 278 279static std ::shared_ptr < ov::Node > create_weight_node ( ggml_tensor * tensor, bool naive = false); 280 281static std ::map < std::string, std::shared_ptr < ov::Node>> create_weight_nodes ( ggml_cgraph * cgraph, 282bool naive = false); 283 284// Collect just the set of weight-tensor names referenced by the graph, without 285// building (or requantizing) any OV weight nodes. Used by topology checks like 286// is_model_splitted that only need name membership. 287static std ::set < std::string > collect_weight_names ( ggml_cgraph * cgraph); 288 289const ggml_tensor * get_tensor_used_op ( const ggml_tensor * tensor) const; 290 291const ggml_tensor * get_tensor_from_name ( const std ::string & name) const; 292 293void clear_model_weights () { m_model_weights. clear (); } 294 295static std ::pair < ModelParams, ComputeParams > compute_llm_params ( ggml_cgraph * cgraph, bool is_static); 296 297ModelParams get_model_params () const { return m_model_params; } 298 299ComputeParams get_compute_params () const { return m_compute_params; } 300 301void set_model_params ( const ModelParams & model_params) { m_model_params = model_params; } 302 303void set_compute_params ( const ComputeParams & compute_params) { m_compute_params = compute_params; } 304 305bool m_is_static = false; 306bool m_is_stateful = false; 307bool m_is_prefill = false; 308bool m_naive = false; 309int m_prefill_chunk_size = 0 ; 310bool m_model_is_splitted = false; // label the cgraph is splited or not 311 312static ov ::Shape get_shape ( const ggml_tensor * tensor); 313static std ::vector < size_t > get_stride ( const ggml_tensor * tensor); 314static ov ::element::Type get_ov_type ( const ggml_tensor * tensor); 315static std ::string compute_op_type ( const ggml_tensor * node); 316void add_extra_inputs (); 317 318void update_io ( ggml_cgraph * cgraph); 319 320inline static bool is_inp_tok ( const ggml_tensor * tensor, const ggml_tensor * op) { 321return op -> op == GGML_OP_GET_ROWS && tensor == op -> src [ 1 ] && op -> src [ 0 ] -> op == GGML_OP_NONE ; 322} 323 324inline static bool is_inp_pos ( const ggml_tensor * tensor, const ggml_tensor * op) { 325return op -> op == GGML_OP_ROPE && tensor == op -> src [ 1 ]; 326} 327 328// IMROPE packs 4 stacked position planes (t/h/w/e) into inp_pos, each of length 329// n_tokens; other modes carry a single position per token. 330inline static int get_inp_pos_n_planes ( const ggml_tensor * op) { 331return op -> op_params [ 2 ] == GGML_ROPE_TYPE_IMROPE ? 4 : 1 ; 332} 333 334inline static bool is_inp_emb ( const ggml_tensor * tensor, const ggml_tensor * op) { 335return tensor -> op == GGML_OP_GET_ROWS && op -> op == GGML_OP_RMS_NORM ; 336} 337 338inline static bool is_inp_mask ( const ggml_tensor * tensor, const ggml_tensor * op) { 339return op -> op == GGML_OP_CPY || (op -> op == GGML_OP_FLASH_ATTN_EXT && tensor == op -> src [ 3 ]) || 340(op -> op == GGML_OP_SOFT_MAX && tensor == op -> src [ 1 ]); 341} 342 343inline static bool is_rope_freqs_weight ( const ggml_tensor * tensor, const ggml_tensor * op) { 344return op -> op == GGML_OP_ROPE && tensor == op -> src [ 2 ]; 345} 346 347// also returns true for cache_s and cache_r in SSM/DeltaNet models 348inline static bool is_kvcache ( const ggml_tensor * tensor, const ggml_tensor * op) { 349if (tensor == nullptr ) { 350return false; 351} 352return (tensor -> buffer != nullptr && tensor -> buffer -> usage == GGML_BACKEND_BUFFER_USAGE_ANY ) || 353(op != nullptr && op -> op == GGML_OP_SET_ROWS && op -> src [ 2 ] == tensor); 354} 355 356inline static bool is_kv_idx ( const ggml_tensor * tensor, const ggml_tensor * op) { 357return op -> op == GGML_OP_SET_ROWS && op -> src [ 1 ] == tensor; 358} 359 360inline static bool is_output_idx ( const ggml_tensor * tensor, const ggml_tensor * op) { 361return op -> op == GGML_OP_GET_ROWS && tensor == op -> src [ 1 ] && op -> src [ 0 ] -> op != GGML_OP_NONE && 362op -> src [ 1 ] -> op == GGML_OP_NONE ; 363} 364 365// the state permutation index input used in SSM/DeltaNet models (inp->s_copy in llama-graph.cpp) 366inline static bool is_inp_s_copy ( const ggml_tensor * tensor, const ggml_tensor * op) { 367return op -> op == GGML_OP_GET_ROWS && tensor == op -> src [ 1 ] && 368op -> src [ 0 ] -> buffer -> usage == GGML_BACKEND_BUFFER_USAGE_ANY ; 369} 370 371std :: string get_graph_input_ov_name ( const ggml_tensor * tensor, const ggml_tensor * op) const { 372if ( is_inp_pos (tensor, op)) { 373return "inp_pos" ; 374} 375if ( is_inp_emb (tensor, op)) { 376return "embd" ; 377} 378if ( is_stateful () && is_inp_mask (tensor, op)) { 379return std:: string (tensor -> name ). find ( "swa" ) == std::string::npos ? "self_kq_mask" : "self_kq_mask_swa" ; 380} 381return tensor -> name ; 382} 383 384private : 385void set_input_output (); 386int compute_op_case ( const ggml_tensor * node) const; 387bool node_is_used_as_src ( const int node_idx); 388void compute_model_inputs (); 389void compute_model_outputs (); 390 391// True if tensor is the inp->s_copy index leaf gathered by a recurrent state cache GET_ROWS 392// (possibly through a VIEW), so it gets a dynamic [1,1,1,-1] graph-input shape. 393bool is_s_copy_leaf ( const ggml_tensor * tensor) const; 394 395// Infer and propagate dynamic-dimension indices for all tensors in the GGML graph. 396void compute_node_dynamic_dims (); 397 398void validate_cgraph () const ; 399 400ggml_cgraph * m_cgraph = nullptr ; 401std ::map < std::string, ggml_tensor *> m_inputs; 402 403std ::map < std::string, ov::frontend::ggml::ModelInputInfo > m_model_inputs; 404std ::map < std::string, ov::frontend::ggml::ModelExtraInputInfo > m_model_extra_inputs; 405std ::map < std::string, std::shared_ptr < ov::Node>> m_model_weights; 406std ::map < std::string, ggml_tensor *> m_model_outputs; 407std ::set < std::string > m_model_output_names; 408std ::vector < NodeInfo > m_node_info_list; 409std ::map < ggml_tensor * , int > m_node_dynamic_dims; 410 411ModelParams m_model_params; 412ComputeParams m_compute_params; 413}; 414 415void print_tensor_address_map ( const ggml_cgraph * cgraph); 416 417std ::optional < int > extract_layer_from_name ( const std ::string & name );