Skip to content

Fix UVit2DModel forward with gradient checkpointing enabled - #15442

Open
AxtonJi wants to merge 1 commit into
huggingface:mainfrom
AxtonJi:fix-uvit2d-gradient-checkpointing
Open

AxtonJi wants to merge 1 commit into
huggingface:mainfrom
AxtonJi:fix-uvit2d-gradient-checkpointing

Conversation

@AxtonJi

@AxtonJi AxtonJi commented Nov 30, 2026

Copy link
Copy Markdown

What does this PR do?

With gradient checkpointing enabled, UVit2DModel.forward wrapped each transformer layer in a local layer_(*args) closure but called it with keyword arguments. Every training step therefore failed with TypeError: layer_() got an unexpected keyword argument 'encoder_hidden_states', for example in examples/amused/train_amused.py --gradient_checkpointing.

This calls the layer through self._gradient_checkpointing_func, as Transformer2DModel does, and drops the unused torch.utils.checkpoint import. The arguments are passed positionally in BasicTransformerBlock.forward order (hidden_states, attention_mask, encoder_hidden_states, encoder_attention_mask, timestep, cross_attention_kwargs, class_labels, added_cond_kwargs). A custom function passed to enable_gradient_checkpointing() is now honoured too. The path without checkpointing is unchanged.

The PR adds tests/models/unets/test_models_uvit_2d.py, a tiny CPU test that checks loss and parameter gradients match with and without gradient checkpointing. On main it fails with the TypeError above. ruff check, ruff format --check and utils/check_copies.py pass. I didn't use TrainingTesterMixin, because it only runs with an accelerator and calls the model with return_dict=False, which UVit2DModel doesn't accept.

Self-review notes: I checked that the positional order matches BasicTransformerBlock.forward. The only blocks in transformer_layers are BasicTransformerBlocks. I didn't run the full train_amused.py script or make quality.

Fixes #11214

Before submitting

  • Did you use an AI agent? Yes (Claude Code). I reviewed the change and the text above.
  • Did you read the contributor guideline?
  • Did you write any new necessary tests?

Who can review?

@sayakpaul @yiyixuxu

🤖 Generated with Claude Code

With gradient checkpointing enabled, UVit2DModel.forward wrapped each
transformer layer in a local `layer_(*args)` closure but then called it
with keyword arguments (`encoder_hidden_states`, `cross_attention_kwargs`,
`added_cond_kwargs`), so any training step raised
`TypeError: layer_() got an unexpected keyword argument
'encoder_hidden_states'` (for example `examples/amused/train_amused.py`
with `--gradient_checkpointing`).

Call the layer through `self._gradient_checkpointing_func` with
positional arguments in BasicTransformerBlock.forward order, as
Transformer2DModel does. This also makes the model honour a custom
checkpointing function passed to `enable_gradient_checkpointing()`.

Add a CPU test that checks loss and gradients match with and without
gradient checkpointing.

Fixes huggingface#11214

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There should not be a test like this. We should follow what other modeling tests follow.

@sayakpaul sayakpaul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Left just one comment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: UVit2DModel.forward.<locals>.layer_() got an unexpected keyword argument 'encoder_hidden_states'

2 participants