Skip to content

[Fea] beam search sm8x support - #407

Merged
shijieliu merged 3 commits into
NVIDIA:mainfrom
z52527:fea-sid-gr-sm8x-support
May 28, 2026
Merged

shijieliu merged 3 commits into
NVIDIA:mainfrom
z52527:fea-sid-gr-sm8x-support

Conversation

@z52527

@z52527 z52527 commented May 28, 2026 •

Copy link
Copy Markdown
Collaborator

Description

  • Fast-path jagged prefill: flash_attn.cute → flash_attn (upstream FA2). FA2 ships pre-built in
    the nvcr base image, supports SM80+. Zero Dockerfile change.
  • Gate the remaining cute-bound tests (baseline generate() + Mode-3 dense prefill) on
    _SM90_AVAILABLE so the A100 CI lane skips them.

Effect

generate_beam_decode() runs end-to-end on A100. Training + baseline still SM90+ (out of scope).

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

@z52527 z52527 changed the title [Fea] beam search baseline sm8x support [Fea] beam search sm8x support May 28, 2026
@greptile-apps

greptile-apps Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR enables generate_beam_decode() to run on A100 (SM80+) by switching the jagged prefill path (Mode 1) from flash_attn.cute.interface to the upstream FA2 flash_attn_varlen_func, which ships pre-built and supports SM80+. Tests that still depend on flash_attn.cute (Mode 2 arbitrary-mask and Mode 3 dense causal) are gated on _SM90_AVAILABLE to prevent CI failures on A100.

  • jagged_flash_attn_block.py: Mode 1 now imports from flash_attn.flash_attn_interface, requires an explicit max_seqlen argument (FA2 does not infer it), and correctly assigns the return value directly (FA2 returns a plain tensor, not a tuple). Modes 2 and 3 continue using flash_attn.cute with deferred imports.
  • test_utils.py: Adds shared is_sm90_or_above() utility; test_beam_decode_generate.py and test_model_smoke.py both import it, eliminating the duplicated lambda in each file.
  • Test gating: @pytest.mark.skipif(not _SM90_AVAILABLE, ...) is added to test_prefill_returns_kv_cache, test_generate_vs_generate_beam_decode_regression_guard, test_generate_is_deterministic, and the full test_model_smoke module, while test_generate_beam_decode_e2e intentionally runs ungated on SM80+.

Confidence Score: 5/5

Safe to merge — the change is narrowly scoped to the jagged prefill fast path and the return-value handling correctly matches FA2's interface.

The Mode 1 switch from cute to FA2 is mechanically correct: FA2's flash_attn_varlen_func returns a plain tensor (not a tuple), so dropping the tuple-unpack is right. The max_seqlen argument is properly typed as int, guarded by an assertion, and threaded through all call sites. Test gating is logically consistent — only tests that exercise cute FA paths (Mode 2/3) are skipped on A100, while test_generate_beam_decode_e2e intentionally runs ungated.

No files require special attention.

Important Files Changed

Filename Overview
examples/sid_gr/model/jagged_flash_attn_block.py Mode 1 switched from cute to FA2 with explicit max_seqlen; return value correctly changed from tuple-unpack to direct assignment; Modes 2/3 imports moved inline; all clean.
examples/sid_gr/model/gpt_model.py Single-line addition of max_seqlen=input_max_seqlen to the jagged prefill call; input_max_seqlen is an int returned by _prepare_embeddings, matching the Optional[int] type expected by prefill().
examples/sid_gr/tests/test_utils.py Adds is_sm90_or_above() shared utility; correctly handles no-CUDA case; consolidates previously duplicated detection logic.
examples/sid_gr/tests/test_beam_decode_generate.py Imports shared is_sm90_or_above(), gates Mode-2/3 dependent tests on _SM90_AVAILABLE; test_generate_beam_decode_e2e intentionally left ungated since it exercises only the FA2 jagged prefill path.
examples/sid_gr/tests/test_model_smoke.py Whole file gated via pytestmark on is_sm90_or_above(); pytest is already imported so the pytestmark expression is valid.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["JaggedFlashAttnBlock.prefill()"] --> B{cu_seqlens set?}
    B -- Yes --> C["Mode 1: Jagged varlen\nflash_attn.flash_attn_interface\nflash_attn_varlen_func\n✅ SM80+ (FA2)"]
    B -- No --> D{arbitrary_func set?}
    D -- Yes --> E["Mode 2: Arbitrary mask\nflash_attn.cute.interface\nflash_attn_func\n⚠️ SM90+ only"]
    D -- No --> F["Mode 3: Dense causal\nflash_attn.cute.interface\nflash_attn_func\n⚠️ SM90+ only"]
    C --> G["max_seqlen required\n(FA2 needs explicit value)"]
    G --> J["attn_flat = flash_attn_varlen_func(...)"]
    E --> K["attn_out, _ = flash_attn_func(...)"]
    F --> L["attn_out, _ = flash_attn_func(...)"]
Loading

Reviews (3): Last reviewed commit: "refactor(sid_gr/tests): consolidate SM90..." | Re-trigger Greptile

z52527 and others added 3 commits May 28, 2026 12:31
The cute `flash_attn.cute.interface.flash_attn_varlen_func` hard-asserts
compute_capability in [9, 10], blocking A100. The jagged prefill we use
in `generate_beam_decode()` is plain causal over `[history+BOS]` packed
via `cu_seqlens` — no arbitrary mask — which standard FA2
(`flash_attn.flash_attn_interface.flash_attn_varlen_func`) supports
natively on SM80+. FA2 is shipped pre-built in the nvcr pytorch base
image (flash_attn_2_cuda.so already on PYTHONPATH), so the swap is a
local import change plus the `max_seqlen_q/k` positional args that FA2
requires (cute could infer from `cu_seqlens`; FA2 cannot).

After the swap, `generate_beam_decode()` runs end-to-end on A100:
  - prefill via FA2 (SM80+)
  - decode via Jerry's beam_decode_attn kernel (SM8x supported per its
    README arch table)

The baseline `generate()` path and the dense Mode-3 prefill fallback
still use cute FA (arbitrary mask / causal-dense), so they remain
SM90+ only. Test gating for those is added in a follow-up commit.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Runchu Zhao <[email protected]>
After the fast-path swap to FA2, the only remaining cute FA call sites
are:
  - `JaggedGPTLayer.forward()` arbitrary-mask path (baseline
    `generate()` and training)
  - `JaggedGPTLayer.prefill()` Mode-3 dense causal fallback
    (`use_jagged_kv=False`)

Both go through `flash_attn.cute.interface` which asserts
compute_capability in [9, 10], so any test that exercises them crashes
on A100. Gate those tests on a new `_SM90_AVAILABLE` flag so the A100
CI lane can run the fast-path and unit coverage without hitting the
assert.

Gated:
  - `test_generate_is_deterministic` — baseline `m.generate()`
  - `test_generate_vs_generate_beam_decode_regression_guard` —
    runs baseline as part of the regression compare
  - `test_generate_beam_decode_jagged_kv_matches_dense` — exercises
    Mode-3 dense for the comparison side
  - `TestJaggedGPTLayerPrefillDecode::test_prefill_returns_kv_cache` —
    calls `layer.prefill(x)` without `cu_seqlens`, hitting Mode-3
  - `test_model_smoke.py` entire file — both tests forward through
    the cute arbitrary-mask path

Fast-path E2E, decode-beam smoke, variable-length history padding, and
all pure-Python unit tests remain unconditionally collected and now
pass on A100.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Runchu Zhao <[email protected]>
The arch detection helper was duplicated in test_model_smoke.py and
test_beam_decode_generate.py. Single source of truth in test_utils
keeps the check coherent if it ever needs to handle multi-GPU rank
selection or switch CUDA APIs.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Runchu Zhao <[email protected]>
@shijieliu
shijieliu force-pushed the fea-sid-gr-sm8x-support branch from f1c5872 to db8810e Compare May 28, 2026 04:31
@shijieliu
shijieliu merged commit a0b364a into NVIDIA:main May 28, 2026
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.

2 participants