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.

4.5 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

"""A failed sd.cpp load must call restore_owner_account, returning GPU ownership to the prior account."""

from __future__ import annotations

import types

import pytest

from auth import policy
from core.inference import gpu_arbiter
from core.inference.sd_cpp_backend import SdCppDiffusionBackend, _SdLoading
from hub.services.models import account_access as access
from utils.account_context import AccountContext, run_as

ALICE = AccountContext("a" * 32, "alice")
BOB = AccountContext("b" * 32, "bob")


@pytest.fixture(autouse = True)
def isolated(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
    monkeypatch.setattr(policy, "installation_is_multi_user", lambda: True)
    monkeypatch.setattr(access, "_resident_accounts", {})
    monkeypatch.setattr(access, "_resident_components", {}, raising = False)
    monkeypatch.setattr(access, "_prior_resident_accounts", {})
    monkeypatch.setattr(access, "_uncommitted_resident", {}, raising = False)
    monkeypatch.setattr(access, "_uncommitted_components", {}, raising = False)
    monkeypatch.setattr(gpu_arbiter, "_owner", "diffusion")
    monkeypatch.setattr(gpu_arbiter, "_owner_account", ALICE.account_id)
    monkeypatch.setattr(gpu_arbiter, "_prior_account", None)
    yield


def _failing_backend(monkeypatch):
    backend = SdCppDiffusionBackend(engine = None)
    monkeypatch.setattr(
        SdCppDiffusionBackend,
        "_resolve_backend",
        lambda self: (_ for _ in ()).throw(
            RuntimeError("sd-cli binary is present but not runnable.")
        ),
    )
    backend._state = types.SimpleNamespace(repo_id = "org/alice-model")
    backend._loading = _SdLoading(repo_id = "org/bob-model", base_repo = "")
    return backend


def test_failed_native_load_returns_gpu_residency_to_the_displaced_account(monkeypatch):
    backend = _failing_backend(monkeypatch)
    run_as(BOB, gpu_arbiter.acquire_for, "diffusion", lambda: None)
    run_as(BOB, access.note_resident_account, "diffusion", "org/bob-model")
    assert gpu_arbiter.owner_account() == BOB.account_id

    run_as(
        BOB,
        lambda: backend._run_load(
            repo_id = "org/bob-model",
            gguf_filename = "flux1-dev-Q4_K_M.gguf",
            base = "",
            fam = None,
            hf_token = None,
            _load_token = backend._load_token,
        ),
    )

    assert backend._loading.error
    assert backend._state is not None
    assert gpu_arbiter.owner_account() == ALICE.account_id
    assert run_as(ALICE, access.resident_hidden, "diffusion", "org/alice-model") is False
    assert run_as(BOB, access.resident_hidden, "diffusion", "org/alice-model") is True


def test_failed_native_cpu_load_leaves_a_released_arbiter_alone(monkeypatch):
    """Single-user / CPU parity: the CPU path releases DIFFUSION, so the restore must no-op."""
    backend = _failing_backend(monkeypatch)
    gpu_arbiter.release("diffusion")
    run_as(
        BOB,
        lambda: backend._run_load(
            repo_id = "org/bob-model",
            gguf_filename = "flux1-dev-Q4_K_M.gguf",
            base = "",
            fam = None,
            hf_token = None,
            _load_token = backend._load_token,
        ),
    )
    assert gpu_arbiter.current_owner() is None
    assert gpu_arbiter.owner_account() is None


def test_failed_native_replacement_keeps_the_resident_priced_as_itself(monkeypatch):
    backend = _failing_backend(monkeypatch)
    old, new = {0: 8000}, {0: 1000}
    monkeypatch.setattr(gpu_arbiter, "_residents", {"diffusion": frozenset({0})})
    monkeypatch.setattr(gpu_arbiter, "_media_needs", {"diffusion": old})
    monkeypatch.setattr(gpu_arbiter, "_media_need_rollback", {}, raising = False)
    monkeypatch.setattr(gpu_arbiter, "physical_gpu_ids", lambda ids: None)
    run_as(
        BOB,
        gpu_arbiter.acquire_for,
        "diffusion",
        lambda: None,
        policy = "fit",
        need_mib = new,
    )
    assert gpu_arbiter._media_needs["diffusion"] == new

    run_as(
        BOB,
        lambda: backend._run_load(
            repo_id = "org/bob-model",
            gguf_filename = "flux1-dev-Q4_K_M.gguf",
            base = "",
            fam = None,
            hf_token = None,
            _load_token = backend._load_token,
        ),
    )

    assert backend._loading.error and backend._state is not None
    assert gpu_arbiter._media_needs["diffusion"] == old