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 Iterable ,TYPE_CHECKING 4 5import torch 6 7if TYPE_CHECKING : 8from torch import Tensor 9 10from .base import LazyTorchTensor ,ModelBase ,TextModel ,gguf 11 12 13@ ModelBase . register ( "TalkieForCausalLM" ) 14@ ModelBase . example ( "lewtun/talkie-1930-13b-it-hf" ) 15class TalkieModel (TextModel ): 16model_arch = gguf .MODEL_ARCH .TALKIE 17 18def set_gguf_parameters (self ): 19super ().set_gguf_parameters () 20# Talkie used F.rms_norm without an explicit eps 21self .gguf_writer .add_layer_norm_rms_eps (torch .finfo (torch .float32 ).eps ) 22 23def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 24prefix = f"model.blocks. { bid } ." if bid is not None else "" 25suffix = name .removeprefix (prefix ) 26 27if suffix == "attn_gain.a_g" : 28yield self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_OUT ,bid ,".scale" ),data_torch 29return 30elif suffix == "mlp_gain.a_g" : 31yield self .format_tensor_name (gguf .MODEL_TENSOR .FFN_DOWN ,bid ,".scale" ),data_torch 32return 33elif suffix == "lm_head_gain.w_g" : 34self .gguf_writer .add_logit_scale (LazyTorchTensor .to_eager (data_torch ).item ()) 35return 36elif suffix in ("attn.attn_query.weight" ,"attn.attn_key.weight" ): 37# absorb inverse rope 38head_dim = self .hparams ["head_dim" ] 39shape = data_torch .shape 40data_torch = torch .reshape (data_torch , (- 1 ,head_dim ,shape [- 1 ])) 41signs = torch .ones ((1 ,head_dim ,1 ),dtype = data_torch .dtype ) 42signs [:,head_dim // 2 :, :]= - 1 43if self .lazy : 44signs = LazyTorchTensor .from_eager (signs ) 45# (n_head, head_dim, n_in) -> (n_out, n_in) 46data_torch = torch .reshape (data_torch * signs ,shape ) 47elif suffix == "attn.head_gain.head_g" : 48# allow head gain to broadcast 49data_torch = data_torch .unsqueeze (- 1 ) 50 51if not name .endswith (".weight" ): 52name += ".weight" 53 54yield from super ().modify_tensors (data_torch ,name ,bid )