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.

3.4 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

"""Saved per-model overrides must not yield a tensor_split ratio on load, so the upgrade is safe."""

from __future__ import annotations

import sys
from pathlib import Path

_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
if _BACKEND_DIR not in sys.path:
    sys.path.insert(0, _BACKEND_DIR)

import pytest  # noqa: E402

from utils.openai_auto_switch_settings import (  # noqa: E402
    model_override_load_kwargs,
    normalize_model_override,
)

LEGACY_OVERRIDE = {
    "gpu_memory_mode": "manual",
    "gpu_layers": 99,
    "n_cpu_moe": 4,
    "tensor_parallel": True,
    "gpu_ids": [0, 1],
    "gpu_index_kind": "cuda",
    "max_seq_length": 8192,
    "kv_cache_dtype": "q8_0",
    "n_parallel": 2,
    "llama_extra_args": ["--flash-attn", "on"],
}


def test_a_saved_setting_can_never_carry_a_tensor_split():
    """The allow-list is the whole attack surface: a stored override can never carry a ratio."""
    stored = normalize_model_override(dict(LEGACY_OVERRIDE))
    assert "tensor_split" not in stored


@pytest.mark.parametrize(
    "smuggled",
    [
        {"tensor_split": None},
        {"tensorSplit": [3, 1]},
        {"tensor_split": "3,1"},
    ],
)
def test_a_ratio_written_into_the_settings_row_by_hand_is_dropped(smuggled):
    """The sqlite row is editable JSON, so a hand-written ratio is dropped unless the allow-list changes."""
    stored = normalize_model_override({**LEGACY_OVERRIDE, **smuggled})
    assert "tensor_split" not in stored
    assert "tensorSplit" not in stored


@pytest.mark.parametrize("ids", [None, [0], [1, 0, 2], [0, 0]])
def test_a_saved_ratio_needs_the_exact_gpu_ids_it_was_written_for(ids):
    """Since #12774 a ratio is stored, but only beside the ordered pin it indexes."""
    row = {**LEGACY_OVERRIDE, "tensor_split": [3, 1]}
    assert normalize_model_override(row)["tensor_split"] == [3, 1]
    row["gpu_ids"] = ids
    assert "tensor_split" not in normalize_model_override(row)


@pytest.mark.parametrize("is_gguf", [True, False])
def test_the_replayed_load_asks_for_no_ratio(is_gguf):
    """The other end of the same path: what the replay actually sends."""
    stored = normalize_model_override(dict(LEGACY_OVERRIDE))
    kwargs = model_override_load_kwargs(stored, is_gguf = is_gguf)
    assert "tensor_split" not in kwargs


def test_the_replayed_load_is_unchanged_in_every_other_respect():
    """Pins the replayed load so a later edit cannot silently change what an existing install replays."""
    stored = normalize_model_override(dict(LEGACY_OVERRIDE))
    kwargs = model_override_load_kwargs(stored, is_gguf = True)

    assert kwargs["gpu_memory_mode"] == "manual"
    assert kwargs["gpu_layers"] == 99
    assert kwargs["n_cpu_moe"] == 4
    assert kwargs["tensor_parallel"] is True
    assert kwargs["gpu_ids"] == [0, 1]
    assert kwargs["max_seq_length"] == 8192
    assert kwargs["cache_type_kv"] == "q8_0"
    assert kwargs["n_parallel"] == 2
    assert kwargs["llama_extra_args"] == ["--flash-attn", "on"]


def test_an_override_from_a_newer_version_loses_nothing_an_older_one_kept():
    """Normalizing a stored row must be idempotent, or a rolled-back install finds settings rewritten."""
    once = normalize_model_override(dict(LEGACY_OVERRIDE))
    twice = normalize_model_override(dict(once))
    assert once == twice