Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UNet2DConditionOutput pickle-able #3857

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/diffusers/models/unet_2d_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class UNet2DConditionOutput(BaseOutput):
Hidden states conditioned on `encoder_hidden_states` input. Output of last layer of model.
"""

sample: torch.FloatTensor
sample: torch.FloatTensor = None


class UNet2DConditionModel(ModelMixin, ConfigMixin, UNet2DConditionLoadersMixin):
Expand Down
17 changes: 17 additions & 0 deletions tests/models/test_models_unet_2d_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import gc
import os
import tempfile
Expand Down Expand Up @@ -782,6 +783,22 @@ def test_custom_diffusion_xformers_on_off(self):
assert (sample - on_sample).abs().max() < 1e-4
assert (sample - off_sample).abs().max() < 1e-4

def test_pickle(self):
# enable deterministic behavior for gradient checkpointing
init_dict, inputs_dict = self.prepare_init_args_and_inputs_for_common()

init_dict["attention_head_dim"] = (8, 16)

model = self.model_class(**init_dict)
model.to(torch_device)

with torch.no_grad():
sample = model(**inputs_dict).sample

sample_copy = copy.copy(sample)

assert (sample - sample_copy).abs().max() < 1e-4


@slow
class UNet2DConditionModelIntegrationTests(unittest.TestCase):
Expand Down