Read the equation
For each query, compute its dot product with every key and divide by the square root of the query/key feature width. The transposed key axes put corresponding features into the contraction. These are unnormalized attention logits; the mask and softmax occur in the surrounding code.
Terms & definitions
The recovered passage does not supply a separate symbol table. Read the source context for the notation used in this version; familiar letters may carry different meanings in other builds.
How it works
Read the definition from its right-hand expression to the quantity on the left. Its surrounding source records the intended input, state, or parameter context. Where the source leaves an operation unspecified, this page preserves that gap.
Assumptions & limits
Source recovery establishes that the expression appears in this record. Its mathematical validity, implementation behavior, and interpretation must be assessed separately. See the linked source and research notes for the checks available for this version.
Original context
Read the surrounding source passage
k = k.transpose(0, 1)
v = v.transpose(0, 1)
attn_weight = q @ k.transpose(-2, -1) / math.sqrt(q.shape[-1])
attn_weight += attention_mask
attn_weight = torch.softmax(attn_weight, dim=-1,
dtype=torch.float32).to(q.dtype)Exact original notation
attn_weight = q @ k.transpose(-2, -1) / math.sqrt(q.shape[-1])
Related builds
Sources & provenance
attn_weight = q @ k.transpose(-2, -1) / math.sqrt(q.shape[-1])
attn_weight = q @ k.transpose(-2, -1) / math.sqrt(q.shape[-1])