CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 4d agoView on Hugging Face
0likes1.1kdownloads
youtuvl.py66 linesDownload Raw Back to conversion
1from __future__ import annotations2 3from typing import Callable, Iterable, TYPE_CHECKING4 5if TYPE_CHECKING:6    from torch import Tensor7 8from .base import MmprojModel, ModelBase, gguf, logger9 10 11@ModelBase.register("YoutuVLForConditionalGeneration")12@ModelBase.example("tencent/Youtu-VL-4B-Instruct")13class YoutuVLVisionModel(MmprojModel):14    def __init__(self, *args, **kwargs):15        super().__init__(*args, **kwargs)16        assert self.hparams_vision is not None17        self.hparams_vision["image_size"] = self.hparams_vision.get("image_size", 560)18 19    def set_gguf_parameters(self):20        super().set_gguf_parameters()21 22        self.gguf_writer.add_clip_projector_type(gguf.VisionProjectorType.YOUTUVL)23        self.gguf_writer.add_vision_attention_layernorm_eps(self.hparams.get("layer_norm_eps", 1e-6))24 25        # Handle activation function26        hidden_act = str(self.hparams.get("hidden_act", "gelu_pytorch_tanh")).lower()27        if hidden_act in ("gelu", "gelu_pytorch_tanh", "gelu_fast", "gelu_new", "gelu_accurate"):28            self.gguf_writer.add_vision_use_gelu(True)29        elif hidden_act == "silu":30            self.gguf_writer.add_vision_use_silu(True)31        else:32            raise ValueError(f"Unsupported activation function for YOUTUVL: {hidden_act}")33 34        self.gguf_writer.add_vision_spatial_merge_size(self.hparams.get("spatial_merge_size", 2))35 36        window_size = self.hparams.get("window_size")37        if window_size is not None:38            self.gguf_writer.add_vision_window_size(window_size)39        # fullatt_block_indexes contains explicit layer indices that use full attention40        # e.g., [2, 5, 8, 11] means layers 2, 5, 8, 11 use full attention41        # All other layers use window attention42        fullatt_block_indexes = self.hparams.get("fullatt_block_indexes")43        assert fullatt_block_indexes is not None, "fullatt_block_indexes is required for youtuvl"44        # Store the explicit layer indices for YoutuVL (irregular pattern approach)45        self.gguf_writer.add_vision_wa_layer_indexes(layers=fullatt_block_indexes)46 47    @classmethod48    def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:49        name, gen = item50 51        # Skip language model tensors52        skip_prefixes = ('lm_head.', 'model.layers.', 'model.embed_tokens.', 'model.norm.')53        if name.startswith(skip_prefixes):54            return None55 56        return super().filter_tensors(item)57 58    def 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)60        try:61            yield from super().modify_tensors(data_torch, name, bid)62        except ValueError:63            # If mapping fails, log warning and skip64            logger.warning(f"Cannot map tensor: {name}")65            return66