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 Callable ,Iterable ,TYPE_CHECKING 4 5if TYPE_CHECKING : 6from torch import Tensor 7 8from .base import MmprojModel ,ModelBase ,gguf ,logger 9 10 11@ ModelBase . register ( "YoutuVLForConditionalGeneration" ) 12@ ModelBase . example ( "tencent/Youtu-VL-4B-Instruct" ) 13class YoutuVLVisionModel (MmprojModel ): 14def __init__ (self ,* args ,** kwargs ): 15super ().__init__ (* args ,** kwargs ) 16assert self .hparams_vision is not None 17self .hparams_vision ["image_size" ]= self .hparams_vision .get ("image_size" ,560 ) 18 19def set_gguf_parameters (self ): 20super ().set_gguf_parameters () 21 22self .gguf_writer .add_clip_projector_type (gguf .VisionProjectorType .YOUTUVL ) 23self .gguf_writer .add_vision_attention_layernorm_eps (self .hparams .get ("layer_norm_eps" ,1e-6 )) 24 25# Handle activation function 26hidden_act = str (self .hparams .get ("hidden_act" ,"gelu_pytorch_tanh" )).lower () 27if hidden_act in ("gelu" ,"gelu_pytorch_tanh" ,"gelu_fast" ,"gelu_new" ,"gelu_accurate" ): 28self .gguf_writer .add_vision_use_gelu (True ) 29elif hidden_act == "silu" : 30self .gguf_writer .add_vision_use_silu (True ) 31else : 32raise ValueError (f"Unsupported activation function for YOUTUVL: { hidden_act } " ) 33 34self .gguf_writer .add_vision_spatial_merge_size (self .hparams .get ("spatial_merge_size" ,2 )) 35 36window_size = self .hparams .get ("window_size" ) 37if window_size is not None : 38self .gguf_writer .add_vision_window_size (window_size ) 39# fullatt_block_indexes contains explicit layer indices that use full attention 40# e.g., [2, 5, 8, 11] means layers 2, 5, 8, 11 use full attention 41# All other layers use window attention 42fullatt_block_indexes = self .hparams .get ("fullatt_block_indexes" ) 43assert fullatt_block_indexes is not None ,"fullatt_block_indexes is required for youtuvl" 44# Store the explicit layer indices for YoutuVL (irregular pattern approach) 45self .gguf_writer .add_vision_wa_layer_indexes (layers = fullatt_block_indexes ) 46 47@classmethod 48def filter_tensors (cls ,item :tuple [str ,Callable [[],Tensor ]])-> tuple [str ,Callable [[],Tensor ]]| None : 49name ,gen = item 50 51# Skip language model tensors 52skip_prefixes = ('lm_head.' ,'model.layers.' ,'model.embed_tokens.' ,'model.norm.' ) 53if name .startswith (skip_prefixes ): 54return None 55 56return super ().filter_tensors (item ) 57 58def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 59# Try to map the tensor using TensorNameMap (handles vision encoder and projector) 60try : 61yield from super ().modify_tensors (data_torch ,name ,bid ) 62except ValueError : 63# If mapping fails, log warning and skip 64logger .warning (f"Cannot map tensor: { name } " ) 65return