TorchLean NN: Attention #
Softmax over the visible keys of each attention row.
The mask uses true for a visible key. Gathering those keys before softmax keeps masked scores
out of both the row maximum and its denominator. An entirely masked row stays zero. In particular,
we do not approximate exclusion with a large negative constant, whose behavior depends on the
score scale and cannot represent an empty row.
Instances For
Attention with optional affine projection biases and dropout on softmax probabilities.
The operation order is projection, head splitting, scaled dot products, masked softmax, probability dropout, value aggregation, and output projection. Dropout therefore removes individual query/key contributions before values are mixed. The existing fused attention path remains available to the bias-free constructors without probability dropout.
Instances For
Include one state slot only when the corresponding option is enabled.
Instances For
Initial value for an optional state slot, with no placeholder when it is disabled.
Instances For
Native initialization follows the same optional shape list as the ordinary state pack.
Instances For
Read one optional state slot and return the remaining, still shape-indexed references.
Instances For
Multi-head self-attention layer for a sequence
(sequenceLength × modelWidth) → (sequenceLength × modelWidth).
This layer packs the four projection matrices (Wq, Wk, Wv, Wo) and calls the TorchLean attention
primitive. An optional boolean mask of shape (sequenceLength × sequenceLength) can be provided,
for example for causal masking.
PyTorch analogy: torch.nn.MultiheadAttention(embed_dim=modelWidth, num_heads=headCount) in
self-attention mode.
Instances For
Multi-head self-attention with a trainable bias on the final output projection.
The Q/K/V projections remain bias-free. This is the parameterization used in Karpathy's educational GPT implementation: the three per-head projections are linear maps without bias, while the projection applied after concatenating the heads is affine.
Instances For
Configurable affine self-attention with optional probability dropout.
The first four state slots remain queryWeight, keyWeight, valueWeight, outputWeight.
Enabling input bias appends queryBias, keyBias, valueBias; enabling output bias appends
outputBias; enabling dropout appends its non-trainable scalar probability. Disabled options
allocate no state slots. All biases start at zero and consume no initialization seeds.