fix: mmulStrassen returns the matrix product for every input shape - #217
Closed
tayal-sarthak wants to merge 6 commits into
Closed
fix: mmulStrassen returns the matrix product for every input shape#217tayal-sarthak wants to merge 6 commits into
tayal-sarthak wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #217 +/- ##
==========================================
+ Coverage 68.88% 70.15% +1.26%
==========================================
Files 49 49
Lines 5904 5910 +6
Branches 1060 1076 +16
==========================================
+ Hits 4067 4146 +79
+ Misses 1824 1751 -73
Partials 13 13 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Closes #114.
Summary
mmulStrassendoes not compute the matrix product. Three separate defects were found, the middle one silent.1. The two off diagonal output blocks are swapped
The recombination step writes
C12whereC21belongs and the reverse:The seven Strassen products themselves are correct, only the placement was crossed.
The recursive path activates once both dimensions pass 512, below that the call is handed to
mmul, which is why nothing caught this. Onmain, with small integers that keep every intermediate exact in a float64:mmulThe first disagreement always sits at column
n / 2, the start of the top right block. Roughly half the result is wrong, returned with no error.2. Shapes where
max(r1, r2)differs frommax(c1, c2)crashBoth operands were padded to
max(r1, r2)bymax(c1, c2), which is only a valid pair to multiply when those two happen to be equal. A 3x2 by 2x4 product raisedTypeError: Cannot read properties of undefined (reading '0')from inside the multiplication.Padding into a common square of side
max(r1, c1, r2, c2)fixes this. Zeros never reach the top leftr1byc2corner, which is the part that gets returned.3. The result was zero padded, the case reported in the issue
new Matrix([[1, 2]]).mmulStrassen(new Matrix([[1, 2], [3, 4]]))gave[[7, 10], [0, 0]]. The product now comes back atr1byc2, matchingmmul.Result
mmulStrassenagrees withmmulon every shape tried, including sizes above the recursion threshold where the two now match entry for entry.On the performance question raised in the thread, the algorithm does earn its place at the sizes it engages, measured on a 1024 x 1024 product:
mmulmmulStrassenNotes
A degenerate pair such as 0x2 by 2x0 now comes back at 0x0 rather than padded to 2x2. That shape was previously pinned in place with a comment recording 0x0 as the mathematically correct answer.
The dimension mismatch warning is untouched, since
mmuldoes not validate its operands either.