Open-source inference server and production cluster for all the models your agent needs.
Python bgecolbertdata-pipelinedeep-learningembeddingsinferenceinference-serverinformation-retrievalllmmlmlopsnatural-language-processingnlppythonrerankingretrievalretrieval-augmented-generationsemantic-searchspladevector-search
"""Tests for batch formation module."""
import asyncio
import random
import time
from dataclasses import dataclass
import pytest
from sie_server.core.batcher import (
BatchConfig,
BatchFormer,
FormattedBatch,
HasCost,
PendingRequest,
collate_batch,
)
from sie_server.core.prepared import ExtractPreparedItem, TextPayload, TextPreparedItem, make_text_item
class TestPendingRequest:
"""Tests for PendingRequest dataclass."""
def test_create_request(self) -> None:
"""Can create a pending request."""
item = make_text_item([1, 2, 3])
request = PendingRequest(item=item, metadata={"id": "req-1"})
assert request.item == item
assert request.metadata == {"id": "req-1"}
assert request.arrival_time > 0
def test_arrival_time_auto_set(self) -> None:
"""Arrival time is automatically set."""
item = make_text_item([1])
before = time.monotonic()
request = PendingRequest(item=item, metadata=None)
after = time.monotonic()
assert before <= request.arrival_time <= after
class TestFormattedBatch:
"""Tests for FormattedBatch dataclass."""
def test_create_batch(self) -> None:
"""Can create a formatted batch."""
items = [
make_text_item([1, 2], 0),
make_text_item([3, 4, 5], 1),
]
batch = FormattedBatch(
items=items,
metadata=["meta-0", "meta-1"],
total_cost=5,
)
assert batch.size == 2
assert batch.total_tokens == 5 # Backward compat alias
assert batch.total_cost == 5
assert batch.metadata == ["meta-0", "meta-1"]
def test_size_property(self) -> None:
"""Size returns number of items."""
items = [make_text_item([1], i) for i in range(5)]
batch = FormattedBatch(items=items, metadata=list(range(5)), total_cost=5)
assert batch.size == 5
def test_sorted_by_length(self) -> None:
"""Sorted by length returns items in ascending cost order."""
items = [
make_text_item([1, 2, 3, 4, 5], 0), # longest
make_text_item([1], 1), # shortest
make_text_item([1, 2, 3], 2), # middle
]
batch = FormattedBatch(
items=items,
metadata=["longest", "shortest", "middle"],
total_cost=9,
)
sorted_batch = batch.sorted_by_length()
# Items sorted ascending by cost
assert sorted_batch.items[0].cost == 1
assert sorted_batch.items[1].cost == 3
assert sorted_batch.items[2].cost == 5
# Metadata follows items
assert sorted_batch.metadata[0] == "shortest"
assert sorted_batch.metadata[1] == "middle"
assert sorted_batch.metadata[2] == "longest"
# Total tokens unchanged
assert sorted_batch.total_tokens == 9
def test_sorted_by_length_preserves_original(self) -> None:
"""Sorting creates new batch, doesn't modify original."""
items = [
make_text_item([1, 2], 0),
make_text_item([1], 1),
]
original = FormattedBatch(items=items, metadata=["a", "b"], total_cost=3)
sorted_batch = original.sorted_by_length()
# Original unchanged
assert original.items[0].cost == 2
assert original.metadata[0] == "a"
# Sorted is different
assert sorted_batch.items[0].cost == 1
assert sorted_batch.metadata[0] == "b"
class TestBatchConfig:
"""Tests for BatchConfig dataclass."""
def test_default_values(self) -> None:
"""Default config has expected values."""
config = BatchConfig()
assert config.max_batch_tokens == 16384
assert config.max_batch_requests == 64
assert config.max_batch_wait_ms == 15
assert config.coalesce_ms == 15.0
assert config.coalesce_ratio == 0.5
def test_custom_values(self) -> None:
"""Can create config with custom values."""
config = BatchConfig(
max_batch_tokens=8192,
max_batch_requests=32,
max_batch_wait_ms=5,
)
assert config.max_batch_tokens == 8192
assert config.max_batch_requests == 32
assert config.max_batch_wait_ms == 5
class TestBatchFormer:
"""Tests for BatchFormer."""
@pytest.fixture
def config(self) -> BatchConfig:
"""Create a test config with short timeout."""
return BatchConfig(
max_batch_tokens=100,
max_batch_requests=4,
max_batch_wait_ms=50,
coalesce_ms=10.0,
)
@pytest.fixture
def batcher(self, config: BatchConfig) -> BatchFormer[TextPreparedItem, str]:
"""Create a BatchFormer with test config."""
return BatchFormer(config)
def test_init_default_config(self) -> None:
"""BatchFormer uses default config if none provided."""
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer()
assert batcher.config.max_batch_tokens == 16384
assert batcher.config.max_batch_requests == 64
def test_init_custom_config(self, config: BatchConfig) -> None:
"""BatchFormer uses provided config."""
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
assert batcher.config.max_batch_tokens == 100
assert batcher.config.max_batch_requests == 4
def test_initial_state(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""BatchFormer starts empty."""
assert batcher.pending_count == 0
assert batcher.pending_tokens == 0
@pytest.mark.asyncio
async def test_submit_increases_counts(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""Submit increases pending count and tokens."""
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
assert batcher.pending_count == 1
assert batcher.pending_tokens == 3
@pytest.mark.asyncio
async def test_submit_multiple(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""Can submit multiple requests."""
item1 = make_text_item([1, 2], 0)
item2 = make_text_item([3, 4, 5], 1)
await batcher.submit(item1, "req-1")
await batcher.submit(item2, "req-2")
assert batcher.pending_count == 2
assert batcher.pending_tokens == 5
@pytest.mark.asyncio
async def test_get_batch_returns_on_coalesce(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""get_batch returns after coalescing window when no more items arrive."""
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed = time.monotonic() - start
# Should wait roughly the coalesce period (~10ms), not the full timeout (50ms)
assert elapsed >= 0.005 # At least ~5ms
assert elapsed < 0.10 # Well under the 50ms batch timeout
assert batch.size == 1
@pytest.mark.asyncio
async def test_get_batch_returns_on_request_limit(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""get_batch returns immediately when request limit reached."""
# Config has max_batch_requests=4
items = [make_text_item([i], i) for i in range(4)]
for i, item in enumerate(items):
await batcher.submit(item, f"req-{i}")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed = time.monotonic() - start
# Should return immediately (not wait for timeout)
assert elapsed < 0.02
assert batch.size == 4
@pytest.mark.asyncio
async def test_get_batch_returns_on_token_limit(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""get_batch returns immediately when token limit reached."""
# Config has max_batch_tokens=100
# Create one big item that hits the limit
item = make_text_item(list(range(100)))
await batcher.submit(item, "big-req")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed = time.monotonic() - start
# Should return immediately
assert elapsed < 0.02
assert batch.size == 1
assert batch.total_tokens == 100
@pytest.mark.asyncio
async def test_get_batch_returns_sorted(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""get_batch returns items sorted by cost."""
items = [
make_text_item([1, 2, 3, 4, 5], 0),
make_text_item([1], 1),
make_text_item([1, 2, 3], 2),
make_text_item([1, 2], 3), # triggers limit
]
for i, item in enumerate(items):
await batcher.submit(item, f"req-{i}")
batch = await batcher.get_batch()
# Should be sorted ascending by cost
assert batch.items[0].cost == 1
assert batch.items[1].cost == 2
assert batch.items[2].cost == 3
assert batch.items[3].cost == 5
@pytest.mark.asyncio
async def test_get_batch_clears_pending(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""get_batch clears pending requests."""
items = [make_text_item([i], i) for i in range(4)]
for i, item in enumerate(items):
await batcher.submit(item, f"req-{i}")
await batcher.get_batch()
assert batcher.pending_count == 0
assert batcher.pending_tokens == 0
@pytest.mark.asyncio
async def test_get_batch_preserves_metadata(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""get_batch preserves metadata correspondence with items."""
items = [
make_text_item([1, 2], 0),
make_text_item([3], 1),
]
await batcher.submit(items[0], "longer")
await batcher.submit(items[1], "shorter")
# Trigger batch
await batcher.submit(make_text_item([4, 5], 2), "trigger")
await batcher.submit(make_text_item([6], 3), "last")
batch = await batcher.get_batch()
# After sorting: shortest items first
# Original order: 2 tokens, 1 token, 2 tokens, 1 token
# Sorted: 1, 1, 2, 2
assert batch.items[0].cost == 1
assert batch.items[1].cost == 1
assert batch.items[2].cost == 2
assert batch.items[3].cost == 2
def test_try_get_batch_returns_none_when_empty(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_get_batch returns None when no pending requests."""
result = batcher.try_get_batch()
assert result is None
@pytest.mark.asyncio
async def test_try_get_batch_returns_none_when_not_ready(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_get_batch returns None when batch not ready."""
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
# Batch not full and timeout not expired
result = batcher.try_get_batch()
assert result is None
@pytest.mark.asyncio
async def test_try_get_batch_returns_when_full(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_get_batch returns batch when full."""
items = [make_text_item([i], i) for i in range(4)]
for i, item in enumerate(items):
await batcher.submit(item, f"req-{i}")
result = batcher.try_get_batch()
assert result is not None
assert result.size == 4
class TestBatchFormerConcurrency:
"""Tests for BatchFormer concurrent behavior."""
@pytest.mark.asyncio
async def test_concurrent_submits(self) -> None:
"""Multiple coroutines can submit concurrently."""
config = BatchConfig(max_batch_requests=10, max_batch_wait_ms=1, coalesce_ms=0.5)
batcher: BatchFormer[TextPreparedItem, int] = BatchFormer(config)
async def submit_one(idx: int) -> None:
item = make_text_item([idx], idx)
await batcher.submit(item, idx)
# Submit 10 items concurrently
await asyncio.gather(*[submit_one(i) for i in range(10)])
assert batcher.pending_count == 10
assert batcher.pending_tokens == 10
@pytest.mark.asyncio
async def test_producer_consumer_pattern(self) -> None:
"""Producer submits while consumer waits for batch."""
config = BatchConfig(
max_batch_requests=4,
max_batch_tokens=1000,
max_batch_wait_ms=50,
coalesce_ms=10.0,
)
batcher: BatchFormer[TextPreparedItem, int] = BatchFormer(config)
batches: list[FormattedBatch[TextPreparedItem, int]] = []
async def producer() -> None:
for i in range(8):
item = make_text_item([i], i)
await batcher.submit(item, i)
await asyncio.sleep(0)
async def consumer() -> None:
# Get two batches (8 items, 4 per batch)
for _ in range(2):
batch = await batcher.get_batch()
batches.append(batch)
await asyncio.gather(producer(), consumer())
assert len(batches) == 2
assert batches[0].size == 4
assert batches[1].size == 4
@pytest.mark.asyncio
async def test_get_batch_waits_for_first_request(self) -> None:
"""get_batch returns after first request plus coalescing window."""
# Use a very short timeout so test runs quickly
config = BatchConfig(max_batch_wait_ms=200, max_batch_requests=10, coalesce_ms=20.0)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
async def delayed_submit() -> None:
await asyncio.sleep(0.010) # Small delay before submit
item = make_text_item([1, 2, 3])
await batcher.submit(item, "delayed")
# Start submit in background
submit_task = asyncio.create_task(delayed_submit())
# get_batch will wait for first request, then coalesce window
start = time.monotonic()
batch = await asyncio.wait_for(batcher.get_batch(), timeout=1.0)
elapsed = time.monotonic() - start
await submit_task
# Should have gotten the batch (submit at ~10ms + coalesce ~20ms = ~30ms)
assert batch is not None
assert batch.size == 1
# Should take at least the coalesce period after first request
assert elapsed >= 0.020 # At least submit delay + coalesce
class TestImmediateDispatch:
"""Tests for immediate dispatch mode in get_batch()."""
@pytest.mark.asyncio
async def test_immediate_returns_without_timeout(self) -> None:
"""get_batch(immediate=True) returns immediately when items are pending."""
config = BatchConfig(max_batch_wait_ms=50, max_batch_requests=10)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
start = time.monotonic()
batch = await batcher.get_batch(immediate=True)
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Should return in well under the 50ms timeout
assert elapsed_ms < 5
@pytest.mark.asyncio
async def test_immediate_false_waits_for_timeout(self) -> None:
"""get_batch(immediate=False) waits for timeout as usual."""
# Use coalesce_ratio=1.0 to disable proportional scaling so the raw
# coalesce_ms (100) is used. This ensures the batch timeout (50ms)
# is the binding constraint, not the effective coalesce.
config = BatchConfig(max_batch_wait_ms=50, max_batch_requests=10, coalesce_ms=100, coalesce_ratio=1.0)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
start = time.monotonic()
batch = await batcher.get_batch(immediate=False)
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Should wait at least close to the timeout (coalesce_ms > max_batch_wait_ms
# so the batch timeout is the binding constraint, not coalescing)
assert elapsed_ms >= 40
# Should not wait excessively beyond the batch timeout
assert elapsed_ms <= 150 # 50ms timeout + generous margin for CI jitter
@pytest.mark.asyncio
async def test_immediate_empty_waits_for_first_request(self) -> None:
"""get_batch(immediate=True) still waits when no items are pending."""
config = BatchConfig(max_batch_wait_ms=50, max_batch_requests=10)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
async def delayed_submit() -> None:
await asyncio.sleep(0.005) # 5ms delay
item = make_text_item([1, 2, 3])
await batcher.submit(item, "delayed")
submit_task = asyncio.create_task(delayed_submit())
start = time.monotonic()
batch = await asyncio.wait_for(batcher.get_batch(immediate=True), timeout=1.0)
elapsed_ms = (time.monotonic() - start) * 1000
await submit_task
assert batch.size == 1
# Should have waited for the submit (~5ms) but not the full timeout (~50ms)
assert elapsed_ms >= 4
assert elapsed_ms < 30
@pytest.mark.asyncio
async def test_immediate_still_sorts_by_cost(self) -> None:
"""Immediate mode still returns items sorted by cost."""
config = BatchConfig(max_batch_wait_ms=50, max_batch_requests=10)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1, 2, 3, 4, 5], 0), "big")
await batcher.submit(make_text_item([1], 1), "small")
await batcher.submit(make_text_item([1, 2, 3], 2), "mid")
batch = await batcher.get_batch(immediate=True)
assert batch.size == 3
assert batch.items[0].cost == 1
assert batch.items[1].cost == 3
assert batch.items[2].cost == 5
class TestCoalesceCap:
"""Tests for the idle-dispatch coalesce cap in get_batch() (#2874)."""
@pytest.mark.asyncio
async def test_cap_bounds_lone_item_wait(self) -> None:
"""A lone item waits only the cap, not the full coalesce/batch window."""
config = BatchConfig(max_batch_wait_ms=1000, max_batch_requests=64, coalesce_ms=800, coalesce_ratio=1.0)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1, 2, 3]), "req-1")
start = time.monotonic()
batch = await asyncio.wait_for(batcher.get_batch(coalesce_cap_ms=10), timeout=2.0)
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Coalesce fires at ~10ms of quiet, far below the 800ms coalesce
# window and the 1000ms batch timeout.
assert elapsed_ms < 400
@pytest.mark.asyncio
async def test_cap_still_accumulates_a_burst(self) -> None:
"""Arrivals inside the cap keep extending accumulation (burst fuses)."""
config = BatchConfig(max_batch_wait_ms=1000, max_batch_requests=64, coalesce_ms=800, coalesce_ratio=1.0)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
batch_task = asyncio.create_task(batcher.get_batch(coalesce_cap_ms=60))
for i in range(5):
await batcher.submit(make_text_item([1, 2, 3], 0), f"req-{i}")
await asyncio.sleep(0.005) # gaps well inside the 60ms cap
batch = await asyncio.wait_for(batch_task, timeout=2.0)
assert batch.size == 5
@pytest.mark.asyncio
async def test_batch_timeout_still_bounds_capped_wait(self) -> None:
"""A cap larger than the batch window cannot extend the total wait."""
config = BatchConfig(max_batch_wait_ms=30, max_batch_requests=64, coalesce_ms=1_000, coalesce_ratio=1.0)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1, 2, 3]), "req-1")
start = time.monotonic()
batch = await asyncio.wait_for(batcher.get_batch(coalesce_cap_ms=10_000), timeout=2.0)
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Neither the large cap nor the 1000ms coalesce window can extend the
# 30ms batch timeout — the batch window is the binding bound here.
assert elapsed_ms < 500
@pytest.mark.asyncio
async def test_no_cap_keeps_existing_behavior(self) -> None:
"""Without a cap the coalesce window is unchanged."""
config = BatchConfig(max_batch_wait_ms=200, max_batch_requests=64, coalesce_ms=40, coalesce_ratio=1.0)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1, 2, 3]), "req-1")
start = time.monotonic()
batch = await asyncio.wait_for(batcher.get_batch(), timeout=2.0)
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Coalesce fires at ~40ms of quiet — not immediately.
assert elapsed_ms >= 30
class TestSubBatching:
"""Tests for sub-batching behavior (splitting large batches)."""
@pytest.fixture
def sub_batch_config(self) -> BatchConfig:
"""Config with small token limit to trigger sub-batching."""
return BatchConfig(
max_batch_tokens=50, # Small limit to trigger splits
max_batch_requests=10,
max_batch_wait_ms=50,
coalesce_ms=10.0,
)
@pytest.fixture
def sub_batcher(self, sub_batch_config: BatchConfig) -> BatchFormer[TextPreparedItem, str]:
"""Create a BatchFormer configured for sub-batching tests."""
return BatchFormer(sub_batch_config)
@pytest.mark.asyncio
async def test_splits_batch_on_token_limit(self, sub_batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""Batch is split when items exceed token limit."""
# Submit items totaling 80 tokens (30 + 10 + 25 + 15)
items = [
make_text_item(list(range(30)), 0),
make_text_item(list(range(10)), 1),
make_text_item(list(range(25)), 2),
make_text_item(list(range(15)), 3),
]
for i, item in enumerate(items):
await sub_batcher.submit(item, f"req-{i}")
# First batch should respect 50 token limit
batch1 = await sub_batcher.get_batch()
assert batch1.total_tokens <= 50
# Should have remaining items
assert sub_batcher.pending_count > 0
# Second batch gets remaining items
batch2 = await sub_batcher.get_batch()
assert batch2.size >= 1
# All items processed
total_items = batch1.size + batch2.size
assert total_items == 4
@pytest.mark.asyncio
async def test_sub_batches_are_sorted(self, sub_batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""Each sub-batch is sorted by cost (ascending)."""
items = [
make_text_item(list(range(30)), 0),
make_text_item(list(range(10)), 1),
make_text_item(list(range(25)), 2),
make_text_item(list(range(15)), 3),
]
for i, item in enumerate(items):
await sub_batcher.submit(item, f"req-{i}")
batch1 = await sub_batcher.get_batch()
costs1 = [item.cost for item in batch1.items]
assert costs1 == sorted(costs1), "Batch 1 not sorted"
if sub_batcher.pending_count > 0:
batch2 = await sub_batcher.get_batch()
costs2 = [item.cost for item in batch2.items]
assert costs2 == sorted(costs2), "Batch 2 not sorted"
@pytest.mark.asyncio
async def test_metadata_preserved_through_split(self, sub_batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""Metadata stays with correct items through split and sort."""
items = [
make_text_item(list(range(30)), 0),
make_text_item(list(range(10)), 1),
]
await sub_batcher.submit(items[0], "large")
await sub_batcher.submit(items[1], "small")
batch = await sub_batcher.get_batch()
# After sorting, smallest should be first
for item, meta in zip(batch.items, batch.metadata, strict=False):
if item.cost == 10:
assert meta == "small"
elif item.cost == 30:
assert meta == "large"
@pytest.mark.asyncio
async def test_single_large_item_exceeds_limit(self) -> None:
"""Single item larger than limit is still returned."""
config = BatchConfig(max_batch_tokens=10, max_batch_requests=10, max_batch_wait_ms=10)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
# Single item with 50 tokens (exceeds 10 token limit)
big_item = make_text_item(list(range(50)))
await batcher.submit(big_item, "big")
batch = await batcher.get_batch()
# Should still get the item even though it exceeds limit
assert batch.size == 1
assert batch.total_tokens == 50
@pytest.mark.asyncio
async def test_greedy_packing(self, sub_batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""Greedy packing takes items until limit reached."""
# Submit items: 10, 15, 20, 25 tokens
# After sorting: 10, 15, 20, 25
# Greedy packing with 50 limit: 10+15+20 = 45 (fits), 45+25 = 70 (doesn't fit)
# So batch 1 should have 3 items (45 tokens), batch 2 should have 1 item (25 tokens)
items = [
make_text_item(list(range(25)), 0),
make_text_item(list(range(10)), 1),
make_text_item(list(range(20)), 2),
make_text_item(list(range(15)), 3),
]
for i, item in enumerate(items):
await sub_batcher.submit(item, f"req-{i}")
batch1 = await sub_batcher.get_batch()
assert batch1.size == 3
assert batch1.total_tokens == 45 # 10 + 15 + 20
batch2 = await sub_batcher.get_batch()
assert batch2.size == 1
assert batch2.total_tokens == 25
class TestBatchFormerDrain:
"""Tests for BatchFormer try_drain (continuous batching)."""
@pytest.fixture
def config(self) -> BatchConfig:
return BatchConfig(max_batch_tokens=100, max_batch_requests=4, max_batch_wait_ms=50)
@pytest.fixture
def batcher(self, config: BatchConfig) -> BatchFormer[TextPreparedItem, str]:
return BatchFormer(config)
@pytest.mark.asyncio
async def test_try_drain_returns_none_when_empty(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_drain returns None when no pending requests."""
result = await batcher.try_drain()
assert result is None
@pytest.mark.asyncio
async def test_try_drain_returns_pending_items(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_drain returns pending items immediately without waiting for timeout."""
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
start = time.monotonic()
result = await batcher.try_drain()
elapsed = time.monotonic() - start
# Should return immediately (not wait for 50ms timeout)
assert elapsed < 0.005
assert result is not None
assert result.size == 1
assert batcher.pending_count == 0
@pytest.mark.asyncio
async def test_try_drain_returns_all_accumulated(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_drain returns all accumulated items as a batch."""
for i in range(3):
await batcher.submit(make_text_item([i], i), f"req-{i}")
result = await batcher.try_drain()
assert result is not None
assert result.size == 3
assert batcher.pending_count == 0
@pytest.mark.asyncio
async def test_try_drain_respects_max_items_cap(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""try_drain(max_items=N) takes at most N items and leaves the rest pending.
This is the fairness bound behind the LoRA drain loop (#1606): a drain
must not sweep in more than the caller's snapshot budget, even when the
batcher holds more items (including ones that arrived mid-drain).
"""
for i in range(5):
await batcher.submit(make_text_item([i], i), f"req-{i}")
assert batcher.pending_count == 5
first = await batcher.try_drain(max_items=2)
assert first is not None
assert first.size == 2
assert batcher.pending_count == 3
# A follow-up drain honors its own cap and drains part of the remainder.
second = await batcher.try_drain(max_items=2)
assert second is not None
assert second.size == 2
assert batcher.pending_count == 1
class TestBatchFormerSubmitMany:
"""Tests for BatchFormer submit_many (bulk submit)."""
@pytest.fixture
def batcher(self) -> BatchFormer[TextPreparedItem, str]:
return BatchFormer(BatchConfig(max_batch_tokens=100, max_batch_requests=10, max_batch_wait_ms=5))
@pytest.mark.asyncio
async def test_submit_many_adds_all_items(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""submit_many adds all items with single lock acquisition."""
items = [(make_text_item([i], i), f"req-{i}") for i in range(5)]
await batcher.submit_many(items)
assert batcher.pending_count == 5
assert batcher.pending_tokens == 5
@pytest.mark.asyncio
async def test_submit_many_empty_list(self, batcher: BatchFormer[TextPreparedItem, str]) -> None:
"""submit_many with empty list is a no-op."""
await batcher.submit_many([])
assert batcher.pending_count == 0
class TestCollateBatch:
"""Tests for collate_batch function."""
def test_empty_items(self) -> None:
"""Collate returns empty for empty input."""
result = collate_batch([])
assert result == {"input_ids": [], "attention_mask": []}
def test_single_item(self) -> None:
"""Collate handles single item."""
payloads = [TextPayload([1, 2, 3], [1, 1, 1])]
result = collate_batch(payloads)
assert result["input_ids"] == [[1, 2, 3]]
assert result["attention_mask"] == [[1, 1, 1]]
def test_same_length_items(self) -> None:
"""Collate handles items of same length."""
payloads = [
TextPayload([1, 2, 3], [1, 1, 1]),
TextPayload([4, 5, 6], [1, 1, 1]),
]
result = collate_batch(payloads)
assert result["input_ids"] == [[1, 2, 3], [4, 5, 6]]
assert result["attention_mask"] == [[1, 1, 1], [1, 1, 1]]
def test_pads_shorter_items(self) -> None:
"""Collate pads shorter items to max length."""
payloads = [
TextPayload([1, 2, 3], [1, 1, 1]),
TextPayload([4, 5], [1, 1]),
TextPayload([6], [1]),
]
result = collate_batch(payloads)
# All padded to length 3
assert result["input_ids"] == [[1, 2, 3], [4, 5, 0], [6, 0, 0]]
assert result["attention_mask"] == [[1, 1, 1], [1, 1, 0], [1, 0, 0]]
def test_custom_pad_token(self) -> None:
"""Collate uses custom pad token."""
payloads = [
TextPayload([1, 2, 3], [1, 1, 1]),
TextPayload([4], [1]),
]
result = collate_batch(payloads, pad_token_id=99)
assert result["input_ids"] == [[1, 2, 3], [4, 99, 99]]
assert result["attention_mask"] == [[1, 1, 1], [1, 0, 0]]
def test_preserves_order(self) -> None:
"""Collate preserves item order (caller should pre-sort)."""
payloads = [
TextPayload([1], [1]), # shortest
TextPayload([2, 3], [1, 1]), # middle
TextPayload([4, 5, 6], [1, 1, 1]), # longest
]
result = collate_batch(payloads)
# Order preserved, all padded to length 3
assert result["input_ids"][0] == [1, 0, 0]
assert result["input_ids"][1] == [2, 3, 0]
assert result["input_ids"][2] == [4, 5, 6]
class TestCoalescePrecision:
"""Tests for coalesce timing precision (Finding 2 fix)."""
@pytest.mark.asyncio
async def test_coalesce_fires_near_deadline(self) -> None:
"""Batch yields within a tight window after the last submit + coalesce_ms.
With the precise timeout fix, the batcher should wake up close to
the exact coalesce deadline rather than polling every coalesce_ms.
"""
coalesce = 20.0
config = BatchConfig(
max_batch_wait_ms=200,
max_batch_requests=100,
coalesce_ms=coalesce,
)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
item = make_text_item([1, 2, 3])
await batcher.submit(item, "req-1")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Should fire close to coalesce_ms (within 10ms tolerance for CI)
assert elapsed_ms >= coalesce * 0.8, f"Fired too early: {elapsed_ms:.1f}ms"
assert elapsed_ms < coalesce * 2.5, f"Fired too late: {elapsed_ms:.1f}ms"
@pytest.mark.asyncio
async def test_coalesce_resets_on_new_submit(self) -> None:
"""Coalesce window resets when a new item arrives.
If items arrive at t=0, t=15ms with coalesce_ms=20ms, the batch
should yield at ~t=35ms (15ms + 20ms), not at t=20ms.
"""
coalesce = 30.0
config = BatchConfig(
max_batch_wait_ms=200,
max_batch_requests=100,
coalesce_ms=coalesce,
)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
async def staggered_submits() -> None:
await batcher.submit(make_text_item([1], 0), "first")
await asyncio.sleep(0.015) # 15ms gap
await batcher.submit(make_text_item([2], 1), "second")
submit_task = asyncio.create_task(staggered_submits())
start = time.monotonic()
batch = await asyncio.wait_for(batcher.get_batch(), timeout=1.0)
elapsed_ms = (time.monotonic() - start) * 1000
await submit_task
assert batch.size == 2
# Should fire ~15ms (second submit) + ~30ms (coalesce) = ~45ms
assert elapsed_ms >= 35, f"Fired too early: {elapsed_ms:.1f}ms"
assert elapsed_ms < 80, f"Fired too late: {elapsed_ms:.1f}ms"
class TestCoalesceVsTimeout:
"""Tests for coalesce and timeout interaction."""
@pytest.mark.asyncio
async def test_timeout_fires_when_shorter_than_coalesce(self) -> None:
"""When max_batch_wait_ms < coalesce_ms, timeout is the binding constraint."""
config = BatchConfig(
max_batch_wait_ms=20,
max_batch_requests=100,
coalesce_ms=100, # coalesce ceiling much larger than timeout
)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1]), "req-1")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Should fire at batch timeout (~20ms), not coalesce (~100ms)
assert elapsed_ms < 50, f"Waited too long: {elapsed_ms:.1f}ms"
@pytest.mark.asyncio
async def test_coalesce_fires_when_shorter_than_timeout(self) -> None:
"""When coalesce_ms < max_batch_wait_ms, coalesce detects end-of-burst."""
config = BatchConfig(
max_batch_wait_ms=200,
max_batch_requests=100,
coalesce_ms=15,
)
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1]), "req-1")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Should fire at coalesce (~15ms), well before timeout (200ms)
assert elapsed_ms < 50, f"Waited too long: {elapsed_ms:.1f}ms"
assert elapsed_ms >= 10, f"Fired too early: {elapsed_ms:.1f}ms"
class TestEffectiveCoalesce:
"""Tests for proportional coalesce scaling (effective_coalesce_ms)."""
def test_default_scaling(self) -> None:
"""With default ratio (0.5), effective coalesce is half of batch wait."""
config = BatchConfig(max_batch_wait_ms=50, coalesce_ms=20.0)
# 50 * 0.5 = 25, min(20, 25) = 20
assert config.effective_coalesce_ms == 20.0
def test_ceiling_respected(self) -> None:
"""When ratio * wait > coalesce_ms, coalesce_ms is the ceiling."""
config = BatchConfig(max_batch_wait_ms=50, coalesce_ms=5.0)
# 50 * 0.5 = 25, min(5, 25) = 5
assert config.effective_coalesce_ms == 5.0
def test_scales_with_adaptive_wait(self) -> None:
"""Effective coalesce tracks changes to max_batch_wait_ms."""
config = BatchConfig(max_batch_wait_ms=10, coalesce_ms=20.0)
# Initial: 10 * 0.5 = 5, min(20, 5) = 5
assert config.effective_coalesce_ms == 5.0
# Simulate adaptive controller increasing wait
config.max_batch_wait_ms = 50.0
# 50 * 0.5 = 25, min(20, 25) = 20
assert config.effective_coalesce_ms == 20.0
# Simulate adaptive controller decreasing wait
config.max_batch_wait_ms = 2.0
# 2 * 0.5 = 1, min(20, 1) = 1
assert config.effective_coalesce_ms == pytest.approx(1.0)
def test_custom_ratio(self) -> None:
"""Custom coalesce_ratio is respected."""
config = BatchConfig(max_batch_wait_ms=100, coalesce_ms=50.0, coalesce_ratio=0.1)
# 100 * 0.1 = 10, min(50, 10) = 10
assert config.effective_coalesce_ms == 10.0
def test_zero_wait_gives_zero_coalesce(self) -> None:
"""When max_batch_wait_ms is 0 (or near-zero), coalesce is effectively 0."""
config = BatchConfig(max_batch_wait_ms=0, coalesce_ms=5.0)
assert config.effective_coalesce_ms == 0.0
@pytest.mark.asyncio
async def test_effective_coalesce_used_in_batcher(self) -> None:
"""BatchFormer uses effective_coalesce_ms, not raw coalesce_ms.
With max_batch_wait_ms=100 and coalesce_ms=50, ratio=0.2:
effective_coalesce = min(50, 100*0.2) = 20ms.
Batch should yield at ~20ms, not ~50ms.
"""
config = BatchConfig(
max_batch_wait_ms=200,
max_batch_requests=100,
coalesce_ms=50.0,
coalesce_ratio=0.2,
)
# effective_coalesce = min(50, 200*0.2) = 40ms
assert config.effective_coalesce_ms == 40.0
batcher: BatchFormer[TextPreparedItem, str] = BatchFormer(config)
await batcher.submit(make_text_item([1]), "req-1")
start = time.monotonic()
batch = await batcher.get_batch()
elapsed_ms = (time.monotonic() - start) * 1000
assert batch.size == 1
# Should fire near effective_coalesce (40ms), well before timeout (200ms)
assert elapsed_ms < 80, f"Waited too long: {elapsed_ms:.1f}ms"
assert elapsed_ms >= 30, f"Fired too early: {elapsed_ms:.1f}ms"
@dataclass(slots=True)
class _Audio:
"""Audio item stub: cost is duration in ms, as ``AudioPreprocessor`` sets it."""
cost: int
original_index: int = 0
runs_alone: bool = False
def _clip(seconds: float) -> _Audio:
return _Audio(cost=round(seconds * 1_000))
def _long_form(seconds: float) -> _Audio:
return _Audio(cost=round(seconds * 1_000), runs_alone=True)
def _arrive(batcher: BatchFormer[_Audio, str], item: _Audio, name: str, *, at: float) -> None:
"""Queue ``item`` as if it arrived at monotonic time ``at``."""
batcher._append_item(item, name)
batcher._pending[-1].arrival_time = at
def _greedy_reference(pending: list[tuple[int, int]], config: BatchConfig, max_items: int | None) -> list[int]:
"""Cost-sorted greedy packing as it was before the runs_alone lane.
``pending`` holds ``(cost, id)`` in queue order and is updated in place,
as ``BatchFormer`` updates its own queue.
"""
pending.sort(key=lambda entry: entry[0])
taken: list[int] = []
batch_cost = 0
for cost, request_id in pending:
if max_items is not None and len(taken) >= max_items:
break
if batch_cost + cost > config.max_batch_cost and taken:
break
taken.append(request_id)
batch_cost += cost
if len(taken) >= config.max_batch_requests:
break
del pending[: len(taken)]
return taken
class TestRunsAloneLane:
"""Items flagged ``runs_alone`` (long-form audio, #585) never share a batch."""
@pytest.mark.parametrize("seed", range(20))
def test_unflagged_items_batch_exactly_as_cost_sorted_greedy_packing(self, seed: int) -> None:
"""Without a flagged item pending, every batch matches the old algorithm."""
rng = random.Random(seed) # noqa: S311 -- reproducible test data
config = BatchConfig(max_batch_cost=rng.randint(1, 4_000), max_batch_requests=rng.randint(1, 8))
batcher: BatchFormer[HasCost, int] = BatchFormer(config)
reference: list[tuple[int, int]] = []
next_id = 0
for _ in range(60):
for _ in range(rng.randint(0, 5)):
cost = rng.randint(1, 1_500)
# Items with runs_alone=False and items without the attribute.
item: HasCost = _Audio(cost=cost) if rng.random() < 0.5 else ExtractPreparedItem(cost, 0)
batcher._append_item(item, next_id)
reference.append((cost, next_id))
next_id += 1
if not reference:
continue
max_items = rng.choice([None, None, 1, 2, 5])
batch = batcher._extract_batch(max_items=max_items)
assert batch.metadata == _greedy_reference(reference, config, max_items)
assert batcher.pending_count == len(reference)
assert batcher.pending_cost == sum(cost for cost, _ in reference)
def test_long_form_runs_alone_after_clips_that_arrived_around_it(self) -> None:
"""Clips are not held in a long recording's batch (head-of-line wait)."""
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=720_000))
_arrive(batcher, _clip(5), "clip-5s", at=0.0)
_arrive(batcher, _long_form(240), "long-240s", at=1.0)
_arrive(batcher, _clip(8), "clip-8s", at=2.0)
assert batcher._extract_batch().metadata == ["clip-5s", "clip-8s"]
assert batcher._extract_batch().metadata == ["long-240s"]
assert batcher.pending_count == 0
def test_long_form_items_run_in_arrival_order(self) -> None:
"""An older long recording is not starved by a newer, shorter one."""
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=720_000))
_arrive(batcher, _long_form(250), "long-250s", at=0.0)
_arrive(batcher, _long_form(190), "long-190s", at=1.0)
assert batcher._extract_batch().metadata == ["long-250s"]
assert batcher._extract_batch().metadata == ["long-190s"]
@pytest.mark.parametrize(("long_arrival", "order"), [(0.0, ["long", "clip"]), (2.0, ["clip", "long"])])
def test_first_batch_comes_from_the_lane_of_the_oldest_request(self, long_arrival: float, order: list[str]) -> None:
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=720_000))
_arrive(batcher, _clip(5), "clip", at=1.0)
_arrive(batcher, _long_form(240), "long", at=long_arrival)
first = batcher._extract_batch().metadata
second = batcher._extract_batch().metadata
assert first + second == order
def test_lanes_take_turns_while_both_are_pending(self) -> None:
"""Two long recordings and clips that keep arriving: clips, long, clips, long."""
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=720_000))
_arrive(batcher, _clip(4), "clip-a", at=0.0)
_arrive(batcher, _clip(6), "clip-b", at=1.0)
_arrive(batcher, _long_form(240), "long-1", at=2.0)
_arrive(batcher, _long_form(200), "long-2", at=3.0)
served = []
for second in range(4, 9):
served.append(batcher._extract_batch().metadata)
# One clip lands while each batch runs.
_arrive(batcher, _clip(5), f"clip@{second}", at=float(second))
assert served == [
["clip-a", "clip-b"],
["long-1"],
["clip@4", "clip@5"],
["long-2"],
["clip@6", "clip@7"],
]
assert batcher.pending_count == 1
def test_long_form_amid_steady_clips_waits_for_one_clip_batch(self) -> None:
"""Clip traffic that never lets up delays a flagged item by one clip batch.
A flagged recording just over 30 s is not held back for as long as
short traffic keeps the queue busy.
"""
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=720_000))
_arrive(batcher, _long_form(240), "long-240s", at=0.0)
assert batcher._extract_batch().metadata == ["long-240s"]
# A clip and a 31 s recording land while long-240s runs, and one more
# clip lands while each later batch runs.
_arrive(batcher, _clip(4), "clip@1", at=1.0)
_arrive(batcher, _long_form(31), "long-31s", at=2.0)
served = []
for second in range(3, 7):
_arrive(batcher, _clip(4), f"clip@{second}", at=float(second))
served.append(batcher._extract_batch().metadata)
assert served == [["clip@1", "clip@3"], ["long-31s"], ["clip@4", "clip@5"], ["clip@6"]]
@pytest.mark.asyncio
async def test_try_drain_takes_turns_and_keeps_counts_consistent(self) -> None:
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=720_000))
await batcher.submit_many(
[
(_clip(5), "clip-5s"),
(_long_form(240), "long-240s"),
(_clip(8), "clip-8s"),
(_long_form(200), "long-200s"),
(_clip(3), "clip-3s"),
]
)
expected = [
(1, ["clip-3s"], 4, 453_000),
(4, ["long-240s"], 3, 213_000),
(2, ["clip-5s", "clip-8s"], 1, 200_000),
(None, ["long-200s"], 0, 0),
]
for max_items, names, pending_count, pending_cost in expected:
batch = await batcher.try_drain(max_items=max_items)
assert batch is not None
assert batch.metadata == names
assert batch.total_cost == sum(item.cost for item in batch.items)
assert batcher.pending_count == pending_count
assert batcher.pending_cost == pending_cost
assert await batcher.try_drain() is None
assert batcher._first_request_time is None
assert batcher._last_submit_time is None
@pytest.mark.parametrize("max_batch_cost", [180_000, 2_880_000])
def test_lanes_never_mix_and_take_turns_at_any_cost_cap(self, max_batch_cost: int) -> None:
"""Holds at both ends of Whisper's adaptive cost range."""
rng = random.Random(max_batch_cost) # noqa: S311 -- reproducible test data
batcher: BatchFormer[_Audio, str] = BatchFormer(BatchConfig(max_batch_cost=max_batch_cost))
submitted: set[str] = set()
served: list[str] = []
last_alone: bool | None = None
# Pending flagged item -> [flagged items ahead of it on arrival, clip batches since].
flagged_waits: dict[str, list[int]] = {}
def extract() -> None:
nonlocal last_alone
pending = list(batcher._pending)
batch = batcher._extract_batch(max_items=rng.choice([None, 1, 3]))
assert batch.size > 0
alone = batch.items[0].runs_alone
assert all(item.runs_alone is alone for item in batch.items)
if alone:
assert batch.size == 1
if {request.runs_alone for request in pending} == {True, False}:
# The lanes take turns, starting with the lane of the oldest request.
oldest_alone = min(pending, key=lambda request: request.arrival_time).runs_alone
assert alone is (oldest_alone if last_alone is None else not last_alone)
last_alone = alone
if alone:
ahead, clip_batches = flagged_waits.pop(batch.metadata[0])
assert clip_batches <= ahead + 1
else:
for wait in flagged_waits.values():
wait[1] += 1
served.extend(batch.metadata)
now = 0.0
for index in range(300):
now += rng.uniform(0.0, 2.0)
if rng.random() < 0.6:
name = f"item-{index}"
if rng.random() < 0.3:
flagged_waits[name] = [len(flagged_waits), 0]
item = _long_form(rng.uniform(30.001, 300.0))
else:
item = _clip(rng.uniform(1.0, 30.0))
_arrive(batcher, item, name, at=now)
submitted.add(name)
if batcher.pending_count and rng.random() < 0.5:
extract()
while batcher.pending_count:
extract()
assert sorted(served) == sorted(submitted)
assert not flagged_waits
assert batcher.pending_cost == 0