Skip to content

Fix locale-dependent float printing in GGUF metadata#17331

Merged
JohannesGaessler merged 3 commits intoggml-org:masterfrom
ssam18:fix-locale-float-printing
Mar 4, 2026
Merged

Fix locale-dependent float printing in GGUF metadata#17331
JohannesGaessler merged 3 commits intoggml-org:masterfrom
ssam18:fix-locale-float-printing

Conversation

@ssam18
Copy link
Copy Markdown
Contributor

@ssam18 ssam18 commented Nov 17, 2025

I was running some llama.cpp examples on a system with a German locale (de_DE) and noticed something odd - when llama-cli printed out the model metadata, all the float values had commas as decimal separators (like "0,000000") instead of periods. But when I ran llama-perplexity on the same model, it used periods normally.

After some digging, I found the issue was in the gguf_data_to_str() function in llama-impl.cpp. It was using std::to_string() to format floats, which respects the system's LC_NUMERIC locale setting. So depending on which tool you used and what locale it was running with, you'd get different formatting.

I've changed it to use std::ostringstream with std::locale::classic() instead, which always formats floats with a period as the decimal separator, regardless of the system locale. This should make the output consistent across all tools and locales.

Fixes #10613

@ssam18 ssam18 requested a review from ggerganov as a code owner November 17, 2025 21:31
@ssam18
Copy link
Copy Markdown
Contributor Author

ssam18 commented Nov 17, 2025

This was a fun one to track down! The locale dependency was subtle enough that most people probably wouldn't notice it unless they happened to be running with a non-English locale. I'm glad I could spot the inconsistency and fix it.

@SamareshSingh
Copy link
Copy Markdown

@ggerganov Please let me know if you need further help to finish this PR?

@ggerganov
Copy link
Copy Markdown
Member

@JohannesGaessler Can you confirm it fixes #10613?

@JohannesGaessler
Copy link
Copy Markdown
Contributor

This makes the prints in llama_model_loader consistent. However, these are not the only instances of inconsistent prints. If you scroll further down you'll see that essentially all floating-point prints are inconsistent. I think it would be preferable to use consistent locale settings for the llama.cpp binaries than to try and squash every single instance of std::to_string in the llama.cpp codebase.

@JohannesGaessler
Copy link
Copy Markdown
Contributor

Originally all binaries were in the examples folder but nowadays there are also binaries in the tools folder (which I assume you overlooked?).

@github-actions github-actions Bot added examples SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language labels Nov 30, 2025
@ssam18
Copy link
Copy Markdown
Contributor Author

ssam18 commented Nov 30, 2025

Originally all binaries were in the examples folder but nowadays there are also binaries in the tools folder (which I assume you overlooked?).

yeah, just now I fixed it.

Comment thread tools/llama-bench/llama-bench.cpp Outdated
@ngxson
Copy link
Copy Markdown
Contributor

ngxson commented Dec 1, 2025

I'm wondering if there is any tricks to avoid adding this to every example. It adds a bit of boilerplate everywhere IMO.

Since most examples initialize common_params right at the beginning of main(), can we put this inside the constructor of common_params instead?

@SamareshSingh
Copy link
Copy Markdown

I'm wondering if there is any tricks to avoid adding this to every example. It adds a bit of boilerplate everywhere IMO.

Since most examples initialize common_params right at the beginning of main(), can we put this inside the constructor of common_params instead?

Yes, this is a good idea to put this inside the constructor. I am working on this

@ngxson
Copy link
Copy Markdown
Contributor

ngxson commented Dec 1, 2025

Ah sorry, common_params_parse should be the better place. common_params doesn't have a constructor

@JohannesGaessler
Copy link
Copy Markdown
Contributor

I don't think we use common_params in every example/tool though. And my opinion is that a library should not be changing global locale settings upon use, that should only be done in the main function.

@ssam18 ssam18 force-pushed the fix-locale-float-printing branch 2 times, most recently from 6fd289e to 7196124 Compare December 1, 2025 21:37
@ngxson
Copy link
Copy Markdown
Contributor

ngxson commented Dec 1, 2025

@JohannesGaessler for all examples don't use it, we can set it explicitly on main.

I think doing it in common_params_parse does make sense though, as the libcommon also have various code paths to setup the CLI environment, including logging system, console, etc. So adding this as a "formatting setup" does make sense IMO

Copy link
Copy Markdown
Contributor

@JohannesGaessler JohannesGaessler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see us changing global settings anywhere in common_params_parse. That is fundamentally not something that a library should be doing upon use unless it is done by an explicit and dedicated function.

@ngxson
Copy link
Copy Markdown
Contributor

ngxson commented Dec 1, 2025

Technically the global settings are set by common_init(), but I agree that it's only settings bound to the application, not system settings like locale.

I'm ok with both approaches (either explicitly set on main() or libcommon), but if you don't prefer to add it to libcommon then no problem, @ssam18 I think you will need to revert the last commit

@ssam18 ssam18 force-pushed the fix-locale-float-printing branch 2 times, most recently from fbe1865 to 050fc7f Compare December 1, 2025 23:27
@ssam18
Copy link
Copy Markdown
Contributor Author

ssam18 commented Feb 14, 2026

What are we waiting on for this PR? Did I miss something?

@JohannesGaessler
Copy link
Copy Markdown
Contributor

Sorry, IIRC there were CI failures where I wasn't sure whether they were being caused by this PR. I meant to look into it again and then forgot.

@ssam18
Copy link
Copy Markdown
Contributor Author

ssam18 commented Mar 2, 2026

I want to followup for this PR. please get it reviewed.

@JohannesGaessler
Copy link
Copy Markdown
Contributor

In that case, please rebase on top of master, resolve the merge conflicts, and ensure that there are no CI failures.

ssam18 and others added 3 commits March 3, 2026 07:58
Add std::setlocale(LC_NUMERIC, "C") to all 16 binaries in the tools/
directory to ensure consistent floating-point formatting.
@ssam18 ssam18 force-pushed the fix-locale-float-printing branch from a4ef097 to 1fb6374 Compare March 3, 2026 14:02
@ssam18
Copy link
Copy Markdown
Contributor Author

ssam18 commented Mar 3, 2026

I am not sure the failure we are seeing is related to this change.

Copy link
Copy Markdown
Contributor

@JohannesGaessler JohannesGaessler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the CI failures seem to be unrelated.

@JohannesGaessler JohannesGaessler merged commit cb8f4fa into ggml-org:master Mar 4, 2026
77 of 78 checks passed
Ethan-a2 pushed a commit to Ethan-a2/llama.cpp that referenced this pull request Mar 20, 2026
* Set C locale for consistent float formatting across all binaries.

* Add C locale setting to all tools binaries

Add std::setlocale(LC_NUMERIC, "C") to all 16 binaries in the tools/
directory to ensure consistent floating-point formatting.

* Apply suggestion from @JohannesGaessler

---------

Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* Set C locale for consistent float formatting across all binaries.

* Add C locale setting to all tools binaries

Add std::setlocale(LC_NUMERIC, "C") to all 16 binaries in the tools/
directory to ensure consistent floating-point formatting.

* Apply suggestion from @JohannesGaessler

---------

Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
rsenthilkumar6 pushed a commit to rsenthilkumar6/llama.cpp that referenced this pull request May 1, 2026
* Set C locale for consistent float formatting across all binaries.

* Add C locale setting to all tools binaries

Add std::setlocale(LC_NUMERIC, "C") to all 16 binaries in the tools/
directory to ensure consistent floating-point formatting.

* Apply suggestion from @JohannesGaessler

---------

Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples server SYCL https://en.wikipedia.org/wiki/SYCL - GPU programming language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: inconsistent locale for printing GGUF kv data across examples

5 participants