Skip to content

Commit

Permalink
Make UNet2DConditionOutput pickle-able (#3857)
Browse files Browse the repository at this point in the history
* add default to unet output to prevent it from being a required arg

* add unit test

* make style

* adjust unit test

* mark as fast test

* adjust assert statement in test

---------

Co-authored-by: Prathik Rao <prathikrao@microsoft.com@orttrainingdev8.d32nl1ml4oruzj4qz3bqlggovf.px.internal.cloudapp.net>
Co-authored-by: root <root@orttrainingdev8.d32nl1ml4oruzj4qz3bqlggovf.px.internal.cloudapp.net>
  • Loading branch information
3 people committed Jul 6, 2023
1 parent 41ea88f commit de14261
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 @@ -57,7 +57,7 @@ class UNet2DConditionOutput(BaseOutput):
The hidden states output 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

0 comments on commit de14261

Please sign in to comment.