char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
fa88ae936
char/aliceai-t5-moe
1from __future__import annotations 2 3import math 4 5from typing import Callable ,Iterable ,TYPE_CHECKING 6 7if TYPE_CHECKING : 8from torch import Tensor 9 10from .base import ModelBase ,TextModel ,gguf 11 12 13@ ModelBase . register ( "Jais2ForCausalLM" ) 14# [TAG_HF_EXAMPLE_GATED] inceptionai/Jais-2-8B-Chat is gated 15# [TAG_HF_EXAMPLE_MISSING] 16class Jais2Model (TextModel ): 17model_arch = gguf .MODEL_ARCH .JAIS2 18 19def set_gguf_parameters (self ): 20super ().set_gguf_parameters () 21hparams = self .hparams 22head_dim = hparams .get ("head_dim" ,hparams ["hidden_size" ]// hparams ["num_attention_heads" ]) 23self .gguf_writer .add_rope_dimension_count (head_dim ) 24 25 26@ ModelBase . register ( "JAISLMHeadModel" ) 27@ ModelBase . example ( "inceptionai/jais-family-590m" ) 28class JaisModel (TextModel ): 29model_arch = gguf .MODEL_ARCH .JAIS 30 31def __init__ (self ,* args ,** kwargs ): 32super ().__init__ (* args ,** kwargs ) 33 34# SwigLU activation 35assert self .hparams ["activation_function" ]== "swiglu" 36# ALiBi position embedding 37assert self .hparams ["position_embedding_type" ]== "alibi" 38 39# Embeddings scale 40self .embeddings_scale = 1.0 41if 'mup_embeddings_scale' in self .hparams : 42self .embeddings_scale = self .hparams ['mup_embeddings_scale' ] 43elif 'embeddings_scale' in self .hparams : 44self .embeddings_scale = self .hparams ['embeddings_scale' ] 45else : 46assert False 47 48self .width_scale = 1.0 49if 'mup_output_alpha' in self .hparams : 50assert 'mup_width_scale' in self .hparams 51self .width_scale = self .hparams ['mup_output_alpha' ]* self .hparams ['mup_width_scale' ] 52elif 'width_scale' in self .hparams : 53self .width_scale = self .hparams ['width_scale' ] 54else : 55assert False 56 57self .max_alibi_bias = 8.0 58 59def set_vocab (self ): 60self ._set_vocab_gpt2 () 61 62def set_gguf_parameters (self ): 63self .gguf_writer .add_block_count (self .block_count ) 64self .gguf_writer .add_context_length (self .hparams ["n_positions" ]) 65self .gguf_writer .add_embedding_length (self .hparams ["n_embd" ]) 66self .gguf_writer .add_feed_forward_length (self .hparams ["n_inner" ]) 67self .gguf_writer .add_head_count (self .hparams ["n_head" ]) 68self .gguf_writer .add_layer_norm_eps (self .hparams ["layer_norm_epsilon" ]) 69self .gguf_writer .add_file_type (self .ftype ) 70 71@classmethod 72def filter_tensors (cls ,item :tuple [str ,Callable [[],Tensor ]])-> tuple [str ,Callable [[],Tensor ]]| None : 73name ,gen = item 74 75# we don't need these 76if name .endswith ((".attn.bias" )): 77return None 78 79return super ().filter_tensors (item ) 80 81def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 82if name .endswith (("relative_pe.slopes" )): 83# Calculate max ALiBi bias (this is the inverse of the ALiBi calculation) 84# Some other models has max_alibi_bias spelled out explicitly in the hyperparams, 85# but Jais's PyTorch model simply precalculates the slope values and places them 86# in relative_pes.slopes 87n_head_closest_log2 = 2 ** math .floor (math .log2 (self .hparams ["n_head" ])) 88first_val = float (data_torch [0 ].item ()) 89self .max_alibi_bias = - round (math .log2 (first_val )* n_head_closest_log2 ) 90 91return 92 93if name .endswith ((".c_attn.weight" ,".c_proj.weight" ,".c_fc.weight" ,".c_fc2.weight" )): 94data_torch = data_torch .transpose (1 ,0 ) 95 96new_name = self .map_tensor_name (name ) 97 98if new_name == self .format_tensor_name (gguf .MODEL_TENSOR .TOKEN_EMBD ): 99yield from super ().modify_tensors (data_torch * self .embeddings_scale ,new_name ,bid ) 100elif new_name == self .format_tensor_name (gguf .MODEL_TENSOR .OUTPUT ): 101yield from super ().modify_tensors (data_torch * self .width_scale ,new_name ,bid ) 102else : 103yield from super ().modify_tensors (data_torch ,new_name ,bid ) 104 105def prepare_tensors (self ): 106super ().prepare_tensors () 107self .gguf_writer .add_max_alibi_bias (self .max_alibi_bias )