Skip to content

metal : gated_delta_net cache fusion - #25788

Closed
angt wants to merge 1 commit into
ggml-org:masterfrom
angt:metal-gated_delta_net-cache-fusion
Closed

angt wants to merge 1 commit into
ggml-org:masterfrom
angt:metal-gated_delta_net-cache-fusion

Conversation

@angt

@angt angt commented Jul 16, 2026

Copy link
Copy Markdown
Member

gated_delta_net cache fusion

Overview

Mirror the CUDA backend's ggml_cuda_try_gdn_cache_fusion: route the KV cache pointer into the kernel so it writes recurrent-state snapshots directly into the cache and skips the redundant per-layer cpy.

Additional information

CPU Model Test t/s OLD t/s NEW Speedup
Accelerate, Apple M3 gemma3 270M Q4_0 pp512 5942.93 5786.28 0.97
Accelerate, Apple M3 gemma3 270M Q4_0 tg128 145.18 145.59 1.00
Accelerate, Apple M3 qwen35 0.8B BF16 pp512 1580.12 1865.99 1.18
Accelerate, Apple M3 qwen35 0.8B BF16 tg128 26.68 34.96 1.31
Accelerate, Apple M3 qwen35 0.8B IQ2_M - 2.7 bpw pp512 1211.22 1448.67 1.20
Accelerate, Apple M3 qwen35 0.8B IQ2_M - 2.7 bpw tg128 51.27 64.71 1.26
Accelerate, Apple M3 qwen35 0.8B IQ2_XXS - 2.0625 bpw pp512 1593.18 1874.77 1.18
Accelerate, Apple M3 qwen35 0.8B IQ2_XXS - 2.0625 bpw tg128 52.47 130.20 2.48
Accelerate, Apple M3 qwen35 0.8B IQ3_XXS - 3.0625 bpw pp512 1132.65 1842.77 1.63
Accelerate, Apple M3 qwen35 0.8B IQ3_XXS - 3.0625 bpw tg128 48.02 123.24 2.57
Accelerate, Apple M3 qwen35 0.8B IQ4_NL - 4.5 bpw pp512 1407.56 1364.16 0.97
Accelerate, Apple M3 qwen35 0.8B IQ4_NL - 4.5 bpw tg128 49.81 62.94 1.26
Accelerate, Apple M3 qwen35 0.8B IQ4_XS - 4.25 bpw pp512 1049.38 1762.12 1.68
Accelerate, Apple M3 qwen35 0.8B IQ4_XS - 4.25 bpw tg128 47.59 117.57 2.47
Accelerate, Apple M3 qwen35 0.8B Q2_K_M pp512 1399.04 1198.09 0.86
Accelerate, Apple M3 qwen35 0.8B Q2_K_M tg128 53.94 63.83 1.18
Accelerate, Apple M3 qwen35 0.8B Q3_K_M pp512 1245.84 1307.13 1.05
Accelerate, Apple M3 qwen35 0.8B Q3_K_M tg128 50.72 59.40 1.17
Accelerate, Apple M3 qwen35 0.8B Q3_K_S pp512 1122.24 1274.85 1.14
Accelerate, Apple M3 qwen35 0.8B Q3_K_S tg128 47.41 56.63 1.19
Accelerate, Apple M3 qwen35 0.8B Q4_0 pp512 1387.63 1191.85 0.86
Accelerate, Apple M3 qwen35 0.8B Q4_0 tg128 55.75 51.60 0.93
Accelerate, Apple M3 qwen35 0.8B Q4_1 pp512 1307.73 1750.53 1.34
Accelerate, Apple M3 qwen35 0.8B Q4_1 tg128 50.79 104.04 2.05
Accelerate, Apple M3 qwen35 0.8B Q4_K_M pp512 1225.77 1269.81 1.04
Accelerate, Apple M3 qwen35 0.8B Q4_K_M tg128 48.16 52.95 1.10
Accelerate, Apple M3 qwen35 0.8B Q4_K_S pp512 1148.35 1407.57 1.23
Accelerate, Apple M3 qwen35 0.8B Q4_K_S tg128 47.66 64.60 1.36
Accelerate, Apple M3 qwen35 0.8B Q5_K_M pp512 1219.83 1296.53 1.06
Accelerate, Apple M3 qwen35 0.8B Q5_K_M tg128 44.42 49.93 1.12
Accelerate, Apple M3 qwen35 0.8B Q5_K_S pp512 1248.74 1210.40 0.97
Accelerate, Apple M3 qwen35 0.8B Q5_K_S tg128 43.06 50.01 1.16
Accelerate, Apple M3 qwen35 0.8B Q6_K pp512 1169.88 1667.61 1.43
Accelerate, Apple M3 qwen35 0.8B Q6_K tg128 42.13 93.17 2.21
Accelerate, Apple M3 qwen35 0.8B Q8_0 pp512 1510.92 1378.43 0.91
Accelerate, Apple M3 qwen35 0.8B Q8_0 tg128 33.95 36.59 1.08

Requirements

@angt
angt requested review from a team and ggerganov as code owners July 16, 2026 17:40
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Jul 16, 2026
@ggerganov ggerganov self-assigned this Jul 16, 2026
@angt

angt commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

This was totally opportunistic while comparing CUDA and Metal for something else

@angt

angt commented Jul 16, 2026

Copy link
Copy Markdown
Member Author

with -r 15:

CPU Model Test t/s OLD t/s NEW Speedup
Accelerate, Apple M3 gemma3 270M Q4_0 pp512 6932.31 6848.97 0.99
Accelerate, Apple M3 gemma3 270M Q4_0 tg128 97.03 103.48 1.07
Accelerate, Apple M3 qwen35 0.8B BF16 pp512 900.18 1060.05 1.18
Accelerate, Apple M3 qwen35 0.8B BF16 tg128 24.28 26.70 1.10
Accelerate, Apple M3 qwen35 0.8B IQ2_M - 2.7 bpw pp512 954.65 980.58 1.03
Accelerate, Apple M3 qwen35 0.8B IQ2_M - 2.7 bpw tg128 46.13 48.31 1.05
Accelerate, Apple M3 qwen35 0.8B IQ2_XXS - 2.0625 bpw pp512 917.66 1857.96 2.02
Accelerate, Apple M3 qwen35 0.8B IQ2_XXS - 2.0625 bpw tg128 42.72 124.01 2.90
Accelerate, Apple M3 qwen35 0.8B IQ3_XXS - 3.0625 bpw pp512 933.33 1542.18 1.65
Accelerate, Apple M3 qwen35 0.8B IQ3_XXS - 3.0625 bpw tg128 43.48 59.28 1.36
Accelerate, Apple M3 qwen35 0.8B IQ4_NL - 4.5 bpw pp512 980.90 1012.97 1.03
Accelerate, Apple M3 qwen35 0.8B IQ4_NL - 4.5 bpw tg128 42.08 44.64 1.06
Accelerate, Apple M3 qwen35 0.8B IQ4_XS - 4.25 bpw pp512 873.90 679.53 0.78
Accelerate, Apple M3 qwen35 0.8B IQ4_XS - 4.25 bpw tg128 41.61 46.43 1.12
Accelerate, Apple M3 qwen35 0.8B Q2_K_M pp512 962.13 1031.17 1.07
Accelerate, Apple M3 qwen35 0.8B Q2_K_M tg128 47.96 50.78 1.06
Accelerate, Apple M3 qwen35 0.8B Q3_K_M pp512 898.41 961.80 1.07
Accelerate, Apple M3 qwen35 0.8B Q3_K_M tg128 40.67 44.99 1.11
Accelerate, Apple M3 qwen35 0.8B Q3_K_S pp512 811.56 882.04 1.09
Accelerate, Apple M3 qwen35 0.8B Q3_K_S tg128 41.38 43.38 1.05
Accelerate, Apple M3 qwen35 0.8B Q4_0 pp512 1181.05 1231.39 1.04
Accelerate, Apple M3 qwen35 0.8B Q4_0 tg128 53.00 56.63 1.07
Accelerate, Apple M3 qwen35 0.8B Q4_1 pp512 976.68 1067.52 1.09
Accelerate, Apple M3 qwen35 0.8B Q4_1 tg128 43.84 49.97 1.14
Accelerate, Apple M3 qwen35 0.8B Q4_K_M pp512 916.63 981.75 1.07
Accelerate, Apple M3 qwen35 0.8B Q4_K_M tg128 39.04 39.06 1.00
Accelerate, Apple M3 qwen35 0.8B Q4_K_S pp512 942.65 1003.55 1.06
Accelerate, Apple M3 qwen35 0.8B Q4_K_S tg128 43.47 43.70 1.01
Accelerate, Apple M3 qwen35 0.8B Q5_K_M pp512 991.94 907.73 0.92
Accelerate, Apple M3 qwen35 0.8B Q5_K_M tg128 38.39 39.39 1.03
Accelerate, Apple M3 qwen35 0.8B Q5_K_S pp512 853.83 923.37 1.08
Accelerate, Apple M3 qwen35 0.8B Q5_K_S tg128 36.59 37.48 1.02
Accelerate, Apple M3 qwen35 0.8B Q6_K pp512 923.09 1068.44 1.16
Accelerate, Apple M3 qwen35 0.8B Q6_K tg128 34.51 38.12 1.10
Accelerate, Apple M3 qwen35 0.8B Q8_0 pp512 859.50 1057.21 1.23
Accelerate, Apple M3 qwen35 0.8B Q8_0 tg128 29.70 30.37 1.02

@angt
angt force-pushed the metal-gated_delta_net-cache-fusion branch from ad6269a to e924e7d Compare July 18, 2026 15:08

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

LGTM :) clean mirror of the CUDA gdn cache fusion. Built and ran test-backend-ops locally on my M4 Max and new cases all pass, the existing GATED_DELTA_NET tests are unaffected.

@forforever73

Copy link
Copy Markdown
Contributor

Also ran llama-bench (-r 15)

GPU Model Test t/s OLD t/s NEW Speedup
Metal, Apple M4 Max qwen35 9B Q4_K_M pp512 828.81 831.98 1.00
Metal, Apple M4 Max qwen35 9B Q4_K_M tg128 64.06 67.58 1.05
Metal, Apple M4 Max qwen35 27B Q8_0 pp512 256.00 256.90 1.00
Metal, Apple M4 Max qwen35 27B Q8_0 tg128 15.67 16.37 1.04
Metal, Apple M4 Max qwen35moe 35B.A3B Q8_0 pp512 1451.35 1472.47 1.01
Metal, Apple M4 Max qwen35moe 35B.A3B Q8_0 tg128 67.92 78.28 1.15

@forforever73 forforever73 added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 28, 2026
Comment thread ggml/src/ggml-metal/ggml-metal-ops.cpp Resolved
@ggerganov ggerganov removed the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Jul 28, 2026
@angt
angt force-pushed the metal-gated_delta_net-cache-fusion branch from e924e7d to 85a4663 Compare August 1, 2026 18:21
@angt

angt commented Aug 12, 2026

Copy link
Copy Markdown
Member Author

@ggerganov is this better ?

Comment thread ggml/src/ggml-metal/ggml-metal-ops.cpp Outdated
Comment on lines +1621 to +1622
// The cpy reads the gdn output through a view of a different shape, so ctx->can_fuse() cannot
// express it; the linkage and geometry are validated directly instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's better, but still, I am not sure what to do about this part. Not being able to use can_fuse() required that you implement the "elision" logic, which is a duplication of the logic in can_fuse(). Does not seem right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, I have tried many things to not lose too much perf compared to the first iteration.
I’ll dig that when I come back

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, this should not affect the performance. The goal is rather to avoid adding the dst_elide stuff. Having to add it means that the existing ggml_can_fuse_ext() is not general enough to capture such cases. So we should look to extend it in some way, or to formulate the graph in alternative way that would be compatible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it not possible to utilize the existing ggml_can_fuse_subgraph() in this case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I dont think so 🤔 The cpy is a view on an external cache tensor not really a subgraph and would require to patch ggml_can_fuse_subgraph. But I believe that the allow_view_consumer added is better as it matches the cuda pattern (kind of future proof) and is minimal. Or maybe i miss something?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, will take a look at the proposal tomorrow.

@rajeeja

This comment was marked as off-topic.

@angt
angt force-pushed the metal-gated_delta_net-cache-fusion branch 2 times, most recently from 4509960 to 98d0d2f Compare August 24, 2026 17:29
@ggerganov

Copy link
Copy Markdown
Member

I think there is some confusion here - the fusion never takes place.

For example on M2 Ultra:

CMAKE_OPTS="-DGGML_BLAS=OFF" scripts/compare-commits.sh master pr/25788 llama-bench -hf ggml-org/Qwen3.5-0.8B-GGUF:Q8_0 -fa 1 -ub 2048 -d 0,2048,4096,8192 -r 3 -n 32 -t 1
CPU Model Test t/s master t/s pr/25788 Speedup
Apple M2 Ultra qwen35 0.8B Q8_0 pp512 10907.19 10854.52 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 pp512@d2048 10028.44 10018.39 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 pp512@d4096 9062.28 9058.70 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 pp512@d8192 7621.59 7621.79 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32 256.29 256.03 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32@d2048 251.89 250.72 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32@d4096 247.86 248.68 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32@d8192 236.84 237.15 1.00
CMAKE_OPTS="-DGGML_BLAS=OFF" scripts/compare-commits.sh master pr/25788 llama-bench -hf ggml-org/Qwen3.8-27B-GGUF:Q8_0 -fa 1 -ub 2048 -d 0,2048,4096,8192 -r 3 -n 32 -t 1
CPU Model Test t/s master t/s pr/25788 Speedup
Apple M2 Ultra qwen35 27B Q8_0 pp512 383.56 383.33 1.00
Apple M2 Ultra qwen35 27B Q8_0 pp512@d2048 377.05 376.74 1.00
Apple M2 Ultra qwen35 27B Q8_0 pp512@d4096 366.65 366.59 1.00
Apple M2 Ultra qwen35 27B Q8_0 pp512@d8192 348.61 348.53 1.00
Apple M2 Ultra qwen35 27B Q8_0 tg32 21.54 21.57 1.00
Apple M2 Ultra qwen35 27B Q8_0 tg32@d2048 21.35 21.54 1.01
Apple M2 Ultra qwen35 27B Q8_0 tg32@d4096 21.13 21.34 1.01
Apple M2 Ultra qwen35 27B Q8_0 tg32@d8192 21.03 21.02 1.00

The reason is because the Metal backend optimizes the graph by rearranging the ops to run concurrently.

Here is how the graph looks for the Metal backend after the graph optimization is performed:

make -j && GGML_METAL_GRAPH_DEBUG=1 ./bin/llama-completion -hf ggml-org/Qwen3.5-0.8B-GGUF:Q8_0 -p "I believe the meaning of life is" --top-k 1 -n 1 -st -no-cnv -lv 5
0.01.404.904 D ggml_metal_op_encode_impl: node[   14] - L2_NORM      q_conv_predelta-2 
0.01.404.905 D ggml_metal_op_encode_impl: node[   15] - L2_NORM      k_conv_predelta-2 (concurrent)
0.01.404.906 D ggml_metal_op_encode_impl: node[   16] - MUL_MAT      z-2          (concurrent)
0.01.404.907 D ggml_metal_op_encode_impl: node[   17] - GATED_DELTA_NET node_170     
0.01.404.909 D ggml_metal_op_encode_impl: node[   18] - UNARY        node_179     
0.01.404.910 D ggml_metal_op_encode_impl: node[   19] - CPY          cache_s_l2 (view) (copy of new_state-2) (concurrent)
0.01.404.911 D ggml_metal_op_encode_impl: node[   20] - RMS_NORM     norm-2       (concurrent)
0.01.404.912 D ggml_metal_op_encode_impl:               fuse 2 ops
0.01.404.913 D ggml_metal_op_encode_impl: node[   22] - MUL          node_180     

So the fusion pattern is no longer GATED_DELTA_NET + CPY because an UNARY op is now inbetween in order to improve concurrency.

This graph optimization can be disabled with the GGML_METAL_GRAPH_OPTIMIZE_DISABLE=1 env, but it's not worth it (#19555). I don't know why you and @forforever73 have measured any performance change - when I put printfs at the end of ggml_metal_op_can_fuse_gdn_cache they are never executed. So double-check your measurements.

@angt

angt commented Aug 25, 2026 •

Copy link
Copy Markdown
Member Author

I think there is some confusion here - the fusion never takes place.

For example on M2 Ultra:

CMAKE_OPTS="-DGGML_BLAS=OFF" scripts/compare-commits.sh master pr/25788 llama-bench -hf ggml-org/Qwen3.5-0.8B-GGUF:Q8_0 -fa 1 -ub 2048 -d 0,2048,4096,8192 -r 3 -n 32 -t 1

CPU Model Test t/s master t/s pr/25788 Speedup
Apple M2 Ultra qwen35 0.8B Q8_0 pp512 10907.19 10854.52 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 pp512@d2048 10028.44 10018.39 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 pp512@d4096 9062.28 9058.70 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 pp512@d8192 7621.59 7621.79 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32 256.29 256.03 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32@d2048 251.89 250.72 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32@d4096 247.86 248.68 1.00
Apple M2 Ultra qwen35 0.8B Q8_0 tg32@d8192 236.84 237.15 1.00

CMAKE_OPTS="-DGGML_BLAS=OFF" scripts/compare-commits.sh master pr/25788 llama-bench -hf ggml-org/Qwen3.8-27B-GGUF:Q8_0 -fa 1 -ub 2048 -d 0,2048,4096,8192 -r 3 -n 32 -t 1

CPU Model Test t/s master t/s pr/25788 Speedup
Apple M2 Ultra qwen35 27B Q8_0 pp512 383.56 383.33 1.00
Apple M2 Ultra qwen35 27B Q8_0 pp512@d2048 377.05 376.74 1.00
Apple M2 Ultra qwen35 27B Q8_0 pp512@d4096 366.65 366.59 1.00
Apple M2 Ultra qwen35 27B Q8_0 pp512@d8192 348.61 348.53 1.00
Apple M2 Ultra qwen35 27B Q8_0 tg32 21.54 21.57 1.00
Apple M2 Ultra qwen35 27B Q8_0 tg32@d2048 21.35 21.54 1.01
Apple M2 Ultra qwen35 27B Q8_0 tg32@d4096 21.13 21.34 1.01
Apple M2 Ultra qwen35 27B Q8_0 tg32@d8192 21.03 21.02 1.00
The reason is because the Metal backend optimizes the graph by rearranging the ops to run concurrently.

Here is how the graph looks for the Metal backend after the graph optimization is performed:

make -j && GGML_METAL_GRAPH_DEBUG=1 ./bin/llama-completion -hf ggml-org/Qwen3.5-0.8B-GGUF:Q8_0 -p "I believe the meaning of life is" --top-k 1 -n 1 -st -no-cnv -lv 5
0.01.404.904 D ggml_metal_op_encode_impl: node[   14] - L2_NORM      q_conv_predelta-2 
0.01.404.905 D ggml_metal_op_encode_impl: node[   15] - L2_NORM      k_conv_predelta-2 (concurrent)
0.01.404.906 D ggml_metal_op_encode_impl: node[   16] - MUL_MAT      z-2          (concurrent)
0.01.404.907 D ggml_metal_op_encode_impl: node[   17] - GATED_DELTA_NET node_170     
0.01.404.909 D ggml_metal_op_encode_impl: node[   18] - UNARY        node_179     
0.01.404.910 D ggml_metal_op_encode_impl: node[   19] - CPY          cache_s_l2 (view) (copy of new_state-2) (concurrent)
0.01.404.911 D ggml_metal_op_encode_impl: node[   20] - RMS_NORM     norm-2       (concurrent)
0.01.404.912 D ggml_metal_op_encode_impl:               fuse 2 ops
0.01.404.913 D ggml_metal_op_encode_impl: node[   22] - MUL          node_180     

So the fusion pattern is no longer GATED_DELTA_NET + CPY because an UNARY op is now inbetween in order to improve concurrency.

This graph optimization can be disabled with the GGML_METAL_GRAPH_OPTIMIZE_DISABLE=1 env, but it's not worth it (#19555). I don't know why you and @forforever73 have measured any performance change - when I put printfs at the end of ggml_metal_op_can_fuse_gdn_cache they are never executed. So double-check your measurements.

I guess i did too many changes 😅 let me check that

Mirror the CUDA backend's ggml_cuda_try_gdn_cache_fusion: route the KV
cache pointer into the kernel so it writes recurrent-state snapshots
directly into the cache and skips the redundant per-layer cpy.

Signed-off-by: Adrien Gallouët <[email protected]>
@angt

angt commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

I confirm the current code is broken.. i'm going to revert to the last state and redo all benches

@angt
angt force-pushed the metal-gated_delta_net-cache-fusion branch from 98d0d2f to c2e7c1c Compare August 25, 2026 09:19
Comment on lines +313 to +318
// a cpy that scatters gated_delta_net state snapshots into the cache must stay right after
// its gdn node, otherwise the gdn+copy fusion cannot apply. do not pull other nodes in front of it.
const bool pinned = node0.op() == GGML_OP_CPY &&
node0.node->src[0] && node0.node->src[0]->view_src &&
node0.node->src[0]->view_src->op == GGML_OP_GATED_DELTA_NET;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we can avoid this extra logic. We already have related logic for preventing reorder of fusable nodes. Ideally it should be extended to cover this case and not need to add this new logic here.

Currently, the existing logic in ggml_graph_optimize() uses the ggml_can_fuse() to figure out which nodes could be fused, which we know does not work for this case because of the different shapes. I would try to replace it with ggml_can_fuse_subgraph() since it's more general. You mentioned earlier that ggml_can_fuse_subgraph() does not work, but I am not sure I understand why would that be.

@ggerganov

ggerganov commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

@angt I propose a reworked version of the Metal backend fusion: #28164 and add similar support for GDN+CPY fusion. PTAL when you have the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) 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.

4 participants