concept
Causal self-attention mask
For aligned token sequences, query position i can attend to key positions no later than i. True means allowed in this teaching convention; verify library semantics and padding constraints. Empty length produces an empty matrix.
Formal Syntax & Structure
artificial-intelligence syntaxartificial-intelligence
allowed[i][j] = (j <= i)Working Example
Example Snippetartificial-intelligence
def causal_mask(n): if n < 0: raise ValueError("length must be nonnegative") return [[j <= i for j in range(n)] for i in range(n)]
def prefix_means(values): total = 0 outputs = [] for i, value in enumerate(values, 1): total += value outputs.append(total/i) return outputsprint(causal_mask(3))print(prefix_means([2,4,9]))print(prefix_means([2,4,99])[:2])