Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/diffusers/models/unets/uvit_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import torch
import torch.nn.functional as F
from torch import nn
from torch.utils.checkpoint import checkpoint

from ...configuration_utils import ConfigMixin, register_to_config
from ...loaders import PeftAdapterMixin
Expand Down Expand Up @@ -196,21 +195,27 @@ def forward(
hidden_states = self.project_to_hidden_norm(hidden_states)
hidden_states = self.project_to_hidden(hidden_states)

added_cond_kwargs = {"pooled_text_emb": pooled_text_emb}
for layer in self.transformer_layers:
if torch.is_grad_enabled() and self.gradient_checkpointing:

def layer_(*args):
return checkpoint(layer, *args)

hidden_states = self._gradient_checkpointing_func(
layer,
hidden_states,
None,
encoder_hidden_states,
None,
None,
cross_attention_kwargs,
None,
added_cond_kwargs,
)
else:
layer_ = layer

hidden_states = layer_(
hidden_states,
encoder_hidden_states=encoder_hidden_states,
cross_attention_kwargs=cross_attention_kwargs,
added_cond_kwargs={"pooled_text_emb": pooled_text_emb},
)
hidden_states = layer(
hidden_states,
encoder_hidden_states=encoder_hidden_states,
cross_attention_kwargs=cross_attention_kwargs,
added_cond_kwargs=added_cond_kwargs,
)

hidden_states = self.project_from_hidden_norm(hidden_states)
hidden_states = self.project_from_hidden(hidden_states)
Expand Down
76 changes: 76 additions & 0 deletions tests/models/unets/test_models_uvit_2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# coding=utf-8
# Copyright 2026 HuggingFace Inc.
#
# 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 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.


from diffusers import UVit2DModel


def get_init_dict():
return {
"hidden_size": 32,
"use_bias": False,
"hidden_dropout": 0.0,
"cond_embed_dim": 32,
"micro_cond_encode_dim": 2,
"micro_cond_embed_dim": 10,
"encoder_hidden_size": 32,
"vocab_size": 32,
"codebook_size": 32,
"in_channels": 32,
"block_out_channels": 32,
"num_res_blocks": 1,
"downsample": True,
"upsample": True,
"block_num_heads": 1,
"num_hidden_layers": 1,
"num_attention_heads": 1,
"attention_dropout": 0.0,
"intermediate_size": 32,
"layer_norm_eps": 1e-06,
"ln_elementwise_affine": True,
}


def get_dummy_inputs():
generator = torch.Generator().manual_seed(0)
return {
"input_ids": torch.randint(0, 32, (2, 4, 4), generator=generator),
"encoder_hidden_states": torch.randn(2, 8, 32, generator=generator),
"pooled_text_emb": torch.randn(2, 32, generator=generator),
"micro_conds": torch.randn(2, 5, generator=generator),
}


def test_gradient_checkpointing_matches_regular_backward():
torch.manual_seed(0)
model = UVit2DModel(**get_init_dict()).train()
model_gc = UVit2DModel(**get_init_dict()).train()
model_gc.load_state_dict(model.state_dict())
model_gc.enable_gradient_checkpointing()

inputs = get_dummy_inputs()
loss = model(**inputs).float().pow(2).mean()
loss.backward()
loss_gc = model_gc(**inputs).float().pow(2).mean()
loss_gc.backward()

assert torch.allclose(loss, loss_gc, atol=1e-6)
grads_gc = dict(model_gc.named_parameters())
for name, param in model.named_parameters():
if param.grad is None:
assert grads_gc[name].grad is None, name
continue
assert torch.allclose(param.grad, grads_gc[name].grad, atol=1e-5), name