-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Be more strict about converting float to double #458
Conversation
clang on macOS is apparently stricter, I'll clean this up using the warnings from the CI run. I'm not sure if the |
i assume inference speed changes will be minimal, and only really a thing with simd disabled? |
I believe |
I somewhat agree with:
However, I am not qualified to comment on the math itself. I can only say that changes like these require extra scrutiny, since having correct rounding is an integral part for the models to work properly. One example: 3afbe43#diff-6d9ce99fcb6f51ff76f59e479f6e6fc0bb62edef7442805d7a5bb15b23996b5dL482 To me it seems that calculations like this are very deliberate const float v0 = x[i*QK + l + 0]*id;
const float v1 = x[i*QK + l + 1]*id;
const uint8_t vi0 = ((int8_t) (round(v0))) + 8;
const uint8_t vi1 = ((int8_t) (round(v1))) + 8; and to correctly explicitly cast it would be const uint8_t vi0 = (uint8_t)(((int8_t)(round((double)v0))) + 8); this already shows why explicit typecastings can be more trouble than worth in many cases however the way you changed it to const uint8_t vi0 = (int8_t)roundf(v0) + 8; completely changes how the rounding and truncation works (+ the typecast still isn't correct) and require extra scrutiny and dialog between who originally wrote those calculations and anyone changing them. Like I said, I am not qualified enough to comment on the math itself. I am just advising anyone merging any changes to the calculations until the changes being made and the effects they have are completely understood. I am also not 100% certain whether this is a change which should go forward, but I'm not the one writing the formulas. As shown in the example above, explicit type-casting can also make longer formulas which include typecastings/truncations more obtuse by introducing long chains of extra types and parentheses. This is just my few cents. In any case, this should be a dialog between the people writing the formulas. It should be their choice on how to proceed. In any case, no decision should be based on such a superfluous thing as a compiler 'warning' you not to do something. You (should) understand better what you are doing than the compiler does. Therefore, for adding |
@anzz1 Thanks for your comments.
I absolutely agree in principle...
... but not in this specific case. To prove it, I added a test that will loop through all possible floats (except NaNs and infinities) and verifies that the result is the same. This should take several seconds, I'll have to check why it isn't properly run on the CI machines. Only the windows machine seems to run the test: I agree that putting explicit casts can make the code a bit longer, but ggml.c is already in a style where a lot of simple assignments on their own line are used, sometimes for the purpose of type conversions. So I don't think it would make things a lot harder to read in this case. |
When you're right, you're right. In this case of just 256 possible values any rounding errors do not materialize. Yeah, I shouldn't comment on the math stuff. 😄 To be honest there is room for improvement, there are some questionable choices like Anyway good work and I hope this will get attention from the maths wizards. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good change. Should have merged it before the refactoring.
If you resolve the conflicts, we can merge it
-Werror
is a great idea, but too early to add it. Want to get to a more stable state first
I have resolved the conflicts and looked over the changes again. I added a test for SILU, but I have disabled the test module to avoid long CI times and high load on the machines. For GELU there is some difference, but the way I understand it this is an approximation anyway. @anzz1 made some good points, but you @ggerganov seemed to like it, so I'll consider ready. It certainly requires a close look again. |
examples/main/main.cpp
Outdated
const int top_k = params.top_k; | ||
const double top_p = (double)params.top_p; | ||
const double temp = (double)params.temp; | ||
const double repeat_penalty = (double)params.repeat_penalty; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make the params
struct have doubles?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to change it too much, but we could alternatively make everything involved with the logits a float
instead, except maybe for sum
and cumsum
in llama_sample_top_p_top_k
.
After all, these three parameters are set by the user with 2 decimal places or so...
Test module is commented out in CMakeLists.txt because the tests may take a long time, depending on how much the compiler optimizes.
I disabled There are some code paths that haven't been updated yet, so we have to clear the remaining warnings there in |
fprintf(stderr, "%s : calculating perplexity over %d chunks\n", __func__, seq_count); | ||
|
||
for (int i = 0; i < seq_count; ++i) { | ||
int start = i * params.n_ctx; | ||
int end = start + params.n_ctx - 1; | ||
int end = start + params.n_ctx - 1; // TODO: this is not optimal, e.g. it makes the batch 511 instead of 512 | ||
// it is better to always be power of 2 for better performance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@glinscott
Might want to fix this on master
.
It's better to compute with powers of 2 for optimal performance
This enables
-Wdouble-promotion
and syncs theMakefile
andCMakeLists.txt
with regards to warnings.Reasoning:
The llama.cpp codebase depends on the correct use of number types, whether those are
float
,double
or some of the spezialized types such as q4_0. Converting from one type to another should be a concious decision and not happen by accident.In order to avoid any type promotion warnings, I have updated the code base, sometimes by making an implicit cast explicit, but in some places I did change the actual semantics. I'm not confident at this point that all changes are good.
Consequences:
Further steps if and when this PR is merged:
-Werror
?