llama : make tensor-split regex patterns static to avoid per-token recompilation - #24710
Merged
JohannesGaessler merged 1 commit intoJul 10, 2026
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
eduardopessin
force-pushed
the
fix/static-regex-tensor-split
branch
from
June 16, 2026 22:18
e4aedf2 to
f0667e6
Compare
Contributor
Author
|
Done |
Member
|
This just introduces MT issues, I suppose they need to live outside Also, please use our PR template in the OP. |
eduardopessin
force-pushed
the
fix/static-regex-tensor-split
branch
from
June 17, 2026 08:49
f0667e6 to
87a50f6
Compare
Contributor
Author
|
Done, plz let me know if you need something else |
Contributor
Author
|
Also tested and profiled, regex completly gone from hot path |
Contributor
|
Regarding multi-threading issues: to my knowledge since C++ 11 the creation of local static variables is thread safe so it should be fine to just add |
llama_meta_device_get_split_state() recompiled 29 std::regex on every call. In -sm tensor mode the callback runs once per tensor per token, so this dominated the decode thread in profiling. Mark them static const so they are compiled once. Kept inside the function (local statics are thread-safe since C++11). Patterns are literal and stateless, so behavior is unchanged.
eduardopessin
force-pushed
the
fix/static-regex-tensor-split
branch
from
July 3, 2026 20:31
87a50f6 to
4f19d39
Compare
Contributor
|
@CISC can you re-review? |
Contributor
|
@ggml-org/maintainers can I get a second approval, please? |
pwilkin
approved these changes
Jul 10, 2026
JohannesGaessler
approved these changes
Jul 10, 2026
fukuro-kun
pushed a commit
to fukuro-kun/fukuro-llama-cpp-turboquant
that referenced
this pull request
Jul 12, 2026
AtomicBot-ai#6 MUL_MAT_ID Subgroup (PR ggml-org#15524): - Subgroup ballot operations für MoE-Expert-Selection auf non-coopmat GPUs - 18 Merge-Konflikte in ggml-vulkan.cpp gelöst (HEAD-Features erhalten) - 2 PR-Änderungen übernommen: subgroup_min_size<=16 Check, fused RMS_NORM+MUL - mul_mm.comp: neuer subgroup load_row_ids() Code - vulkan-shaders-gen.cpp: MatMulIdType::SUBGROUP integration AtomicBot-ai#2 Tensor Split Regex (PR ggml-org#24710, open): - 29 std::regex Patterns von const auf static const geändert - Verhindert pro-Token Regex-Recompilation im Tensor-Split-Modus - Befreit ~40% des Decode-Threads auf Multi-GPU-Systemen Build: grün auf Hydra (CUDA)
fukuro-kun
pushed a commit
to fukuro-kun/fukuro-llama-cpp-turboquant
that referenced
this pull request
Jul 12, 2026
…AtomicBot-ai#6 revert AtomicBot-ai#6 MUL_MAT_ID Subgroup (PR ggml-org#15524) wurde revertiert: - 23 Merge-Konflikte in 3 Vulkan-Dateien, davon 18 in ggml-vulkan.cpp - Subagent-Lösung beschädigte Shader-Generierung (undefined references) - Erfordert manuelle Portierung mit tiefem Vulkan-Shader-Verständnis - Für spätere Session mit fokussiertem Ansatz AtomicBot-ai#2 Tensor Split Regex (PR ggml-org#24710, open) erfolgreich: - 29 std::regex Patterns von const auf static const geändert - Verhindert pro-Token Regex-Recompilation im Tensor-Split-Modus
CowboyTim
pushed a commit
to aardbeiplantje/llama.cpp
that referenced
this pull request
Jul 21, 2026
llama_meta_device_get_split_state() recompiled 29 std::regex on every call. In -sm tensor mode the callback runs once per tensor per token, so this dominated the decode thread in profiling. Mark them static const so they are compiled once. Kept inside the function (local statics are thread-safe since C++11). Patterns are literal and stateless, so behavior is unchanged.
satindergrewal
pushed a commit
to satindergrewal/llama.cpp
that referenced
this pull request
Aug 12, 2026
llama_meta_device_get_split_state() recompiled 29 std::regex on every call. In -sm tensor mode the callback runs once per tensor per token, so this dominated the decode thread in profiling. Mark them static const so they are compiled once. Kept inside the function (local statics are thread-safe since C++11). Patterns are literal and stateless, so behavior is unchanged.
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
I hit this while running
Qwen3.6-35B-A3Bwith-sm tensor,-np 5, and a large context on 3 consumer GPUs over PCIe Gen3, without NVLink.The visible symptom was simple: the decode thread was stuck at 100% CPU, while the GPUs were only around 30% utilized. They were basically waiting on the CPU.
I profiled only the decode thread, not the whole process, to avoid mixing this with the HTTP worker threads:
That showed a large amount of time under
llama_meta_device_get_split_state()and the related split-state path. About 30% of the decode thread was instd::regexcompilation frames, including_Compiler::_M_*,_BracketMatcher,std::collate::do_transform,__strxfrm_l, and__dynamic_cast. Another ~22% was in theggml_backend_meta_get_split_statemachinery.The problem is that
llama_meta_device_get_split_state()declared 29std::regexobjects as local variables. In tensor split mode, this callback runs once per tensor, per token, because the graph is rebuilt on each decode step. That means the same regexes are compiled again and again in the decode path.This moves those patterns to file scope (compiled once, reused). The patterns are literals and do not carry mutable state, so the behavior is unchanged. Keeping them outside the function also avoids the function-local static concern raised in review.
After the change, the
std::regexcompilation frames disappear from the profile.get_split_statedrops from roughly 22% to roughly 13%, freeing about 40% of the decode thread in this setup. With concurrent load, the GPUs are then able to saturate.I am not quoting absolute tok/s numbers here, since those depend heavily on the hardware and workload. The useful result is the profile delta.
Requirements