Fix flex attention compilation failures in Megatron training #647
Merged
Conversation
FurtherAI
approved these changes
Apr 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue:
Megatron jobs were crashing during compilation of flex_attention, with two different errors depending on the setup:
No valid triton configs. OutOfMemoryError: out of resource: triton_flex_decoding
Required: 312320 Hardware limit: 232448
File ".../torch/_inductor/kernel/flex/flex_decoding.py", line 286, in create_flex_decoding_kernel
V.graph.sizevars.check_leq(...)
AssertionError
Root cause
Both errors come from the same wrong kernel. Inductor's flex_attention lowering has two Triton templates:
With packed training sequences + shared-prefix block masks, query lengths are small and symbolic (s29, s64 in the logs), so Inductor's dispatch heuristic decided the shape "looks like decoding" and silently routed the call into create_flex_decoding_kernel. That kernel:
So we were never actually running the training kernel — Inductor was specializing us into the wrong one.
Fix
Pass kernel_options={"FORCE_USE_FLEX_ATTENTION": True} to the compiled call in FlexAttentionWrapper. This disables the flex_decoding dispatch so all calls go through the regular training kernel.