Blanks in the Batch

TransformersMedium+20 XP+20

Blanks in the Batch

To process sentences of different lengths together, a batch pads the short ones with a <PAD> token until they are all the same length. Padding fixes the shapes, but attention has no idea those slots are blanks. Left alone, real words will spend part of their attention on them.

The fix is a padding mask, applied exactly like the causal mask: before the softmax, every score pointing at a <PAD> key is set to −∞-\infty−∞, so its weight comes out as exactly 0.

Task: write masked_attention(scores, tokens, causal).

  • scores holds one square grid per sentence in the batch. scores[b][i][j] is the (already scaled) score for query position i looking at key position j in sentence b.
  • tokens holds the batch's token lists. tokens[b][j] == "<PAD>" marks a blank.
  • If causal is True, also block every key to the right of the query (j>ij > ij>i), as a decoder does.

For every sentence and every query row, block the masked keys, softmax the remaining scores, and report blocked positions as 0.0. Return one grid of weights per sentence, with every value rounded to 4 decimal places.

Two rules settle the edge cases:

  • The mask blocks keys (columns) only. A <PAD> query still gets an ordinary row; its output is thrown away later anyway.
  • If a row has no unblocked key at all, all its weights are 0.0.
the
cat
sat
down
the
✓
×
×
×
cat
✓
✓
×
×
sat
✓
✓
✓
×
down
✓
✓
✓
✓

A triangle of blocked cells. Each row may only look at itself and everything to its left.

Examples

scores = [[[1.0, 0.0, 2.0], [0.5, 0.5, 3.0], [2.0, 1.0, 0.0]]]
tokens = [['the', 'cat', '<PAD>']]
causal = False
returns [[[0.7311, 0.2689, 0.0], [0.5, 0.5, 0.0], [0.7311, 0.2689, 0.0]]]
scores = [[[0.2, 1.0, 4.0, 4.0], [1.0, 2.0, 4.0, 4.0], [0.0, 0.0, 0.0, 4.0], [3.0, 1.0, 2.0, 0.0]]]
tokens = [['dogs', 'bark', 'loudly', '<PAD>']]
causal = True
returns [[[1.0, 0.0, 0.0, 0.0], [0.2689, 0.7311, 0.0, 0.0], [0.3333, 0.3333, 0.3333, 0.0], [0.6652, 0.09, 0.2447, 0.0]]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run