KatInterview

Continuous-Batch Inference Loop with Slot Refilling

Data Structure DesignModel answer
MediumModel answerData Structure Designbatchinginferencesimulation

Last updated

Implement a dynamic batching generation loop that keeps a fixed-size batch full by refilling completed slots from a waiting queue until all requests are served.

Continuous-Batch Inference Loop with Slot Refilling

You are building the generation loop for a text inference server. A mock language model exposes one method: given a batch of token sequences (each sequence is the full list of tokens seen so far for one request), it returns the next token for every sequence in the batch.

class MockModel:
    def next_tokens(self, batch: list[list[int]]) -> list[int]:
        """
        batch[i] is the complete token sequence for one in-progress request.
        Returns one next token per entry, preserving input order.
        """

Your task is to implement generate, which takes a list of requests and a maximum number of concurrent slots, and returns every request's generated tokens once all requests are complete.

from dataclasses import dataclass

@dataclass
class Request:
    request_id: str
    prompt: list[int]
    max_tokens: int

def generate(
    model: MockModel,
    requests: list[Request],
    num_slots: int,
    stop_token: int,
) -> dict[str, list[int]]:
    """
    Runs generation for all requests using dynamic batching.
    Returns a mapping from request_id to the list of generated tokens.
    """

How it works

At any point in time, up to num_slots requests run concurrently. Each model call receives the current token sequences for every active slot and produces one new token per slot. After each step:

  • A request is complete if the new token is the stop token, or if the number of tokens generated so far equals the request's max_tokens limit.
  • Every slot that held a completed request is immediately refilled from the front of the waiting queue before the next model call.
  • The loop ends when every request has finished and the batch is empty.

Constraints

  • A single model call processes at most num_slots sequences.
  • The model is stateless: every call must supply the full token prefix built up so far (prompt + all previously generated tokens).
  • Requests are admitted from the queue in the order they appear in requests.
  • model.next_tokens returns exactly one token per input entry, in the same order.
  • max_tokens counts only the newly generated tokens, not the prompt.
  • The stop token should not be included in the returned output for a completed request.
  • If fewer than num_slots requests remain (queue empty, some slots idle), the model call covers only the occupied slots.

What your solution should handle

  • Requests that finish at very different steps (uneven output lengths).
  • More total requests than available slots, requiring multiple refill events.
  • A request whose very first generated token is the stop token.
  • A max_tokens of 1 (single-token output).
  • No request lost, duplicated, or returned with extra tokens.

Hints

Hint 1

Track each active slot with a small record: which request occupies it, the tokens generated so far, and the full current sequence to send to the model. A list indexed by slot number works well.

Hint 2

After receiving the model's output, check completion for every slot before building the next batch. Refill freed slots from the queue in the same pass so the next model call sees a full batch.

Hint 3

The model call should only include occupied slots, not empty ones. Keep a separate mapping from model-call position to slot index so you can route each output token back to the right request.