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 ModelBase ,TextModel ,gguf 11 12 13@ ModelBase . register ( "FalconForCausalLM" , "RWForCausalLM" ) 14@ ModelBase . example ( "tiiuae/falcon-7b" ) 15class FalconModel (TextModel ): 16model_arch = gguf .MODEL_ARCH .FALCON 17 18def set_gguf_parameters (self ): 19n_head = self .hparams .get ("num_attention_heads" ) 20if n_head is None : 21n_head = self .hparams ["n_head" ]# old name 22 23n_head_kv = self .hparams .get ("num_kv_heads" ) 24if n_head_kv is None : 25n_head_kv = self .hparams .get ("n_head_kv" ,1 )# old name 26 27self .gguf_writer .add_context_length (2048 )# not in config.json 28self .gguf_writer .add_tensor_data_layout ("jploski" )# qkv tensor transform 29self .gguf_writer .add_embedding_length (self .hparams ["hidden_size" ]) 30self .gguf_writer .add_feed_forward_length (4 * self .hparams ["hidden_size" ]) 31self .gguf_writer .add_block_count (self .block_count ) 32self .gguf_writer .add_head_count (n_head ) 33self .gguf_writer .add_head_count_kv (n_head_kv ) 34self .gguf_writer .add_layer_norm_eps (self .hparams ["layer_norm_epsilon" ]) 35self .gguf_writer .add_file_type (self .ftype ) 36 37def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 38# QKV tensor transform 39# The original query_key_value tensor contains n_head_kv "kv groups", 40# each consisting of n_head/n_head_kv query weights followed by one key 41# and one value weight (shared by all query heads in the kv group). 42# This layout makes it a big pain to work with in GGML. 43# So we rearrange them here,, so that we have n_head query weights 44# followed by n_head_kv key weights followed by n_head_kv value weights, 45# in contiguous fashion. 46# ref: https://github.com/jploski/ggml/blob/falcon40b/examples/falcon/convert-hf-to-ggml.py 47 48if "query_key_value" in name : 49n_head = self .find_hparam (["num_attention_heads" ,"n_head" ]) 50n_head_kv = self .find_hparam (["num_kv_heads" ,"n_head_kv" ],optional = True )or 1 51head_dim = self .hparams ["hidden_size" ]// n_head 52 53qkv = data_torch .view (n_head_kv ,n_head // n_head_kv + 2 ,head_dim ,head_dim * n_head ) 54q = qkv [:, :- 2 ].reshape (n_head * head_dim ,head_dim * n_head ) 55k = qkv [:, [- 2 ]].reshape (n_head_kv * head_dim ,head_dim * n_head ) 56v = qkv [:, [- 1 ]].reshape (n_head_kv * head_dim ,head_dim * n_head ) 57data_torch = torch .cat ((q ,k ,v )).reshape_as (data_torch ) 58 59yield from super ().modify_tensors (data_torch ,name ,bid )