Implement dynamic batching for token decoding
xAIMachine Learning Engineer · OnsiteLast updated
This question evaluates dynamic batching, per-request state management, and sequence-decoding correctness for language-model inference, including handling stop conditions, max-token limits, and maintaining a correct slot-to-request mapping.
You are given a black-box “simulated language model” interface that can advance many sequences in a batch.
Model interface
- Tokens are integers.
model_next(batch_prefixes) -> next_tokensbatch_prefixesis a list of token lists, one per active sequence in the current batch.next_tokensis a list of integers of the same length, wherenext_tokens[i]is the next generated token forbatch_prefixes[i].
Requests
Each request/sequence has:
prompt_tokens: initial prefix tokensmax_tokens: maximum number of generated tokens allowed (not counting the prompt)- A stopping rule:
- either a
stop_token(single token), or - a
stop_sequence(a list of tokens that, when it appears as a suffix of the generated output, ends generation) - A callback to return the final generated tokens (or you may collect results to return at the end).
Batch execution requirement
Implement a decoding/sampling engine with dynamic batching:
- There is a fixed batch capacity
B. - Requests arrive in a waiting queue (you can assume they are all available initially, or you can model an input queue).
- You repeatedly call
model_nextto advance active sequences. - Sequences may finish at different times due to:
- reaching
max_tokens, or - hitting the stop condition.
- When a sequence finishes, its slot becomes free and should be refilled from the waiting queue if possible.
- Near the end, the batch may be partially filled; your code must handle
len(active) < Bcorrectly.
Correctness requirement
Maintain a correct mapping between batch slots and requests so that tokens and final outputs are never mixed up after refilling (e.g., via a slot_id -> request_id mapping).
Task
Write a function (or class) that runs this dynamic-batching decoding loop until all requests are completed, and returns (or callbacks) the generated outputs per request.
Clearly define:
- your data structures (active slots, waiting queue, per-request state),
- the main loop and termination condition,
- how you detect stop conditions (especially
stop_sequence), - and how you handle partially filled batches.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.