Repository navigation
Conversation
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>
sayakpaul
reviewed
Dec 3, 2026
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch |
Member
There was a problem hiding this comment.
There should not be a test like this. We should follow what other modeling tests follow.
sayakpaul
reviewed
Dec 3, 2026
sayakpaul
left a comment
Member
There was a problem hiding this comment.
Thanks! Left just one comment.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
With gradient checkpointing enabled,
UVit2DModel.forwardwrapped each transformer layer in a locallayer_(*args)closure but called it with keyword arguments. Every training step therefore failed withTypeError: layer_() got an unexpected keyword argument 'encoder_hidden_states', for example inexamples/amused/train_amused.py --gradient_checkpointing.This calls the layer through
self._gradient_checkpointing_func, asTransformer2DModeldoes, and drops the unusedtorch.utils.checkpointimport. The arguments are passed positionally inBasicTransformerBlock.forwardorder (hidden_states, attention_mask, encoder_hidden_states, encoder_attention_mask, timestep, cross_attention_kwargs, class_labels, added_cond_kwargs). A custom function passed toenable_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. Onmainit fails with the TypeError above.ruff check,ruff format --checkandutils/check_copies.pypass. I didn't useTrainingTesterMixin, because it only runs with an accelerator and calls the model withreturn_dict=False, whichUVit2DModeldoesn't accept.Self-review notes: I checked that the positional order matches
BasicTransformerBlock.forward. The only blocks intransformer_layersareBasicTransformerBlocks. I didn't run the fulltrain_amused.pyscript ormake quality.Fixes #11214
Before submitting
Who can review?
@sayakpaul @yiyixuxu
🤖 Generated with Claude Code