|
| 1 | +"""Evicted conversations go to disk and come back instead of a full prefill; older checkpoints of a live one do not.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from tensorfold.server.checkpoints import CheckpointStore |
| 8 | + |
| 9 | + |
| 10 | +def _store(slots=2, budget=None, spilled=None): |
| 11 | + return CheckpointStore(slots, copier=lambda c: c, budget_bytes=budget, sizer=lambda c: c[0], |
| 12 | + on_evict=(lambda entry: spilled.append(list(entry.tokens))) if spilled is not None else None) |
| 13 | + |
| 14 | + |
| 15 | +def test_an_evicted_conversation_is_handed_to_the_hook(): |
| 16 | + spilled = [] |
| 17 | + store = _store(slots=2, spilled=spilled) |
| 18 | + store.insert([1, 2], [10], last_prompt=[1, 2]) |
| 19 | + store.insert([5, 6], [10], last_prompt=[5, 6]) |
| 20 | + store.insert([8, 9], [10], last_prompt=[8, 9]) |
| 21 | + assert spilled == [[1, 2]] and store.spilled == 1 |
| 22 | + |
| 23 | + |
| 24 | +def test_an_older_checkpoint_of_a_conversation_that_moved_on_is_not_spilled(): |
| 25 | + spilled = [] |
| 26 | + store = _store(slots=2, spilled=spilled) |
| 27 | + store.insert([1, 2], [10], last_prompt=[1, 2]) |
| 28 | + store.insert([1, 2, 3, 4], [10], last_prompt=[1, 2, 3, 4]) # the same conversation, one turn on |
| 29 | + store.insert([7, 8], [10], last_prompt=[7, 8]) # evicts [1, 2], which [1, 2, 3, 4] extends |
| 30 | + assert spilled == [] |
| 31 | + |
| 32 | + |
| 33 | +def test_budget_evictions_are_spilled_too(): |
| 34 | + spilled = [] |
| 35 | + store = _store(slots=8, budget=25, spilled=spilled) |
| 36 | + store.insert([1], [10], last_prompt=[1]) |
| 37 | + store.insert([2], [10], last_prompt=[2]) |
| 38 | + store.insert([3], [10], last_prompt=[3]) |
| 39 | + assert spilled == [[1]] |
| 40 | + |
| 41 | + |
| 42 | +def test_memory_pressure_evictions_are_spilled(): |
| 43 | + spilled = [] |
| 44 | + store = _store(slots=4, spilled=spilled) |
| 45 | + store.insert([1], [10], last_prompt=[1]) |
| 46 | + store.insert([2], [10], last_prompt=[2]) |
| 47 | + assert store.evict_one() and spilled == [[1]] |
| 48 | + |
| 49 | + |
| 50 | +def test_system_blocks_are_never_spilled(): |
| 51 | + spilled = [] |
| 52 | + store = _store(slots=4, spilled=spilled) |
| 53 | + store.insert([1, 2, 3], [10], last_prompt=[1, 2, 3], pinned=True) |
| 54 | + assert store.evict_one() and spilled == [] |
| 55 | + |
| 56 | + |
| 57 | +def test_a_failing_hook_never_fails_the_insert(): |
| 58 | + def boom(entry): |
| 59 | + raise OSError("disk full") |
| 60 | + |
| 61 | + store = CheckpointStore(1, copier=lambda c: c, sizer=lambda c: c[0], on_evict=boom) |
| 62 | + store.insert([1], [10], last_prompt=[1]) |
| 63 | + store.insert([2], [10], last_prompt=[2]) |
| 64 | + assert len(store) == 1 and store.spilled == 0 |
| 65 | + |
| 66 | + |
| 67 | +class Layer: |
| 68 | + def __init__(self, n: int) -> None: |
| 69 | + import mlx.core as mx |
| 70 | + |
| 71 | + self.keys = mx.ones((1, 2, n, 4)) |
| 72 | + self.offset = n |
| 73 | + |
| 74 | + |
| 75 | +def test_a_spilled_conversation_is_found_and_read_back(tmp_path): |
| 76 | + mx = pytest.importorskip("mlx.core") |
| 77 | + from tensorfold.engine.prefix_snapshots import DiskBlocks, load_snapshot |
| 78 | + from tensorfold.server.checkpoints import spill_conversation |
| 79 | + |
| 80 | + model = "/models/qwen|mlx=1" |
| 81 | + store = CheckpointStore(1, copier=lambda c: c, sizer=lambda c: sum(int(x.keys.nbytes) for x in c), |
| 82 | + on_evict=lambda e: spill_conversation(e, tmp_path, model, limit_bytes=1 << 30)) |
| 83 | + store.insert([1, 2, 3], [Layer(3)], last_prompt=[1, 2, 3]) |
| 84 | + store.insert([9, 9], [Layer(2)], last_prompt=[9, 9]) # evicts and writes [1, 2, 3] |
| 85 | + hit = DiskBlocks(tmp_path, model).best([1, 2, 3, 4], 0) |
| 86 | + assert hit is not None and hit[1] == [1, 2, 3] |
| 87 | + tokens, cache = load_snapshot(hit[0], model) |
| 88 | + assert tokens == [1, 2, 3] and cache[0].offset == 3 and bool(mx.array_equal(cache[0].keys, Layer(3).keys).item()) |
| 89 | + |
| 90 | + |
| 91 | +def test_the_spill_directory_keeps_the_newest_within_its_byte_limit(tmp_path): |
| 92 | + import os |
| 93 | + |
| 94 | + pytest.importorskip("mlx.core") |
| 95 | + |
| 96 | + from tensorfold.server.checkpoints import CheckpointEntry, spill_conversation |
| 97 | + |
| 98 | + model = "/models/qwen|mlx=1" |
| 99 | + for i in range(3): |
| 100 | + spill_conversation(CheckpointEntry([i, 1], [Layer(64)], [i, 1], nbytes=10), tmp_path, model, limit_bytes=1 << 30) |
| 101 | + files = sorted(tmp_path.glob("*.safetensors"), key=lambda p: p.stat().st_mtime) |
| 102 | + for n, path in enumerate(files): # distinct mtimes, oldest first |
| 103 | + os.utime(path, (1_000_000 + n, 1_000_000 + n)) |
| 104 | + one = files[0].stat().st_size |
| 105 | + spill_conversation(CheckpointEntry([7, 1], [Layer(64)], [7, 1], nbytes=10), tmp_path, model, |
| 106 | + limit_bytes=int(2.5 * one)) |
| 107 | + assert len(list(tmp_path.glob("*.safetensors"))) == 2 |
| 108 | + assert not files[0].exists() and not files[1].exists() |
0 commit comments