Repository navigation
Conversation
…tiple of 128 The cuDNN SDPA backward bundled with PyTorch 2.7 (cudnn-frontend 1.10, cuDNN 9.7) computes the last key tile without a padding mask when s_kv is not a multiple of 128. Query rows whose visible logits are all very negative (logsumexp below about -88.7) then evaluate exp(0 - lse) = inf on the out-of-range columns and get NaN in dQ, while the forward output is correct (NVIDIA/cudnn-frontend#160, pytorch/pytorch#166211). presets/fast.env forces cuDNN, and OpenWAM's joint MoT attention (392 keys at 384x320) and text cross-attention (64 keys) are affected: dual-system joint_self_attn trained from the raw Wan2.2 weights with attention_mask_mode=action_sees_video diverged to NaN within 5-21 steps. When cuDNN is preferred and the call will be differentiated, sdpa() now pads K/V with zero keys to the next multiple of 128 and hides them in the mask (bool False, additive -inf, or a key-padding mask when none is given), keeping the physical K/V layout. The hidden keys get zero attention, so outputs and gradients are unchanged; calls without autograd are not padded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…NN NaN regression The low-logsumexp regression now also runs with head_dim 64 (the Cosmos joint_cross_attn bridge heads) and with the low row in the last, partial query tile. Both variants produce NaN dQ on main and pass with the K/V padding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
presets/fast.env, whichscripts/train.shloads by default since #58, setsOPENWAM_SDPA_CUDNN=1, so masked SDPA runs on cuDNN first.The cuDNN SDPA backward bundled with PyTorch 2.7.1 (cudnn-frontend 1.10, cuDNN 9.7.1) has a bug. When
s_kv % 128 != 0, it computes the last 128-key tile without a padding mask. For a query row whose visible logits are all very negative (logsumexp below about −88.7), the backward then evaluatesexp(0 − lse) = infon the out-of-range key columns and writes NaN into that row of dQ. The forward output is still correct. See NVIDIA/cudnn-frontend#160 and pytorch/pytorch#166211; cudnn-frontend ≥ 1.15 applies a padding mask itself.OpenWAM hits this. At 384×320 with 33-frame windows, the joint MoT attention has 392 keys and the text cross-attention has 64. Training dual-system
joint_self_attnfrom the raw Wan2.2-TI2V-5B weights withattention_mask_mode=action_sees_videoon LIBERO went NaN after 5–21 steps:Fix. When cuDNN is preferred and the call will be differentiated,
sdpa()pads K/V with zero keys up to the next multiple of 128. The padded keys are hidden in the mask:Falsefor a bool mask,-inffor an additive mask, or a new key-padding mask when none is given. They receive exactly zero attention, so outputs and dQ/dK/dV are unchanged. K/V keep their physical layout, for the same reason given in_zero_masked_rows. Calls without autograd are not padded, because the forward kernel is unaffected.Validation (H200, torch 2.7.1+cu128, cuDNN 9.7.1)
Minimal reproduction. cuDNN forced. One query row is anti-aligned with keys that share a large component (row logsumexp ≈ −111 to −120). B=2, H=4, D=128.
mainmain.Training.
action_sees_video, Wan2.2 init, LIBERO, 7×H200, 32/GPU, seed 42,presets/fast.env):main: NaN from step 22.OPENWAM_SDPA_CUDNN=0.mutual, cuDNN on):mainwithin 0.0018 over 40 steps.Tests.
tests/test_sdpa_kv_padding.py:s_qon the tile withs_kvoff it) × 3 mask kinds × 2 layouts = 30 cases.joint_cross_attnbridge heads) and 128.mainand pass here.tests/test_sdpa_empty_rows.pyandtests/test_fast_kernels.py, including their GPU tests: 77 passed together with the new file.-m "not gpu"): 2152 passed, 1 failed; the failure,test_cosmos_predict25_build_with_bad_path_raises_clear_error, fails identically onmain.Notes.
OPENWAM_SDPA_CUDNN=1).copy_, which is vectorized for bf16, unliketorch.catin PyTorch 2.7.🤖 Generated with Claude Code