2022-06-12 11:07:56 -06:00
|
|
|
# coding=utf-8
|
|
|
|
# Copyright 2022 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.
|
2022-06-12 11:59:39 -06:00
|
|
|
import tempfile
|
2022-06-12 15:20:39 -06:00
|
|
|
import unittest
|
2022-09-05 06:49:26 -06:00
|
|
|
from typing import Dict, List, Tuple
|
2022-06-12 11:07:56 -06:00
|
|
|
|
2022-06-12 15:20:39 -06:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
|
|
|
|
2022-09-16 11:10:56 -06:00
|
|
|
from diffusers import DDIMScheduler, DDPMScheduler, LMSDiscreteScheduler, PNDMScheduler, ScoreSdeVeScheduler
|
2022-06-12 11:07:56 -06:00
|
|
|
|
|
|
|
|
|
|
|
torch.backends.cuda.matmul.allow_tf32 = False
|
|
|
|
|
|
|
|
|
|
|
|
class SchedulerCommonTest(unittest.TestCase):
|
2022-06-12 13:56:13 -06:00
|
|
|
scheduler_classes = ()
|
|
|
|
forward_default_kwargs = ()
|
2022-06-12 11:07:56 -06:00
|
|
|
|
|
|
|
@property
|
2022-06-17 07:51:33 -06:00
|
|
|
def dummy_sample(self):
|
2022-06-12 11:07:56 -06:00
|
|
|
batch_size = 4
|
|
|
|
num_channels = 3
|
|
|
|
height = 8
|
|
|
|
width = 8
|
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
sample = torch.rand((batch_size, num_channels, height, width))
|
2022-06-12 11:07:56 -06:00
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
return sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
@property
|
2022-06-17 07:51:33 -06:00
|
|
|
def dummy_sample_deter(self):
|
2022-06-12 13:56:13 -06:00
|
|
|
batch_size = 4
|
|
|
|
num_channels = 3
|
|
|
|
height = 8
|
|
|
|
width = 8
|
|
|
|
|
|
|
|
num_elems = batch_size * num_channels * height * width
|
2022-07-20 18:24:59 -06:00
|
|
|
sample = torch.arange(num_elems)
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = sample.reshape(num_channels, height, width, batch_size)
|
|
|
|
sample = sample / num_elems
|
2022-07-20 18:24:59 -06:00
|
|
|
sample = sample.permute(3, 0, 1, 2)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
return sample
|
2022-06-12 11:07:56 -06:00
|
|
|
|
|
|
|
def get_scheduler_config(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def dummy_model(self):
|
2022-06-17 07:51:33 -06:00
|
|
|
def model(sample, t, *args):
|
|
|
|
return sample * t / (t + 1)
|
2022-06-12 11:07:56 -06:00
|
|
|
|
|
|
|
return model
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
def check_over_configs(self, time_step=0, **config):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
for scheduler_class in self.scheduler_classes:
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
scheduler_config = self.get_scheduler_config(**config)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
new_scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step(residual, time_step, sample, **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step(residual, time_step, sample, **kwargs).prev_sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def check_over_forward(self, time_step=0, **forward_kwargs):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
kwargs.update(forward_kwargs)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
for scheduler_class in self.scheduler_classes:
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
new_scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
|
|
|
torch.manual_seed(0)
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step(residual, time_step, sample, **kwargs).prev_sample
|
2022-07-15 07:04:01 -06:00
|
|
|
torch.manual_seed(0)
|
2022-09-05 06:49:26 -06:00
|
|
|
new_output = new_scheduler.step(residual, time_step, sample, **kwargs).prev_sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-06-12 11:07:56 -06:00
|
|
|
def test_from_pretrained_save_pretrained(self):
|
2022-06-12 13:56:13 -06:00
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
for scheduler_class in self.scheduler_classes:
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
new_scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
torch.manual_seed(0)
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step(residual, 1, sample, **kwargs).prev_sample
|
2022-07-20 18:24:59 -06:00
|
|
|
torch.manual_seed(0)
|
2022-09-05 06:49:26 -06:00
|
|
|
new_output = new_scheduler.step(residual, 1, sample, **kwargs).prev_sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def test_step_shape(self):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-07-15 07:04:01 -06:00
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output_0 = scheduler.step(residual, 0, sample, **kwargs).prev_sample
|
|
|
|
output_1 = scheduler.step(residual, 1, sample, **kwargs).prev_sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
self.assertEqual(output_0.shape, sample.shape)
|
2022-06-12 13:56:13 -06:00
|
|
|
self.assertEqual(output_0.shape, output_1.shape)
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
def test_scheduler_outputs_equivalence(self):
|
|
|
|
def set_nan_tensor_to_zero(t):
|
|
|
|
t[t != t] = 0
|
|
|
|
return t
|
|
|
|
|
|
|
|
def recursive_check(tuple_object, dict_object):
|
|
|
|
if isinstance(tuple_object, (List, Tuple)):
|
|
|
|
for tuple_iterable_value, dict_iterable_value in zip(tuple_object, dict_object.values()):
|
|
|
|
recursive_check(tuple_iterable_value, dict_iterable_value)
|
|
|
|
elif isinstance(tuple_object, Dict):
|
|
|
|
for tuple_iterable_value, dict_iterable_value in zip(tuple_object.values(), dict_object.values()):
|
|
|
|
recursive_check(tuple_iterable_value, dict_iterable_value)
|
|
|
|
elif tuple_object is None:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self.assertTrue(
|
|
|
|
torch.allclose(
|
|
|
|
set_nan_tensor_to_zero(tuple_object), set_nan_tensor_to_zero(dict_object), atol=1e-5
|
|
|
|
),
|
|
|
|
msg=(
|
|
|
|
"Tuple and dict output are not equal. Difference:"
|
|
|
|
f" {torch.max(torch.abs(tuple_object - dict_object))}. Tuple has `nan`:"
|
|
|
|
f" {torch.isnan(tuple_object).any()} and `inf`: {torch.isinf(tuple_object)}. Dict has"
|
|
|
|
f" `nan`: {torch.isnan(dict_object).any()} and `inf`: {torch.isinf(dict_object)}."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
|
|
|
|
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
|
|
|
outputs_dict = scheduler.step(residual, 0, sample, **kwargs)
|
|
|
|
|
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
|
|
|
outputs_tuple = scheduler.step(residual, 0, sample, return_dict=False, **kwargs)
|
|
|
|
|
|
|
|
recursive_check(outputs_tuple, outputs_dict)
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
class DDPMSchedulerTest(SchedulerCommonTest):
|
2022-06-13 02:39:53 -06:00
|
|
|
scheduler_classes = (DDPMScheduler,)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def get_scheduler_config(self, **kwargs):
|
|
|
|
config = {
|
2022-07-17 19:08:08 -06:00
|
|
|
"num_train_timesteps": 1000,
|
2022-06-12 13:56:13 -06:00
|
|
|
"beta_start": 0.0001,
|
|
|
|
"beta_end": 0.02,
|
|
|
|
"beta_schedule": "linear",
|
|
|
|
"variance_type": "fixed_small",
|
2022-06-17 04:11:40 -06:00
|
|
|
"clip_sample": True,
|
2022-06-12 13:56:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
config.update(**kwargs)
|
|
|
|
return config
|
2022-06-12 11:12:01 -06:00
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
def test_timesteps(self):
|
|
|
|
for timesteps in [1, 5, 100, 1000]:
|
2022-07-17 19:08:08 -06:00
|
|
|
self.check_over_configs(num_train_timesteps=timesteps)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def test_betas(self):
|
|
|
|
for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1], [0.002, 0.02, 0.2, 2]):
|
|
|
|
self.check_over_configs(beta_start=beta_start, beta_end=beta_end)
|
|
|
|
|
|
|
|
def test_schedules(self):
|
|
|
|
for schedule in ["linear", "squaredcos_cap_v2"]:
|
|
|
|
self.check_over_configs(beta_schedule=schedule)
|
|
|
|
|
|
|
|
def test_variance_type(self):
|
|
|
|
for variance in ["fixed_small", "fixed_large", "other"]:
|
|
|
|
self.check_over_configs(variance_type=variance)
|
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
def test_clip_sample(self):
|
2022-06-17 04:11:40 -06:00
|
|
|
for clip_sample in [True, False]:
|
|
|
|
self.check_over_configs(clip_sample=clip_sample)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def test_time_indices(self):
|
|
|
|
for t in [0, 500, 999]:
|
|
|
|
self.check_over_forward(time_step=t)
|
|
|
|
|
|
|
|
def test_variance(self):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(0) - 0.0)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(487) - 0.00979)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(999) - 0.02)) < 1e-5
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
def test_full_loop_no_noise(self):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
2022-06-12 11:07:56 -06:00
|
|
|
scheduler_config = self.get_scheduler_config()
|
2022-06-12 13:56:13 -06:00
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
num_trained_timesteps = len(scheduler)
|
|
|
|
|
|
|
|
model = self.dummy_model()
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample_deter
|
2022-09-12 07:49:39 -06:00
|
|
|
generator = torch.manual_seed(0)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
for t in reversed(range(num_trained_timesteps)):
|
|
|
|
# 1. predict noise residual
|
2022-06-17 07:51:33 -06:00
|
|
|
residual = model(sample, t)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
# 2. predict previous mean of sample x_t-1
|
2022-09-12 07:49:39 -06:00
|
|
|
pred_prev_sample = scheduler.step(residual, t, sample, generator=generator).prev_sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
# if t > 0:
|
|
|
|
# noise = self.dummy_sample_deter
|
|
|
|
# variance = scheduler.get_variance(t) ** (0.5) * noise
|
|
|
|
#
|
|
|
|
# sample = pred_prev_sample + variance
|
|
|
|
sample = pred_prev_sample
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
assert abs(result_sum.item() - 258.9070) < 1e-2
|
2022-07-20 18:24:59 -06:00
|
|
|
assert abs(result_mean.item() - 0.3374) < 1e-3
|
2022-06-12 11:07:56 -06:00
|
|
|
|
2022-06-12 11:12:01 -06:00
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
class DDIMSchedulerTest(SchedulerCommonTest):
|
|
|
|
scheduler_classes = (DDIMScheduler,)
|
2022-07-15 07:04:01 -06:00
|
|
|
forward_default_kwargs = (("eta", 0.0), ("num_inference_steps", 50))
|
2022-06-12 11:12:01 -06:00
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
def get_scheduler_config(self, **kwargs):
|
|
|
|
config = {
|
2022-07-17 19:08:08 -06:00
|
|
|
"num_train_timesteps": 1000,
|
2022-06-12 13:56:13 -06:00
|
|
|
"beta_start": 0.0001,
|
|
|
|
"beta_end": 0.02,
|
|
|
|
"beta_schedule": "linear",
|
2022-06-17 04:11:40 -06:00
|
|
|
"clip_sample": True,
|
2022-06-12 13:56:13 -06:00
|
|
|
}
|
2022-06-12 11:59:39 -06:00
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
config.update(**kwargs)
|
|
|
|
return config
|
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def full_loop(self, **config):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config(**config)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
num_inference_steps, eta = 10, 0.0
|
|
|
|
|
|
|
|
model = self.dummy_model()
|
|
|
|
sample = self.dummy_sample_deter
|
|
|
|
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
|
|
|
|
for t in scheduler.timesteps:
|
|
|
|
residual = model(sample, t)
|
|
|
|
sample = scheduler.step(residual, t, sample, eta).prev_sample
|
|
|
|
|
|
|
|
return sample
|
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
def test_timesteps(self):
|
2022-07-15 07:04:01 -06:00
|
|
|
for timesteps in [100, 500, 1000]:
|
2022-07-17 19:08:08 -06:00
|
|
|
self.check_over_configs(num_train_timesteps=timesteps)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def test_steps_offset(self):
|
|
|
|
for steps_offset in [0, 1]:
|
|
|
|
self.check_over_configs(steps_offset=steps_offset)
|
|
|
|
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config(steps_offset=1)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
scheduler.set_timesteps(5)
|
2022-09-27 07:27:34 -06:00
|
|
|
assert np.equal(scheduler.timesteps, np.array([801, 601, 401, 201, 1])).all()
|
2022-09-17 06:07:43 -06:00
|
|
|
|
2022-06-12 13:56:13 -06:00
|
|
|
def test_betas(self):
|
|
|
|
for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1], [0.002, 0.02, 0.2, 2]):
|
|
|
|
self.check_over_configs(beta_start=beta_start, beta_end=beta_end)
|
|
|
|
|
|
|
|
def test_schedules(self):
|
|
|
|
for schedule in ["linear", "squaredcos_cap_v2"]:
|
|
|
|
self.check_over_configs(beta_schedule=schedule)
|
|
|
|
|
2022-06-17 07:51:33 -06:00
|
|
|
def test_clip_sample(self):
|
2022-06-17 04:11:40 -06:00
|
|
|
for clip_sample in [True, False]:
|
|
|
|
self.check_over_configs(clip_sample=clip_sample)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def test_time_indices(self):
|
|
|
|
for t in [1, 10, 49]:
|
|
|
|
self.check_over_forward(time_step=t)
|
|
|
|
|
|
|
|
def test_inference_steps(self):
|
|
|
|
for t, num_inference_steps in zip([1, 10, 50], [10, 50, 500]):
|
2022-09-13 09:48:33 -06:00
|
|
|
self.check_over_forward(time_step=t, num_inference_steps=num_inference_steps)
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def test_eta(self):
|
|
|
|
for t, eta in zip([1, 10, 49], [0.0, 0.5, 1.0]):
|
|
|
|
self.check_over_forward(time_step=t, eta=eta)
|
|
|
|
|
|
|
|
def test_variance(self):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
2022-06-12 11:59:39 -06:00
|
|
|
scheduler_config = self.get_scheduler_config()
|
2022-06-12 13:56:13 -06:00
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(0, 0) - 0.0)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(420, 400) - 0.14771)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(980, 960) - 0.32460)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(0, 0) - 0.0)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(487, 486) - 0.00979)) < 1e-5
|
|
|
|
assert torch.sum(torch.abs(scheduler._get_variance(999, 998) - 0.02)) < 1e-5
|
2022-06-12 13:56:13 -06:00
|
|
|
|
|
|
|
def test_full_loop_no_noise(self):
|
2022-09-17 06:07:43 -06:00
|
|
|
sample = self.full_loop()
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
assert abs(result_sum.item() - 172.0067) < 1e-2
|
|
|
|
assert abs(result_mean.item() - 0.223967) < 1e-3
|
2022-06-12 13:56:13 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def test_full_loop_with_set_alpha_to_one(self):
|
|
|
|
# We specify different beta, so that the first alpha is 0.99
|
|
|
|
sample = self.full_loop(set_alpha_to_one=True, beta_start=0.01)
|
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-12 11:59:39 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
assert abs(result_sum.item() - 149.8295) < 1e-2
|
|
|
|
assert abs(result_mean.item() - 0.1951) < 1e-3
|
2022-06-12 11:59:39 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def test_full_loop_with_no_set_alpha_to_one(self):
|
|
|
|
# We specify different beta, so that the first alpha is 0.99
|
|
|
|
sample = self.full_loop(set_alpha_to_one=False, beta_start=0.01)
|
2022-07-20 18:24:59 -06:00
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-12 11:59:39 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
assert abs(result_sum.item() - 149.0784) < 1e-2
|
|
|
|
assert abs(result_mean.item() - 0.1941) < 1e-3
|
2022-06-17 07:51:03 -06:00
|
|
|
|
|
|
|
|
|
|
|
class PNDMSchedulerTest(SchedulerCommonTest):
|
|
|
|
scheduler_classes = (PNDMScheduler,)
|
|
|
|
forward_default_kwargs = (("num_inference_steps", 50),)
|
|
|
|
|
|
|
|
def get_scheduler_config(self, **kwargs):
|
|
|
|
config = {
|
2022-07-17 19:08:08 -06:00
|
|
|
"num_train_timesteps": 1000,
|
2022-06-17 07:51:03 -06:00
|
|
|
"beta_start": 0.0001,
|
|
|
|
"beta_end": 0.02,
|
|
|
|
"beta_schedule": "linear",
|
|
|
|
}
|
|
|
|
|
|
|
|
config.update(**kwargs)
|
|
|
|
return config
|
|
|
|
|
2022-07-20 13:47:39 -06:00
|
|
|
def check_over_configs(self, time_step=0, **config):
|
2022-06-17 07:51:03 -06:00
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
2022-07-20 19:50:12 -06:00
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
2022-06-17 07:51:03 -06:00
|
|
|
dummy_past_residuals = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
scheduler_config = self.get_scheduler_config(**config)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
2022-07-20 19:50:12 -06:00
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
2022-06-17 07:51:03 -06:00
|
|
|
# copy over dummy past residuals
|
|
|
|
scheduler.ets = dummy_past_residuals[:]
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
2022-07-20 19:50:12 -06:00
|
|
|
new_scheduler.set_timesteps(num_inference_steps)
|
2022-06-17 07:51:03 -06:00
|
|
|
# copy over dummy past residuals
|
|
|
|
new_scheduler.ets = dummy_past_residuals[:]
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step_prk(residual, time_step, sample, **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step_prk(residual, time_step, sample, **kwargs).prev_sample
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step_plms(residual, time_step, sample, **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step_plms(residual, time_step, sample, **kwargs).prev_sample
|
2022-07-20 13:47:39 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-07-20 13:47:39 -06:00
|
|
|
|
|
|
|
def test_from_pretrained_save_pretrained(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def check_over_forward(self, time_step=0, **forward_kwargs):
|
2022-06-17 07:51:03 -06:00
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
2022-07-20 19:50:12 -06:00
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
2022-06-17 07:51:33 -06:00
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
2022-06-17 07:51:03 -06:00
|
|
|
dummy_past_residuals = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
2022-07-20 19:50:12 -06:00
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
2022-07-20 13:47:39 -06:00
|
|
|
|
2022-08-21 23:23:59 -06:00
|
|
|
# copy over dummy past residuals (must be after setting timesteps)
|
2022-06-17 07:51:03 -06:00
|
|
|
scheduler.ets = dummy_past_residuals[:]
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
|
|
|
# copy over dummy past residuals
|
2022-07-20 19:50:12 -06:00
|
|
|
new_scheduler.set_timesteps(num_inference_steps)
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-08-21 23:23:59 -06:00
|
|
|
# copy over dummy past residual (must be after setting timesteps)
|
|
|
|
new_scheduler.ets = dummy_past_residuals[:]
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step_prk(residual, time_step, sample, **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step_prk(residual, time_step, sample, **kwargs).prev_sample
|
2022-07-20 13:47:39 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-07-20 13:47:39 -06:00
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output = scheduler.step_plms(residual, time_step, sample, **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step_plms(residual, time_step, sample, **kwargs).prev_sample
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-07-20 18:24:59 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def full_loop(self, **config):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config(**config)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
num_inference_steps = 10
|
|
|
|
model = self.dummy_model()
|
|
|
|
sample = self.dummy_sample_deter
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
|
|
|
|
for i, t in enumerate(scheduler.prk_timesteps):
|
|
|
|
residual = model(sample, t)
|
|
|
|
sample = scheduler.step_prk(residual, t, sample).prev_sample
|
|
|
|
|
|
|
|
for i, t in enumerate(scheduler.plms_timesteps):
|
|
|
|
residual = model(sample, t)
|
|
|
|
sample = scheduler.step_plms(residual, t, sample).prev_sample
|
|
|
|
|
|
|
|
return sample
|
|
|
|
|
2022-07-20 13:47:39 -06:00
|
|
|
def test_step_shape(self):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
|
|
|
|
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
2022-08-21 23:23:59 -06:00
|
|
|
# copy over dummy past residuals (must be done after set_timesteps)
|
|
|
|
dummy_past_residuals = [residual + 0.2, residual + 0.15, residual + 0.1, residual + 0.05]
|
|
|
|
scheduler.ets = dummy_past_residuals[:]
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output_0 = scheduler.step_prk(residual, 0, sample, **kwargs).prev_sample
|
|
|
|
output_1 = scheduler.step_prk(residual, 1, sample, **kwargs).prev_sample
|
2022-07-20 13:47:39 -06:00
|
|
|
|
|
|
|
self.assertEqual(output_0.shape, sample.shape)
|
|
|
|
self.assertEqual(output_0.shape, output_1.shape)
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
output_0 = scheduler.step_plms(residual, 0, sample, **kwargs).prev_sample
|
|
|
|
output_1 = scheduler.step_plms(residual, 1, sample, **kwargs).prev_sample
|
2022-07-20 13:47:39 -06:00
|
|
|
|
|
|
|
self.assertEqual(output_0.shape, sample.shape)
|
|
|
|
self.assertEqual(output_0.shape, output_1.shape)
|
|
|
|
|
2022-06-17 07:51:03 -06:00
|
|
|
def test_timesteps(self):
|
|
|
|
for timesteps in [100, 1000]:
|
2022-07-17 19:08:08 -06:00
|
|
|
self.check_over_configs(num_train_timesteps=timesteps)
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def test_steps_offset(self):
|
|
|
|
for steps_offset in [0, 1]:
|
|
|
|
self.check_over_configs(steps_offset=steps_offset)
|
|
|
|
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config(steps_offset=1)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
scheduler.set_timesteps(10)
|
2022-09-27 07:27:34 -06:00
|
|
|
assert np.equal(
|
2022-09-17 06:07:43 -06:00
|
|
|
scheduler.timesteps,
|
2022-09-27 07:27:34 -06:00
|
|
|
np.array([901, 851, 851, 801, 801, 751, 751, 701, 701, 651, 651, 601, 601, 501, 401, 301, 201, 101, 1]),
|
|
|
|
).all()
|
2022-09-17 06:07:43 -06:00
|
|
|
|
2022-06-17 07:51:03 -06:00
|
|
|
def test_betas(self):
|
2022-09-17 06:07:43 -06:00
|
|
|
for beta_start, beta_end in zip([0.0001, 0.001], [0.002, 0.02]):
|
2022-06-17 07:51:03 -06:00
|
|
|
self.check_over_configs(beta_start=beta_start, beta_end=beta_end)
|
|
|
|
|
|
|
|
def test_schedules(self):
|
|
|
|
for schedule in ["linear", "squaredcos_cap_v2"]:
|
|
|
|
self.check_over_configs(beta_schedule=schedule)
|
|
|
|
|
|
|
|
def test_time_indices(self):
|
|
|
|
for t in [1, 5, 10]:
|
|
|
|
self.check_over_forward(time_step=t)
|
|
|
|
|
|
|
|
def test_inference_steps(self):
|
|
|
|
for t, num_inference_steps in zip([1, 5, 10], [10, 50, 100]):
|
2022-09-17 06:07:43 -06:00
|
|
|
self.check_over_forward(num_inference_steps=num_inference_steps)
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-13 09:48:33 -06:00
|
|
|
def test_pow_of_3_inference_steps(self):
|
|
|
|
# earlier version of set_timesteps() caused an error indexing alpha's with inference steps as power of 3
|
|
|
|
num_inference_steps = 27
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
|
|
|
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
|
|
|
|
# before power of 3 fix, would error on first step, so we only need to do two
|
|
|
|
for i, t in enumerate(scheduler.prk_timesteps[:2]):
|
|
|
|
sample = scheduler.step_prk(residual, t, sample).prev_sample
|
|
|
|
|
2022-07-20 13:47:39 -06:00
|
|
|
def test_inference_plms_no_past_residuals(self):
|
2022-06-17 07:51:03 -06:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
2022-09-05 06:49:26 -06:00
|
|
|
scheduler.step_plms(self.dummy_sample, 1, self.dummy_sample).prev_sample
|
2022-06-17 07:51:03 -06:00
|
|
|
|
|
|
|
def test_full_loop_no_noise(self):
|
2022-09-17 06:07:43 -06:00
|
|
|
sample = self.full_loop()
|
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
assert abs(result_sum.item() - 198.1318) < 1e-2
|
|
|
|
assert abs(result_mean.item() - 0.2580) < 1e-3
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def test_full_loop_with_set_alpha_to_one(self):
|
|
|
|
# We specify different beta, so that the first alpha is 0.99
|
|
|
|
sample = self.full_loop(set_alpha_to_one=True, beta_start=0.01)
|
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
assert abs(result_sum.item() - 230.0399) < 1e-2
|
|
|
|
assert abs(result_mean.item() - 0.2995) < 1e-3
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
def test_full_loop_with_no_set_alpha_to_one(self):
|
|
|
|
# We specify different beta, so that the first alpha is 0.99
|
|
|
|
sample = self.full_loop(set_alpha_to_one=False, beta_start=0.01)
|
2022-07-20 18:24:59 -06:00
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-06-17 07:51:03 -06:00
|
|
|
|
2022-09-17 06:07:43 -06:00
|
|
|
assert abs(result_sum.item() - 186.9482) < 1e-2
|
|
|
|
assert abs(result_mean.item() - 0.2434) < 1e-3
|
2022-07-17 19:08:08 -06:00
|
|
|
|
|
|
|
|
2022-07-19 04:12:45 -06:00
|
|
|
class ScoreSdeVeSchedulerTest(unittest.TestCase):
|
|
|
|
# TODO adapt with class SchedulerCommonTest (scheduler needs Numpy Integration)
|
2022-07-17 19:08:08 -06:00
|
|
|
scheduler_classes = (ScoreSdeVeScheduler,)
|
2022-09-12 07:49:39 -06:00
|
|
|
forward_default_kwargs = ()
|
2022-07-19 04:12:45 -06:00
|
|
|
|
|
|
|
@property
|
|
|
|
def dummy_sample(self):
|
|
|
|
batch_size = 4
|
|
|
|
num_channels = 3
|
|
|
|
height = 8
|
|
|
|
width = 8
|
|
|
|
|
|
|
|
sample = torch.rand((batch_size, num_channels, height, width))
|
|
|
|
|
|
|
|
return sample
|
|
|
|
|
|
|
|
@property
|
|
|
|
def dummy_sample_deter(self):
|
|
|
|
batch_size = 4
|
|
|
|
num_channels = 3
|
|
|
|
height = 8
|
|
|
|
width = 8
|
|
|
|
|
|
|
|
num_elems = batch_size * num_channels * height * width
|
|
|
|
sample = torch.arange(num_elems)
|
|
|
|
sample = sample.reshape(num_channels, height, width, batch_size)
|
|
|
|
sample = sample / num_elems
|
|
|
|
sample = sample.permute(3, 0, 1, 2)
|
|
|
|
|
|
|
|
return sample
|
|
|
|
|
|
|
|
def dummy_model(self):
|
|
|
|
def model(sample, t, *args):
|
|
|
|
return sample * t / (t + 1)
|
|
|
|
|
|
|
|
return model
|
2022-07-17 19:08:08 -06:00
|
|
|
|
|
|
|
def get_scheduler_config(self, **kwargs):
|
|
|
|
config = {
|
|
|
|
"num_train_timesteps": 2000,
|
|
|
|
"snr": 0.15,
|
|
|
|
"sigma_min": 0.01,
|
|
|
|
"sigma_max": 1348,
|
|
|
|
"sampling_eps": 1e-5,
|
|
|
|
}
|
|
|
|
|
|
|
|
config.update(**kwargs)
|
|
|
|
return config
|
|
|
|
|
|
|
|
def check_over_configs(self, time_step=0, **config):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
|
|
|
|
|
|
|
scheduler_config = self.get_scheduler_config(**config)
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
output = scheduler.step_pred(
|
|
|
|
residual, time_step, sample, generator=torch.manual_seed(0), **kwargs
|
|
|
|
).prev_sample
|
|
|
|
new_output = new_scheduler.step_pred(
|
|
|
|
residual, time_step, sample, generator=torch.manual_seed(0), **kwargs
|
|
|
|
).prev_sample
|
2022-07-17 19:08:08 -06:00
|
|
|
|
2022-07-19 04:12:45 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-07-17 19:08:08 -06:00
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
output = scheduler.step_correct(residual, sample, generator=torch.manual_seed(0), **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step_correct(
|
|
|
|
residual, sample, generator=torch.manual_seed(0), **kwargs
|
|
|
|
).prev_sample
|
2022-07-17 19:08:08 -06:00
|
|
|
|
2022-07-19 04:12:45 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler correction are not identical"
|
2022-07-17 19:08:08 -06:00
|
|
|
|
|
|
|
def check_over_forward(self, time_step=0, **forward_kwargs):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
kwargs.update(forward_kwargs)
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
|
|
|
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
scheduler.save_config(tmpdirname)
|
|
|
|
new_scheduler = scheduler_class.from_config(tmpdirname)
|
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
output = scheduler.step_pred(
|
|
|
|
residual, time_step, sample, generator=torch.manual_seed(0), **kwargs
|
|
|
|
).prev_sample
|
|
|
|
new_output = new_scheduler.step_pred(
|
|
|
|
residual, time_step, sample, generator=torch.manual_seed(0), **kwargs
|
|
|
|
).prev_sample
|
2022-07-17 19:08:08 -06:00
|
|
|
|
2022-07-19 04:12:45 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler outputs are not identical"
|
2022-07-17 19:08:08 -06:00
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
output = scheduler.step_correct(residual, sample, generator=torch.manual_seed(0), **kwargs).prev_sample
|
|
|
|
new_output = new_scheduler.step_correct(
|
|
|
|
residual, sample, generator=torch.manual_seed(0), **kwargs
|
|
|
|
).prev_sample
|
2022-07-17 19:08:08 -06:00
|
|
|
|
2022-07-19 04:12:45 -06:00
|
|
|
assert torch.sum(torch.abs(output - new_output)) < 1e-5, "Scheduler correction are not identical"
|
2022-07-17 19:08:08 -06:00
|
|
|
|
|
|
|
def test_timesteps(self):
|
|
|
|
for timesteps in [10, 100, 1000]:
|
|
|
|
self.check_over_configs(num_train_timesteps=timesteps)
|
|
|
|
|
|
|
|
def test_sigmas(self):
|
|
|
|
for sigma_min, sigma_max in zip([0.0001, 0.001, 0.01], [1, 100, 1000]):
|
|
|
|
self.check_over_configs(sigma_min=sigma_min, sigma_max=sigma_max)
|
|
|
|
|
|
|
|
def test_time_indices(self):
|
2022-07-19 04:12:45 -06:00
|
|
|
for t in [0.1, 0.5, 0.75]:
|
2022-07-17 19:08:08 -06:00
|
|
|
self.check_over_forward(time_step=t)
|
|
|
|
|
|
|
|
def test_full_loop_no_noise(self):
|
2022-07-19 04:12:45 -06:00
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
2022-07-17 19:08:08 -06:00
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
num_inference_steps = 3
|
|
|
|
|
|
|
|
model = self.dummy_model()
|
|
|
|
sample = self.dummy_sample_deter
|
|
|
|
|
|
|
|
scheduler.set_sigmas(num_inference_steps)
|
2022-07-19 04:12:45 -06:00
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
2022-09-12 07:49:39 -06:00
|
|
|
generator = torch.manual_seed(0)
|
2022-07-17 19:08:08 -06:00
|
|
|
|
|
|
|
for i, t in enumerate(scheduler.timesteps):
|
|
|
|
sigma_t = scheduler.sigmas[i]
|
|
|
|
|
2022-09-15 07:55:48 -06:00
|
|
|
for _ in range(scheduler.config.correct_steps):
|
2022-07-17 19:08:08 -06:00
|
|
|
with torch.no_grad():
|
2022-07-19 04:12:45 -06:00
|
|
|
model_output = model(sample, sigma_t)
|
2022-09-12 07:49:39 -06:00
|
|
|
sample = scheduler.step_correct(model_output, sample, generator=generator, **kwargs).prev_sample
|
2022-07-17 19:08:08 -06:00
|
|
|
|
|
|
|
with torch.no_grad():
|
2022-07-19 04:12:45 -06:00
|
|
|
model_output = model(sample, sigma_t)
|
2022-07-17 19:29:40 -06:00
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
output = scheduler.step_pred(model_output, t, sample, generator=generator, **kwargs)
|
2022-09-05 06:49:26 -06:00
|
|
|
sample, _ = output.prev_sample, output.prev_sample_mean
|
2022-07-17 19:29:40 -06:00
|
|
|
|
2022-07-19 04:12:45 -06:00
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
2022-07-17 19:29:40 -06:00
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
assert np.isclose(result_sum.item(), 14372758528.0)
|
|
|
|
assert np.isclose(result_mean.item(), 18714530.0)
|
2022-07-17 19:29:40 -06:00
|
|
|
|
|
|
|
def test_step_shape(self):
|
|
|
|
kwargs = dict(self.forward_default_kwargs)
|
|
|
|
|
|
|
|
num_inference_steps = kwargs.pop("num_inference_steps", None)
|
|
|
|
|
|
|
|
for scheduler_class in self.scheduler_classes:
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
sample = self.dummy_sample
|
|
|
|
residual = 0.1 * sample
|
|
|
|
|
|
|
|
if num_inference_steps is not None and hasattr(scheduler, "set_timesteps"):
|
|
|
|
scheduler.set_timesteps(num_inference_steps)
|
|
|
|
elif num_inference_steps is not None and not hasattr(scheduler, "set_timesteps"):
|
|
|
|
kwargs["num_inference_steps"] = num_inference_steps
|
|
|
|
|
2022-09-12 07:49:39 -06:00
|
|
|
output_0 = scheduler.step_pred(residual, 0, sample, generator=torch.manual_seed(0), **kwargs).prev_sample
|
|
|
|
output_1 = scheduler.step_pred(residual, 1, sample, generator=torch.manual_seed(0), **kwargs).prev_sample
|
2022-07-17 19:29:40 -06:00
|
|
|
|
|
|
|
self.assertEqual(output_0.shape, sample.shape)
|
|
|
|
self.assertEqual(output_0.shape, output_1.shape)
|
2022-09-16 11:10:56 -06:00
|
|
|
|
|
|
|
|
|
|
|
class LMSDiscreteSchedulerTest(SchedulerCommonTest):
|
|
|
|
scheduler_classes = (LMSDiscreteScheduler,)
|
|
|
|
num_inference_steps = 10
|
|
|
|
|
|
|
|
def get_scheduler_config(self, **kwargs):
|
|
|
|
config = {
|
|
|
|
"num_train_timesteps": 1100,
|
|
|
|
"beta_start": 0.0001,
|
|
|
|
"beta_end": 0.02,
|
|
|
|
"beta_schedule": "linear",
|
|
|
|
"trained_betas": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
config.update(**kwargs)
|
|
|
|
return config
|
|
|
|
|
|
|
|
def test_timesteps(self):
|
|
|
|
for timesteps in [10, 50, 100, 1000]:
|
|
|
|
self.check_over_configs(num_train_timesteps=timesteps)
|
|
|
|
|
|
|
|
def test_betas(self):
|
|
|
|
for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1], [0.002, 0.02, 0.2, 2]):
|
|
|
|
self.check_over_configs(beta_start=beta_start, beta_end=beta_end)
|
|
|
|
|
|
|
|
def test_schedules(self):
|
|
|
|
for schedule in ["linear", "scaled_linear"]:
|
|
|
|
self.check_over_configs(beta_schedule=schedule)
|
|
|
|
|
|
|
|
def test_time_indices(self):
|
|
|
|
for t in [0, 500, 800]:
|
|
|
|
self.check_over_forward(time_step=t)
|
|
|
|
|
|
|
|
def test_full_loop_no_noise(self):
|
|
|
|
scheduler_class = self.scheduler_classes[0]
|
|
|
|
scheduler_config = self.get_scheduler_config()
|
|
|
|
scheduler = scheduler_class(**scheduler_config)
|
|
|
|
|
|
|
|
scheduler.set_timesteps(self.num_inference_steps)
|
|
|
|
|
|
|
|
model = self.dummy_model()
|
|
|
|
sample = self.dummy_sample_deter * scheduler.sigmas[0]
|
|
|
|
|
|
|
|
for i, t in enumerate(scheduler.timesteps):
|
|
|
|
sample = sample / ((scheduler.sigmas[i] ** 2 + 1) ** 0.5)
|
|
|
|
|
|
|
|
model_output = model(sample, t)
|
|
|
|
|
|
|
|
output = scheduler.step(model_output, i, sample)
|
|
|
|
sample = output.prev_sample
|
|
|
|
|
|
|
|
result_sum = torch.sum(torch.abs(sample))
|
|
|
|
result_mean = torch.mean(torch.abs(sample))
|
|
|
|
|
2022-09-27 07:27:34 -06:00
|
|
|
assert abs(result_sum.item() - 1006.370) < 1e-2
|
2022-09-16 11:10:56 -06:00
|
|
|
assert abs(result_mean.item() - 1.31) < 1e-3
|