#222The block_size hard capEasy

The block_size hard cap

Background

Learned positional embeddings have a fixed table of block_size rows, so a sequence of length T is only valid when every position index 0..T-1 exists — i.e. T ≤ block_size. A longer sequence (GPT-2 small caps at 1024) would index a row that isn't there.

Problem statement

Implement check_block_size(T, block_size) returning T if it fits, else raising IndexError.

Input

  • Tint, sequence length.
  • block_sizeint, the positional table size (max context).

Output

Returns T (the valid length) when T <= block_size; raises IndexError otherwise.

Examples

Example 1 — exactly at the cap

Input:  T = 1024, block_size = 1024
Output: 1024

Example 2 — too long

Input:  T = 1500, block_size = 1024
Raises: IndexError

Constraints

  • T <= block_size → return T.
  • T > block_sizeraise IndexError(...).

Notes

  • This is why GPT-2 truncates long inputs; schemes like rotary or ALiBi extrapolate instead of capping.
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.

  • Exactly at the cap (example)
  • Short sequence returns T (reference)
  • Overflow raises IndexError (sample)