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 9 10 11@ ModelBase . register ( "DotsOCRForCausalLM" ) 12@ ModelBase . example ( "rednote-hilab/dots.ocr" ) 13class DotsOCRVisionModel (MmprojModel ): 14def __init__ (self ,* args ,** kwargs ): 15super ().__init__ (* args ,** kwargs ) 16assert self .hparams_vision is not None 17self .hparams_vision ["image_size" ]= 0 # dynamic resolution 18 19def set_gguf_parameters (self ): 20super ().set_gguf_parameters () 21self .gguf_writer .add_clip_projector_type (gguf .VisionProjectorType .DOTSOCR ) 22self .gguf_writer .add_vision_min_pixels (self .preprocessor_config ["min_pixels" ]) 23self .gguf_writer .add_vision_max_pixels (self .preprocessor_config ["max_pixels" ]) 24self .gguf_writer .add_vision_attention_layernorm_eps (self .find_vparam (["rms_norm_eps" ])) 25self .gguf_writer .add_vision_projector_scale_factor (self .find_vparam (["spatial_merge_size" ])) 26self .gguf_writer .add_vision_use_silu (True ) 27 28@classmethod 29def filter_tensors (cls ,item :tuple [str ,Callable [[],Tensor ]])-> tuple [str ,Callable [[],Tensor ]]| None : 30name ,gen = item 31 32if not name .startswith ("vision_tower." ): 33return None 34 35if "vision_tower.blocks." in name and ".mlp." in name : 36# note: to avoid naming conflicts in tensor_mapping.py, we need to handle FFN renaming here 37# x = F.silu(self.fc1(x)) * self.fc3(x) 38# x = self.fc2(x) 39# fc1 -> gate, fc2 -> down, fc3 -> up 40# mapping original names to Qwen2.5 naming scheme 41name = name .replace ("vision_tower.blocks." ,"visual.blocks." ) 42name = name .replace (".fc1" ,".gate_proj" ) 43name = name .replace (".fc2" ,".down_proj" ) 44name = name .replace (".fc3" ,".up_proj" ) 45 46return super ().filter_tensors ((name ,gen )) 47 48def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 49yield from super ().modify_tensors (data_torch ,name ,bid )