Skip to content

ggml : implement fast walsh-hadamard transform for kv rotation (#21352)#22631

Merged
am17an merged 1 commit into
ggml-org:masterfrom
AlrIsmail:feature/fwht-kv-rotation
May 5, 2026
Merged

ggml : implement fast walsh-hadamard transform for kv rotation (#21352)#22631
am17an merged 1 commit into
ggml-org:masterfrom
AlrIsmail:feature/fwht-kv-rotation

Conversation

@AlrIsmail

@AlrIsmail AlrIsmail commented May 2, 2026

Copy link
Copy Markdown
Contributor

Overview

Resolves #21352

Replaces the $O(N^2)$ matrix multiplication for Hadamard KV-cache rotation with an $O(N \log N)$ Fast Walsh-Hadamard Transform (FWHT).

Instead of adding a new ggml_op, this uses a hint flag (GGML_HINT_SRC0_IS_HADAMARD) to dynamically route the computation to the FWHT kernel in ggml-cpu

Additional information

The FWHT kernel uses a hybrid SIMD approach: a clean scalar loop for initial passes, and AVX/AVX-512/NEON intrinsics for the main workload. This maximizes performance while avoiding complex code.

Benchmarks (Apple Silicon M-series, N=128, M=4096, 8 Threads):

  • Baseline (mul_mat): 1285.84 ms
  • FWHT (SIMD): 303.66 ms

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - Used an AI coding assistant to analyze code and draft the SIMD intrinsics.

@AlrIsmail AlrIsmail requested review from CISC and ggerganov as code owners May 2, 2026 22:20
@github-actions github-actions Bot added the ggml changes relating to the ggml tensor library for machine learning label May 2, 2026

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should use ggml_vec* methods

@AlrIsmail AlrIsmail force-pushed the feature/fwht-kv-rotation branch from 7eafd3b to 490a22c Compare May 3, 2026 11:01
Comment thread ggml/src/ggml-cpu/ops.cpp Outdated
Comment thread ggml/src/ggml-cpu/ops.cpp Outdated
@AlrIsmail AlrIsmail force-pushed the feature/fwht-kv-rotation branch 2 times, most recently from 5eed37c to ba5ce01 Compare May 3, 2026 11:18
@AlrIsmail

Copy link
Copy Markdown
Contributor Author

@am17an I initially tried using the ggml_vec_add_f32 and ggml_vec_sub_f32 functions from vec.h, but the benchmark tanked. Is there a reason to not to use the internal GGML_F32_VEC_* macros instead?

@am17an

am17an commented May 3, 2026

Copy link
Copy Markdown
Contributor

Yes any generic API is fine

@AlrIsmail AlrIsmail requested a review from am17an May 3, 2026 12:08

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you need to also add a test-case in test-backend-ops which exercise this path

Comment thread ggml/src/ggml-cpu/ggml-cpu.c
Comment thread ggml/src/ggml-cpu/ops.cpp
Comment thread ggml/src/ggml-cpu/ops.cpp
Comment thread ggml/src/ggml-cpu/ops.cpp
@AlrIsmail AlrIsmail force-pushed the feature/fwht-kv-rotation branch 2 times, most recently from fcb2c79 to 4320d7d Compare May 3, 2026 15:33
@github-actions github-actions Bot added the testing Everything test related label May 3, 2026

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove the binary file benchmark_fwht_test

@AlrIsmail AlrIsmail force-pushed the feature/fwht-kv-rotation branch 2 times, most recently from 88c6e08 to d1a46e4 Compare May 3, 2026 19:54
@am17an

am17an commented May 4, 2026

Copy link
Copy Markdown
Contributor

Pls fix the CI: https://github.com/ggml-org/llama.cpp/actions/runs/25289207574/job/74155384900?pr=22631

@AlrIsmail AlrIsmail force-pushed the feature/fwht-kv-rotation branch from d1a46e4 to 3abc7f8 Compare May 4, 2026 18:35
@am17an am17an merged commit a817a22 into ggml-org:master May 5, 2026
45 of 46 checks passed
muxanick added a commit to muxanick/llama.cpp that referenced this pull request May 19, 2026
Closes the CUDA gap left by PR ggml-org#22631 (CPU FWHT for KV cache rotation,
merged 2026-05-05). When ggml_mul_mat_set_hint(t,
GGML_HINT_SRC0_IS_HADAMARD) is set on a MUL_MAT node, the CPU backend
replaces the O(N^2) literal matmul against the Hadamard matrix with an
O(N log N) Fast Walsh-Hadamard Transform via
ggml_compute_forward_fwht. The CUDA backend previously fell through to
a literal mul_mat which produced numerically equivalent output (since
the scaled Hadamard matrix dotted with the input equals FWHT
mathematically) but did O(N^2) work.

This commit adds:
  - ggml/src/ggml-cuda/fwht.cuh, fwht.cu: a CUDA FWHT kernel mirroring
    the CPU algorithm — one block per row, threads cooperate via
    shared memory across log2(n) butterfly passes after a 1/sqrt(n)
    scale. Block size = 128 (4 warps); shared-memory budget caps n at
    1024 elements per row (current upstream callers in
    src/llama-graph.cpp and src/llama-kv-cache.cpp use head_dim,
    typically 64-128, so this is fine).
  - ggml-cuda.cu: dispatch hook at the start of ggml_cuda_mul_mat that
    checks op_params[1] against GGML_HINT_SRC0_IS_HADAMARD and routes
    to ggml_cuda_op_fwht when set.
  - common.cuh: added #include <atomic> next to the other standard
    includes. The std::atomic field added in the prior bundle commit
    is reachable from translation units that don't transitively pull
    in <atomic> via other headers; the new fwht.cu translation unit
    is one such case and surfaced the missing include during build.
  - BUNDLE_README.md: c4 entry added; the corresponding entry under
    "What's deliberately NOT in this bundle" updated to acknowledge
    the gap is now closed.

Validation: test-backend-ops MUL_MAT_HADAMARD on CUDA passes
byte-identically to CPU reference for n = 64, 128, 256 (single batch)
and n = 128 (batch = 32) — the 4 test cases added by PR ggml-org#22631.
test-backend-ops GET_ROWS continues to pass 67/67 (regression check
for c1 from the prior bundle commit).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
baramofme pushed a commit to baramofme/llama-cpp-turboquant that referenced this pull request May 23, 2026
carlosfundora pushed a commit to carlosfundora/llama.cpp-1-bit-turbo that referenced this pull request May 24, 2026
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jun 20, 2026
fukuro-kun pushed a commit to fukuro-kun/fukuro-llama-cpp-turboquant that referenced this pull request Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Implement Fast Walsh Hadamard Transform

3 participants