- Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathtest_scheduler_ddim_inverse.py
138 lines (102 loc) · 4.83 KB
/
test_scheduler_ddim_inverse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
importunittest
importtorch
fromdiffusersimportDDIMInverseScheduler
from .test_schedulersimportSchedulerCommonTest
classDDIMInverseSchedulerTest(SchedulerCommonTest):
scheduler_classes= (DDIMInverseScheduler,)
forward_default_kwargs= (("num_inference_steps", 50),)
defget_scheduler_config(self, **kwargs):
config= {
"num_train_timesteps": 1000,
"beta_start": 0.0001,
"beta_end": 0.02,
"beta_schedule": "linear",
"clip_sample": True,
}
config.update(**kwargs)
returnconfig
deffull_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)
fortinscheduler.timesteps:
residual=model(sample, t)
sample=scheduler.step(residual, t, sample).prev_sample
returnsample
deftest_timesteps(self):
fortimestepsin [100, 500, 1000]:
self.check_over_configs(num_train_timesteps=timesteps)
deftest_steps_offset(self):
forsteps_offsetin [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)
asserttorch.equal(scheduler.timesteps, torch.LongTensor([1, 201, 401, 601, 801]))
deftest_betas(self):
forbeta_start, beta_endinzip([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)
deftest_schedules(self):
forschedulein ["linear", "squaredcos_cap_v2"]:
self.check_over_configs(beta_schedule=schedule)
deftest_prediction_type(self):
forprediction_typein ["epsilon", "v_prediction"]:
self.check_over_configs(prediction_type=prediction_type)
deftest_clip_sample(self):
forclip_samplein [True, False]:
self.check_over_configs(clip_sample=clip_sample)
deftest_timestep_spacing(self):
fortimestep_spacingin ["trailing", "leading"]:
self.check_over_configs(timestep_spacing=timestep_spacing)
deftest_rescale_betas_zero_snr(self):
forrescale_betas_zero_snrin [True, False]:
self.check_over_configs(rescale_betas_zero_snr=rescale_betas_zero_snr)
deftest_thresholding(self):
self.check_over_configs(thresholding=False)
forthresholdin [0.5, 1.0, 2.0]:
forprediction_typein ["epsilon", "v_prediction"]:
self.check_over_configs(
thresholding=True,
prediction_type=prediction_type,
sample_max_value=threshold,
)
deftest_time_indices(self):
fortin [1, 10, 49]:
self.check_over_forward(time_step=t)
deftest_inference_steps(self):
fort, num_inference_stepsinzip([1, 10, 50], [10, 50, 500]):
self.check_over_forward(time_step=t, num_inference_steps=num_inference_steps)
@unittest.skip("Test not supported.")
deftest_add_noise_device(self):
pass
deftest_full_loop_no_noise(self):
sample=self.full_loop()
result_sum=torch.sum(torch.abs(sample))
result_mean=torch.mean(torch.abs(sample))
assertabs(result_sum.item() -671.6816) <1e-2
assertabs(result_mean.item() -0.8746) <1e-3
deftest_full_loop_with_v_prediction(self):
sample=self.full_loop(prediction_type="v_prediction")
result_sum=torch.sum(torch.abs(sample))
result_mean=torch.mean(torch.abs(sample))
assertabs(result_sum.item() -1394.2185) <1e-2
assertabs(result_mean.item() -1.8154) <1e-3
deftest_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))
assertabs(result_sum.item() -539.9622) <1e-2
assertabs(result_mean.item() -0.7031) <1e-3
deftest_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)
result_sum=torch.sum(torch.abs(sample))
result_mean=torch.mean(torch.abs(sample))
assertabs(result_sum.item() -542.6722) <1e-2
assertabs(result_mean.item() -0.7066) <1e-3