Skip to content

fix: add POSIX functionality for Linux compilation - #51

Merged
ggerganov merged 2 commits into
ggml-org:masterfrom
valentynbez:master
Mar 22, 2023
Merged

ggerganov merged 2 commits into
ggml-org:masterfrom
valentynbez:master

Conversation

@valentynbez

@valentynbez valentynbez commented Mar 12, 2023 •

Copy link
Copy Markdown
Contributor

Small fix to compile binaries properly on Linux:

@prusnak

prusnak commented Mar 12, 2023

Copy link
Copy Markdown
Contributor

Can you add a short comment why this is required? (I assume some functions are not recognized but not obvious which ones)

@valentynbez

Copy link
Copy Markdown
Contributor Author

Sure!

@prusnak

prusnak commented Mar 12, 2023 •

Copy link
Copy Markdown
Contributor

Sure!

I meant into the source code just above the #define

Btw the issue #54 mentions _POSIX_C_SOURCE=199309L; why did you chose newer standard and thus less compatibility?

@prusnak

prusnak commented Mar 13, 2023 •

Copy link
Copy Markdown
Contributor

Small fix to compile binaries properly on Linux:

Which OS and which GCC version do you use?

For me the master compiles just fine on Debian 11 with GCC 10.2.1 (even without the proposed define).

@valentynbez

Copy link
Copy Markdown
Contributor Author

CentOS7 with GCC 10.2.0

@ggerganov

Copy link
Copy Markdown
Member

This flags has been discussed in whisper.cpp too and I still don't know when it should be added and when not.
Hopefully someone can clarify.

ggml-org/whisper.cpp#37
ggml-org/whisper.cpp#576

@ggerganov ggerganov added the help wanted Needs help from the community label Mar 14, 2023
@prusnak

prusnak commented Mar 18, 2023 •

Copy link
Copy Markdown
Contributor

Now thinking more about this, probably the cleanest option is to add compilation flags to the build system like suggested in ggml-org/whisper.cpp#37 + ggml-org/whisper.cpp#576

CFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
CXXFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE

We can probably set this for all compilers on all OSes, because either a compiler understands the flag and sets the value to the supported level or the flag is ignored.

@valentynbez can you confirm this fixed the issue for you on CentOS 7?

@gjmulder gjmulder added the build Compilation issues label Mar 20, 2023
@ggerganov
ggerganov merged commit 9794052 into ggml-org:master Mar 22, 2023
spiritbuun referenced this pull request in spiritbuun/buun-llama-cpp Mar 27, 2026
- turbo4 K+V results on Qwen3.5-27B (-0.32% vs q8_0) and Qwen3-14B (+6.3%)
- Sparse V dequant benchmarks: MoE native dequant +10.9% at 8K
- Gemma-3 turbo3 results post-iSWA fix (+3.3%)
- KVLinC no-K-rotation negative result
- Speculative decoding negative result
- CUDA 13.2 compatibility verified
- Experiments #31, #39, #42, #45, #49, #50, #51 status updates

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
* fix: add POSIX functionality for Linux compilation

* fix: older standard for compatibility
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
* fix: add POSIX functionality for Linux compilation

* fix: older standard for compatibility
phuongncn pushed a commit to phuongncn/llama.cpp-gx10-dgx-sparks-deepseekv4 that referenced this pull request Apr 28, 2026
* NEON Flash Attention: add support for Q8_0, Q4_0, Q4_1

* NEON Flash Attention: quantized K*Q for q4_0

I could finally take advantage of the matrix multiplication
templates. We get quite a bit of speedup that way for q4_0:
For Gemma-2b using mul_mat_qX_0_q8_0<DequantizerQ40, q_step>
results in PP-2048 = 287 t/s vs 268 t/s when converting the
q4_0 k-cache and Q to fp16 and using fp16 multiplication.

* NEON Flash Attention: quantized K*Q for q4_1

* NEON Flash Attention: quantized K*Q for q8_0

This makes quite a bit of difference:
For Gemma2-2b PP-8192 is 228 t/s with quantized K*Q vs
178 t/s when converting things to fp16 and using fp16
matrix multiplication.
We have PP-512 = 307 t/s, so PP-8192 is now ~75% of the
performance of PP-512. In contrast, llama.cpp with Q8_0
cache is 38% of PP-512.

* Zen4 Flash Attention: quantized K*Q for q4_0, q4_1, q8_0

* AVX2 Flash Attention: quantized K*Q for q4_0, q4_1, q8_0

* Tidy up FlashMS

* Delete no longer used stuff

With the usage of quantized matrix multiplications for
quantized k- and/or v-cache, we no longer need the
helper methods loading entire rows.

* Disallow mixing bf16 with other types for kv caches

---------

Co-authored-by: Iwan Kawrakow <[email protected]>
ljubomirj pushed a commit to ljubomirj/llama.cpp that referenced this pull request May 6, 2026
* fix: add POSIX functionality for Linux compilation

* fix: older standard for compatibility
AlexiAlp pushed a commit to minghaop/llama.cpp that referenced this pull request Jun 2, 2026
* fix: add POSIX functionality for Linux compilation

* fix: older standard for compatibility
HEchternacht pushed a commit to HEchternacht/llama.cpp that referenced this pull request Jul 18, 2026
…org#51)

* kv-cache: add optional per-channel K-cache mean-centering (Q4_0 only)

GGML_TYPE_Q4_0 is a symmetric quantizer, so a K channel with a real,
consistent nonzero mean across tokens wastes dynamic range encoding
that constant bias. This adds an opt-in mechanism that subtracts a
fixed per-(kv-head, channel) bias from K right before it is written
into the cache in llama_kv_cache::cpy_k(), gated strictly to
k->type == GGML_TYPE_Q4_0.

Subtracting the same bias from every cached key is exactly
softmax-invariant: it adds the same constant (q . k_bar) to every
logit in a query's row, which softmax does not see. Nothing else in
attention needs to change, so this is a zero decode-time-cost
quantization-fidelity improvement.

llama_context_params gets a new path_kv_mean_center field (default
NULL); llama_init_from_model() hard-rejects it when the K cache type
isn't Q4_0, matching the existing convention for other cache-type-gated
mismatches (e.g. "V cache quantization requires flash_attn").
llama_kv_cache::load_kv_mean_center() loads the bias tensors from a
GGUF file and applies them; a require_q4_0 escape hatch (used only by
tests) exists to validate the underlying math against an unquantized
cache without confounding it with real quantization error.

Also tags the K tensor right before cpy_k() with the existing cb()
graph-build hook ("k_cache_in"), so calibration tooling can capture
exactly the tensor that gets written into the cache regardless of
what RoPE/rotation preprocessing a given architecture applies upstream.

* common: add --kv-mean-center flag and GGUF bias-file writer

Adds the CLI-facing side of K-cache mean-centering: common_params
gains kv_mean_center_path (plumbed into llama_context_params via
common_context_params_to_llama), and --kv-mean-center takes a path to
a bias file generated by tools/kv-mean-center.

The bias file format is a small GGUF file with one F32 tensor per
layer, named "kv_bar.blk.<il>.k", holding n_embd_head_k(il) *
n_head_kv(il) values laid out as [n_embd_head_k, n_head_kv]. The
writer lives in common/kv-mean-center.* so it can be shared between
the calibration tool and the test suite (which needs to synthesize a
bias file to check the underlying math).

* tools: add llama-kv-mean-center calibration tool

New tool, following the tools/imatrix convention: loads a model, runs
a plain text calibration corpus through it in chunks, and captures the
"k_cache_in" tensor tagged in llm_graph_context::build_attn() via the
same backend-scheduler eval-callback mechanism llama-imatrix uses to
capture activations (params.cb_eval). The per-(head,channel) mean
across all calibration tokens is written out as a bias file consumable
by --kv-mean-center.

* tests: add K-cache mean-centering regression + invariance tests

Uses the same tiny-synthetic-model machinery as test-llama-archs.cpp
(llama_model_saver + llama_model_init_from_user with a deterministic
random tensor initializer), trimmed to plain LLM_ARCH_LLAMA, to check:

- regression safety: two independent contexts with centering disabled
  produce bit-for-bit identical logits, and a Q4_0 K cache with no
  bias file loaded still decodes normally (cpy_k()'s new code path is
  a true no-op when k_bar is empty).
- the --kv-mean-center gate: a non-Q4_0 K cache with a bias file is
  hard-rejected by llama_init_from_model(), while Q4_0 succeeds
  end-to-end through a real decode.
- the softmax-invariance argument itself, against an unquantized F32 K
  cache with a synthetic nonzero bias applied through the exact same
  cpy_k() code path (bypassing the Q4_0 gate via
  load_kv_mean_center()'s require_q4_0=false test seam): output logits
  match the uncentered baseline to fp32 rounding (nmse ~5e-10 in
  practice), confirming the math without confounding it with real
  quantization error.

* docs: document K-cache mean-centering

Explains the technique, the softmax-invariance argument, usage, the
bias file format, and the current scope/limitations (Q4_0-only, plain
KV cache only, standard dense/GQA attention path only).

* kv-mean-center: address review feedback

- kv-mean-center.cpp: the K tensor captured at "k_cache_in" can be
  F16 or BF16 depending on backend/compute settings, not just F32.
  The collector was asserting F32-only and reinterpreting raw bytes
  as float*, which either aborts or silently computes a garbage mean
  on non-F32 backends. Now accepts F32/F16/BF16 and converts to F32
  via ggml_fp16_to_fp32_row/ggml_bf16_to_fp32_row before accumulating.
- common/arg.cpp: wire --chunks into the LLAMA_EXAMPLE_KV_MEAN_CENTER
  example set so the flag the tool's own README documents is actually
  available, instead of being silently filtered out by the shared arg
  parser.
- docs/kv-mean-center.md: replace non-ASCII "~=" and "." characters
  (was U+2248 and U+00B7) with ASCII equivalents, matching the
  project's ASCII-only docs convention.
ckti pushed a commit to ckti/llama.cpp-consolidated that referenced this pull request Sep 13, 2026
Re-port of the fork's --kv-mean-center feature onto upstream master. Adds
common/kv-mean-center, the arg + LLAMA_EXAMPLE_KV_MEAN_CENTER, the llama.h API,
and the kv-cache/context/graph hooks. Conflicts resolved against upstream's
rewritten kv-cache/context (kept only the path_kv_mean_center guard).
polivitus added a commit to polivitus/llama.cpp that referenced this pull request Sep 16, 2026
- Патчи ggml-org#44-ggml-org#51 (8 новых): Svelte-компоненты, Server Error, MCP,
  Model Info, formatters, vocab_type, tooltipSide
- patch-src.mjs: APPLIED_MARKERS (идемпотентность)
- ru.json: 246 → 254
- Удалён message-actions.patch (покрыт ggml-org#44)
- Все конфликты решены
imaami pushed a commit to imaami/llama.cpp that referenced this pull request Oct 10, 2026
Re-port of the fork's --kv-mean-center feature onto upstream master. Adds
common/kv-mean-center, the arg + LLAMA_EXAMPLE_KV_MEAN_CENTER, the llama.h API,
and the kv-cache/context/graph hooks. Conflicts resolved against upstream's
rewritten kv-cache/context (kept only the path_kv_mean_center guard).

(cherry picked from commit 7fb3d5b)
Assisted-by: GitHub Copilot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Compilation issues help wanted Needs help from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error: 'CLOCK_MONOTONIC' undeclared

4 participants