Repository navigation
Use 32-row flex attention blocks for head_dim > 256 #49444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qgallouedec
wants to merge
8
commits into
main
Choose a base branch
from
flex-large-head-dim-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−0
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
055f2ff
Use 32-row flex attention blocks for head_dim > 256
qgallouedec ea872f7
Merge remote-tracking branch 'origin/main' into flex-large-head-dim-b…
qgallouedec e0011f7
Point the flex block-size comment at pytorch/pytorch#200348
qgallouedec 32befb0
Merge remote-tracking branch 'origin/main' into flex-large-head-dim-b…
qgallouedec c4eb865
Merge remote-tracking branch 'origin/main' into tfu-49444
qgallouedec 2aca365
Only use the 32-row flex blocks on Hopper and datacenter Blackwell, w…
qgallouedec 6e7bdc8
Merge branch 'main' into flex-large-head-dim-blocks
qgallouedec f0f9e2f
Merge branch 'main' into flex-large-head-dim-blocks
qgallouedec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
|
|
||
| from transformers.testing_utils import ( | ||
| is_torch_available, | ||
| require_flex_attention, | ||
| require_torch_accelerator, | ||
| torch_device, | ||
| ) | ||
|
|
||
|
|
||
| if is_torch_available(): | ||
| import torch | ||
| from torch.nn.attention.flex_attention import create_block_mask | ||
|
|
||
| from transformers.integrations.flex_attention import flex_attention_forward | ||
|
|
||
|
|
||
| @require_flex_attention | ||
| @require_torch_accelerator | ||
| class FlexAttentionForwardTest(unittest.TestCase): | ||
| def test_head_dim_512_forward_and_backward(self): | ||
| # Gemma 4 global layers use head_dim 512, where the default flex block sizes fault on Blackwell | ||
| torch.manual_seed(0) | ||
| batch_size, num_heads, num_kv_heads, seq_len, head_dim = 1, 8, 2, 1024, 512 | ||
| query = torch.randn(batch_size, num_heads, seq_len, head_dim, device=torch_device, dtype=torch.bfloat16) | ||
| key = torch.randn(batch_size, num_kv_heads, seq_len, head_dim, device=torch_device, dtype=torch.bfloat16) | ||
| value = torch.randn(batch_size, num_kv_heads, seq_len, head_dim, device=torch_device, dtype=torch.bfloat16) | ||
| query.requires_grad_() | ||
| block_mask = create_block_mask(lambda b, h, q, kv: q >= kv, None, None, seq_len, seq_len, device=torch_device) | ||
|
|
||
| output, _ = flex_attention_forward(torch.nn.Module(), query, key, value, block_mask) | ||
| output.float().sum().backward() | ||
|
|
||
| reference_query = query.detach().float().requires_grad_() | ||
| reference = torch.nn.functional.scaled_dot_product_attention( | ||
| reference_query, key.float(), value.float(), is_causal=True, enable_gqa=True | ||
| ) | ||
| reference.sum().backward() | ||
|
|
||
| torch.testing.assert_close(output.float(), reference.transpose(1, 2), rtol=2e-2, atol=2e-2) | ||
| torch.testing.assert_close(query.grad.float(), reference_query.grad, rtol=2e-2, atol=2e-2) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about adding this new file. The thing is, there's no existing flex attention test file to put it in, and the per-model
test_flex_attention_with_gradsforceshead_dim16, so it never reaches this path.Also the test only catches the bug on Blackwell: it fails on main on B300 and passes with this PR. Our CI runners have no Blackwell, so there it passes either way.
Happy to drop the file or move the test if you prefer.