- Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathtest_scheduler_ipndm.py
165 lines (119 loc) · 6.91 KB
/
test_scheduler_ipndm.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
importtempfile
importunittest
importtorch
fromdiffusersimportIPNDMScheduler
from .test_schedulersimportSchedulerCommonTest
classIPNDMSchedulerTest(SchedulerCommonTest):
scheduler_classes= (IPNDMScheduler,)
forward_default_kwargs= (("num_inference_steps", 50),)
defget_scheduler_config(self, **kwargs):
config= {"num_train_timesteps": 1000}
config.update(**kwargs)
returnconfig
defcheck_over_configs(self, time_step=0, **config):
kwargs=dict(self.forward_default_kwargs)
num_inference_steps=kwargs.pop("num_inference_steps", None)
sample=self.dummy_sample
residual=0.1*sample
dummy_past_residuals= [residual+0.2, residual+0.15, residual+0.1, residual+0.05]
forscheduler_classinself.scheduler_classes:
scheduler_config=self.get_scheduler_config(**config)
scheduler=scheduler_class(**scheduler_config)
scheduler.set_timesteps(num_inference_steps)
# copy over dummy past residuals
scheduler.ets=dummy_past_residuals[:]
iftime_stepisNone:
time_step=scheduler.timesteps[len(scheduler.timesteps) //2]
withtempfile.TemporaryDirectory() astmpdirname:
scheduler.save_config(tmpdirname)
new_scheduler=scheduler_class.from_pretrained(tmpdirname)
new_scheduler.set_timesteps(num_inference_steps)
# copy over dummy past residuals
new_scheduler.ets=dummy_past_residuals[:]
output=scheduler.step(residual, time_step, sample, **kwargs).prev_sample
new_output=new_scheduler.step(residual, time_step, sample, **kwargs).prev_sample
asserttorch.sum(torch.abs(output-new_output)) <1e-5, "Scheduler outputs are not identical"
output=scheduler.step(residual, time_step, sample, **kwargs).prev_sample
new_output=new_scheduler.step(residual, time_step, sample, **kwargs).prev_sample
asserttorch.sum(torch.abs(output-new_output)) <1e-5, "Scheduler outputs are not identical"
@unittest.skip("Test not supported.")
deftest_from_save_pretrained(self):
pass
defcheck_over_forward(self, time_step=0, **forward_kwargs):
kwargs=dict(self.forward_default_kwargs)
num_inference_steps=kwargs.pop("num_inference_steps", None)
sample=self.dummy_sample
residual=0.1*sample
dummy_past_residuals= [residual+0.2, residual+0.15, residual+0.1, residual+0.05]
forscheduler_classinself.scheduler_classes:
scheduler_config=self.get_scheduler_config()
scheduler=scheduler_class(**scheduler_config)
scheduler.set_timesteps(num_inference_steps)
# copy over dummy past residuals (must be after setting timesteps)
scheduler.ets=dummy_past_residuals[:]
iftime_stepisNone:
time_step=scheduler.timesteps[len(scheduler.timesteps) //2]
withtempfile.TemporaryDirectory() astmpdirname:
scheduler.save_config(tmpdirname)
new_scheduler=scheduler_class.from_pretrained(tmpdirname)
# copy over dummy past residuals
new_scheduler.set_timesteps(num_inference_steps)
# copy over dummy past residual (must be after setting timesteps)
new_scheduler.ets=dummy_past_residuals[:]
output=scheduler.step(residual, time_step, sample, **kwargs).prev_sample
new_output=new_scheduler.step(residual, time_step, sample, **kwargs).prev_sample
asserttorch.sum(torch.abs(output-new_output)) <1e-5, "Scheduler outputs are not identical"
output=scheduler.step(residual, time_step, sample, **kwargs).prev_sample
new_output=new_scheduler.step(residual, time_step, sample, **kwargs).prev_sample
asserttorch.sum(torch.abs(output-new_output)) <1e-5, "Scheduler outputs are not identical"
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)
fori, tinenumerate(scheduler.timesteps):
residual=model(sample, t)
sample=scheduler.step(residual, t, sample).prev_sample
scheduler._step_index=None
fori, tinenumerate(scheduler.timesteps):
residual=model(sample, t)
sample=scheduler.step(residual, t, sample).prev_sample
returnsample
deftest_step_shape(self):
kwargs=dict(self.forward_default_kwargs)
num_inference_steps=kwargs.pop("num_inference_steps", None)
forscheduler_classinself.scheduler_classes:
scheduler_config=self.get_scheduler_config()
scheduler=scheduler_class(**scheduler_config)
sample=self.dummy_sample
residual=0.1*sample
ifnum_inference_stepsisnotNoneandhasattr(scheduler, "set_timesteps"):
scheduler.set_timesteps(num_inference_steps)
elifnum_inference_stepsisnotNoneandnothasattr(scheduler, "set_timesteps"):
kwargs["num_inference_steps"] =num_inference_steps
# 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[:]
time_step_0=scheduler.timesteps[5]
time_step_1=scheduler.timesteps[6]
output_0=scheduler.step(residual, time_step_0, sample, **kwargs).prev_sample
output_1=scheduler.step(residual, time_step_1, sample, **kwargs).prev_sample
self.assertEqual(output_0.shape, sample.shape)
self.assertEqual(output_0.shape, output_1.shape)
output_0=scheduler.step(residual, time_step_0, sample, **kwargs).prev_sample
output_1=scheduler.step(residual, time_step_1, sample, **kwargs).prev_sample
self.assertEqual(output_0.shape, sample.shape)
self.assertEqual(output_0.shape, output_1.shape)
deftest_timesteps(self):
fortimestepsin [100, 1000]:
self.check_over_configs(num_train_timesteps=timesteps, time_step=None)
deftest_inference_steps(self):
fort, num_inference_stepsinzip([1, 5, 10], [10, 50, 100]):
self.check_over_forward(num_inference_steps=num_inference_steps, time_step=None)
deftest_full_loop_no_noise(self):
sample=self.full_loop()
result_mean=torch.mean(torch.abs(sample))
assertabs(result_mean.item() -2540529) <10