Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

31.1 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Tests for :mod:`utils.models.gguf_metadata`. Synthesise small GGUF headers
in tmp dirs so we never depend on real model files."""

from __future__ import annotations

import struct
from pathlib import Path
from typing import Iterable, Mapping
from unittest.mock import patch


import pytest

from utils.models.gguf_metadata import (
    is_gguf_embedding_architecture,
    is_gguf_embedding_model,
    is_mmproj_by_metadata,
    mmproj_accepts_image,
    pairing_score,
    read_gguf_architecture,
    read_gguf_context_length,
    read_gguf_general_metadata,
    read_gguf_nextn_predict_layers,
    read_gguf_staged_dims,
    read_mmproj_audio_capability,
)


_GGUF_MAGIC = 0x46554747
_VTYPE_STRING = 8
_VTYPE_UINT32 = 4
_VTYPE_UINT64 = 10
_VTYPE_ARRAY = 9
_VTYPE_BOOL = 7


def _enc_string(s: str) -> bytes:
    b = s.encode("utf-8")
    return struct.pack("<Q", len(b)) + b


def _enc_kv_string(key: str, value: str) -> bytes:
    return _enc_string(key) + struct.pack("<I", _VTYPE_STRING) + _enc_string(value)


def _enc_kv_uint32(key: str, value: int) -> bytes:
    return _enc_string(key) + struct.pack("<I", _VTYPE_UINT32) + struct.pack("<I", value)


def _enc_kv_uint64(key: str, value: int) -> bytes:
    return _enc_string(key) + struct.pack("<I", _VTYPE_UINT64) + struct.pack("<Q", value)


def _enc_kv_bool(key: str, value: bool) -> bytes:
    return _enc_string(key) + struct.pack("<I", _VTYPE_BOOL) + struct.pack("<B", 1 if value else 0)


def _enc_kv_string_array(key: str, values: Iterable[str]) -> bytes:
    vals = list(values)
    out = _enc_string(key) + struct.pack("<I", _VTYPE_ARRAY)
    out += struct.pack("<I", _VTYPE_STRING) + struct.pack("<Q", len(vals))
    for v in vals:
        out += _enc_string(v)
    return out


def _write_synthetic_gguf(
    path: Path,
    general_strings: Mapping[str, str],
    *,
    tensor_names: Iterable[str] | None = None,
    extra_uint32: Mapping[str, int] | None = None,
    extra_uint64: Mapping[str, int] | None = None,
    extra_string_arrays: Mapping[str, Iterable[str]] | None = None,
    extra_bools: Mapping[str, bool] | None = None,
) -> Path:
    """Minimal GGUF header with optional tensor-table entries and no tensor data."""
    tensor_names = list(tensor_names or ())
    extra_uint32 = extra_uint32 or {}
    extra_uint64 = extra_uint64 or {}
    extra_string_arrays = extra_string_arrays or {}
    extra_bools = extra_bools or {}
    kv_count = (
        len(general_strings)
        + len(extra_uint32)
        + len(extra_uint64)
        + len(extra_string_arrays)
        + len(extra_bools)
    )
    body = b""
    for k, v in general_strings.items():
        body += _enc_kv_string(k, v)
    for k, v in extra_uint32.items():
        body += _enc_kv_uint32(k, v)
    for k, v in extra_uint64.items():
        body += _enc_kv_uint64(k, v)
    for k, v in extra_string_arrays.items():
        body += _enc_kv_string_array(k, v)
    for k, v in extra_bools.items():
        body += _enc_kv_bool(k, v)
    tensor_info = b""
    for name in tensor_names:
        tensor_info += _enc_string(name)
        tensor_info += struct.pack("<I", 1)  # n_dimensions
        tensor_info += struct.pack("<Q", 1)  # dimensions
        tensor_info += struct.pack("<I", 0)  # GGML_TYPE_F32
        tensor_info += struct.pack("<Q", 0)  # data offset
    header = struct.pack(
        "<IIQQ",
        _GGUF_MAGIC,
        3,  # version
        len(tensor_names),
        kv_count,
    )
    path.parent.mkdir(parents = True, exist_ok = True)
    path.write_bytes(header + body + tensor_info)
    return path


def test_returns_none_for_missing_file(tmp_path: Path):
    assert read_gguf_general_metadata(str(tmp_path / "nope.gguf")) is None


def test_returns_none_for_non_gguf(tmp_path: Path):
    p = tmp_path / "garbage.gguf"
    p.write_bytes(b"not a gguf file at all, just bytes")
    assert read_gguf_general_metadata(str(p)) is None


def test_context_length_none_for_missing_file(tmp_path: Path):
    assert read_gguf_context_length(str(tmp_path / "nope.gguf")) is None


def test_context_length_none_for_non_gguf(tmp_path: Path):
    p = tmp_path / "garbage.gguf"
    p.write_bytes(b"not a gguf file at all, just bytes")
    assert read_gguf_context_length(str(p)) is None


def test_context_length_read_from_arch_namespaced_key(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.architecture": "llama"},
        extra_uint32 = {"llama.context_length": 4096, "llama.block_count": 32},
    )
    assert read_gguf_context_length(str(p)) == 4096


def test_context_length_none_when_absent(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.architecture": "llama"},
        extra_uint32 = {"llama.block_count": 32},
    )
    assert read_gguf_context_length(str(p)) is None


def test_context_length_ignores_foreign_arch_key(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.architecture": "llama"},
        extra_uint32 = {"qwen2.context_length": 8192},
    )
    assert read_gguf_context_length(str(p)) is None


def test_nextn_predict_layers_uses_the_active_architecture_namespace(tmp_path: Path, monkeypatch):
    embedded = _write_synthetic_gguf(
        tmp_path / "embedded.gguf",
        {"general.architecture": "qwen35"},
        extra_uint32 = {
            "qwen35.nextn_predict_layers": 1,
            "qwen3.nextn_predict_layers": 9,
        },
    )
    headless = _write_synthetic_gguf(
        tmp_path / "headless.gguf",
        {"general.architecture": "qwen35"},
        extra_uint64 = {"qwen35.nextn_predict_layers": 0},
    )
    absent = _write_synthetic_gguf(
        tmp_path / "absent.gguf",
        {"general.architecture": "qwen35"},
        extra_uint32 = {"qwen3.nextn_predict_layers": 7},
    )
    malformed = tmp_path / "malformed.gguf"
    malformed.write_bytes(b"not a GGUF")

    reversed_order = tmp_path / "reversed.gguf"
    reversed_body = _enc_kv_uint32("qwen35.nextn_predict_layers", 2) + _enc_kv_string(
        "general.architecture", "qwen35"
    )
    reversed_order.write_bytes(struct.pack("<IIQQ", _GGUF_MAGIC, 3, 0, 2) + reversed_body)

    assert read_gguf_nextn_predict_layers(str(embedded)) == 1
    assert read_gguf_nextn_predict_layers(str(headless)) == 0
    assert read_gguf_nextn_predict_layers(str(absent)) is None
    assert read_gguf_nextn_predict_layers(str(malformed)) is None

    assert read_gguf_nextn_predict_layers(str(reversed_order)) == 2

    import utils.models.gguf_metadata as metadata

    monkeypatch.setattr(
        metadata,
        "_parse_gguf_arch_uints",
        lambda *_args, **_kwargs: pytest.fail("cached NextN metadata was reparsed"),
    )
    assert read_gguf_nextn_predict_layers(str(embedded)) == 1


def test_staged_dims_none_for_missing_or_non_gguf(tmp_path: Path):
    assert read_gguf_staged_dims(str(tmp_path / "nope.gguf")) is None
    p = tmp_path / "garbage.gguf"
    p.write_bytes(b"not a gguf at all")
    assert read_gguf_staged_dims(str(p)) is None


def test_staged_dims_moe_with_leading_dense(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "glm.gguf",
        {"general.architecture": "deepseek2"},
        extra_uint32 = {
            "deepseek2.context_length": 202752,
            "deepseek2.block_count": 47,
            "deepseek2.expert_count": 64,
            "deepseek2.leading_dense_block_count": 1,
        },
    )
    assert read_gguf_staged_dims(str(p)) == {
        "context_length": 202752,
        "layer_count": 47,
        "moe_layer_count": 46,
    }


def test_staged_dims_dense_model(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "dense.gguf",
        {"general.architecture": "qwen3"},
        extra_uint32 = {"qwen3.context_length": 40960, "qwen3.block_count": 36},
    )
    assert read_gguf_staged_dims(str(p)) == {
        "context_length": 40960,
        "layer_count": 36,
        "moe_layer_count": 0,
    }


def test_staged_dims_all_moe_no_leading_dense(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "moe.gguf",
        {"general.architecture": "qwen35moe"},
        extra_uint32 = {"qwen35moe.block_count": 40, "qwen35moe.expert_count": 256},
    )
    assert read_gguf_staged_dims(str(p)) == {
        "context_length": None,
        "layer_count": 40,
        "moe_layer_count": 40,
    }


def test_staged_dims_uint64_block_count(tmp_path: Path):
    # block_count stored as uint64 (vtype 10) still parses; moe equals block_count
    p = _write_synthetic_gguf(
        tmp_path / "moe64.gguf",
        {"general.architecture": "gpt-oss"},
        extra_uint32 = {"gpt-oss.expert_count": 32},
        extra_uint64 = {"gpt-oss.block_count": 24},
    )
    assert read_gguf_staged_dims(str(p)) == {
        "context_length": None,
        "layer_count": 24,
        "moe_layer_count": 24,
    }


def test_context_length_read_from_uint64(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.architecture": "qwen3"},
        extra_uint64 = {"qwen3.context_length": 262144},
    )
    assert read_gguf_context_length(str(p)) == 262144


def test_context_length_zero_treated_as_absent(tmp_path: Path):
    # Zero/garbage ceiling reads as None so the UI never builds a slider with max < min.
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.architecture": "llama"},
        extra_uint32 = {"llama.context_length": 0},
    )
    assert read_gguf_context_length(str(p)) is None


def test_extracts_general_string_fields(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {
            "general.architecture": "qwen2vl",
            "general.type": "model",
            "general.basename": "Qwen3.5",
            "general.organization": "Qwen",
            "general.base_model.0.repo_url": "https://huggingface.co/Qwen/Qwen3.5-9B",
            "general.base_model.0.name": "Qwen3.5 9B",
            "general.base_model.0.organization": "Qwen",
        },
    )
    meta = read_gguf_general_metadata(str(p))
    assert meta is not None
    assert meta["general.architecture"] == "qwen2vl"
    assert meta["general.basename"] == "Qwen3.5"
    assert meta["general.base_model.0.repo_url"] == "https://huggingface.co/Qwen/Qwen3.5-9B"


def test_skips_unrelated_fields_without_breaking(tmp_path: Path):
    """Skip unwanted arrays and uint32s without losing position."""
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.basename": "Foo"},
        extra_uint32 = {"qwen2vl.context_length": 32768},
        extra_string_arrays = {"tokenizer.ggml.tokens": ["a", "bc", "def"]},
    )
    meta = read_gguf_general_metadata(str(p))
    assert meta == {"general.basename": "Foo"}


def test_metadata_is_cached(tmp_path: Path):
    """Cache invalidates on size change."""
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.basename": "First"},
    )
    first = read_gguf_general_metadata(str(p))
    assert first == {"general.basename": "First"}
    # Change size so the (path, mtime, size) cache key invalidates.
    _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.basename": "Second", "general.organization": "X"},
    )
    second = read_gguf_general_metadata(str(p))
    assert second == {"general.basename": "Second", "general.organization": "X"}


def test_is_mmproj_by_metadata_signals():
    assert is_mmproj_by_metadata({"general.type": "mmproj"}) is True
    assert is_mmproj_by_metadata({"general.type": "MMProj"}) is True
    assert is_mmproj_by_metadata({"general.type": "model"}) is False
    assert is_mmproj_by_metadata({"general.basename": "foo"}) is None
    assert is_mmproj_by_metadata({}) is None
    assert is_mmproj_by_metadata(None) is None
    # ggml-org's SmolVLM projector uses general.type 'clip-vision'.
    assert is_mmproj_by_metadata({"general.architecture": "clip", "general.type": "clip-vision"})
    assert (
        is_mmproj_by_metadata({"general.architecture": "llama", "general.type": "model"}) is False
    )


# --- pairing_score -----------------------------------------------------


def _md(
    url = None,
    basename = None,
    org = None,
):
    keys = (
        "general.base_model.0.repo_url",
        "general.basename",
        "general.base_model.0.organization",
    )
    return {k: v for k, v in zip(keys, (url, basename, org)) if v is not None}


# 100: same base repo, 90: derivative repo with matching basename, 80/60: basename fallbacks, -1: mismatch.
@pytest.mark.parametrize(
    "weight, mmproj, expected",
    [
        pytest.param(
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B"),
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B"),
            100,
            id = "base_model_url_match",
        ),
        pytest.param(
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B"),
            _md(url = "https://huggingface.co/google/gemma-3-9B"),
            -1,
            id = "base_model_url_mismatch",
        ),
        pytest.param(
            _md(
                url = "https://huggingface.co/lmstudio-community/gemma-4-26B-A4B-it-GGUF",
                basename = "gemma-4-26B-A4B-it",
            ),
            _md(
                url = "https://huggingface.co/google/gemma-4-26B-A4B-it",
                basename = "gemma-4-26B-A4B-it",
            ),
            90,
            id = "base_model_url_derivative_repack_match",
        ),
        pytest.param(
            _md(
                url = "https://huggingface.co/vendor/gemma-4-26B-A4B-it-qat-q4_0-uncensored-heretic",
                basename = "gemma-4-26B-A4B-it",
            ),
            _md(
                url = "https://huggingface.co/google/gemma-4-26B-A4B-it",
                basename = "gemma-4-26B-A4B-it",
            ),
            90,
            id = "base_model_url_derivative_quant_match",
        ),
        pytest.param(
            _md(url = "https://github.com/acme/Model-VL-GGUF", basename = "Model-VL"),
            _md(url = "https://gitlab.example.com/acme/Model-VL", basename = "Model-VL"),
            90,
            id = "derivative_url_handles_non_hf_hosts",
        ),
        pytest.param(
            _md(url = "https://gitlab.example.com/acme/models/Model-VL-GGUF", basename = "Model-VL"),
            _md(url = "https://gitlab.example.com/acme/models/Model-VL", basename = "Model-VL"),
            90,
            id = "derivative_url_handles_nested_namespaces",
        ),
        pytest.param(
            _md(url = "acme/Model-VL-GGUF", basename = "Model-VL"),
            _md(url = "acme/Model-VL", basename = "Model-VL"),
            90,
            id = "derivative_url_handles_bare_repo_ids",
        ),
        pytest.param(
            _md(url = "acme.ai/Model-GGUF", basename = "Model"),
            _md(url = "acme.ai/Model", basename = "Model"),
            90,
            id = "derivative_url_handles_dotted_bare_owner",
        ),
        pytest.param(
            _md(
                url = "https://huggingface.co/google/gemma-4-26B-A4B-it",
                basename = "gemma-4-26B-A4B-it",
            ),
            _md(url = "https://huggingface.co/google/gemma-4-26B", basename = "gemma-4-26B"),
            -1,
            id = "derivative_url_rejects_basename_mismatch",
        ),
        pytest.param(
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B/"),
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B"),
            100,
            id = "base_model_url_trailing_slash_normalised",
        ),
        pytest.param(
            _md(url = "http://huggingface.co/Qwen/Qwen3.5-9B.GIT"),
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B"),
            100,
            id = "base_model_url_scheme_and_git_normalised",
        ),
        pytest.param(
            _md(url = "https://huggingface.co/acme/Model"),
            _md(url = "acme/Model"),
            100,
            id = "hosted_and_bare_repo_ids_match",
        ),
        pytest.param(
            _md(url = "https://github.com/acme/Model"),
            _md(url = "acme/Model"),
            -1,
            id = "non_hf_url_and_bare_repo_ids_do_not_match",
        ),
        pytest.param(
            _md(url = "https://huggingface.co/vendor/model-v2-GGUF"),
            _md(url = "https://huggingface.co/vendor/model"),
            -1,
            id = "derivative_url_requires_basename_evidence",
        ),
        pytest.param(
            _md(url = "https://huggingface.co/Qwen/Qwen3.5-9B", basename = "Qwen3.5"),
            _md(url = "https://huggingface.co/Qwen/Qwen3.5", basename = "Qwen3.5"),
            -1,
            id = "rejects_arbitrary_slug_prefix",
        ),
        pytest.param(
            _md(url = "org/ModelGGUF", basename = "Model"),
            _md(url = "other/Model", basename = "Model"),
            -1,
            id = "rejects_qualifier_without_separator",
        ),
        pytest.param(
            _md(url = "https://git.example.com/Org/Model", basename = "Model"),
            _md(url = "https://git.example.com/org/model", basename = "Model"),
            -1,
            id = "preserves_case_sensitive_repo_paths",
        ),
        pytest.param(
            _md(url = "Org/Model", basename = "Model"),
            _md(url = "org/Model", basename = "Model"),
            -1,
            id = "preserves_case_sensitive_bare_repo_ids",
        ),
        pytest.param(
            _md(url = "https://huggingface.co/org/model", basename = "model"),
            _md(url = "https://huggingface.co/other/model-ft", basename = "model"),
            -1,
            id = "rejects_derivative_projector_for_base_weight",
        ),
        pytest.param(
            _md(basename = "Nanonets-Ocr-S", org = "Nanonets"),
            _md(basename = "Nanonets-Ocr-S", org = "Nanonets"),
            80,
            id = "basename_plus_org_fallback",
        ),
    ],
)
def test_pairing_score(weight, mmproj, expected):
    assert pairing_score(weight, mmproj) == expected


@pytest.mark.parametrize(
    "left,right",
    [
        (
            "https://huggingface.co/google/gemma-4-12B-it",
            "https://huggingface.co/google/gemma-4-12b-it",
        ),
        (
            "HTTPS://HUGGINGFACE.CO/Google/Gemma-4-12B-it.git/",
            "http://huggingface.co/google/gemma-4-12b-it",
        ),
        ("https://huggingface.co/Google/Gemma-4-12B-it", "google/gemma-4-12b-it"),
        ("Google/Gemma-4-12B-it", "https://huggingface.co/google/gemma-4-12b-it"),
    ],
)
def test_pairing_score_hf_repo_case_insensitive(left, right):
    key = "general.base_model.0.repo_url"
    assert pairing_score({key: left}, {key: right}) == 100


@pytest.mark.parametrize("host", ["huggingface.co", "huggingface.co.example.com"])
def test_pairing_score_rejects_distinct_hf_repositories(host):
    key = "general.base_model.0.repo_url"
    assert (
        pairing_score(
            {key: f"https://{host}/google/gemma-4-12B-it"},
            {key: f"https://{host}/google/gemma-4-27b-it"},
        )
        == -1
    )


def test_pairing_score_preserves_case_on_hf_lookalike_host():
    key = "general.base_model.0.repo_url"
    assert (
        pairing_score(
            {key: "https://huggingface.co.example.com/Google/Model"},
            {key: "https://huggingface.co.example.com/google/model"},
        )
        == -1
    )


def test_pairing_score_preserves_case_in_hf_file_paths():
    key = "general.base_model.0.repo_url"
    assert (
        pairing_score(
            {key: "https://huggingface.co/google/model/blob/main/Config.json"},
            {key: "https://huggingface.co/google/model/blob/main/config.json"},
        )
        == -1
    )


def test_pairing_score_basename_only_fallback():
    assert (
        pairing_score(
            {"general.basename": "Nanonets-Ocr-S"},
            {"general.basename": "Nanonets-Ocr-S"},
        )
        == 60
    )


def test_pairing_score_no_overlap_returns_zero():
    """One side empty: scorer punts to filename fallback."""
    assert pairing_score({"general.basename": "Foo"}, {}) == 0
    assert pairing_score({}, {"general.basename": "Foo"}) == 0
    assert pairing_score(None, {"general.basename": "Foo"}) == 0


def test_mmproj_audio_capability_true(tmp_path: Path):
    """clip.has_audio_encoder=True (e.g. Gemma 4's gemma4ua projector)."""
    p = _write_synthetic_gguf(
        tmp_path / "mmproj.gguf",
        {"general.type": "mmproj"},
        extra_bools = {
            "clip.has_vision_encoder": True,
            "clip.has_audio_encoder": True,
        },
    )
    assert read_mmproj_audio_capability(str(p)) is True


def test_mmproj_audio_capability_false(tmp_path: Path):
    """Vision-only projector: key present but false."""
    p = _write_synthetic_gguf(
        tmp_path / "mmproj.gguf",
        {"general.type": "mmproj"},
        extra_bools = {
            "clip.has_vision_encoder": True,
            "clip.has_audio_encoder": False,
        },
    )
    assert read_mmproj_audio_capability(str(p)) is False


def test_mmproj_audio_capability_absent_returns_none(tmp_path: Path):
    """Key absent (older/vision-only mmproj): None, not False."""
    p = _write_synthetic_gguf(
        tmp_path / "mmproj.gguf",
        {"general.type": "mmproj"},
        extra_bools = {"clip.has_vision_encoder": True},
    )
    assert read_mmproj_audio_capability(str(p)) is None


def test_mmproj_audio_capability_missing_or_non_gguf(tmp_path: Path):
    assert read_mmproj_audio_capability(str(tmp_path / "nope.gguf")) is None
    junk = tmp_path / "garbage.gguf"
    junk.write_bytes(b"not a gguf header at all")
    assert read_mmproj_audio_capability(str(junk)) is None


class _CountingFile:
    """A file handle that records how many reads and seeks a parser performs on it."""

    def __init__(self, handle) -> None:
        self._handle = handle
        self.operations = 0
        self.furthest = 0

    def read(self, size = -1):
        self.operations += 1
        data = self._handle.read(size)
        self.furthest = max(self.furthest, self._handle.tell())
        return data

    def seek(
        self,
        offset,
        whence = 0,
    ):
        self.operations += 1
        pos = self._handle.seek(offset, whence)
        self.furthest = max(self.furthest, pos)
        return pos

    def __enter__(self):
        return self

    def __exit__(self, *exc_info):
        return self._handle.__exit__(*exc_info)


def _count_header_operations(
    monkeypatch,
    path: Path,
    read,
    measure = "operations",
) -> int:
    """File operations ``read`` performs against ``path`` (or, with ``measure="furthest"``, the
    furthest offset it reached). Other files are untouched."""
    import builtins

    real_open = builtins.open
    counters: list[_CountingFile] = []

    def counting_open(file, *args, **kwargs):
        handle = real_open(file, *args, **kwargs)
        if str(file) != str(path):
            return handle
        counter = _CountingFile(handle)
        counters.append(counter)
        return counter

    monkeypatch.setattr(builtins, "open", counting_open)
    try:
        read(str(path))
    finally:
        monkeypatch.undo()
    if measure == "furthest":
        return max(counter.furthest for counter in counters)
    return sum(counter.operations for counter in counters)


def test_architecture_read_stops_before_a_large_tokenizer_array(tmp_path: Path, monkeypatch):
    """Reading one key must not scan a large tokenizer array."""
    p = _write_synthetic_gguf(
        tmp_path / "model.gguf",
        {"general.architecture": "llama", "general.name": "Test"},
        extra_string_arrays = {"tokenizer.ggml.tokens": [f"tok{i}" for i in range(20000)]},
    )

    assert read_gguf_architecture(str(p)) == "llama"
    assert read_gguf_general_metadata(str(p)) == {
        "general.architecture": "llama",
        "general.name": "Test",
    }

    # Separate paths avoid the readers' file-stat caches.
    targeted = tmp_path / "targeted.gguf"
    targeted.write_bytes(p.read_bytes())
    whole = tmp_path / "whole.gguf"
    whole.write_bytes(p.read_bytes())

    targeted_ops = _count_header_operations(monkeypatch, targeted, read_gguf_architecture)
    reach = tmp_path / "reach.gguf"
    reach.write_bytes(p.read_bytes())
    targeted_reach = _count_header_operations(
        monkeypatch, reach, read_gguf_architecture, measure = "furthest"
    )
    whole_reach = _count_header_operations(
        monkeypatch, whole, read_gguf_general_metadata, measure = "furthest"
    )
    assert targeted_ops < 10, targeted_ops
    # The whole-header read walks the 20000-entry array (one buffered pass); the targeted one stops before it.
    assert targeted_reach < 1024, targeted_reach
    assert whole_reach > 20000 * 8, whole_reach


def test_architecture_is_stripped_and_absent_values_are_none(tmp_path: Path):
    stripped = _write_synthetic_gguf(
        tmp_path / "padded.gguf", {"general.architecture": "  ltxv \n"}
    )
    assert read_gguf_architecture(str(stripped)) == "ltxv"

    blank = _write_synthetic_gguf(tmp_path / "blank.gguf", {"general.architecture": "   "})
    assert read_gguf_architecture(str(blank)) is None

    empty = _write_synthetic_gguf(tmp_path / "empty.gguf", {})
    assert read_gguf_architecture(str(empty)) is None

    assert read_gguf_architecture(str(tmp_path / "nope.gguf")) is None
    junk = tmp_path / "garbage.gguf"
    junk.write_bytes(b"not a gguf file at all, just bytes")
    assert read_gguf_architecture(str(junk)) is None


def test_architecture_matches_the_general_metadata_reader(tmp_path: Path):
    """The targeted and general readers must return the same architecture."""
    for index, arch in enumerate(("llama", "flux2", "ltxv", "dflash", "qwen3vl")):
        p = _write_synthetic_gguf(
            tmp_path / f"model{index}.gguf",
            {"general.architecture": arch, "general.name": "n", "general.type": "model"},
            extra_uint32 = {f"{arch}.block_count": 32},
        )
        expected = (read_gguf_general_metadata(str(p)) or {}).get("general.architecture")
        assert read_gguf_architecture(str(p)) == expected == arch


def _projector(tmp_path: Path, **bools) -> str:
    return str(
        _write_synthetic_gguf(
            tmp_path / "mmproj.gguf", {"general.type": "mmproj"}, extra_bools = bools
        )
    )


def test_accepts_image_when_vision_declared(tmp_path: Path):
    """A projector declaring vision accepts images, whatever it says about audio."""
    assert mmproj_accepts_image(_projector(tmp_path, **{"clip.has_vision_encoder": True})) is True


def test_audio_only_projector_is_not_a_vision_tower(tmp_path: Path):
    """ultravox / Voxtral / Qwen3-ASR: audio declared, vision key absent."""
    assert mmproj_accepts_image(_projector(tmp_path, **{"clip.has_audio_encoder": True})) is False


def test_dual_projector_still_accepts_images(tmp_path: Path):
    """Qwen2.5-Omni declares both, which is what makes a lone audio claim evidence."""
    p = _projector(tmp_path, **{"clip.has_vision_encoder": True, "clip.has_audio_encoder": True})
    assert mmproj_accepts_image(p) is True


def test_projector_declaring_neither_stays_image_capable(tmp_path: Path):
    """An unreadable or older convert must not be refused: the load decides."""
    assert mmproj_accepts_image(_projector(tmp_path)) is True
    assert mmproj_accepts_image(str(tmp_path / "nope.gguf")) is True


def test_declared_audio_false_is_not_an_audio_claim(tmp_path: Path):
    """A vision projector writing audio=False is still a vision tower."""
    p = _projector(tmp_path, **{"clip.has_vision_encoder": True, "clip.has_audio_encoder": False})
    assert mmproj_accepts_image(p) is True


def test_is_gguf_embedding_architecture_recognises_encoder_arches():
    assert is_gguf_embedding_architecture("nomic-bert")
    assert is_gguf_embedding_architecture("NOMIC-BERT-MOE")
    assert not is_gguf_embedding_architecture("bert")
    assert not is_gguf_embedding_architecture("llama")
    assert not is_gguf_embedding_architecture(None)


def test_is_gguf_embedding_model_from_architecture(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "nomic.gguf",
        {"general.architecture": "nomic-bert"},
    )
    assert is_gguf_embedding_model(str(p)) is True


def test_is_gguf_embedding_model_from_name_hint(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "Qwen3-Embedding-4B-Q4_K_M.gguf",
        {"general.architecture": "qwen3"},
    )
    with patch("utils.models.model_config.is_embedding_model") as remote_classifier:
        assert (
            is_gguf_embedding_model(str(p), model_identifier = "unsloth/Qwen3-Embedding-4B") is True
        )
    remote_classifier.assert_not_called()


def test_is_gguf_embedding_model_ignores_owner_name_hint(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "Llama-3-Q4_K_M.gguf",
        {"general.architecture": "llama"},
    )
    with patch("utils.models.model_config.is_embedding_model") as remote_classifier:
        assert (
            is_gguf_embedding_model(str(p), model_identifier = "embedding-lab/Llama-3-GGUF") is False
        )
    remote_classifier.assert_not_called()


def test_is_gguf_embedding_model_from_intrinsic_name_hints(tmp_path: Path):
    for index, (key, value) in enumerate(
        (
            ("general.name", "Qwen3 Embedding 4B"),
            ("general.basename", "Qwen3-Embedding"),
        )
    ):
        p = _write_synthetic_gguf(
            tmp_path / f"model-{index}.gguf",
            {"general.architecture": "qwen3", key: value},
        )
        assert is_gguf_embedding_model(str(p), model_identifier = "local/model") is True


def test_is_gguf_embedding_model_excludes_reranker_without_pooling(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "bge-reranker-v2-m3-Q4_K_M.gguf",
        {"general.architecture": "bert", "general.name": "Bge M3"},
    )
    assert (
        is_gguf_embedding_model(str(p), model_identifier = "gpustack/bge-reranker-v2-m3-GGUF")
        is False
    )


def test_is_gguf_embedding_model_rejects_generic_bert_without_pooling(tmp_path: Path):
    p = _write_synthetic_gguf(
        tmp_path / "bge-small-en-v1.5.gguf",
        {"general.architecture": "bert", "general.name": "Bge Small Encoder"},
    )
    # No classifier head rules out a reranker but cannot pick CLS vs MEAN pooling.
    assert is_gguf_embedding_model(str(p), model_identifier = "local/bge-small") is False


def test_is_gguf_embedding_model_excludes_unnamed_encoder_classifier_heads(tmp_path: Path):
    for index, classifier_tensor in enumerate(("cls.weight", "cls.output.weight")):
        p = _write_synthetic_gguf(
            tmp_path / f"model-{index}.gguf",
            {"general.architecture": "modern-bert", "general.name": "MS MARCO Encoder"},
            tensor_names = (classifier_tensor,),
        )
        assert is_gguf_embedding_model(str(p), model_identifier = "local/model") is False


def test_is_gguf_embedding_model_checks_every_split_for_classifier_head(tmp_path: Path):
    first = _write_synthetic_gguf(
        tmp_path / "model-00001-of-00002.gguf",
        {"general.architecture": "modern-bert"},
    )
    _write_synthetic_gguf(
        tmp_path / "model-00002-of-00002.gguf",
        {"general.architecture": "modern-bert"},
        tensor_names = ("cls.weight",),
    )
    assert is_gguf_embedding_model(str(first), model_identifier = "local/model") is False