Skip to content

Optimizing Gemma4 graph for prompt processing - #29189

Closed
Meet91721 wants to merge 4 commits into
ggml-org:masterfrom
Meet91721:optim/gemma4-pp
Closed

Meet91721 wants to merge 4 commits into
ggml-org:masterfrom
Meet91721:optim/gemma4-pp

Conversation

@Meet91721

@Meet91721 Meet91721 commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

I have been discussing with @am17an regarding the high difference in prompt processing rate for Gemma 4 model on llama.cpp and mlx-lm. On debugging I found that the TODO in the src/models/gemma4.cpp by @ngxson states that the prompt processing can be improved by stripping the unused tokens after the last KV layer.

This PR skips the computations that are happening after the calculation of attention for the tokens that do not require to calculate the logits. The improvement in the prompt processing rate is mentioned below:

The commit: 6c44591 has the following prompt processing rate:

model size params backend threads test t/s
gemma4 E2B BF16 8.66 GiB 4.65 B MTL,BLAS 4 pp2048 626.27 ± 29.98
gemma4 E2B BF16 8.66 GiB 4.65 B MTL,BLAS 4 tg128 47.63 ± 2.27

Current PR's prompt processing rate:

model size params backend threads test t/s
gemma4 E2B BF16 8.66 GiB 4.65 B MTL,BLAS 4 pp2048 1860.01 ± 10.52
gemma4 E2B BF16 8.66 GiB 4.65 B MTL,BLAS 4 tg128 49.90 ± 0.46

I have tested this using the following command:

./llama-bench -m ../models/gguf/gemma-4-E2B-it-BF16.gguf \
    -p 2048 \
    -n 128 \
    -r 5

Additional information

I am testing this on the MacBook Air M3, 16GB RAM.
I have used the following model: ggml-org/gemma-4-E2B-it-GGUF

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, I have used GPT 5.6 SOL for understanding some parts of the codebase. All the changes made are done manually.

TODO:
	- Currently complete microbatch needs to contain tokens which does not require the logits; need to optimize this further
	- There are a few more operations that are performed after the kv cache is calculated, need to safely optimize them.
@github-actions github-actions Bot added the model Model specific label Sep 20, 2026
Comment thread src/models/gemma4.cpp Resolved
@Meet91721
Meet91721 marked this pull request as ready for review October 28, 2026 19:17
@Meet91721
Meet91721 requested a review from a team as a code owner October 28, 2026 19:17
@Meet91721

Copy link
Copy Markdown
Contributor Author

Hi @ggerganov, @ngxson

I was referring to the @max-krasnyansky 's recent PR on similar changes, and found that I was missing the crop of attention mask. I have added it in the latest commit.
I have tested this by generating long text response and found that these changes are generating the same responses as the master branch.
Please review this PR and share feedback on this.

If you have more ideas on the tests that I can perform to check the correctness of this PR, please let me know.

@max-krasnyansky

Copy link
Copy Markdown
Member

Hi @ggerganov, @ngxson

I was referring to the @max-krasnyansky 's recent PR on similar changes, and found that I was missing the crop of attention mask. I have added it in the latest commit. I have tested this by generating long text response and found that these changes are generating the same responses as the master branch. Please review this PR and share feedback on this.

If you have more ideas on the tests that I can perform to check the correctness of this PR, please let me know.

The other things you listed in the TODO in the description (ubatches) are already in that PR as well.
Should we just move further iterations here? No need for two PRs. Just bring in the rest of the changes here and we'll use this PR to iterate.
Or we can use my PR and I'll add you as the co-Author. I don't really have a strong preference (well, maybe a slight preference to the other one just because it's more complete already and it's in a branch in upstream repo).
Let me know.

@Meet91721

Copy link
Copy Markdown
Contributor Author

Hi @max-krasnyansky ,
Both the TODOs mentioned in the description were already addressed in the commits which were made 2 days ago (295bf28).

And regarding which PR should we move forward, I am fine with any option.

@max-krasnyansky

Copy link
Copy Markdown
Member

OK. Sounds good.
I went ahead and rebased #31356 on top of this.
That includes further fixes for ubatch outputs and the PLE stuff.
I also followed up on Georgi's comments on the MTP (working on resolved the graph branches next).
Let's just use #31356 for the follow up (easier with the upstream banch).
Please close this one when you get the chance.

@Meet91721

Copy link
Copy Markdown
Contributor Author

Sure Max, I am closing this one. We can continue on your PR.

@Meet91721 Meet91721 closed this Oct 31, 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.

3 participants