Repository navigation
Update beam search benchmark RESULTS.md - #406
Conversation
…ay light `tests/test_utils.py` had `commons.distributed.sharding`, `commons.modules.embedding`, `commons.optimizer`, `configs.gpt_config`, `model.gpt_model`, `model.mcore_model_specs` at module top. That meant any `from tests.test_utils import X` paid the full Megatron + torchrec + dynamicemb import chain, even when X is just a pure-tensor function like `validate_compare_outputs`. `test_compare_kv_modes_default_raises_on_validation_failure` was designed as a lightweight control-flow check that imports `validate_compare_outputs` directly. It now fails outside the full Docker stack with `ModuleNotFoundError: No module named 'commons'` because the test_utils module load can't complete. Fix: move the heavy imports inside `create_sid_gr_model_and_optimizer` (the only function that actually needs them) and switch the `ShardedEmbeddingConfig` parameter annotation to a TYPE_CHECKING-only string forward ref. Pure-tensor validators above the model factory load cleanly without commons/megatron present. Verified: `pytest examples/sid_gr/tests/test_beam_decode_generate.py examples/sid_gr/tests/test_dense_mask_to_arbitrary_func.py`: 62 passed, 10 skipped, 0 failed (was 61 / 10 / 1). Signed-off-by: Runchu Zhao <[email protected]>
Trims 389 → 141 lines and restructures around what Alex asked for in
review:
- Open with an explicit scope line: this is end-to-end (full transformer
stack + MLP + LayerNorm + LM head + beam-search bookkeeping), not the
attention kernel in isolation. Point readers at upstream
cjerry/gr-decode_atten for the per-call kernel correctness sweep
and micro-benchmark.
- Replace the six-table layout (Summary table, Long-history table,
Sweep table, Jagged-vs-dense phase table, "When jagged would win"
extrapolation table, tunable-flags table) with a single 4 × 4
speedup grid (Batch ∈ {1, 4, 8, 16} × hist ∈ {256, 512, 1024, 2048})
plus a sibling absolute-latency grid for context. Drop the
Observations section — everything in it was either visible from the
table or duplicated the method paragraph.
- Update jagged-vs-dense with real measurements at the new config
(hidden=1024, beam_w=200, B=16). The earlier "crossover ≥ hist 2000"
extrapolation is empirically wrong: dense is 17% – 6× faster across
hist 256 – 2048. Keep the flag as developer-facing but recommend the
default everywhere.
Data: re-run on H100 NVL 94 GB, hidden=1024 / heads=8 / head_dim=128 /
layers=8 / hierarchies=4 / beam_w=200 / bf16. All 16 configs pass top-K
beam overlap ≥ 70%. Headline: B=16 hist=2048 hits 28.3× e2e
(generate=7.58 s vs generate_beam_decode=268 ms). B=1 stays ≈ 1×
because Python orchestration dominates wallclock at single-user scale.
Signed-off-by: Runchu Zhao <[email protected]>
The jagged-native prefill path in generate_beam_decode used to build a block-diagonal causal mask via build_jagged_causal_arbitrary_func and feed it through FA's arbitrary_func path. arbitrary_func goes through a generic CSR block-sparsity setup that scales as O(N²) with history length — at B=16 hist=2048 it took ~1.6 s, vs ~262 ms for the dense seqused_k path (6× slower). The semantics are just per-sample causal on packed input, so FA's flash_attn_varlen_func (causal=True + cu_seqlens) already covers it with the standard causal fast path. Plumb cu_seqlens + max_seqlen through JaggedGPTLayer.prefill and JaggedFlashAttnBlock.prefill, and switch the generate_beam_decode jagged caller to use it. Measured on H100 80GB HBM3 (SXM), bf16, hidden=1024, layers=8, hierarchies=4, beam_w=200, B=16: | hist | dense (ms) | jagged old (cute arbitrary) | jagged new (varlen) | |------|-----------:|----------------------------:|--------------------:| | 256 | 27.1 | 34.5 | 23.3 | | 1024 | 59.5 | 268.8 | 41.6 | | 2048 | 118.1 | 1603.1 | 80.1 | Jagged flips from 0.16× dense (decisively losing) to 1.48× dense (decisively winning) at hist=2048. The cu_seqlens path keeps the "no padding waste" benefit of jagged while avoiding the arbitrary mask's setup tax. Correctness: --compare_kv_modes at the same config yields top-K beam overlap 95-97% across all 3 paths (A=generate, B=dense beam_decode, C=jagged beam_decode), well above the 70% regression threshold. Signed-off-by: Runchu Zhao <[email protected]>
Replace the H100 NVL measurements with a clean run on H100 80GB HBM3 (SXM). The previous NVL node turned out to be shared / throttled — generate() at hist=2048 B=16 dropped from 7.6 s to 4.0 s after the re-allocation despite NVL having higher headline memory bandwidth on paper. Switch the headline numbers to the new use_jagged_kv=True default (introduced in the previous commit). Updated grid: | Batch ↓ hist → | 256 | 512 | 1024 | 2048 | | 1 | 1.04× | 1.03× | 1.03× | 1.49× | | 4 | 1.14× | 1.63× | 2.19× | 8.59× | | 8 | 2.27× | 3.61× | 6.15× | 16.34× | | 16 | 5.97× | 10.77× | 20.98× | 49.73× | Rewrite the jagged-vs-dense section: dense (B_ms/C_ms = 0.85× to 0.16× "dense wins") flips to jagged (1.17× to 1.48× "jagged wins") at the same shapes because the prefill path is now varlen+causal rather than arbitrary_func+CSR. Reproduce command updated to pass --use_jagged_kv. Signed-off-by: Runchu Zhao <[email protected]>
After the varlen prefill switch (previous commits), use_jagged_kv=True is the recommended default — the jagged path wins on every measured config at B >= 4 and ties at B=1. Drop the "jagged-vs-dense" framing that implied the choice was still contested and call out the default inline. Signed-off-by: Runchu Zhao <[email protected]>
The two 4×4 grid tables (speedup and absolute generate() latency) made the trend hard to read at a glance. Replace with a single grouped bar chart on log y-axis at figs/speedup_grid.png: x = history length, color = batch size, value labels above each bar. The 1× line is drawn as a dashed reference so the B=1 short-hist plateau is visible as bars sitting on the baseline. The numeric take-home (B=16 hist=2048 → ~50× speedup; B=1 short-hist is overhead-bound and stays near 1×) is now in the figure caption + the existing Summary section. Signed-off-by: Runchu Zhao <[email protected]>
The previous default was False, contradicting RESULTS.md (which has claimed the jagged path is the recommended default since the varlen prefill switch). Flip the function-level default + the benchmark CLI default so the code matches the docs and users get the faster path by default. CLI flag uses argparse.BooleanOptionalAction so the old `--use_jagged_kv` invocation still works (now a no-op since default is already True), and `--no-use_jagged_kv` opts back into the dense fallback for callers that need it. Update generate_beam_decode's use_jagged_kv docstring to match the implementation: prefill runs through flash_attn_varlen_func with causal=True + cu_seqlens. Drop the stale "dense wins short / jagged wins long" framing left over from the arbitrary_func era. Signed-off-by: Runchu Zhao <[email protected]>
…jagged-only) Five fixes from codex review on the previous RESULTS rewrite: 1. Reproduce command was `--batch_size 16` only, but the bar chart covers four batches. Replaced with a 4-iteration shell loop so a reviewer can actually reproduce the figure. 2. Correctness verification was tagged "weakest to strongest" but the list runs kernel oracle → mask isolation → overlap guard, which is strongest to weakest. Flip the tag. 3. Restore benchmark provenance (date, branch, kernel pinned commit, flash-attention pinned commit, container torch / CUDA versions) on the hardware line. 4. Drop the entire "Jagged-native context K/V" section and the matching "use_jagged_kv=False kept as alternative" bullet from Known issues. The decision is settled — jagged + varlen is the default; dense path stays in code as a legacy fallback but doesn't need user-facing framing. 5. examples/sid_gr/README.md: drop the `use_jagged_kv` mention from the "see benchmark/RESULTS.md" pointer line so the upper-level doc doesn't reintroduce the same comparison framing. Issue #1 from the codex audit (default flip + docstring refresh) was addressed in the previous commit. Signed-off-by: Runchu Zhao <[email protected]>
…eview Per review feedback: the speedup ratio chart obscured the absolute cost story. Replace with a 4-panel (one per batch) grouped bar chart of e2e latency in ms, log scale, with generate() and generate_beam_decode() side-by-side per history length. "Lower is better" is on the title. Speedup ratio now lives in a 4-row corner-case table in the prose below the figure. The growth-with-batch / growth-with-history explanation that used to be a Summary section is folded into the same prose, with a short paragraph on why B=1 stays near 1×. Drops examples/sid_gr/figs/speedup_grid.png; adds examples/sid_gr/figs/latency_grid.png. Signed-off-by: Runchu Zhao <[email protected]>
Signed-off-by: Runchu Zhao <[email protected]>
…nstraints Two follow-ups from codex review: 1. "generate_beam_decode() processes only beam_width new tokens per step" was wrong — it processes B × beam_width (one beam set per sample). Likewise the "Along batch" bullet implied decode-side compute didn't scale with B, which is incorrect. Reword both places to say "per sample beam_width, batch-wide B × beam_width" and note that the savings vs generate() are proportional to history rather than to batch. 2. Surface the two ValueErrors that generate_beam_decode raises at entry: (a) uniform beam_widths across hierarchy steps required by the kernel's fixed stride, and (b) beam_width ≤ min(codebook_sizes) required so BeamSearch.propagate doesn't clamp topk and break that stride. Realistic SID-GR retrieval uses beam_width in the hundreds — calling out the codebook-size floor matters for users sizing their tables. Signed-off-by: Runchu Zhao <[email protected]>
Greptile SummaryThis PR rewrites the beam search
Confidence Score: 5/5Safe to merge; all logic changes are well-scoped, KV cache shapes are correctly handled, and the benchmark numbers are consistent with the code. The core correctness path — jagged prefill producing [1, T, H, D] KV caches that are squeezed to [T, H, D] before the decode kernel — is implemented correctly and confirmed by the reported 70%-overlap correctness check across all 16 configs. The flash_attn_varlen_func import is container-specific but intentional and documented. No logic errors or data-correctness issues were found. jagged_flash_attn_block.py is the most changed functional file and warrants a read-through by anyone running outside the pinned Docker container. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["generate_beam_decode(use_jagged_kv=True [default])"] --> B{backend?}
B -->|3kernel + use_jagged_kv=True| C["Jagged prefill (NEW default)\nflash_attn_varlen_func — varlen+causal fast path"]
B -->|3kernel + use_jagged_kv=False| D["Dense prefill (legacy)\nflash_attn_func — causal=True"]
B -->|dsl| E["Dense prefill (uniform history only)"]
C --> F["Squeeze KV caches: [1,T,H,D] → [T,H,D]\nStore cu_seqlens_k for decode kernel"]
D --> G["Keep [B,S,H,D] KV caches\nStore seqused_k for decode kernel"]
F --> I["Decode loop: beam_decode_attn kernel (cu_seqlens_k path)"]
G --> J["Decode loop: beam_decode_attn kernel (seqused_k path)"]
Reviews (2): Last reviewed commit: "fix(sid_gr): drop dead max_seqlen param ..." | Re-trigger Greptile |
Greptile flagged that max_seqlen was declared on both JaggedGPTLayer.prefill and JaggedFlashAttnBlock.prefill and documented as "required when cu_seqlens is set; passed through to FA", but never forwarded to flash_attn_varlen_func. The cute interface (flash_attn.cute.interface.flash_attn_varlen_func) does NOT accept max_seqlen_q / max_seqlen_k — it infers them from cu_seqlens at call time. Greptile's suggested fix (add them to the FA call) would actually fail with TypeError on the cute version. Right fix is the other direction: drop the dead param from our API, drop the misleading docstring claim, and drop the now-unused max_seqlen=input_max_seqlen kwarg from the gpt_model.py jagged prefill caller. Tests (62 passed, 10 skipped) confirm behavior is unchanged. Signed-off-by: Runchu Zhao <[email protected]>
Description
Update beam search benchmark RESULTS.md
Checklist