-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_CSamplerWatcher.py
227 lines (198 loc) · 7.65 KB
/
test_CSamplerWatcher.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import tensorflow as tf
from Utils.utils import CFakeObject
from NN.restorators.samplers import sampler_from_config
from NN.restorators.samplers.CSamplerWatcher import CSamplerWatcher
from NN.utils import masked
def _fake_sampler(stochasticity=1.0, timesteps=10):
interpolant = sampler_from_config({
'name': 'DDIM',
'stochasticity': stochasticity,
'noise stddev': 'zero',
'schedule': {
'name': 'discrete',
'beta schedule': 'linear',
'timesteps': timesteps,
},
'steps skip type': { 'name': 'uniform', 'K': 1 },
})
shape = (32, 3)
fakeNoise = tf.random.normal(shape)
def fakeModel(V, T, **kwargs):
return fakeNoise + tf.cast(T, tf.float32) * V
x = tf.random.normal(shape)
return CFakeObject(x=x, model=fakeModel, interpolant=interpolant)
def _fake_AR(threshold, timesteps=10, scale=1.0):
interpolant = sampler_from_config({
"name": "autoregressive",
"noise provider": "zero",
"threshold": threshold,
"steps": {
"start": 1.0,
"end": 0.001,
"steps": timesteps,
"decay": 0.9
},
"interpolant": { "name": "direction" }
})
shape = (32, 3)
fakeNoise = tf.random.normal(shape)
def fakeModel(V, T, mask, **kwargs):
s = fakeNoise
if mask is not None:
s = masked(fakeNoise, mask)
T = masked(T, mask)
V = masked(V, mask)
return s + tf.cast(T, tf.float32) * V * scale
x = tf.random.normal(shape)
return CFakeObject(x=x, model=fakeModel, interpolant=interpolant)
def test_notAffectResults():
fake = _fake_sampler()
X_withoutWatcher = fake.interpolant.sample(value=fake.x, model=fake.model)
watcher = CSamplerWatcher(steps=10, tracked=dict())
X_withWatcher = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
tf.debugging.assert_equal(X_withoutWatcher, X_withWatcher)
return
def _commonChecks(collectedSteps, x, results):
tf.debugging.assert_equal(tf.shape(collectedSteps)[1:], tf.shape(x))
tf.debugging.assert_equal(collectedSteps[0], x, 'First step must be equal to initial value')
tf.debugging.assert_equal(collectedSteps[-1], results, 'Last step must be equal to results')
return
def test_collectsSteps():
fake = _fake_sampler()
watcher = CSamplerWatcher(
steps=10,
tracked=dict(
value=(32, 3)
)
)
oldX = watcher.tracked('value').numpy()
results = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
collectedSteps = watcher.tracked('value')
tf.debugging.assert_greater(
tf.reduce_sum(tf.abs(collectedSteps - oldX)), 0.0,
'Tracked steps must be different from initial value'
)
_commonChecks(collectedSteps, fake.x, results)
tf.debugging.assert_equal(tf.shape(collectedSteps)[0], 11, 'Must collect 11 values')
tf.debugging.assert_equal(watcher.iteration, 10, 'Must collect 10 steps')
return
def test_collectsOnlyIndicedValues():
fake = _fake_sampler()
indices = [0, 2, 6]
watcher = CSamplerWatcher(
steps=10,
tracked=dict(
value=(3,)
),
indices=indices
)
results = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
collectedSteps = watcher.tracked('value')
_commonChecks(
collectedSteps,
x=tf.gather(fake.x, indices, axis=0),
results=tf.gather(results, indices, axis=0)
)
tf.debugging.assert_equal(tf.shape(collectedSteps)[0], 11, 'Must collect 11 values')
return
def test_resetIteration():
watcher = CSamplerWatcher(steps=10, tracked=dict())
fake = _fake_sampler(timesteps=10)
_ = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
tf.debugging.assert_equal(watcher.iteration, 10, 'Must collect 10 steps')
fake = _fake_sampler(timesteps=5)
_ = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
tf.debugging.assert_equal(watcher.iteration, 5, 'Must collect 5 steps')
return
def _checkTracked(value, N):
assert value is not None, 'Must be tracked'
tf.debugging.assert_equal(tf.shape(value), (N, 32, 3), 'Unexpected shape')
tf.debugging.assert_greater(tf.reduce_sum(tf.abs(value[0] - value[1])), 0.0, 'Must be different')
return
def test_trackSolution():
fake = _fake_sampler()
watcher = CSamplerWatcher(
steps=10,
tracked=dict(x0=(32, 3), x1=(32, 3), value=(32, 3))
)
_ = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
_checkTracked(watcher.tracked('x0'), N=10)
_checkTracked(watcher.tracked('x1'), N=10)
_checkTracked(watcher.tracked('value'), N=11)
return
def test_trackSolutionWithMask():
fake = _fake_AR(threshold=0.1)
watcher = CSamplerWatcher(
steps=10,
tracked=dict(x0=(32, 3), x1=(32, 3))
)
_ = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
_checkTracked(watcher.tracked('x0'), N=10)
_checkTracked(watcher.tracked('x1'), N=10)
return
def test_trackSolutionWithMask_value():
fake = _fake_AR(threshold=0.5)
watcher = CSamplerWatcher(
steps=10,
tracked=dict(value=(32, 3), x0=(32, 3), x1=(32, 3))
)
_ = fake.interpolant.sample(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
_checkTracked(watcher.tracked('value'), N=11)
_checkTracked(watcher.tracked('x0'), N=10)
_checkTracked(watcher.tracked('x1'), N=10)
return
# test multiple calls with index
def test_multipleCallsWithIndex():
fake = _fake_sampler()
watcher = CSamplerWatcher(
steps=10,
tracked=dict(value=(32*3, 3))
)
arg = dict(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
A = fake.interpolant.sample(**arg, index=0)
B = fake.interpolant.sample(**arg, index=32)
C = fake.interpolant.sample(**arg, index=64)
tf.debugging.assert_equal(A, B)
tf.debugging.assert_equal(A, C)
collectedSteps = watcher.tracked('value')
tf.debugging.assert_equal(tf.shape(collectedSteps)[1], 96, 'Must collect 96 values')
tf.debugging.assert_equal(watcher.iteration, 10, 'Must collect 10 steps')
# values must be same across (0..32), (32..64), (64..96)
tf.debugging.assert_equal(collectedSteps[:, :32], collectedSteps[:, 32:64])
tf.debugging.assert_equal(collectedSteps[:, 32:64], collectedSteps[:, 64:])
return
# test multiple calls with index and mask
def test_multipleCallsWithIndexAndMask():
fake = _fake_AR(threshold=0.1)
watcher = CSamplerWatcher(
steps=10,
tracked=dict(value=(3,)),
indices=[0, 32, 64, 65]
)
arg = dict(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
A = fake.interpolant.sample(**arg, index=0)
B = fake.interpolant.sample(**arg, index=32)
C = fake.interpolant.sample(**arg, index=64)
tf.assert_equal(A, B)
tf.assert_equal(A, C)
collectedSteps = watcher.tracked('value')[:watcher.iteration]
tf.assert_equal(tf.shape(collectedSteps)[1:], (4, 3), 'Must be (4, 3)')
tf.debugging.assert_equal(collectedSteps[:, 0:1], collectedSteps[:, 1:2])
tf.debugging.assert_equal(collectedSteps[:, 1:2], collectedSteps[:, 2:3])
return
# test that masked values aren't zeroed
def test_maskedValues():
fake = _fake_AR(threshold=1e+5, scale=0.0)
watcher = CSamplerWatcher(
steps=10,
tracked=dict(value=(32, 3)),
)
arg = dict(value=fake.x, model=fake.model, algorithmInterceptor=watcher.interceptor())
_ = fake.interpolant.sample(**arg, index=0)
collectedSteps = watcher.tracked('value')
afterMask = collectedSteps[3:]
beforeMask = collectedSteps[2]
tf.debugging.assert_greater(3, watcher.iteration, 'Must collect 3 steps')
for i in range(3, watcher.iteration + 1):
tf.debugging.assert_equal(afterMask[i], beforeMask, 'Must be equal')
return