Implement Autoregressive Decoding in PyTorch
InceptionMachine Learning Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to implement autoregressive decoding and sampling strategies in PyTorch, testing competencies in sequence generation, probabilistic sampling methods (greedy, temperature, top-k, top-p), batching, and end-of-sequence handling.
Implement an autoregressive text-generation function in PyTorch.
You are given a language model that, for an input tensor of token IDs, returns logits for the next-token distribution at every position.
Assume the model can be called as:
logits = model(input_ids)
where:
input_idshas shape[batch_size, current_sequence_length].logitshas shape[batch_size, current_sequence_length, vocab_size].- The logits for the next token should be taken from
logits[:, -1, :].
Write a generation function that supports common decoding strategies:
- Greedy decoding.
- Temperature sampling.
- Top-k sampling.
- Top-p / nucleus sampling.
The function should repeatedly generate one token at a time until either:
max_new_tokenstokens have been generated, or- every sequence in the batch has produced
eos_token_id, if provided.
Discuss important implementation details and edge cases.
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.