[Metal] Avoid regex in custom kernel name generation#3869
Open
aleroot wants to merge 1 commit into
Open
Conversation
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.
Proposed changes
Templated custom Metal kernels generate a specialized kernel name on every invocation. This previously constructed a
std::regexand calledstd::regex_replaceto convert the template definition into a valid kernel-name suffix.This cost becomes measurable in workloads that repeatedly invoke templated custom kernels. In an Instruments capture of Qwen 3.5 gated-delta decoding, regex processing accounted for approximately 3.5% of the generation worker's sampled CPU cycles.
This change replaces the regex with a single linear pass over the template definition while preserving the generated kernel names byte-for-byte. It also removes the unused
<regex>dependency.Performance
An isolated C++20
-O3benchmark using a representative template definition:<bfloat16_t, float, 128, 64, 8, 8>Results over 20,000 iterations:
This benchmark measures only template-name transformation and does not imply an equivalent end-to-end inference speedup.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes