Skip to content

Rewrite prefix key-padding masks as k/v truncation to keep SDPA on the flash path (PyTorch) - #80

Open
arnaudlvq wants to merge 1 commit into
google-research:mainfrom
arnaudlvq:prefix-mask-flash-attention
Open

Rewrite prefix key-padding masks as k/v truncation to keep SDPA on the flash path (PyTorch)#80
arnaudlvq wants to merge 1 commit into
google-research:mainfrom
arnaudlvq:prefix-mask-flash-attention

Conversation

@arnaudlvq

Copy link
Copy Markdown

Problem

Any non-null attn_mask forces torch.nn.functional.scaled_dot_product_attention
off the fused flash/memory-efficient backends onto the math backend
(check_for_attn_mask, sdp_utils_cpp.h#L258),
which materializes the [T_q, T_src] score matrix per head — O(N²) memory.

In practice the PyTorch model OOMs around ~16k context rows on a 24 GB GPU.
At a 121k-row context, the score matrix would be ~235 GB per ICL block in bf16.
The fallback is silent: no warning unless a backend is forced via
torch.nn.attention.sdpa_kernel.

Fix

The ICL attention mask is always a [B,1,1,S] boolean prefix key-padding
mask: valid keys are the first n rows, the same n for every query and batch
element. Masking those keys is algebraically identical to truncating k/v to the
first n rows and passing attn_mask=None, which lets SDPA dispatch to the
fused backends (memory linear in N).

MultiheadAttention.forward now detects that exact pattern right before the
SDPA call and rewrites it; any other mask (ragged n per batch element, float
bias, non-4D) falls through unchanged. The masked form is presumably needed at
pretraining time, where packed batches have a different boundary per element —
that case is ragged and is untouched by this rewrite.

~15 lines in tabfm/src/pytorch/model.py.

Measured

100k-row x 20-feature regression context, bf16, n_estimators=8, Colab A100:
fit+predict completes with peak allocated VRAM 13.9 GB (weights included),
R² 0.999 on the synthetic task. Without the rewrite the mask keeps SDPA on the
math backend, which needs ~160 GB of scores per block at that size
(100,000² x 8 heads x 2 bytes) and OOMs around ~16k rows on a 24 GB GPU.
Also validated at 121k rows on a private real dataset with metric-identical
predictions vs. the small-context baseline.

Tests

tabfm/src/pytorch/prefix_mask_test.py (torch-only, CPU, <1 s):

  • masked forward == truncated-k/v forward (up to float32 GEMM-shape round-off,
    observed max |diff| ~2e-7);
  • a ragged per-element prefix mask is NOT rewritten and still matches
    per-element truncated forwards;
  • an all-False mask is not misdetected;
  • end-to-end TabFM forward with a batched train_size stays finite with the
    expected shape.

@google-cla

google-cla Bot commented Jul 21, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

…e flash path

Any non-null attn_mask forces torch.nn.functional.scaled_dot_product_attention
off the fused flash/memory-efficient backends onto the math backend, which
materializes the [T_q, T_src] score matrix per head (O(N^2) memory) and OOMs
around ~16k context rows on a 24 GB GPU. At a 100k-row context the scores
alone are ~160 GB per ICL block in bf16 (100,000^2 x 8 heads x 2 bytes).

The ICL attention mask is always a [B,1,1,S] boolean prefix key-padding mask:
valid keys are the first n rows, the same n for every query. Masking those
keys is algebraically identical to truncating k/v to the first n rows and
passing attn_mask=None, which lets SDPA dispatch to the fused backends with
memory linear in N. The rewrite triggers only on that exact pattern and falls
through unchanged for any other mask (ragged, float, non-4D).

Measured on a Colab A100 (bf16, n_estimators=8): 100k rows x 20 features
fit+predict, peak allocated VRAM 13.9 GB, weights included.
@arnaudlvq
arnaudlvq force-pushed the prefix-mask-flash-attention branch from f24fac7 to 4078baa Compare July 21, 2026 21:40
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.

1 participant