The problem
Full attention lets every token look at all the tokens before it, and its cost grows with the square of the text length. OLMo 3 cuts the cost with sliding-window attention in three layers out of four: a token attends only to the most recent window tokens, itself included. Every fourth layer uses full causal attention, and the last layer is always full. In the released configurations the window is 4,096 tokens.
You will build the masks that implement this. A mask is a table of True (allowed) and False (forbidden), with one row per query position and one column per key position (section 7).
Which layers are full
Layer , counting from 0, is "full" when ( divided by 4 leaves remainder 3), and the last layer is "full" whatever its index; all the others are "sliding".
Example. 8 layers:
sliding, sliding, sliding, full, sliding, sliding, sliding, full. For 6 layers:sliding, sliding, sliding, full, sliding, full: layer 5 is the last, so it is full although .
The sliding-window mask
Query may attend to key when both conditions hold:
- : no looking at the future (causal);
- : the key is among the last
windowtokens.
Example. and
window = 2. Row allows the keys and (when they exist):
1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 1 (1 is
True.) A full layer would also allow the ones at the bottom-left.
With window >= T every key is within the window, so the mask is the ordinary causal mask: a lower triangle.
What to implement
layer_types(n_layers): the list of"sliding"and"full".sliding_window_mask(T, window): the boolean mask.attention_masks(T, n_layers, window): one mask per layer, followinglayer_types. A full layer gets the plain causal mask.
Requirements
sliding_window_mask(T, window)withwindow >= Tis the ordinary causal mask.- No loops over positions.
Hint
Compare a column of query positions np.arange(T)[:, None] with a row of key positions np.arange(T)[None, :]: broadcasting builds the whole table at once, and the two conditions are two comparisons joined with &. In row the number of allowed positions is min(t + 1, window).