Repository navigation
[Grammars] Optimization: Single lookup, remove extra copies - 1.2x-1.3x speedup - #26885
Merged
Merged
Conversation
1) Combine two consecutive lookups (find + insert) into a single insert-attempt/lookup routine so that we don't per form two O(log(n)) lookup operations in a row anymore -- we only need to do it once and then see if the insert succeeded. 2) Instead of copying every potential stack (expensive) and then moving it (cheap) to new_stacks when it's a final output state, we switch the order so that we move every potential stack (cheap), and then only copy it (expensive) to new stacks when it's a final output state. There are a LOT of intermediate states that get generated, and unless they become final output states, then all of these expensive intermediate copies are wasted. Before: lookup -> lookup/insert + copy -> optional move to output New: lookup/insert + move -> optional copy to output
Contributor
Author
aldehir
approved these changes
Aug 13, 2026
aldehir
left a comment
Contributor
There was a problem hiding this comment.
The changes look sensible, and I’m seeing a similar improvement on my end (~1.2x on a JSON grammar).
Contributor
|
@ggerganov what do you think about this small change? |
quimmedes
pushed a commit
to quimmedes/cafe-llama.cpp
that referenced
this pull request
Sep 16, 2026
…/copy mechanics (ggml-org#26885) 1) Combine two consecutive lookups (find + insert) into a single insert-attempt/lookup routine so that we don't per form two O(log(n)) lookup operations in a row anymore -- we only need to do it once and then see if the insert succeeded. 2) Instead of copying every potential stack (expensive) and then moving it (cheap) to new_stacks when it's a final output state, we switch the order so that we move every potential stack (cheap), and then only copy it (expensive) to new stacks when it's a final output state. There are a LOT of intermediate states that get generated, and unless they become final output states, then all of these expensive intermediate copies are wasted. Before: lookup -> lookup/insert + copy -> optional move to output New: lookup/insert + move -> optional copy to output
roverdrubber
added a commit
to roverdrubber/llama.cpp
that referenced
this pull request
Oct 15, 2026
The branch is far behind upstream master and the parts that a card other than this one needs had drifted. This ports the non-backend half of that sync, content-matched against upstream rather than replayed by hash: the parser and JSON schema chain (ggml-org#27764, ggml-org#28736, ggml-org#28742, which adds common/parsers/, common/json-schema.* and tests/test-json-schema.cpp and retires the three example scripts it replaces), the chat, grammar, jinja and server fixes (ggml-org#28302, ggml-org#28469, ggml-org#26885, ggml-org#28279, ggml-org#28511, ggml-org#28620, ggml-org#28817, ggml-org#28749), the qwen4exp RMSNorm and multiply fusion (ggml-org#28896, hand-ported because the norm tensors here load with a different shape), the GDN q/k l2 norm as flash-linear-attention defines it (ggml-org#28068), the ggml_prec_set_acc API that replaces ggml_mul_mat_set_prec and ggml_flash_attn_ext_set_prec at every call site, and the scheduler fixes for zero-sized expert ids (ggml-org#28739) and split inputs (ggml-org#28387). The GPU backend half of the same sync lands in the following two commits; this one alone does not build the CUDA or HIP backend, because their call sites move to the new precision API there. test-chat, test-json-schema, test-json-schema-to-grammar, test-grammar-integration and test-grammar-parser pass. Assisted-by: Claude Opus 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Tinkering with #4218 again. Even though we have llguidance available (which is generally much faster), I would still like to see improvements made to our in-house grammar engine.
This is a small change, but it includes two speed optimizations for grammar-parsing here:
1) 2 lookups => 1 lookup
Combines two consecutive lookups (initial
if (seen.find()), then the internal lookup performed byseen.insert()) into a single insert-attempt/lookup so that we don't perform two lookup operations in a row -- we only need to do it once and then see if the insert succeeded.Before:
After:
Because
insert()already does the check (and returns a boolean indicating whether it already existed or not), we don't need to do thisfind()operation twice -- we just need to utilize the return value frominsert()instead of duplicating its effort.2) always copy, optional move => always move, optional copy
Instead of copying every intermediate stack (expensive) and then moving it (cheap) to
new_stackswhen it's a final output state, we switch the order so that we move every intermediate stack (cheap), and then only copy it (expensive) tonew_stackswhen it's a final output state.There are a LOT of intermediate states that get generated with these stacks, and unless they become final output states, then all of those expensive intermediate copies are wasted.
So now we transfer ownership of each unique intermediate stack into
seen, then copy it only if it must also be returned throughnew_stacks.Before:
After:
In total:
Before: lookup -> lookup/insert + copy -> optional move to output
New: lookup/insert + move -> optional copy to output
Additional information
See overall discussion of the problem here: #4218
This doesn't tackle any of the big-hitter items that we see in that thread, but it's a small improvement that provides a measurable performance benefit and is self-contained enough to be well-understood.
Results:
Ran three different grammar cases I have locally. Results showed anywhere from 1.2x-1.3x speedup:
Each command used 10 warmups and 300 measured runs. Median speedups were 1.19x, 1.27x, and 1.21x respectively. An independent 30-run pass produced 1.18x, 1.30x, and 1.22x.
Full Hyperfine commands and test files available here.
Requirements