test_training.py 3.33 KB
Newer Older
1
# coding=utf-8
Patrick von Platen's avatar
Patrick von Platen committed
2
# Copyright 2023 HuggingFace Inc.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# 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 unittest

import torch

from diffusers import DDIMScheduler, DDPMScheduler, UNet2DModel
21
from diffusers.training_utils import set_seed
22
from diffusers.utils.testing_utils import slow
23
24
25
26
27
28
29
30
31
32
33
34
35
36


torch.backends.cuda.matmul.allow_tf32 = False


class TrainingTests(unittest.TestCase):
    def get_model_optimizer(self, resolution=32):
        set_seed(0)
        model = UNet2DModel(sample_size=resolution, in_channels=3, out_channels=3)
        optimizer = torch.optim.SGD(model.parameters(), lr=0.0001)
        return model, optimizer

    @slow
    def test_training_step_equality(self):
37
        device = "cpu"  # ensure full determinism without setting the CUBLAS_WORKSPACE_CONFIG env variable
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        ddpm_scheduler = DDPMScheduler(
            num_train_timesteps=1000,
            beta_start=0.0001,
            beta_end=0.02,
            beta_schedule="linear",
            clip_sample=True,
        )
        ddim_scheduler = DDIMScheduler(
            num_train_timesteps=1000,
            beta_start=0.0001,
            beta_end=0.02,
            beta_schedule="linear",
            clip_sample=True,
        )

53
        assert ddpm_scheduler.config.num_train_timesteps == ddim_scheduler.config.num_train_timesteps
54
55
56

        # shared batches for DDPM and DDIM
        set_seed(0)
57
58
59
        clean_images = [torch.randn((4, 3, 32, 32)).clip(-1, 1).to(device) for _ in range(4)]
        noise = [torch.randn((4, 3, 32, 32)).to(device) for _ in range(4)]
        timesteps = [torch.randint(0, 1000, (4,)).long().to(device) for _ in range(4)]
60
61
62

        # train with a DDPM scheduler
        model, optimizer = self.get_model_optimizer(resolution=32)
63
        model.train().to(device)
64
65
66
        for i in range(4):
            optimizer.zero_grad()
            ddpm_noisy_images = ddpm_scheduler.add_noise(clean_images[i], noise[i], timesteps[i])
67
            ddpm_noise_pred = model(ddpm_noisy_images, timesteps[i]).sample
68
69
70
71
72
73
74
            loss = torch.nn.functional.mse_loss(ddpm_noise_pred, noise[i])
            loss.backward()
            optimizer.step()
        del model, optimizer

        # recreate the model and optimizer, and retry with DDIM
        model, optimizer = self.get_model_optimizer(resolution=32)
75
        model.train().to(device)
76
77
78
        for i in range(4):
            optimizer.zero_grad()
            ddim_noisy_images = ddim_scheduler.add_noise(clean_images[i], noise[i], timesteps[i])
79
            ddim_noise_pred = model(ddim_noisy_images, timesteps[i]).sample
80
81
82
83
84
85
86
            loss = torch.nn.functional.mse_loss(ddim_noise_pred, noise[i])
            loss.backward()
            optimizer.step()
        del model, optimizer

        self.assertTrue(torch.allclose(ddpm_noisy_images, ddim_noisy_images, atol=1e-5))
        self.assertTrue(torch.allclose(ddpm_noise_pred, ddim_noise_pred, atol=1e-5))