Causal Mask

Experimental alpha. Expect breaking changes and major bugs. Your feedback is much appreciated.

Causal Mask

Sets every score above the diagonal of a T × T grid to −∞. After a Softmax those become weights of exactly zero, so position t gives no weight to anything after t.

the causal mask

Each row is what one token looks at. With the mask on, it sees itself and what came before, and the dark cells above the diagonal count for nothing. Take the mask away and every token reads the ones after it too.

Shapes

A grid of scores […×T×T] in, the same shape out.

Parameters

None.

In PyTorch

def causal_mask(scores):
T = scores.shape[-1]
future = torch.triu(torch.ones(T, T, dtype=torch.bool), diagonal=1)
return scores.masked_fill(future, float("-inf"))

Notes

It belongs between the scores and the Softmax. Without it, a model trained to predict the next token can simply read it, and its loss falls to almost nothing while it learns nothing.

Making it

The Fabricator makes it with no mask. See The Bench and the Fabricator.