-
Notifications
You must be signed in to change notification settings - Fork 12.8k
ggml-quants : fix make_qp_quants NANs and IQ1 assertion errors #15379
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
Conversation
See #7825 (which also proposes the same fix for |
This change was made in #7955 |
@CISC Ah, so the |
Also, should maybe llama.cpp/ggml/src/ggml-quants.c Line 493 in fb573f4
|
I don't think so. It's a sum of squares, which should always be positive. |
Well, that's what I meant, do we assert this somewhere? If so, why check if it's larger than 0. I suppose this should also check for Lines 619 to 626 in 50aa938
|
Personally, I think using
Yes, this is a good idea. But I consider this out of scope for this PR, which is about handling problematic edge cases in quantization functions (and which have happened in the wild), and not necessarily imatrix validation, even if both are related. Still, it's a good idea, and I don't think any valid imatrix should/could have negative values. Arguably, even |
Well, you are wrong. The correct initial value for Here some math, so hopefully this will help to stop "fixing" it. We want to minimize where (assuming the denominator is not zero; if it is, there is no solution as any value of With that, we can now expand (1), plugin the optimum value of The first term on the right-hand side is a constant. Hence, to minimize
1 can happen if all 2 can happen if all |
This is kind of a follow up to #11773, with the fix from #11773 (comment) (which should fix #11764 and #12439).
make_qp_quants
doesn't check for divisions by zero with itssuml2
when calculating the resulting scale, and so I've added a check to avoid producing NAN scales that way. That function is used forQ2_K
,Q4_K
,Q5_K
, andIQ2_XXS
.In the future, I'm hoping to replace
make_qp_quants
with a saner quantization function from #12557. But until that's ready, might as well prevent some known existing errors.Somewhat relatedly, I've been studying the i-quants implementation, and in
IQ1_S
andIQ1_M
,-FLT_MIN
being used as the initialbest_score
seems wrong.llama.cpp/ggml/src/ggml-quants.c
Lines 4269 to 4287 in 21c17b5
-FLT_MIN
is-1.1754944e-38
, (which is the first normal non-zero value below 0).The assertion can fail in the situation where
sumq2
is always smaller or equal to5.9604645e-08
, andsumqx * sumqx
is zero (e.g. ifsumqx
is on the order of1e-24
) and then-FLT_MIN * 5.9604645e-08
is zero, so the branch setting the newbest_score
is never taken in that case.I'm pretty sure the intention was for the initial value of
best_scores
to be-FLT_MAX
(aka-3.4028235e+38
), which doesn't have the problem of rounding to zero (it can overflow to-INFINITY
, but that's also a reasonable starting value, since it would also be smaller than anysumqx*sumqx
).This should hopefully fix variants of #14788, and #6067.
cc @nicoboss, @bartowski1182, @schmorp since you've previously reported issues linked to this.
Make sure to read the contributing guidelines before submitting a PR