Repository navigation
Jagged Arbitrary Masked Self Attention support - #339
Conversation
Greptile SummaryThis PR introduces
Confidence Score: 3/5The core routing logic in decoder_step has a gap: when attention_mask is provided to an FA-configured model, the fallback is bypassed and a bare AssertionError fires, making the documented contract incorrect. The decoder_step routing has a gap where providing attention_mask to an FA-configured model bypasses the fallback and crashes with an uninformative assertion. Combined with unresolved issues from prior rounds (random weights in lazy-init, per-step mask rebuild in generation, beam-branch isolation, unpinned Dockerfile dependency), the change is not ready to merge without targeted fixes. examples/sid_gr/model/gpt_model.py (decoder_step routing), examples/sid_gr/model/attention_mask.py (dead code / missing seqlen assertion), docker/Dockerfile (unpinned branch) Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant SIDGRModel
participant decoder_step
participant SIDGRDecoder
participant JaggedTransformerBlock
participant TransformerBlock
Caller->>SIDGRModel: "forward(batch, arbitrary_func=None, attention_mask=None)"
SIDGRModel->>decoder_step: "input_hidden_states, input_offsets, arbitrary_func=None"
alt both masks None
decoder_step->>decoder_step: build_jagged_causal_arbitrary_func(offsets) [FA path]
decoder_step->>decoder_step: padded_causal_mask_with_optional_bos(offsets) [mcore path]
else "attention_mask provided but arbitrary_func=None on FA path"
decoder_step->>decoder_step: assert arbitrary_func is not None fires
end
alt "use_jagged_flash_attn=True"
decoder_step->>SIDGRDecoder: forward(hidden_states, arbitrary_func)
SIDGRDecoder->>JaggedTransformerBlock: forward([total_tokens,D], arbitrary_func)
JaggedTransformerBlock->>JaggedTransformerBlock: unsqueeze(0) to [1,T,D]
JaggedTransformerBlock->>JaggedTransformerBlock: "flash_attn_func(arbitrary=True)"
JaggedTransformerBlock->>JaggedTransformerBlock: squeeze(0) to [T,D]
JaggedTransformerBlock-->>SIDGRDecoder: [total_tokens, D]
else "use_jagged_flash_attn=False"
decoder_step->>SIDGRDecoder: forward(hidden_states, attention_mask, offsets, max_seqlen)
SIDGRDecoder->>SIDGRDecoder: jagged to dense [S,B,D]
SIDGRDecoder->>TransformerBlock: forward(padded, attention_mask)
TransformerBlock-->>SIDGRDecoder: padded output
SIDGRDecoder->>SIDGRDecoder: dense to jagged [total_tokens,D]
end
decoder_step-->>SIDGRModel: jagged output hidden states
Reviews (11): Last reviewed commit: "Merge branch 'main' into fea-JaggedTrans..." | Re-trigger Greptile |
|
update arbitary mask FA in 3rd party module |
|
/build |
1 similar comment
|
/build |
|
❌ Pipeline #49662233 -- failed
Result: 9/14 jobs passed |
|
track CI here |
| if attention_mask is None and arbitrary_func is None: | ||
| if self.decoder.use_jagged_flash_attn: | ||
| total_tokens = int(input_offsets[-1].item()) | ||
| arbitrary_func = build_jagged_causal_arbitrary_func( | ||
| input_offsets, total_tokens | ||
| ) | ||
| else: | ||
| add_bos = ( | ||
| self.add_bos_to_history_for_training | ||
| if default_mask_add_bos_to_history is None | ||
| else default_mask_add_bos_to_history | ||
| ) | ||
| attention_mask = padded_causal_mask_with_optional_bos( | ||
| input_offsets, | ||
| input_max_seqlen, | ||
| add_bos_to_history=add_bos_to_history, | ||
| add_bos_to_history=add_bos, | ||
| bos_interval=self._num_hierarchies, | ||
| ) | ||
| else: | ||
| # THD still needs batch dimension | ||
| # we need to unsqueeze the hidden states to [T, 1, hidden_size] and unsqueeze back after decoder | ||
| assert input_hidden_states.dim() == 2, "input_hidden_states should be 2D" | ||
| decoder_input_hidden_states = input_hidden_states.unsqueeze(1) | ||
| attention_mask = None | ||
| packed_seq_params = to_packed_seq_params( | ||
| input_offsets, | ||
| input_max_seqlen, | ||
|
|
||
| if self.decoder.use_jagged_flash_attn: | ||
| assert arbitrary_func is not None | ||
| return self.decoder( | ||
| hidden_states=input_hidden_states, | ||
| arbitrary_func=arbitrary_func, | ||
| ) |
There was a problem hiding this comment.
Wrong mask type silently skips fallback and crashes
When use_jagged_flash_attn=True and a caller passes attention_mask (without arbitrary_func), the guard at line 554 is not entered (because attention_mask is not None). No fallback is built, and the assert arbitrary_func is not None at line 574 fires as a bare AssertionError with no message. Any caller that was previously passing attention_mask to this function will silently get an opaque crash after switching to the FA backend, even though the docstring says "Pass either tensor to override." The condition should also handle the case where the wrong mask type is provided for the active backend, either by ignoring attention_mask gracefully on the FA path or by raising an explicit, informative error.
Bring origin/main into the branch so the PR shows only the beam_decode work as net new content (instead of double-counting the fea-JaggedTransformerBlock changes that already landed via PR NVIDIA#339). Conflicts resolved by keeping the beam_decode-side additions and discarding the pre-NVIDIA#339 versions of shared text: * docker/Dockerfile: keep the FA SHA pin (FLASH_ATTN_ARBITRARY_MASK_COMMIT). * examples/sid_gr/beam_search/beam_search.py: keep parent_indices list (used by build_beam_topk_indices). * examples/sid_gr/model/attention_mask.py: keep the n_func single- assignment cleanup; keep build_jagged_target_aware_arbitrary_func and dense_mask_to_jagged_arbitrary_func. * examples/sid_gr/model/gpt_model.py: keep get_jagged_flash_attn_block(), the beam-isolating mask construction in generate(), the dense->jagged arbitrary_func adapter in decoder_step(), and the updated docstring wording. Re-add the padded_target_aware_causal_mask import that generate() needs. * examples/sid_gr/model/jagged_flash_attn_block.py: keep the beam_decode_attn resolver, _beam_decode_attn_reference fallback, build_block_sparsity helper, JaggedGPTLayer decode_beam method, JaggedFlashAttnBlock prefill/decode_beam methods, plus the updated doc tone ("arbitrary-mask FlashAttention" / inference-only scope). * examples/sid_gr/training/README.md: keep the corrected beam-search limitation paragraph and the de-personified FA dependency label. Validation: 44/44 tests in test_beam_decode_generate.py and 37/37 tests in test_jagged_flash_attn_block.py + test_dense_mask_to_arbitrary_func.py pass after the merge. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> Signed-off-by: Runchu Zhao <[email protected]>
Resolves #276
Summary
Replace Megatron-Core's
TransformerBlock(padded dense +DotProductAttention) with aJaggedTransformerBlockthat flattens all batch sequences into a single sequence (B=1) and uses jiayus's Flash Attention witharbitrary_funcmask encoding. Zero padding, block sparsity skips cross-batch blocks.Both backends are preserved via
use_jagged_flash_attnflag; mask /arbitrary_funcis caller-provided, not built inside the model.Architecture
Changed Files
model/jagged_flash_attn_block.pyJaggedGPTLayer(FA kernel per layer),JaggedFlashAttnBlock(layer stack),JaggedTransformerBlock(wrapper: unsqueeze→FA→squeeze)model/attention_mask.pydense_mask_to_arbitrary_func(per-batch dense→interval encoding),build_jagged_causal_arbitrary_func(vectorized, offsets→flattened causal arbitrary_func)model/gpt_model.pySIDGRDecoder:use_jagged_flash_attnflag selectsJaggedTransformerBlockor mcoreTransformerBlock.decoder_step: pure routing, zero mask construction.forward/generate: caller passesarbitrary_funcorattention_maskmodel/__init__.pyget_sid_gr_model: propagateuse_jagged_flash_attnflagtests/test_jagged_flash_attn_block.pyJaggedGPTLayer; FA-vs-reference comparison forJaggedTransformerBlock; batch isolation testtests/test_dense_mask_to_arbitrary_func.pydense_mask_to_arbitrary_func; flattened jagged causal / target-grouped tests;dense_mask_to_jagged_arbitrary_funcas test utilityTest Plan
pytest examples/sid_gr/tests/test_dense_mask_to_arbitrary_func.py— arbitrary_func encoding roundtrippytest examples/sid_gr/tests/test_jagged_flash_attn_block.py— FA layer correctness, JaggedTransformerBlock vs padded reference, batch isolationmypy examples/sid_gr/model/gpt_model.py— type check clean