char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenconvert: add @ModelBase.example (#27208)fa88ae936

char/aliceai-t5-moe
2.1 KiB49 linesraw
1from __future__ import annotations
2
3from typing import Callable, Iterable, TYPE_CHECKING
4
5if TYPE_CHECKING:
6    from 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):
14    def __init__(self, *args, **kwargs):
15        super().__init__(*args, **kwargs)
16        assert self.hparams_vision is not None
17        self.hparams_vision["image_size"] = 0 # dynamic resolution
18
19    def set_gguf_parameters(self):
20        super().set_gguf_parameters()
21        self.gguf_writer.add_clip_projector_type(gguf.VisionProjectorType.DOTSOCR)
22        self.gguf_writer.add_vision_min_pixels(self.preprocessor_config["min_pixels"])
23        self.gguf_writer.add_vision_max_pixels(self.preprocessor_config["max_pixels"])
24        self.gguf_writer.add_vision_attention_layernorm_eps(self.find_vparam(["rms_norm_eps"]))
25        self.gguf_writer.add_vision_projector_scale_factor(self.find_vparam(["spatial_merge_size"]))
26        self.gguf_writer.add_vision_use_silu(True)
27
28    @classmethod
29    def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
30        name, gen = item
31
32        if not name.startswith("vision_tower."):
33            return None
34
35        if "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
41            name = name.replace("vision_tower.blocks.", "visual.blocks.")
42            name = name.replace(".fc1", ".gate_proj")
43            name = name.replace(".fc2", ".down_proj")
44            name = name.replace(".fc3", ".up_proj")
45
46        return super().filter_tensors((name, gen))
47
48    def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
49        yield from super().modify_tensors(data_torch, name, bid)