Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[tests] fix anyflow tests#13855

Merged
sayakpaul merged 5 commits into
mainfrom
fix-anyflow-tests
Jun 4, 2026
Merged

[tests] fix anyflow tests#13855
sayakpaul merged 5 commits into
mainfrom
fix-anyflow-tests

Conversation

@sayakpaul

Copy link
Copy Markdown
Member

What does this PR do?

Fixes https://github.com/huggingface/diffusers/actions/runs/26819669538/job/79072404192?pr=13712. Note that the approach I have taken to fix the dtype mismatch error includes an explicit cast. We can disable the layerwise casting tests if that's preferred.

Additionally, removes the unittest stuff and solely uses pytest.

@github-actions github-actions Bot added models tests size/S PR with diff < 50 LOC labels Jun 3, 2026
@sayakpaul sayakpaul requested a review from dg845 June 3, 2026 04:59
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

key = attn.norm_k(key)

# Make layerwise upcasting work.
value = value.to(query.dtype)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value = value.to(query.dtype)
query = query.to(target_dtype)
key = key.to(target_dtype)

After some debugging with Claude, I think this should fix all of the layerwise casting issues (where target_dtype = hidden_states.dtype is the effective compute dtype, cached before the to_q/to_k/to_v projections happen). I tested this out locally and all the tests pass after this change, including TestAnyFlowFARTransformer3DMemory::test_layerwise_casting_training and TestAnyFlowFARTransformer3DTraining::test_mixed_precision_training, which were still failing even with the PR.

My understanding of the issue is as follows:

  1. Because norm_q and norm_k are skipped due to _skip_layerwise_casting_patterns, and because they are diffusers.models.normalization.RMSNorms which upcast to FP32 during the variance calculation, they end up upcasting query and key to FP32.
  2. Because value is not normed, it stays in the effective compute dtype target_dtype (for example, the autocast dtype inside a torch.amp.autocast region like in test_layerwise_casting_training).
  3. If we cast value to query's dtype, we will therefore end up casting value to FP32, regardless of the effective compute dtype. In this case, the Flex Attention operation will always be performed in FP32.
  4. However, if we are training, during the backward pass the gradients from the first layer after Flex Attention, which is to_out, will be in target_dtype (for example because torch.amp.autocast will automatically downcast it to the autocast dtype).
  5. Whenever target_dtype != torch.float32, we will then get a dtype mismatch error when running the Flex Attention backward pass, because the attention scores will be in FP32, but the incoming gradients will be in target_dtype.

So casting query and key to target_dtype after the QK norm solves the underlying issue by putting query/key/value, and therefore the Flex Attention computation, in the effective compute dtype, which works for both inference and training.

Comment on lines +138 to +139
pytest.mark.skipif(torch_device == "cpu", "FlexAttention has no CPU backward kernel.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pytest.mark.skipif(torch_device == "cpu", "FlexAttention has no CPU backward kernel.")

I think we can remove this because Flex Attention does have a CPU backward path, and the below test (test_training) actually passes after the fixes.

Comment on lines +143 to +144
pytest.mark.skipif(torch_device == "cpu", "FlexAttention has no CPU backward kernel.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pytest.mark.skipif(torch_device == "cpu", "FlexAttention has no CPU backward kernel.")

Same reason as #13855 (comment).

Comment on lines +148 to +149
pytest.mark.skipif(torch_device == "cpu", "FlexAttention has no CPU backward kernel.")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pytest.mark.skipif(torch_device == "cpu", "FlexAttention has no CPU backward kernel.")

Same reason as #13855 (comment).

@dg845 dg845 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Left some comments.

@sayakpaul

Copy link
Copy Markdown
Member Author

Can you please open a PR to this branch? I think it's just easier that way.

@dg845

dg845 commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Opened a PR to this branch with the suggested fix: #13863.

dg845 and others added 2 commits June 4, 2026 13:53
Fix AnyFlow FAR causal transformer training layerwise / mixed precision tests

@dg845 dg845 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sayakpaul sayakpaul merged commit c33abfc into main Jun 4, 2026
14 of 16 checks passed
@sayakpaul sayakpaul deleted the fix-anyflow-tests branch June 4, 2026 09:30
DN6 pushed a commit that referenced this pull request Jul 1, 2026
* fix anyflow tests

* [tests] fix anyflow tests layerwise casting (#13863)

Fix AnyFlow FAR causal transformer training layerwise / mixed precision tests

---------

Co-authored-by: dg845 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models size/S PR with diff < 50 LOC tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants