Skip to content

Optimize EmbeddingGemma prefill performance - #32383

Merged
mickqian merged 16 commits into
mainfrom
agent/embeddinggemma-bcg-performance
Jul 27, 2026
Merged

mickqian merged 16 commits into
mainfrom
agent/embeddinggemma-bcg-performance

Conversation

@mickqian

@mickqian mickqian commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • support SentenceTransformers-packaged EmbeddingGemma, including its Dense projector tail
  • keep SGLang's CUDA graph path BCG-only; no PCG is introduced
  • batch embedding requests before scheduling so BCG captures the complete prefill
  • correct bidirectional local-attention and EOS handling
  • add the sglang serve EmbeddingGemma cookbook

Recommended sglang serve command

The standard command is the recommended command. EmbeddingGemma automatically
enables embedding mode and BCG; on H100/H200 it automatically selects FA3 and
captures the 16,384-token BCG tier:

sglang serve \
  --model-path google/embeddinggemma-300m \
  --host 0.0.0.0

EmbeddingGemma also uses the checkpoint BF16 dtype and enables list-request
batch tokenization automatically. No performance flags are required for the
measured 8 × 2047-token workload. To capture larger aggregate prefills, set
--cuda-graph-max-bs-prefill 32768 (or another required token budget).

Correctness

For the same [BOS, hello, EOS] input, SGLang BCG vs vLLM cosine similarity is 0.999945 (maximum absolute difference 0.00126). Text and pre-tokenized API inputs both use 3 tokens.

Fair H200 comparison

Both services use the same model snapshot, BF16, TP=1, OpenAI /v1/embeddings API, and separate identical H200 GPUs at the same 1980 MHz clock. Each request contains 8 explicit token-id sequences × 2047 tokens = 16,376 tokens; 3 warmups and 10 timed requests, repeated for 3 rounds.

Engine Graph mode Median p50 latency Median p50 throughput
SGLang FA3 + BCG 33.01 ms 496.1k tok/s
vLLM 0.25.1 default Inductor + PIECEWISE 144.29 ms 113.5k tok/s

SGLang BCG is 4.37× faster for this fair long-prefill embedding workload. SGLang logs confirm every timed request is one 8-sequence / 16,376-token BCG replay.

Validation

  • full pre-commit hooks
  • ruff, compileall, and git diff --check
  • H200 default sglang serve confirms FA3 + BCG capture through 16,384 tokens
  • H200 BCG and Triton eager cross-check: cosine 0.99997
  • Mintlify validation is running in PR CI

CI States

Latest PR Test (Base): 🚫 Run #30240342749
Latest PR Test (Extra): 🚫 Run #30240342532

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@github-actions github-actions Bot added the Multi-modal multi-modal language model label Jul 25, 2026
@mickqian
mickqian marked this pull request as ready for review July 27, 2026 01:56
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Base automatically changed from agent/support-embeddinggemma-bcg to main July 27, 2026 02:40
@mickqian
mickqian force-pushed the agent/embeddinggemma-bcg-performance branch from bab1d4c to eba1e06 Compare July 27, 2026 02:48
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 27, 2026
@mintlify

mintlify Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
lmsysorg 🟢 Ready View Preview Jul 27, 2026, 2:50 AM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@mickqian mickqian added the run-ci-extra CI: also run the extra suite (requires run-ci) label Jul 27, 2026
@mickqian

Copy link
Copy Markdown
Collaborator Author

urgent PR but blocked by CI. will bypassing while keeping an eye on CI

@mickqian
mickqian merged commit 08af5ae into main Jul 27, 2026
107 of 128 checks passed
@mickqian
mickqian deleted the agent/embeddinggemma-bcg-performance branch July 27, 2026 09:34
gujialiang123 added a commit to gujialiang123/sglang that referenced this pull request Jul 29, 2026
sgl-project#32383 landed fused paths for the 2-D and residual cases of Gemma3RMSNorm.
Two gaps remain.

1. Higher-rank inputs still fall back to the eager path. The dispatch is guarded
   by x.dim() == 2, but q_norm and k_norm are called with
   [tokens, heads, head_dim], so on Gemma-3 that is 2 of the 6 norms per layer
   -- 52 of 157 norm calls per forward on gemma-3-1b. RMSNorm reduces over the
   last dimension only, so flattening the leading dimensions and restoring the
   shape is exact.

2. Both fused paths pass self.weight.data without checking it. The weight is
   nn.Parameter(torch.zeros(dim)), so its dtype follows whatever default is in
   effect at construction. Under the loader's set_default_torch_dtype that
   matches the activations, but a module built outside that context keeps an
   fp32 weight -- and the fused kernels do not raise on a mismatch, they return
   NaNs:

     gemma_rmsnorm(bf16_input, bf16_weight) -> finite
     gemma_rmsnorm(bf16_input, fp32_weight) -> NaN/Inf, no exception

   Since the loader makes production weights match, this is a robustness gap
   rather than a live bug, so the fix is a guard that falls back to the eager
   path. That keeps fp32-weight semantics intact, leaves the residual path
   byte-identical to main, and adds no state to the module.

Benchmarks are the increment attributable to this change, not to fusing
Gemma3RMSNorm from scratch. Baseline is a build whose norm coverage matches
current main (2-D fused, higher rank eager); the patched arm adds the high-rank
path. gemma-3-1b-it, 1x H200, BF16, 6 reps per arm, Welch t:

  regime                    baseline      patched    gain     p
  low-batch decode          1.300 req/s   1.776     +36.6%   2.4e-14
  concurrent decode        33.648        41.891     +24.5%   1.2e-06
  4K prefill-heavy         23.385        25.085      +7.3%   0.053 (neutral)

Numerics: outputs are not bit-identical to the eager path -- the fused kernel
applies the weight in the activation dtype -- which is the same trade-off the
2-D path already accepted. 97.8-98.4% of elements match bit-for-bit and the rest
differ at the BF16 rounding scale.

Adds TestGemma3RMSNorm, which this class had none of: 2-D and 3-D inputs,
bf16/fp16, fp32 and half weights, non-contiguous views, and the residual path.
Mutation-checked -- dropping the dtype guard fails 68 subtests and dropping the
shape restore fails 28.
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation Multi-modal multi-modal language model run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant