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.

7.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

"""Under any interleaving, committed stays within budget except one escape holder; the queue drains."""

from __future__ import annotations

import asyncio
import random
import threading
import time

import pytest

from core.inference.llama_admission import LlamaAdmissionConfig, LlamaAdmissionQueue


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


class _Ceiling:
    """Watches ``committed`` from another thread and remembers the worst it saw."""

    def __init__(self, queue):
        self.queue = queue
        self.peak = 0
        self._stop = threading.Event()
        self._thread = threading.Thread(target = self._run, daemon = True)

    def _run(self):
        while not self._stop.is_set():
            self.peak = max(self.peak, self.queue.snapshot().committed)
            time.sleep(0.001)

    def __enter__(self):
        self._thread.start()
        return self

    def __exit__(self, *_exc):
        self._stop.set()
        self._thread.join(5)
        self.peak = max(self.peak, self.queue.snapshot().committed)


@pytest.mark.parametrize("seed", range(12))
@pytest.mark.asyncio
async def test_random_traffic_never_exceeds_the_cache_and_always_drains(seed):
    """Many holders, random sizes, random growth, random cancels, all at once."""
    rng = random.Random(seed)
    capacity = rng.choice([1, 2, 4, 8])
    budget = rng.choice([2048, 4096, 65536])
    queue = LlamaAdmissionQueue(f"stress-{seed}")

    # every holder opens at a fitting size, so any excess is the queue's doing
    opening = max(1, budget // max(1, capacity))
    leases = [
        lease
        for lease in (
            _lease(queue, tokens = opening, budget = budget, capacity = capacity) for _ in range(capacity)
        )
        if lease is not None
    ]
    assert leases, f"seed={seed}: nothing was admitted at an equal share"

    def worker(lease):
        for _ in range(rng.randint(1, 4)):
            want = rng.randint(1, budget)
            cancel = threading.Event()
            if rng.random() < 0.3:
                threading.Timer(rng.uniform(0.0, 0.05), cancel.set).start()
            lease.recost_waiting(
                want,
                cancel_event = cancel,
                poll_s = 0.005,
                timeout_s = 5.0,
            )
            time.sleep(rng.uniform(0.0, 0.01))
        lease.release()

    with _Ceiling(queue) as ceiling:
        threads = [threading.Thread(target = worker, args = (lease,), daemon = True) for lease in leases]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join(90)

    assert not any(t.is_alive() for t in threads), f"seed={seed}: a worker never finished"
    snapshot = queue.snapshot()
    assert snapshot.committed == 0, f"seed={seed}: {snapshot.committed} tokens stranded"
    assert queue._reparking == 0, f"seed={seed}: repark counter leaked, the queue is wedged"
    # one holder may exceed the budget via the alone escape, by at most one request
    assert (
        ceiling.peak <= budget * 2
    ), f"seed={seed}: committed peaked at {ceiling.peak} against a {budget} cache"


@pytest.mark.parametrize("seed", range(6))
@pytest.mark.asyncio
async def test_new_arrivals_alongside_growing_holders_still_drain(seed):
    """Reparkers hold the wait line shut: forgetting to reopen leaves arrivals never granted."""
    rng = random.Random(1000 + seed)
    capacity = 4
    budget = 4096
    queue = LlamaAdmissionQueue(f"mixed-{seed}")
    share = budget // capacity

    holders = [
        _lease(queue, tokens = share, budget = budget, capacity = capacity) for _ in range(capacity)
    ]
    holders = [lease for lease in holders if lease is not None]

    def grower(lease):
        lease.recost_waiting(
            rng.randint(share, budget),
            poll_s = 0.005,
            timeout_s = 5.0,
        )
        time.sleep(rng.uniform(0.0, 0.02))
        lease.release()

    threads = [threading.Thread(target = grower, args = (lease,), daemon = True) for lease in holders]
    for thread in threads:
        thread.start()

    arrivals: list = []

    async def arrive():
        reservation = queue.reserve(
            capacity = capacity,
            config = LlamaAdmissionConfig(),
            tokens = rng.randint(1, share),
            budget = budget,
        )
        # Await, never time.sleep: a sync poll blocks the call_soon_threadsafe grant delivery.
        deadline = time.monotonic() + 60
        while time.monotonic() < deadline:
            lease = reservation.lease_nowait()
            if lease is not None:
                arrivals.append(lease)
                lease.release()
                return
            await asyncio.sleep(0.01)
        reservation.cancel()
        arrivals.append(None)

    # reserve() reads the running loop, so each arrival brings its own.
    def arrive_in_loop():
        asyncio.run(arrive())

    newcomers = [threading.Thread(target = arrive_in_loop, daemon = True) for _ in range(4)]
    for thread in newcomers:
        thread.start()
    for thread in threads + newcomers:
        thread.join(120)

    assert not any(t.is_alive() for t in threads + newcomers), f"seed={seed}: did not drain"
    assert arrivals and all(
        lease is not None for lease in arrivals
    ), f"seed={seed}: an arrival was never admitted, the wait line stayed shut"
    assert queue.snapshot().committed == 0
    assert queue._reparking == 0


@pytest.mark.asyncio
async def test_a_wait_that_can_never_be_satisfied_gives_up_rather_than_wedging_the_queue():
    """The blast-radius test. A reparker holds the line shut for everyone, so a wait with
    no possible end must expire, restore its old figure and let the queue run again."""
    budget = 4096
    queue = LlamaAdmissionQueue("timeout")
    # all but one token, so the grower is still admitted and the cache is exactly full
    squatter = _lease(queue, tokens = budget - 1, budget = budget, capacity = 4)
    assert squatter is not None
    grower = _lease(queue, tokens = 1, budget = budget, capacity = 4)
    assert grower is not None, "the grower must be admitted before it can grow"

    start = time.monotonic()
    assert grower.recost_waiting(budget, poll_s = 0.01, timeout_s = 0.5) is False
    waited = time.monotonic() - start
    assert 0.4 <= waited < 15, f"gave up after {waited}s, expected roughly the timeout"
    assert queue._reparking == 0, "the wait line is still shut after a timeout"
    squatter.release()
    assert queue.snapshot().committed == 1
    grower.release()
    assert queue.snapshot().committed == 0


@pytest.mark.asyncio
async def test_release_during_a_wait_does_not_spin_forever():
    """release() runs from the route's teardown without touching the cancel event, so a
    wait that only watched the event would spin on a dead lease and hold the line shut."""
    budget = 4096
    queue = LlamaAdmissionQueue("released")
    squatter = _lease(queue, tokens = budget - 1, budget = budget, capacity = 4)
    grower = _lease(queue, tokens = 1, budget = budget, capacity = 4)
    assert squatter is not None and grower is not None

    out: list = []

    def grow():
        out.append(grower.recost_waiting(budget, poll_s = 0.01, timeout_s = 60))

    thread = threading.Thread(target = grow, daemon = True)
    thread.start()
    time.sleep(0.2)
    assert thread.is_alive(), "expected it to be waiting"
    grower.release()
    thread.join(15)
    assert not thread.is_alive(), "a released lease kept waiting"
    assert out == [False]
    assert queue._reparking == 0
    squatter.release()
    assert queue.snapshot().committed == 0