char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
fa88ae936
char/aliceai-t5-moe
1from __future__import annotations 2 3from typing import TYPE_CHECKING 4 5if TYPE_CHECKING : 6from torch import Tensor 7 8from .base import ModelBase ,TextModel ,gguf 9 10from .deepseek import DeepseekV2Model 11from .llama import LlamaModel 12 13 14@ ModelBase . register ( 15"Mistral3ForConditionalGeneration" , 16"Ministral3ForCausalLM" , 17) 18@ ModelBase . example ( "mistralai/Mistral-Small-3.1-24B-Instruct-2503" , "hf-tiny-v2/tiny-random-Ministral3ForCausalLM" ) 19class Mistral3Model (TextModel ): 20class Ministral3Model (LlamaModel ): 21model_arch = gguf .MODEL_ARCH .MISTRAL3 22 23def set_gguf_parameters (self ): 24super ().set_gguf_parameters () 25rope_params = self .rope_parameters 26if self .hparams .get ("model_type" )== "ministral3" : 27assert rope_params ,"ministral3 must have 'rope_parameters' config" 28assert rope_params ["rope_type" ]== "yarn" ,"ministral3 rope_type must be 'yarn'" 29self .gguf_writer .add_rope_scaling_yarn_log_mul (rope_params ["mscale_all_dim" ]) 30self .gguf_writer .add_attn_temperature_scale (rope_params ["llama_4_scaling_beta" ]) 31 32class Mistral4Model (DeepseekV2Model ): 33model_arch = gguf .MODEL_ARCH .MISTRAL4 34skip_mtp = False # model contains no MTP layers, so no need to skip 35merge_expert = False # experts are already stacked as 3D 36 37def modify_tensors (self ,data_torch ,name ,bid ): 38if name .endswith (".down_proj" )or name .endswith (".gate_up_proj" ): 39name = name + ".weight" 40yield from super ().modify_tensors (data_torch ,name ,bid ) 41 42model_arch = gguf .MODEL_ARCH .MISTRAL3 # unused 43impl :TextModel 44 45def __init__ (self ,* args ,** kwargs ): 46super ().__init__ (* args ,** kwargs ) 47if self .hparams .get ("model_type" )== "mistral4" : 48self .impl = Mistral3Model .Mistral4Model (* args ,** kwargs ) 49else : 50self .impl = Mistral3Model .Ministral3Model (* args ,** kwargs ) 51 52def set_vocab (self ): 53self .impl .set_vocab () 54 55def set_gguf_parameters (self ): 56self .impl .set_gguf_parameters () 57 58def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None ): 59yield from self .impl .modify_tensors (data_torch ,name ,bid ) 60 61def prepare_tensors (self ): 62self .impl .prepare_tensors () 63 64def write_vocab (self ): 65self .impl .write_vocab () 66 67def write (self ): 68self .impl .write ()