#224Slide the context windowEasyTransformersLLMs
Slide the context window
Background
The model's positional table has only block_size rows, so each generation step crops the running context to the most recent block_size tokens:
idx_cond = idx if idx.size(1) <= block_size else idx[:, -block_size:]
Older tokens fall off the left as generation continues.
Problem statement
Implement crop_context(ids, block_size) returning the last block_size tokens (or all of them if shorter).
Input
ids— list of token ids.block_size—int, the model's max context length.
Output
Returns a list: the final block_size ids, or all of ids if len(ids) <= block_size.
Examples
Example 1 — crop to the last 3
Input: ids = [1, 2, 3, 4, 5], block_size = 3
Output: [3, 4, 5]
Example 2 — shorter than the window
Input: ids = [1, 2], block_size = 5
Output: [1, 2]
Constraints
- Return
ids[-block_size:]. - If
len(ids) <= block_size, returnidsunchanged.
Notes
- The model only ever saw
≤ block_sizetokens in training, so the window slide keeps every forward pass in-distribution.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Crop to the last three (example)
- •Shorter than window unchanged (reference)
- •Exactly block_size (sample)