Skip to content

[Metal] Avoid regex in custom kernel name generation#3869

Open
aleroot wants to merge 1 commit into
ml-explore:mainfrom
aleroot:avoid-regex-custom-kernel-name
Open

[Metal] Avoid regex in custom kernel name generation#3869
aleroot wants to merge 1 commit into
ml-explore:mainfrom
aleroot:avoid-regex-custom-kernel-name

Conversation

@aleroot

@aleroot aleroot commented Jul 18, 2026

Copy link
Copy Markdown

Proposed changes

Templated custom Metal kernels generate a specialized kernel name on every invocation. This previously constructed a std::regex and called std::regex_replace to 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 -O3 benchmark using a representative template definition:

<bfloat16_t, float, 128, 64, 8, 8>

Results over 20,000 iterations:

  • Previous regex implementation: approximately 6.86 µs per call
  • New linear implementation: approximately 0.077 µs per call
  • Isolated speedup: approximately 88.6×

This benchmark measures only template-name transformation and does not imply an equivalent end-to-end inference speedup.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (not needed: this is an internal performance change with no API or behavior change)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant