#237Learned positional lookup P[:T]Easy

Learned positional lookup P[:T]

Background

GPT-2 uses learned positional embeddings: a table P ∈ ℝ^{block_size × C} indexed by position, not token id. For a sequence of length T, you take the first T rows — position t gets row P[t]:

pos_emb=P[0:T],shape (T,C)\text{pos\_emb} = P[0{:}T], \qquad \text{shape } (T, C)

These broadcast across the batch when added to the token embeddings.

Problem statement

Implement positional_lookup(P, T) returning the position rows for a length-T sequence.

Input

  • P — learned positional table, shape (block_size, C).
  • Tint, sequence length.

Output

Returns an np.ndarray of shape (T, C): the first T rows of P.

Examples

Example 1

Input:  P = [[0, 0], [1, 1], [2, 2], [3, 3]], T = 2
Output: [[0, 0], [1, 1]]

Constraints

  • Return P[:T] (positions 0..T-1).
  • Result shape (T, C).

Notes

  • Unlike the token table (indexed by token id), this is indexed by arange(T) — the same positions every forward pass, regardless of which tokens appear.
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.

  • Example: first two positions
  • Reference: full table when T equals block_size
  • Sample: reversed rows, T=2