Skip to content

model : inkling2 graph with fewer ops in mix() and control() - #32948

Merged
am17an merged 1 commit into
ggml-org:masterfrom
am17an:inkling2-graph-fewer-ops
Nov 25, 2026
Merged

am17an merged 1 commit into
ggml-org:masterfrom
am17an:inkling2-graph-fewer-ops

Conversation

@am17an

@am17an am17an commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Overview

Four changes to the Inkling 2 graph that keep the results bit-identical and cut the ops per token:

  • mix() rebuilt the stack of all earlier block outputs on every call, with one CONCAT per block, and normalized the whole stack again. The rms_norm is per row, so each block can be normalized once: mix_stack() keeps the normalized stacks for the graph and grows them by one block at a time. In the loop graph all looped layers of a pass mix the same bank, so that stack is built once per pass.
  • mix() does not repeat the queries for a single token.
  • control() normalizes cur and message as two rows of one rms_norm, instead of two rms_norm and a concat of the results.
  • The zero sinks tensor is made once per head count, not with a FILL in every attention layer.

Additional information

Inkling 2 Mini IQ4_NL (ggml-org), master 26f5048 vs this PR, llama-bench -ngl 99 -fa 1, interleaved rounds of -r 3:

master PR
CUDA GB10, kernels per token (tg32, CUDA graphs off, nsys) 6487 6071
CUDA GB10, tg128 t/s 40.44, 40.43 41.37, 41.24
CUDA GB10, pp512 t/s 832, 833 857, 860
Metal M3 Ultra, tg128 t/s 32.13, 32.14, 32.21 32.69, 32.58, 32.69
Metal M3 Ultra, pp512 t/s 726, 726, 726 733, 733, 734

CUDA kernels per token by type: concat 548 -> 413, rms_norm 514 -> 400, repeat 120 -> 0, fill 51 -> 4.

Greedy decode of 96 tokens gives the same text on CUDA and Metal. llama-perplexity --kl-divergence against master gives the same numbers as master against itself on CUDA (40 chunks of 512), Metal (4 chunks) and CPU (2 chunks): mean KLD 0, same top p 100 %.

This merges cleanly with #32855.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code wrote these changes from a profile of Inkling 2 Mini decode, ran the checks above and wrote this description.

@am17an
am17an requested a review from a team as a code owner November 24, 2026 13:24
@ServeurpersoCom

ServeurpersoCom commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

I did this last night with a long agentic optimization loop on an M5 Max, see ServeurpersoCom@ce7f315

Part of it is the same kind of graph cleanup as this PR (mix stack, no query repeat for one token, shared norms and sinks), so that part overlaps.

With all the optimizations, Inkling-2-Mini Q4_K_XL goes from 39.4 to 75.5 t/s in tg64. In chat it goes from 37.9 to 73.3 t/s at 1.3k and from 33.9 to 61.2 t/s at 37k, ahead of Ollama MLX NVFP4 (70.6 and 40.1). Prefill at 37k goes from 457 to 1187 t/s.

I started sorting it out into PR-sized pieces, beginning with a first commit in the spirit of this PR: ServeurpersoCom@7095422f5c (tg64 39.4 -> 58.4 t/s).

Compared to this PR, on the graph side it also casts the F32 mask once for all the layers, skips the repeat of the per-expert scale for a single token, does not copy the inputs of a single token, and writes the clip as l2_norm + scale. Most of the gain comes from the Metal backend though, and it is not model-specific: one fusion pattern per reorder group, exact view ranges, streamed command buffers, an nvfp4 mat-vec with one sub-block per thread, the per-expert scale fused, and a vectorized full-vocab sampler.

@am17an

am17an commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@ServeurpersoCom nice! Keep the improvements flowing in.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

I'm measuring your PR on my M5 Max right now. Since it's better scoped for splitting this into small PRs, I'll approve it once the numbers check out, and we follow up on top of it.

@ServeurpersoCom ServeurpersoCom 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.

Measured on an M5 Max, Inkling-2-Mini UD-Q4_K_XL, master vs this PR:
pp512 1007 -> 1047 t/s, tg128 43.3 -> 45.5 t/s, tg64 @ d1300 42.5 -> 44.6 t/s.
PPL and the greedy output over 96 tokens are bit-identical, and test-llama-archs passes.

@ggerganov

Copy link
Copy Markdown
Member

The zero sinks tensor is made once per head count, not with a FILL in every attention layer.

Does this make a noticeable difference? With a single tensor, it will be copied across devices when using pipeline parallel and partial offloading.

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

The zero sinks tensor is made once per head count, not with a FILL in every attention layer.

Does this make a noticeable difference? With a single tensor, it will be copied across devices when using pipeline parallel and partial offloading.

I measured it on an M5 Max (Inkling-2-Mini UD-Q4_K_XL, single device), this PR vs this PR with the per-layer FILL restored, two interleaved rounds each:

tg128 44.4-44.5 -> 45.2-45.8 t/s, pp512 1031-1033 -> 1050 t/s

So about 2%, and probably more from the barrier the FILL adds before each attention layer than from the FILL itself. For pipeline parallel and partial offload, one zero tensor per backend split, or a zero tensor allocated per layer at load, would keep that gain without the copies across devices.

@ggerganov

ggerganov commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

The zero sinks tensor is made once per head count, not with a FILL in every attention layer.

Does this make a noticeable difference? With a single tensor, it will be copied across devices when using pipeline parallel and partial offloading.

I measured it on an M5 Max (Inkling-2-Mini UD-Q4_K_XL, single device), this PR vs this PR with the per-layer FILL restored, two interleaved rounds each:

tg128 44.4-44.5 -> 45.2-45.8 t/s, pp512 1031-1033 -> 1050 t/s

So about 2%, and probably more from the barrier the FILL adds before each attention layer than from the FILL itself. For pipeline parallel and partial offload, one zero tensor per backend split, or a zero tensor allocated per layer at load, would keep that gain without the copies across devices.

It's not clear which is better - single sinks or multiple?

@ServeurpersoCom

Copy link
Copy Markdown
Contributor

It's not clear which is better - single sinks or multiple?

Sorry, I wasn't clear. The single shared sinks tensor (this PR) is faster:

  • one FILL per layer: tg128 44.4-44.5 t/s, pp512 1031-1033 t/s
  • single sinks (this PR): tg128 45.2-45.8 t/s, pp512 1050 t/s

So single is about 2% faster on Metal, single device.

@github-actions github-actions Bot added the model Model specific label Nov 24, 2026
harshk242 pushed a commit to harshk242/llama.cpp that referenced this pull request Nov 24, 2026
From ggml-org/llama.cpp PR ggml-org#32948 (open, head 64ac064), applied as is:
normalized mix() stacks are built once per graph, no query repeat for one
token, one rms_norm for cur and message in control(), one zero sinks tensor.
@am17an
am17an merged commit abca692 into ggml-org:master Nov 25, 2026
21 of 22 checks passed
ljedrz added a commit to ljedrz/llama.cpp that referenced this pull request Nov 25, 2026
The looped visits of a layer padded the same relative-position bias table again; it is now cached per graph (the zero sinks already are, upstream ggml-org#32948). A SWA layer mixes one input for Q, K and V, which is taken as a reshape of the mix instead of a CONT of a view of it.

Inkling 2 Mini, Arc Pro B70: tg128 60.4 -> 61.2 t/s. KLD over 6 chunks of wikitext-2: 0.0020 (-ub 64), 0.0018 (-ub 4), at the noise of the backend.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P8newWYH2KNUBSwyU42kwd
ljedrz added a commit to ljedrz/llama.cpp that referenced this pull request Nov 25, 2026
The looped visits of a layer padded the same relative-position bias table again; it is now cached per graph (the zero sinks already are, upstream ggml-org#32948). A SWA layer mixes one input for Q, K and V, which is taken as a reshape of the mix instead of a CONT of a view of it.

Inkling 2 Mini, Arc Pro B70: tg128 60.4 -> 61.2 t/s. KLD over 6 chunks of wikitext-2: 0.0020 (-ub 64), 0.0018 (-ub 4), at the noise of the backend.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P8newWYH2KNUBSwyU42kwd
abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 25, 2026
…owner 2026-11-25)

Base b12723 (merge a940ab9), pin ggml-org#401 removed (80211b4), RoPE
multi-section fusion from upstream ggml-org#32500 (owner's decision), MIX.md
(3563400). Build gate: test-backend-ops ROCm 21878/21878, bench r17
(llama-mix-notes, mesures/2026.md): Inkling tg +4.5 % [bon], Qwen3.6 and
Gemma 4 without regression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QBjnvFCJ83vkmygsLNv5cu

* mix/wip-b12723: (55 commits)
  mix: MIX.md — base b12723 (branche wip), retrait du pin ggml-org#401, conflit RoPE tranché (amont ggml-org#32500)
  revert: pin: CUDA: fuse the row L2 clip chain (unslothai#401, commit 6c0e84d)
  model : inkling2 graph with fewer ops in mix() and control() (ggml-org#32948)
  opencl: add clc coopmat moe kernel for q4_0/q4_1/q4_k/q5_0/q5_1/q5_k/q6_k/mxfp4 (ggml-org#32971)
  tests : fix MSVC static build of test-batch-alloc and test-server-component (ggml-org#32870)
  feat: Inkling 2 DSpark support (ggml-org#32218)
  cuda : fix int overflow of the MMVF matrix offsets (ggml-org#32965)
  docs : describe SpacemiT runtime environment variables (ggml-org#32452)
  ggml : fix SpacemiT flash attention null mask, ALiBi and softcap (ggml-org#32182)
  cmake : include the llguidance header from the source tree (ggml-org#32928)
  ci : move some jobs to gb10 runners (ggml-org#32968)
  ui: run Llama Chat apart from llama-server, named in config.json or npx --server (ggml-org#32945)
  ui: fix prebuilt UI download in shallow clones (ggml-org#32952)
  convert : fix qwen3-omni conversion (ggml-org#32941)
  ggml-cpu: repack kernels for targets without dotprod (ARMv8.0 64-bit) for Q4_0/Q8_0 (ggml-org#32142)
  ui: expect the Llama Chat name in the PWA manifest e2e test (ggml-org#32959)
  ggml-cpu: add SVE-optimized q5_0 and q5_1 dot-product kernels (ggml-org#30867)
  server : include MCP image and audio data in tool results (ggml-org#32064)
  context : prevent excessive CPU polling without OpenMP (ggml-org#32378)
  metal : reserve the top-k scratch only for short rows (ggml-org#32942)
  ...
ljedrz added a commit to ljedrz/llama.cpp that referenced this pull request Nov 26, 2026
The looped visits of a layer padded the same relative-position bias table again; it is now cached per graph (the zero sinks already are, upstream ggml-org#32948). A SWA layer mixes one input for Q, K and V, which is taken as a reshape of the mix instead of a CONT of a view of it.

Inkling 2 Mini, Arc Pro B70: tg128 60.4 -> 61.2 t/s. KLD over 6 chunks of wikitext-2: 0.0020 (-ub 64), 0.0018 (-ub 4), at the noise of the backend.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01P8newWYH2KNUBSwyU42kwd
Mr-J-369 pushed a commit to Mr-J-369/llama.cpp-Android that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants