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.

16.8 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

"""Re-costing a live lease as the loop grows instead of reserving the whole KV cache up front."""

from __future__ import annotations

import asyncio
import threading
import time

import pytest

from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueue


def _reserve(queue, *, capacity, tokens, budget):
    return queue.reserve(
        capacity = capacity,
        config = LlamaAdmissionConfig(),
        tokens = tokens,
        budget = budget,
    )


def _lease(
    queue,
    *,
    capacity = 4,
    tokens,
    budget,
):
    """reserve() reads the running loop, so every test here is async."""
    reservation = _reserve(queue, capacity = capacity, tokens = tokens, budget = budget)
    lease = reservation.lease_nowait()
    assert lease is not None, "expected this reservation to be admitted"
    return lease


class TestTheQueueSide:
    @pytest.mark.asyncio
    async def test_growth_that_fits_is_applied(self):
        queue = LlamaAdmissionQueue("test")
        _lease(queue, tokens = 1000, budget = 4096)
        assert queue.try_recost(1000, 2000) is True
        assert queue.snapshot().committed == 2000

    @pytest.mark.asyncio
    async def test_growth_that_does_not_fit_is_refused_and_changes_nothing(self):
        """Refused, not blocked: a waiting round can deadlock with the holder it waits on."""
        queue = LlamaAdmissionQueue("test")
        _lease(queue, tokens = 2000, budget = 4096)
        _lease(queue, tokens = 2000, budget = 4096)
        assert queue.snapshot().committed == 4000
        assert queue.try_recost(2000, 3000) is False
        assert queue.snapshot().committed == 4000, "a refused growth must not move anything"

    @pytest.mark.asyncio
    async def test_a_lone_holder_may_grow_past_the_budget(self):
        """The escape admission uses: refusing the only holder stalls a conversation
        nothing else can unblock, and llama.cpp surfaces a real overflow itself."""
        queue = LlamaAdmissionQueue("test")
        _lease(queue, tokens = 2000, budget = 4096)
        assert queue.try_recost(2000, 9000) is True
        assert queue.snapshot().committed == 9000

    @pytest.mark.asyncio
    async def test_shrinking_always_applies(self):
        queue = LlamaAdmissionQueue("test")
        _lease(queue, tokens = 2000, budget = 4096)
        _lease(queue, tokens = 2000, budget = 4096)
        assert queue.try_recost(2000, 500) is True
        assert queue.snapshot().committed == 2500

    @pytest.mark.asyncio
    async def test_no_budget_means_nothing_to_account(self):
        queue = LlamaAdmissionQueue("test")
        assert queue.try_recost(1000, 999999) is True


class TestTheLeaseSide:
    @pytest.mark.asyncio
    async def test_recost_moves_the_queue_and_the_lease_together(self):
        queue = LlamaAdmissionQueue("test")
        lease = _lease(queue, tokens = 1000, budget = 8192)
        assert lease.recost(2500) is True
        assert queue.snapshot().committed == 2500
        lease.release()
        assert queue.snapshot().committed == 0

    @pytest.mark.asyncio
    async def test_a_refused_recost_leaves_release_correct(self):
        """The leak this guards: if the queue took the growth while the lease kept the old
        number, release would hand back less than it holds and strand the difference."""
        queue = LlamaAdmissionQueue("test")
        first = _lease(queue, tokens = 3000, budget = 4096)
        second = _lease(queue, tokens = 1000, budget = 4096)
        assert second.recost(3000) is False
        second.release()
        first.release()
        assert queue.snapshot().committed == 0

    @pytest.mark.asyncio
    async def test_a_released_lease_recosts_to_nothing(self):
        queue = LlamaAdmissionQueue("test")
        lease = _lease(queue, tokens = 1000, budget = 4096)
        lease.release()
        assert lease.recost(3000) is True, "a finished run is not an error"
        assert queue.snapshot().committed == 0, "and must not re-commit anything"

    @pytest.mark.asyncio
    async def test_recost_is_idempotent_at_the_same_size(self):
        queue = LlamaAdmissionQueue("test")
        lease = _lease(queue, tokens = 1000, budget = 4096)
        for _ in range(5):
            assert lease.recost(1000) is True
        assert queue.snapshot().committed == 1000

    @pytest.mark.asyncio
    async def test_concurrent_recost_and_release_do_not_strand_tokens(self):
        """release() takes the lease lock and then the queue's; recost takes them in the
        same order, which is what keeps this from deadlocking or leaking."""
        for _ in range(40):
            queue = LlamaAdmissionQueue("test")
            lease = _lease(queue, tokens = 1000, budget = 1_000_000)
            barrier = threading.Barrier(2)

            def grow(lease = lease, barrier = barrier):
                barrier.wait()
                lease.recost(5000)

            def drop(lease = lease, barrier = barrier):
                barrier.wait()
                lease.release()

            # Daemon threads: a failed assertion must not leave a spinning thread wedging exit.
            threads = [
                threading.Thread(target = grow, daemon = True),
                threading.Thread(target = drop, daemon = True),
            ]
            for thread in threads:
                thread.start()
            for thread in threads:
                thread.join()
            assert queue.snapshot().committed == 0, "a race left tokens committed with no holder"


class TestFourToolChatsTogether:
    @pytest.mark.asyncio
    async def test_four_tool_chats_are_admitted_and_can_each_grow_a_little(self):
        """The behaviour the change exists for, end to end at the queue level."""
        queue = LlamaAdmissionQueue("test")
        budget = 262144
        share = budget // 4
        leases = [_lease(queue, tokens = share, budget = budget) for _ in range(4)]
        assert queue.snapshot().committed == budget, "all four tool chats admitted at once"
        assert leases[0].recost(share + 1000) is False
        leases[3].release()
        assert leases[0].recost(share + 1000) is True


class TestWaitingForRoomInsteadOfRunningOverIt:
    """``recost`` alone only accounts for growth; ``recost_waiting`` is what makes it binding."""

    @pytest.mark.asyncio
    async def test_growth_that_fits_never_touches_the_wait_line(self):
        queue = LlamaAdmissionQueue("test")
        lease = _lease(queue, tokens = 1000, budget = 8192)
        assert lease.recost_waiting(2000) is True
        assert queue.snapshot().committed == 2000
        assert queue._reparking == 0, "a growth that fit should not have yielded anything"

    @pytest.mark.asyncio
    async def test_a_waiter_is_let_in_when_a_holder_finishes(self):
        queue = LlamaAdmissionQueue("test")
        first = _lease(queue, tokens = 2000, budget = 4096)
        second = _lease(queue, tokens = 2000, budget = 4096)

        done: list = []

        def grow():
            done.append(second.recost_waiting(3500, poll_s = 0.01))

        thread = threading.Thread(target = grow, daemon = True)
        thread.start()
        thread.join(0.2)
        assert thread.is_alive(), "expected the growth to wait, not to be refused"
        assert queue.snapshot().committed == 2000
        first.release()
        thread.join(5)
        assert not thread.is_alive()
        assert done == [True]
        assert queue.snapshot().committed == 3500

    @pytest.mark.asyncio
    async def test_four_loops_growing_together_do_not_overcommit(self):
        """Deadlock case: every holder wants more than a share, so blocking while holding deadlocks
        all four; yielding first lets _committed fall so somebody fits."""
        queue = LlamaAdmissionQueue("test")
        budget = 4096
        leases = [_lease(queue, tokens = budget // 4, budget = budget) for _ in range(4)]
        assert queue.snapshot().committed == budget

        results: list = []
        lock = threading.Lock()

        def grow(lease):
            ok = lease.recost_waiting(budget // 2, poll_s = 0.01)
            with lock:
                results.append(ok)
            lease.release()

        threads = [threading.Thread(target = grow, args = (lease,), daemon = True) for lease in leases]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join(20)
        assert not any(t.is_alive() for t in threads), "a growth deadlocked"
        assert results == [True] * 4
        assert queue.snapshot().committed == 0
        assert queue._reparking == 0

    @pytest.mark.asyncio
    async def test_the_budget_is_never_exceeded_while_they_wait(self):
        """One reparker may take the committed-is-zero escape; four racers would all admit."""
        queue = LlamaAdmissionQueue("test")
        budget = 4096
        leases = [_lease(queue, tokens = budget // 4, budget = budget) for _ in range(4)]
        peak = []
        stop = threading.Event()

        def watch():
            while not stop.is_set():
                peak.append(queue.snapshot().committed)
                time.sleep(0.005)

        watcher = threading.Thread(target = watch, daemon = True)
        watcher.start()

        def grow(lease):
            lease.recost_waiting(budget, poll_s = 0.01)
            lease.release()

        threads = [threading.Thread(target = grow, args = (lease,), daemon = True) for lease in leases]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join(20)
        stop.set()
        watcher.join(5)
        assert max(peak) <= budget, f"committed reached {max(peak)} against a {budget} cache"

    @pytest.mark.asyncio
    async def test_cancelling_a_waiting_round_restores_its_commitment(self):
        """Stop pressed while waiting. Until the run releases it still occupies
        llama-server's cache, so the pool must know about it."""
        queue = LlamaAdmissionQueue("test")
        first = _lease(queue, tokens = 3000, budget = 4096)
        second = _lease(queue, tokens = 1000, budget = 4096)
        cancel = threading.Event()

        out: list = []

        def grow():
            out.append(second.recost_waiting(4000, cancel_event = cancel, poll_s = 0.01))

        thread = threading.Thread(target = grow, daemon = True)
        thread.start()
        thread.join(0.2)
        assert thread.is_alive()
        cancel.set()
        thread.join(5)
        assert out == [False], "a cancelled wait reports that it did not take the new size"
        assert queue.snapshot().committed == 4000, "the old commitment is back"
        assert queue._reparking == 0
        second.release()
        first.release()
        assert queue.snapshot().committed == 0

    @pytest.mark.asyncio
    async def test_a_reparker_is_not_overtaken_by_a_new_arrival(self):
        """An in-flight conversation beats one that has not started. Otherwise a steady
        arrival rate holds a growing run at its opening size indefinitely."""
        queue = LlamaAdmissionQueue("test")
        first = _lease(queue, tokens = 2000, budget = 4096)
        second = _lease(queue, tokens = 2000, budget = 4096)

        def grow():
            second.recost_waiting(3000, poll_s = 0.01)

        thread = threading.Thread(target = grow, daemon = True)
        thread.start()
        thread.join(0.2)
        assert thread.is_alive()
        newcomer = queue.reserve(
            capacity = 4, config = LlamaAdmissionConfig(), tokens = 1000, budget = 4096
        )
        assert newcomer.lease_nowait() is None, "a new arrival overtook a growing run"
        first.release()
        thread.join(5)
        assert not thread.is_alive()
        # the reclaim must run admission itself, else the newcomer waits out the grown run
        for _ in range(100):
            await asyncio.sleep(0.01)
            if newcomer.lease_nowait() is not None:
                break
        assert (
            newcomer.lease_nowait() is not None
        ), "the last repark barrier came down without re-running admission"
        assert queue.snapshot().committed == 4000


class TestGivingUpTheWait:
    """Giving up a wait restores the old commitment as a correction a full cache must never refuse."""

    @pytest.mark.asyncio
    async def test_a_full_cache_cannot_refuse_the_restore(self):
        queue = LlamaAdmissionQueue("test")
        loser = _lease(queue, tokens = 1024, budget = 4096)
        winner = _lease(queue, tokens = 1024, budget = 4096)
        assert queue.snapshot().committed == 2048

        cancel = threading.Event()
        out: list = []

        def grow():
            out.append(loser.recost_waiting(4096, cancel_event = cancel, poll_s = 0.01))

        thread = threading.Thread(target = grow, daemon = True)
        thread.start()
        while queue.snapshot().committed != 1024:
            await asyncio.sleep(0.005)
        assert winner.recost(4096) is True
        assert queue.snapshot().committed == 4096

        cancel.set()
        thread.join(5)
        assert out == [False]
        assert queue._reparking == 0
        # both figures are really resident at llama-server, so record them even over budget
        assert (
            queue.snapshot().committed == 4096 + 1024
        ), "a restore the cache could not fit was dropped instead of recorded"

        loser.release()
        assert (
            queue.snapshot().committed == 4096
        ), "release subtracted a commitment that was never restored"
        newcomer = queue.reserve(
            capacity = 4, config = LlamaAdmissionConfig(), tokens = 1000, budget = 4096
        )
        assert (
            newcomer.lease_nowait() is None
        ), "a newcomer was admitted into room the winner is still using"
        winner.release()

    @pytest.mark.asyncio
    async def test_a_released_lease_restores_nothing(self):
        """release() already returned the parked 0; re-committing would strand the difference."""
        queue = LlamaAdmissionQueue("test")
        holder = _lease(queue, tokens = 2000, budget = 4096)
        _lease(queue, tokens = 2000, budget = 4096)

        cancel = threading.Event()
        thread = threading.Thread(
            target = lambda: holder.recost_waiting(4000, cancel_event = cancel, poll_s = 0.01),
            daemon = True,
        )
        thread.start()
        while queue.snapshot().committed != 2000:
            await asyncio.sleep(0.005)
        holder.release()
        cancel.set()
        thread.join(5)
        assert not thread.is_alive()
        assert queue._reparking == 0
        assert queue.snapshot().committed == 2000, "the released lease was re-committed"


class TestYieldingIsGatedOnTheServerActuallyClearing:
    """Idle slots keep KV cells under --kv-unified until prompt_clear(), which --cache-ram 0 disables."""

    @pytest.mark.asyncio
    async def test_growth_that_fits_does_not_care(self):
        """The cheap path never yields anything, so gating must not disturb it."""
        queue = LlamaAdmissionQueue("test")
        holder = _lease(queue, tokens = 1000, budget = 4096)
        assert holder.recost_waiting(2000, allow_yield = False) is True
        assert queue.snapshot().committed == 2000

    @pytest.mark.asyncio
    async def test_growth_that_does_not_fit_declines_instead_of_yielding(self):
        queue = LlamaAdmissionQueue("test")
        holder = _lease(queue, tokens = 2000, budget = 4096)
        _lease(queue, tokens = 2000, budget = 4096)

        assert holder.recost_waiting(4000, allow_yield = False) is False
        assert holder._tokens == 2000, "the old commitment was not kept"
        assert queue.snapshot().committed == 4000, "capacity was handed out twice"
        assert queue._reparking == 0

    @pytest.mark.asyncio
    async def test_it_does_not_block(self):
        """Declining is pre-existing behaviour: do not wait for room that is not coming back."""
        queue = LlamaAdmissionQueue("test")
        holder = _lease(queue, tokens = 2000, budget = 4096)
        _lease(queue, tokens = 2000, budget = 4096)

        started = time.monotonic()
        assert holder.recost_waiting(4000, allow_yield = False, timeout_s = 30.0, poll_s = 0.5) is False
        assert time.monotonic() - started < 1.0

    @pytest.mark.asyncio
    async def test_yielding_is_still_the_default(self):
        """Old callers, and every server that does clear, keep the waiting behaviour."""
        queue = LlamaAdmissionQueue("test")
        holder = _lease(queue, tokens = 2000, budget = 4096)
        other = _lease(queue, tokens = 2000, budget = 4096)

        thread = threading.Thread(
            target = lambda: holder.recost_waiting(4000, poll_s = 0.01),
            daemon = True,
        )
        thread.start()
        while queue.snapshot().committed != 2000:
            await asyncio.sleep(0.005)
        other.release()
        thread.join(5)
        assert not thread.is_alive()
        assert queue.snapshot().committed == 4000