Skip to content

fix(flex): avoid decoding kernel selection for short GQA - #25

Merged
zhouaihui merged 1 commit into
inclusionAI:mainfrom
zhouaihui:fix/flex-training-backend
Nov 12, 2026
Merged

zhouaihui merged 1 commit into
inclusionAI:mainfrom
zhouaihui:fix/flex-training-backend

Conversation

@zhouaihui

@zhouaihui zhouaihui commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #24

Outcome

On the pinned PyTorch 2.13 runtime, short GQA training rows can enter FlexAttention's automatic
decoding path and fail with NoValidChoicesError. Default the shared wrapper to
BACKEND=TRITON, which uses the standard attention kernel.

Preserve explicit BACKEND and legacy FORCE_USE_FLEX_ATTENTION choices without mutating
caller options. Add CPU policy checks to CI and GPU output/gradient regressions for the
previously failing shape, including calls nested inside torch.compile.

Verification

  • Reproduced raw AUTO failure with Q [1,32,112,128], K/V [1,8,262,128] and a DFlash block
    mask; this requires no model checkpoint. Also reproduced and fixed the initial patch's
    legacy-selector conflict.
  • Isolated Linux CPU/offline checks: 157 passed, 10 skipped, plus 6 subtests.
  • Single-GPU checks on SM90: 6 passed, plus 16 subtests. Short GQA BF16 output
    and Q/K/V-gradient relative L2 errors are below 1% against SDPA's math backend; existing
    attention forward/backward comparisons also pass.
  • Full ruff check, maintained documentation links, and CI YAML/test selection checks pass.
  • Runtime: Python 3.12.3, Torch 2.13.0+cu130, Transformers 5.12.1. Source bytes match the
    verified Linux copy. Other GPU architectures, Torch versions and throughput are unverified.

Compatibility and provenance

  • The policy is local to the shared FlexAttention wrapper. Explicit selectors remain under
    the caller's control; attention masks, model configuration and training objectives retain
    their existing contracts.
  • Adapted from the retained TorchSpec helper at
    6c042a87140a84d13839e341ece2c5c3ada918bc; source headers identify the adaptation and preserve
    the upstream MIT notice. New CPU tests carry ASpec attribution. Existing license texts remain
    applicable.

Default the shared FlexAttention wrapper to BACKEND=TRITON to avoid
PyTorch 2.13 AUTO lowering short GQA rows to an invalid decoding kernel.
Preserve explicit backend and legacy force selectors without mutating
caller options.

Cover option handling in CPU CI and compare short-row outputs and Q/K/V
gradients with SDPA under wrapped and nested compilation.

@Longxmas Longxmas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhouaihui
zhouaihui merged commit b20b5fa into inclusionAI:main Nov 12, 2026
2 checks passed
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.

Fix FlexAttention kernel selection for short GQA training rows

2 participants