Repository navigation
Fix Pixtral Flash Attention image boundaries after axial RoPE refactor - #49373
Conversation
Signed-off-by: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com>
Signed-off-by: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com>
| # We only rely on position_ids when using flash attention | ||
| # Axial RoPE positions have two coordinates per patch, so they cannot | ||
| # describe the packed image boundaries expected by Flash Attention. | ||
| sequence_lengths = [p.shape[-2] * p.shape[-1] for p in patch_embeds_list] | ||
| cu_seqlens = torch.tensor([0, *sequence_lengths], device=patch_embeds.device, dtype=torch.int32) | ||
| cu_seqlens = cu_seqlens.cumsum(dim=0, dtype=torch.int32) | ||
| kwargs.update( | ||
| cu_seq_lens_q=cu_seqlens, | ||
| cu_seq_lens_k=cu_seqlens, | ||
| max_length_q=max(sequence_lengths), | ||
| max_length_k=max(sequence_lengths), | ||
| ) | ||
| attention_mask = None |
There was a problem hiding this comment.
hmm quite the same as vision utils but oke, not really worth materializing image_grid only to infer FA kwargs
transformers/src/transformers/vision_utils.py
Lines 68 to 78 in e598fba
| @require_flash_attn | ||
| @require_torch_gpu | ||
| def test_flash_attention_2_packed_images(self): | ||
| config = PixtralVisionConfig( | ||
| hidden_size=256, | ||
| intermediate_size=512, |
There was a problem hiding this comment.
actually we have a test tests/models/pixtral/test_modeling_pixtral.py::PixtralVisionModelModelTest::test_flash_attn_2_inference_equivalence
and i see it failing on main so lets just make sure it passes now, this new test is not needed
There was a problem hiding this comment.
yeah, the workflow is all green so lets just delete the test and i'll merge
There was a problem hiding this comment.
Thanks for the review and running the tests. I've removed the new test.
|
run-slow: pixtral |
AMD CIThis comment contains models: ["models/pixtral"] |
Nvidia CIThis comment contains models: ["models/pixtral"] |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Signed-off-by: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com>
|
[For maintainers] Suggested jobs to run (before merge) run-slow: pixtral |
CI recapDashboard: View test results in Grafana |
What does this PR do?
Fixes #49371
Pixtral passes axial RoPE coordinates shaped [total_patches, 2] to the Flash Attention packed-sequence detector. It flattens both coordinates, producing false image boundaries and a terminal cumulative length twice the number of Q/K/V tokens. A checkpoint-free Pixtral model raises a CUDA illegal memory access on Transformers 5.17.0, 5.18.0, and main at 571c5cc; smaller configurations can return incorrect output without crashing. The same reproduction passes on 5.16.1.
This change derives cumulative lengths and maximum lengths from the per-image patch grids, and passes them explicitly in the flash attention case. Keeping axial coordinates for rotary embeddings and preserve the existing block mask for other attention implementations.
This follows the Qwen2-VL/Qwen3-VL vision-attention approach:
transformers/src/transformers/models/qwen3_vl/modeling_qwen3_vl.py
Lines 287 to 298 in 571c5cc
Related work / coordination
Who can review?
@zucchini-nlp