hexagon: store HMX flash-attention softmax accumulators in FP32#24389
Draft
njsyw1997 wants to merge 1 commit into
Draft
hexagon: store HMX flash-attention softmax accumulators in FP32#24389njsyw1997 wants to merge 1 commit into
njsyw1997 wants to merge 1 commit into
Conversation
Contributor
Author
|
@max-krasnyansky |
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.
Overview
Store the online-softmax cross-block accumulators of the HMX flash-attention prefill kernel in FP32 instead of FP16.
m(row max),l(row sum), andp_rowsummove to FP32;s_rowmaxstays FP16 (lossless — it's a max of fp16 values).Additional information
Q/K/Vare FP16 on both the HMX path and the Metal reference. However, the HMX kernel also keeps the running softmax statistics (m/l) and the per-blockp_rowsumin FP16 while these intermediate results are stored in fp32 in Metal. Since these values are re-quantized to FP16 for each KV block, the error accumulates over time.PPL does not directly capture this shift, but in some real long-context examples, we do observe a divergence from the CPU backend, while Metal remains aligned.
This PR will slow down prefill speed by roughly 5%.
Potential bug
It works before #23796. After that PR, it may randomly produce incorrect results in some builds on
Qwen3 4b Q4_0. Similar to the previous issue, a good build consistently produces correct results, while a bad build consistently produces incorrect results.Adding
#define FARF_HIGH 1to print log fixes the problem, which suggests that the bug is likely in one of the synchronization procedures.Requirements