Fix ALiBi distance matrix to use a proper (q, k) outer difference#419
Closed
systemblueio wants to merge 1 commit into
Closed
Fix ALiBi distance matrix to use a proper (q, k) outer difference#419systemblueio wants to merge 1 commit into
systemblueio wants to merge 1 commit into
Conversation
The ALiBi position-bias matrix expanded both index vectors on axis 1, so `x1 - x2` was `(q, 1) - (k, 1)`. That throws for differing query and key lengths and collapses to a constant column when they match, so the bias was wrong wherever it mattered. Expand the key vector on axis 0 instead, matching the reference Python implementation (`x1[:, None] - x2[None, :]`), so the difference broadcasts to the intended `(q, k)` relative-distance matrix. Adds positional-encoding tests covering the relative-distance values and differing query/key lengths.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
ALiBi.alibiMatrixbuilds the relative-distance matrix like this:Both vectors are expanded on axis 1, so the shapes are
(q, 1)and(k, 1)andx1 - x2is(q, 1) - (k, 1). That broadcast throws whenever the query and key lengths differ, and when they are equal it collapses to a constant column ((offset + i) - i) instead of the(i - j)outer difference, so the ALiBi bias is wrong in the cases it is meant to handle.This expands the key vector on axis 0 instead, so the difference is
(q, 1) - (1, k)and broadcasts to the intended(q, k)matrix, matching the reference Python implementation inmlx(x1[:, None] - x2[None, :]). AddsMLXNNPositionalEncodingTestscovering the relative-distance values and a differing query/key length case. There is no existing issue for this; the bug was found while reading the layer.Note: I verified
swift build --build-testsandswift format lint --strictlocally, but could not run the suite here because SwiftPM on the command line cannot build the Metal shaders (per the README), so the new tests are scoped to the CPU stream and exercise on CI.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes