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
1.7 KiB40 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("Llama4ForConditionalGeneration")
12# [TAG_HF_EXAMPLE_GATED] meta-llama/Llama-4-Scout-17B-16E-Instruct is gated
13@ModelBase.example("unsloth/Llama-4-Scout-17B-16E-Instruct")
14class Llama4VisionModel(MmprojModel):
15    def set_gguf_parameters(self):
16        super().set_gguf_parameters()
17        self.gguf_writer.add_clip_projector_type(gguf.VisionProjectorType.LLAMA4)
18        self.gguf_writer.add_vision_attention_layernorm_eps(self.hparams["norm_eps"])
19        self.gguf_writer.add_vision_projector_scale_factor(int(1.0 / self.hparams["pixel_shuffle_ratio"]))
20        assert self.hparams["hidden_act"] == "gelu"
21        self.gguf_writer.add_vision_use_gelu(True)
22
23    @classmethod
24    def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
25        name, gen = item
26
27        if "multi_modal_projector" not in name and "vision_model" not in name:
28            return None
29
30        if "positional_embedding_vlm" in name and ".weight" not in name:
31            name += ".weight"
32
33        return super().filter_tensors((name, gen))
34
35    def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
36        if "multi_modal_projector.linear_1" in name:
37            # despite the name with number postfix, this is a single fully connected layer
38            yield (gguf.TENSOR_NAMES[gguf.MODEL_TENSOR.V_MMPROJ_FC] + '.weight', data_torch)
39        else:
40            yield from super().modify_tensors(data_torch, name, bid)