Repository navigation
cuda: potential fix for Q8_1 correction sum - #31247
Conversation
|
Hi @mmalpe-nv, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
ORippler
left a comment
There was a problem hiding this comment.
See
https://github.com/ggml-org/llama.cpp/actions/runs/37794678453/job/113382326226?pr=28713
for an instance of sporadic CI failure caused by the old behavior. Imo it's not worth it to add a dedicated regression test for this, but I'll defer this to @ggml-org/ci
|
Looks like llama.cpp/ggml/src/ggml-cuda/quantize.cu Line 505 in 889f2ec Can you fix that as well? |
Co-authored-by: Gaurav Garg <gaugarg@nvidia.com>
* cuda: potential fix for Q8_1 correction sum * Update ggml/src/ggml-cuda/quantize.cu Co-authored-by: Gaurav Garg <gaugarg@nvidia.com> * addressed review comment to change the quantize_mmq_q8_1 flow as well --------- Co-authored-by: Gaurav Garg <gaugarg@nvidia.com> (cherry picked from commit 8fc0376)
|
Hey @mmalpe-nv and @gaugarg-nv since you approved and merged it. I've been running down new issue I hit after upgrade to latest HEAD of llama.cpp and though binary search arrived at this merge commit. I am able to reproduce this fault each time, below is more details, used llm to make it easier to make sense I’m seeing a repeatable thinking-off tool-replay regression on ROCm after My setup is a Framework Desktop with Ryzen AI Max+ 395, These tests use a downstream patched build. I have not confirmed reproduction on pristine upstream. The failing case replays a completed tool call with thinking disabled. The expected response is exactly: The newer build instead returns: Results from five seeds and one additional temperature-zero check:
For the revert comparison, only the physical The six-case comparison used greedy DFlash2 at depth three. The initial short checks also showed the symptom without DFlash2. For reproduction, use the same model and Jinja template on both revisions, preserve reasoning history, and replay the completed tool turn below. Ensure the template receives curl http://127.0.0.1:8080/v1/chat/completions \
-H 'Content-Type: application/json' \
--data-binary @- <<'JSON'
{
"model": "flash-next",
"reasoning_effort": "none",
"chat_template_kwargs": {"enable_thinking": false},
"temperature": 0.7,
"top_p": 0.8,
"top_k": 20,
"min_p": 0.0,
"presence_penalty": 1.5,
"repeat_penalty": 1.0,
"seed": 42,
"cache_prompt": false,
"max_tokens": 4096,
"stream": false,
"tool_choice": "none",
"messages": [
{
"role": "user",
"content": "Call lookup_record exactly once with query.key ALDER. After its result reply exactly the value returned, with no explanation."
},
{
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "lookup-1",
"type": "function",
"function": {
"name": "lookup_record",
"arguments": "{\"query\":{\"key\":\"ALDER\"}}"
}
}]
},
{
"role": "tool",
"tool_call_id": "lookup-1",
"content": "ALDER_937261"
}
],
"tools": [{
"type": "function",
"function": {
"name": "lookup_record",
"description": "Read the exact stored value for a record key.",
"parameters": {
"type": "object",
"additionalProperties": false,
"required": ["query"],
"properties": {
"query": {
"type": "object",
"additionalProperties": false,
"required": ["key"],
"properties": {"key": {"type": "string"}}
}
}
}
}
}]
}
JSONRepeat with seeds This is a simplified direct-server replay of the tested fixture, not a separately validated pristine-upstream reproducer. The result isolates a behavior change associated with the correction calculation, but does not establish that the numerical correction itself is wrong. |
…bb02c9); SETTINGS: conv-state fusion with set_rows rollback, ggml-org#31247 Q8_1 sums Assisted-by: Claude Opus 5.5 (Claude Code)
…bb02c9); SETTINGS: conv-state fusion with set_rows rollback, ggml-org#31247 Q8_1 sums Assisted-by: Claude Opus 5.5 (Claude Code)
* cuda: potential fix for Q8_1 correction sum * Update ggml/src/ggml-cuda/quantize.cu Co-authored-by: Gaurav Garg <gaugarg@nvidia.com> * addressed review comment to change the quantize_mmq_q8_1 flow as well --------- Co-authored-by: Gaurav Garg <gaugarg@nvidia.com> (cherry picked from commit 8fc0376)
…bb02c9); SETTINGS: conv-state fusion with set_rows rollback, ggml-org#31247 Q8_1 sums Assisted-by: Claude Opus 5.5 (Claude Code)
Overview
This PR includes the changes for updating the Q8_1 correction sum to use quantized input values which are consistent with the integer dot product and CPU behavior. It addresses the reproduced intermittent Q4_1 MUL_MAT failure on Tesla T4.
Additional information
Failed testcase
On Tesla T4:
Command run
./build-ci-release/bin/test-backend-ops -j 1 -b CUDA0 \ -o 'MUL_MAT(type_a=q4_1,type_b=f32,m=16,n=1,k=32,bs=[1,1],nr=[1,1],per=[0,1,2,3],k_v=0,o=1,src_overlap=0,m_v=0,pad=0)'Captured failure -
Calculation
A
Q4_1weight is represented as:The F32 input B is internally quantized:
Here,
d_Aandd_Bare scales,q_Aandq_Bare integer codes, andm_Ais the weight offset. Zero-input blocks are handled explicitly by the code.Ignoring additional floating point rounding, the dot product becomes:
The main contribution already uses quantized B. The correction sum differed between CPU and CUDA:
half(sum(original B))half(d_B × sum(q_B))Using original inputs in the correction and quantized inputs in the main contribution can disrupt cancellation between the two terms.
Observed Difference
For the captured failing input:
For the first output row:
The correction mismatch explains the dominant output shift. Additional arithmetic rounding explains the remaining difference.
This was a numerical accuracy discrepancy, not overflow.
Why the Test Failed
The test measures normalized squared disagreement across the outputs:
The tolerance is
5e-4=0.0005. It limits output disagreement, not individual output magnitudes.Before the change:
After the change, for the same saved inputs:
Computing the correction from the quantized input codes removes the dominant shift for this sample. Small rounding differences remain.
The tolerance is unchanged; the fix reduces the output disagreement.
Validation
Validation below was performed on Tesla T4 at the investigated revision:
4d756bc72bf00a4aacf410ae15a2d315f3db400d5.36123e-46.07016e-80.04119160.0054600The independent calculation uses the decoded Q4_1 weights and original F32 inputs with double precision accumulation. The change reduced RMSE by approximately 7.5× for this sample.
Additional validation on that revision passed:
k=32.k=256.MUL_MATcases.Requirements