forked from ml-explore/mlx-swift
-
Notifications
You must be signed in to change notification settings - Fork 13
fix(metal): gate NAX to gen-18+ (M5/gen-17 miscompute) + regression test #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Regression guard for the M5-class (gen-17) NAX steel-gemm/qmm miscompute. | ||
| // | ||
| // The prism fork gates NAX to GPU gen-18+ (A19+) because the gen-17 NAX matmul | ||
| // path returns wrong results for fp16 GEMM at M>=8, N>=8192 (and quantized matmul | ||
| // at M>=64, N>=9216). If that gate regresses and NAX is (wrongly) enabled on a | ||
| // gen-17 device, a fp16 matmul crossing the NAX dispatch threshold diverges hard | ||
| // from the fp32 reference. This test fails in exactly that case. | ||
| // | ||
| // It is a no-op elsewhere: on GPUs where NAX is off or correct, the fp16 matmul | ||
| // tracks fp32 within normal half-precision accumulation error. | ||
|
|
||
| import Foundation | ||
| import MLX | ||
| import MLXRandom | ||
| import XCTest | ||
|
|
||
| class NAXGateRegressionTests: XCTestCase { | ||
|
|
||
| override class func setUp() { | ||
| setDefaultDevice() | ||
| } | ||
|
|
||
| /// fp16 matmul over a NAX-threshold-crossing shape must match the fp32 result | ||
| /// within half-precision accumulation error. Broken gen-17 NAX yields | ||
| /// O(1e-1)+ relative error (vs ~1e-2 correct), so a 0.05 relative-Frobenius | ||
| /// bound cleanly separates correct from mis-enabled NAX. | ||
| func testNAXThresholdMatmulMatchesReference() { | ||
| MLXRandom.seed(0) | ||
| let m = 16, k = 512, n = 16384 // M>=8, N>=8192 -> NAX-eligible steel-gemm shape | ||
|
|
||
| let a = MLXRandom.normal([m, k]) | ||
| let b = MLXRandom.normal([k, n]) | ||
|
|
||
| let ref = a.matmul(b) // fp32 reference | ||
| let got = a.asType(.float16).matmul(b.asType(.float16)).asType(.float32) | ||
|
|
||
| let diffFro = ((got - ref) * (got - ref)).sum().sqrt().item(Float.self) | ||
| let refFro = (ref * ref).sum().sqrt().item(Float.self) | ||
| let relErr = diffFro / refFro | ||
|
|
||
| XCTAssertLessThan( | ||
| relErr, 0.05, | ||
| "fp16 NAX-threshold matmul rel-Frobenius error \(relErr) exceeds 0.05 " | ||
| + "(~1e-2 expected for correct fp16). NAX is likely mis-enabled on this GPU " | ||
| + "generation — check the gen-18 gate in is_nax_available().") | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.