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 5if TYPE_CHECKING : 6from torch import Tensor 7 8from .base import ModelBase ,TextModel ,gguf 9 10 11@ ModelBase . register ( "GPTRefactForCausalLM" ) 12@ ModelBase . example ( "smallcloudai/Refact-1_6-base" ) 13class RefactModel (TextModel ): 14model_arch = gguf .MODEL_ARCH .REFACT 15 16def set_vocab (self ): 17super ().set_vocab () 18 19# TODO: how to determine special FIM tokens automatically? 20special_vocab = gguf .SpecialVocab (self .dir_model ,load_merges = False , 21special_token_types = ['prefix' ,'suffix' ,'middle' ,'eot' ]) 22special_vocab ._set_special_token ("prefix" ,1 ) 23special_vocab ._set_special_token ("suffix" ,3 ) 24special_vocab ._set_special_token ("middle" ,2 ) 25special_vocab .chat_template = None # do not add it twice 26special_vocab .add_to_gguf (self .gguf_writer ) 27 28def set_gguf_parameters (self ): 29hidden_dim = self .hparams ["n_embd" ] 30inner_dim = 4 * hidden_dim 31hidden_dim = int (2 * inner_dim / 3 ) 32multiple_of = 256 33ff_dim = multiple_of * ((hidden_dim + multiple_of - 1 )// multiple_of ) 34 35# refact uses Alibi. So this is from config.json which might be used by training. 36self .gguf_writer .add_context_length (self .hparams ["n_positions" ]) 37self .gguf_writer .add_embedding_length (self .hparams ["n_embd" ]) 38 39self .gguf_writer .add_feed_forward_length (ff_dim ) 40self .gguf_writer .add_block_count (self .block_count ) 41self .gguf_writer .add_head_count (self .hparams ["n_head" ]) 42self .gguf_writer .add_head_count_kv (1 ) 43self .gguf_writer .add_layer_norm_rms_eps (self .hparams ["layer_norm_epsilon" ]) 44self .gguf_writer .add_file_type (self .ftype ) 45 46def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 47hidden_dim = self .hparams ["n_embd" ] 48inner_dim = 4 * hidden_dim 49hidden_dim = int (2 * inner_dim / 3 ) 50multiple_of = 256 51ff_dim = multiple_of * ((hidden_dim + multiple_of - 1 )// multiple_of ) 52n_head = self .hparams ["n_head" ] 53n_head_kv = 1 54head_dim = self .hparams ["n_embd" ]// n_head 55 56if bid is not None : 57if name == f"transformer.h. { bid } .attn.kv.weight" : 58yield from super ().modify_tensors (data_torch [:n_head_kv * head_dim ],self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_K ,bid ),bid ) 59yield from super ().modify_tensors (data_torch [n_head_kv * head_dim :],self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_V ,bid ),bid ) 60return 61if name == f"transformer.h. { bid } .attn.q.weight" : 62yield from super ().modify_tensors (data_torch ,self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_Q ,bid ),bid ) 63return 64if name == f"transformer.h. { bid } .mlp.gate_up_proj.weight" : 65yield from super ().modify_tensors (data_torch [:ff_dim ],self .format_tensor_name (gguf .MODEL_TENSOR .FFN_GATE ,bid ),bid ) 66yield from super ().modify_tensors (data_torch [ff_dim :],self .format_tensor_name (gguf .MODEL_TENSOR .FFN_UP ,bid ),bid ) 67return 68 69yield from super ().modify_tensors (data_torch ,name ,bid )