Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions python/freetoken/engine/cache_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def plan_cache_budget(
kv_reserve_bytes = kv_reserve_pages * cache_per_page
# MoE-priority: reserve KV first, then experts greedily take the remaining budget.
raw = (budget_bytes - kv_reserve_bytes) // per_expert_bytes
# Overlap is optional: when the budget cannot fund its 2*num_experts floor beside the
# KV reserve, plan without it instead of clamping up past the budget.
if overlap and raw < 2 * num_experts:
overlap, lo = False, num_experts
moe_cache_size = max(lo, min(raw, hi))
# A tiny budget may have forced moe_cache_size below 2*num_experts even with overlap on.
overlap = overlap and moe_cache_size >= 2 * num_experts

remaining = budget_bytes - moe_cache_size * per_expert_bytes
num_pages = max(remaining // cache_per_page, kv_reserve_pages)
Expand Down
11 changes: 11 additions & 0 deletions tests/engine/test_cache_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ def test_small_cache_disables_prefill_overlap():
assert size == 12


def test_budget_below_the_overlap_floor_disables_prefill_overlap():
# 2*num_experts slots (800 B) + the KV reserve (100 B) exceed the 700 B budget, but
# num_experts slots fit beside the reserve: plan without overlap instead of asserting.
size, pages, overlap = plan_cache_budget(
budget_bytes=700, per_expert_bytes=100, cache_per_page=10,
num_experts=4, total_experts=50, prefill_overlap=True,
kv_reserve_pages=10, max_slots=50,
)
assert (size, pages, overlap) == (6, 10, False)


def test_insufficient_kv_memory_raises():
with pytest.raises(AssertionError, match="not enough memory"):
plan_cache_budget(
Expand Down
Loading