Repository navigation
Large head attention: keep CUDA graph capture off the mask scan and bound int32 row offsets - #14790
Conversation
…ound int32 row offsets The router scanned attention masks with a host sync during CUDA graph capture, which raises 'operation not permitted when stream is capturing'. Under capture, masked calls now go to SDPA and only cached self checks are read; eager routing is unchanged. _shapes_qualify bounded numel only, but the kernels compute row offsets in int32, so a strided view with a small numel and a huge row stride read out of bounds and returned NaN. Qualification now bounds the largest reachable row offset, and backward copies a dO whose row offset would overflow.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4ffbe4a55a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| def _wide_row_stride(shape, dtype, seed): | ||
| gen = torch.Generator(device = "cuda").manual_seed(seed) | ||
| x = torch.empty_strided(shape, (0, 0, 1 << 29, 1), device = "cuda", dtype = dtype) |
There was a problem hiding this comment.
Skip wide-stride tests when 4 GiB is unavailable
torch.empty_strided must allocate storage through the last reachable offset, so this helper reserves slightly more than 4 GiB for each new five-row test despite the tensor having only 2,560 logical elements. Both callers are gated only on CUDA availability or architecture; on a supported GPU or contended runner with less than roughly 4 GiB free, test collection proceeds and the test fails with CUDA OOM instead of skipping. Check torch.cuda.mem_get_info() before this allocation, as the repository's other int32-offset tests do.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8d68feb: the wide row stride helper now checks torch.cuda.mem_get_info() and skips when the storage it needs (about 4 GiB for five rows) plus 1 GiB is not free.
…GiB free [skip ci]
|
@codex review |
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806, #14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775, #14774, #14773, #14772, #14770, #14769, #14768, and others): "files were modified by this hook". pre-commit.ci runs the hooks on all files, and 20 files on main (26 by the time this lands) are not in the formatter's output, so every PR is reformatted outside its own diff. Where pre-commit.ci can push, its autofix commit drags those unrelated files into the PR (for example #14790's touches `tests/test_block_compile.py`, `tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`). The drift came in with PRs merged today, among them #14702, #14751, #14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode, GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target changes. ### Change `python scripts/run_ruff_format.py` (the hook's own entry point, `ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those 20 files, plus 6 more from #14739, #14779 and #14806, which merged while this was open. Formatting only. ### Verification - Every changed file parses to an identical AST before and after. - A second run of the hook over the same files changes nothing (fixed point), and over all tracked Python files on this branch it changes nothing. - `ruff check` passes on the changed files; `tests/test_formatter_fixed_point.py` passes (16). Open PRs pick this up on their next pre-commit.ci run after merging main.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…on mask-scan OOM (unslothai#14691) Two edge cases in the head_dim 512 attention router from unslothai#13869 (`unsloth/kernels/large_head_attention.py`). The common paths (no mask, a causal / packed / padded mask without an explicit `is_causal`) take the same kernel route as before; the change adds one boolean check per call (about 0.04 us). **1. Explicit `is_causal=True` next to a mask** transformers 4.57.6 `sdpa_attention_forward` keeps an explicit `is_causal` beside the mask, and SDPA then applies both. The router built its intervals from the mask alone and dropped the causal part, so a call carrying a non-causal mask (all-allowed, or a 4D padding mask) plus `is_causal=True` let early tokens attend to later ones. transformers 5.x drops `is_causal` whenever a mask is present, so the two versions disagree on what that pairing means. The router now hands it to the wrapped function, which keeps each version's own meaning. No transformers model passes `is_causal` into the attention interface, and Unsloth's own callers that pass `is_causal=True` drop the mask first, so this only affects model code that passes both. **2. OOM in the mask scan** `intervals_from_mask` allocates int32 row chunks (512 MiB at mask batch 8, length 16384) before the kernel's own OOM guard, so an OOM there escaped instead of falling back to SDPA. It now falls back like an OOM in the kernel. The strided-view int32 bound this PR first carried landed on main in unslothai#14790 (`_row_span`, which bounds only the int32 row offset since batch and head offsets are int64); main's version is kept and the duplicate helper and tests were dropped in the merge. **Tests** (`tests/test_large_head_attention.py`, both fail on main and pass here): - `test_router_leaves_an_explicit_is_causal_beside_a_mask_to_sdpa` - `test_router_falls_back_to_sdpa_on_mask_scan_oom` **Verification** (B200, torch 2.13, transformers 5.19): - Test file, main source vs this branch with the branch's tests: 2 FIXED (fail on main, pass here), 60 unchanged passes, 0 failures on the branch. - Mask plus `is_causal=True` probe with the 4.57.6 `sdpa_attention_forward` semantics (repeat_kv, then SDPA with both): main returns 6.81 for row 3 where SDPA gives 2.03 (it reads future tokens, max abs diff 10.19); this branch matches SDPA exactly (max abs diff 0.0). - Causal mask without `is_causal`: one kernel call on both main and this branch, max diff 0 vs SDPA. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Two fixes in
unsloth/kernels/large_head_attention.py.CUDA graph capture with an attention mask
The router reads a mask's structure with
intervals_from_mask, which ends inok.item().Inference masks are rescanned on every call, so capturing a forward with
torch.cuda.graphfailed withoperation not permitted when stream is capturing, evenafter warm-up.
While the current stream is capturing, masked calls now go to SDPA, and the self check is
only read from its cache, as in the compiled branch. Unmasked calls still run the kernel
inside the graph. Eager routing is unchanged. SDPA is also the only correct choice here,
because a graph would otherwise replay one mask's verdict for whatever the buffer holds later.
Strided operands past int32 row offsets
_shapes_qualifyonly boundednumel(). The kernels compute row offsets(
offs_m * stride_m + offs_d) in int32, so a legal view with a small numel and a huge rowstride qualified. For example, a [1, 1, 5, 512] query with row stride 229 has 2,560
elements but its last row starts 231 elements in. It then read out of bounds: NaN output,
and 64 invalid global reads under compute-sanitizer.
Qualification now also bounds
(S - 1) * stride_m + (D - 1)per operand. Batch and headoffsets are already int64, so they are not restricted. Inputs that fail fall back to SDPA,
and the fast path stays in int32. Backward also copies a
dOwhose row offset wouldoverflow.
Tests
New tests in
tests/test_large_head_attention.py. All four fail on main and pass here:test_shapes_qualify_bounds_the_reachable_row_offset(CPU, calls_shapes_qualifydirectly, including the exact 2**31 - 1 edge)
test_router_sends_a_wide_row_stride_query_to_sdpa(GPU, vs an fp32 unfused reference)test_backward_copies_a_wide_row_stride_grad(GPU, grads vs fp32 reference)test_router_captures_into_a_cuda_graph_with_an_inference_mask(GPU, capture + replay,compared with eager)
On B200 with torch 2.13 / Triton 3.7:
torch.compile(fullgraph=True), static and dynamic, shows no graph breaks, and compiledoutput equals eager.
Not addressed: inference masks are still rescanned on every eager call, because an
inference tensor has no version counter to detect in-place edits (covered by
test_inference_mask_is_rescanned_after_in_place_update). Short prefill latency isunchanged from main.