char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
3ad1ba733
char/aliceai-t5-moe
1from __future__import annotations 2 3from collections .abc import Iterable 4from typing import TYPE_CHECKING 5 6if TYPE_CHECKING : 7from torch import Tensor 8 9from .base import ModelBase ,TextModel ,gguf 10 11 12@ ModelBase . register ( "Spark2_5ForCausalLM" ) 13@ ModelBase . example ( "XHToken/Spark-X2.5-1.7B" ) 14class Spark2_5Model (TextModel ): 15model_arch = gguf .MODEL_ARCH .SPARK2_5 16 17def set_gguf_parameters (self )-> None : 18super ().set_gguf_parameters () 19 20hparams = self .hparams 21layer_types = hparams ["layer_types" ] 22if len (layer_types )!= self .block_count : 23raise ValueError ( 24f"Spark2_5 layer_types length { len ( layer_types ) } != num_hidden_layers { self . block_count } " 25 ) 26if any (layer_type not in ("sliding_attention" ,"full_attention" )for layer_type in layer_types ): 27raise ValueError (f"Spark2_5 has unsupported layer_types: { layer_types } " ) 28if hparams .get ("gate_attn_act_mode" )!= "sigmoid" or hparams .get ("headwise_attn_output_gate" )is not True : 29raise ValueError ("Spark2_5 conversion requires head-wise sigmoid attention gates" ) 30if hparams .get ("hidden_act" )!= "gelu" : 31raise ValueError (f"Spark2_5 conversion requires GELU, got { hparams . get ( 'hidden_act' )!r } " ) 32 33self .gguf_writer .add_vocab_size (hparams ["vocab_size" ]) 34self .gguf_writer .add_sliding_window (hparams ["sliding_window" ]) 35self .gguf_writer .add_sliding_window_pattern ( 36 [layer_type == "sliding_attention" for layer_type in layer_types ] 37 ) 38 39head_dim = hparams ["head_dim" ] 40full_rope = self .rope_parameters ["full_attention" ] 41swa_rope = self .rope_parameters ["sliding_attention" ] 42self .gguf_writer .add_rope_dimension_count ( 43int (head_dim * float (full_rope ["partial_rotary_factor" ])) 44 ) 45self .gguf_writer .add_rope_dimension_count_swa ( 46int (head_dim * float (swa_rope ["partial_rotary_factor" ])) 47 ) 48 49def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 50if name .endswith (".self_attn.q_k_v_proj.weight" ): 51if bid is None : 52raise ValueError (f"Spark2_5 fused QKV tensor has no block id: { name } " ) 53yield self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_QKV ,bid ),data_torch 54return 55 56if name .endswith (".self_attn.g_proj.weight" ): 57if bid is None : 58raise ValueError (f"Spark2_5 attention gate tensor has no block id: { name } " ) 59expected = self .hparams ["num_attention_heads" ] 60if data_torch .shape [0 ]!= expected : 61raise ValueError ( 62f"Spark2_5 layer { bid } attention gate width { data_torch . shape [ 0 ] } != head count { expected } " 63 ) 64 65yield from super ().modify_tensors (data_torch ,name ,bid )