-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodules_SE.py
414 lines (311 loc) · 13.5 KB
/
modules_SE.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import torch
import torch.nn.functional as F
import torch.nn as nn
from random import shuffle
from torch.distributions.bernoulli import Bernoulli
from torch.distributions.categorical import Categorical
import domain
from constants import *
import constants
from domain_utils import (
concatenate_states,
concatenate_states_list,
)
from utils import (
select_argmax,
)
import math
def show_tra_l(l):
for abstract_state in l:
print("in one abstract state")
tra_len_l = list()
for symbol_table in abstract_state:
if len(symbol_table) > 1:
tra_len_l.append(len(symbol_table['trajectory']))
print(f"c: {symbol_table['x'].c}, delta: {symbol_table['x'].delta}")
else:
tra_len_l.append(0)
print(f"end abstract state")
break
'''
Module used as functions
'''
class Linear(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.weight = torch.nn.Parameter(torch.Tensor(self.in_channels, self.out_channels))
self.bias = torch.nn.Parameter(torch.Tensor(self.out_channels))
self.reset_parameters()
def reset_parameters(self):
if not hasattr(self,'weight') or self.weight is None:
return
n = product(self.weight.size()) / self.out_channels
stdv = 1 / math.sqrt(n)
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, x):
if isinstance(x, torch.Tensor):
if len(x.shape) == 3:
x = torch.squeeze(x, 1)
return x.matmul(self.weight).add(self.bias)
class Conv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=1, padding=1):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.padding = padding
self.weight = torch.nn.Parameter(torch.Tensor(out_channels, in_channels, kernel_size))
self.bias = torch.nn.Parameter(torch.Tensor(self.out_channels))
self.reset_parameters()
def reset_parameters(self):
self.k = 1/(self.in_channels * self.kernel_size)
stdv = 1 / math.sqrt(self.k)
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, x):
if isinstance(x, torch.Tensor):
if len(x.shape) == 2:
x = x[:, None, :]
return F.conv1d(x, self.weight, bias=self.bias, padding=self.padding)
return x.conv(self.weight, self.bias, self.padding)
class Sigmoid(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x.sigmoid()
class Tanh(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x.tanh()
class ReLU(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x.relu()
class SigmoidLinear(nn.Module):
def __init__(self, sig_range):
super().__init__()
self.sig_range = sig_range
def forward(self, x):
return x.sigmoid_linear(sig_range=self.sig_range)
'''
Program Statement
'''
def calculate_states(target_idx, arg_idx, f, states):
x = states['x']
input = x.select_from_index(1, arg_idx)
res = f(input)
x.c[:, target_idx] = res.c
x.delta[:, target_idx] = res.delta
states['x'] = x
return states
def extract_branch_probability(target, test):
p_test = torch.zeros(target.getLeft().shape).cuda()
p_test[target.getRight() <= test] = 1.0
p_test[target.getLeft() > test] = 0.0
cross_idx = torch.logical_and(target.getRight() > test, target.getLeft() <= test)
p_test[cross_idx] = (test - target.getLeft()[cross_idx]) / (target.getRight()[cross_idx] - target.getLeft()[cross_idx])
return p_test, 1 - p_test
def sample_from_p(p_left, p_right):
m = Bernoulli(p_left)
res = m.sample()
left = res > 0
right = (1 - res) > 0
return left, right
def calculate_branch(target_idx, test, states):
body_states, orelse_states = dict(), dict()
x = states['x']
target = x.select_from_index(1, target_idx) # select the batch target from x
# select the idx of left = target.left < test, right = target.right >= test
# select the trajectory accordingly
# select the idx accordingly
# split the other
p_left, p_right = extract_branch_probability(target, test)
left, right = sample_from_p(p_left, p_right)
# print(left, right)
if constants.debug_verifier:
print(f"target c: {target.c}, delta: {target.delta}")
print(f"left probability: {p_left}")
print(f"right probability: {p_right}")
if True in left: # split to left
left_idx = left.nonzero(as_tuple=True)[0].tolist()
x_left = domain.Box(x.c[left.squeeze(1)], x.delta[left.squeeze(1)])
left_target_c, left_target_delta = target.c[left].unsqueeze(1), target.delta[left].unsqueeze(1)
# get the new c, delta
new_left_target_c = ((left_target_c - left_target_delta) + torch.min((left_target_c + left_target_delta), test)) / 2.0
new_left_target_delta = (torch.min((left_target_c + left_target_delta), test) - (left_target_c - left_target_delta)) / 2.0
x_left.c[:, target_idx:target_idx+1] = new_left_target_c
x_left.delta[:, target_idx:target_idx+1] = new_left_target_delta
body_states['x'] = x_left
body_states['trajectories'] = [states['trajectories'][i] for i in left_idx]
body_states['idx_list'] = [states['idx_list'][i] for i in left_idx]
body_states['p_list'] = [states['p_list'][i].mul(p_left[i]) for i in left_idx]
if True in right: # split to right
right_idx = right.nonzero(as_tuple=True)[0].tolist()
x_right = domain.Box(x.c[right.squeeze(1)], x.delta[right.squeeze(1)])
right_target_c, right_target_delta = target.c[right].unsqueeze(1), target.delta[right].unsqueeze(1)
new_right_target_c = (torch.max((right_target_c - right_target_delta), test) + (right_target_c + right_target_delta)) / 2.0
new_right_target_delta = ((right_target_c + right_target_delta) - torch.max((right_target_c - right_target_delta), test)) / 2.0
x_right.c[:, target_idx:target_idx+1] = new_right_target_c
x_right.delta[:, target_idx:target_idx+1] = new_right_target_delta
orelse_states['x'] = x_right
orelse_states['trajectories'] = [states['trajectories'][i] for i in right_idx]
orelse_states['idx_list'] = [states['idx_list'][i] for i in right_idx]
orelse_states['p_list'] = [states['p_list'][i].mul(p_right[i]) for i in right_idx]
return body_states, orelse_states
def extract_branch_probability_list(target, index_mask):
# volume_based probability assignment
# return a list of boolean tensor where the k-th boolean tensor represents the states fall into the k-th branch
zeros = torch.zeros(index_mask.shape)
branch = torch.zeros(index_mask.shape, dtype=torch.bool)
if torch.cuda.is_available():
zeros = zeros.cuda()
branch = branch.cuda()
# add the influnce from c
volume = 2 * target.delta
# all the volumes belonging to the argmax index set are selected, otherwise 0.0
selected_volume = torch.where(index_mask, volume, zeros)
# selected_volume might be all zero
selected_volume[index_mask] = torch.max(selected_volume[index_mask], EPSILON)
sumed_volume = torch.sum(selected_volume, dim=1)[:, None]
p_volume = selected_volume / sumed_volume
if constants.score_f == 'hybrid':
pre_score = p_volume + target.c
sumed_score = torch.sum(pre_score, dim=1)[:, None]
p_volume = pre_score / sumed_score
m = Categorical(p_volume)
res = m.sample()
branch[(torch.arange(p_volume.size(0)), res)] = True
p_volume = torch.where(branch, p_volume, zeros)
return branch, p_volume
def assign_states(states, branch, p_volume):
# K batches, M branches, # no change to the x itself
K, M = branch.shape
states_list = list()
x = states['x']
for i in range(M):
new_states = dict()
p = p_volume[:, i]
this_branch = branch[:, i]
if True in this_branch:
this_idx = this_branch.nonzero(as_tuple=True)[0].tolist()
x_this = domain.Box(x.c[this_branch], x.delta[this_branch])
new_states['x'] = x_this
new_states['trajectories'] = [states['trajectories'][idx] for idx in this_idx]
new_states['idx_list'] = [states['idx_list'][idx] for idx in this_idx]
new_states['p_list'] = [states['p_list'][idx].add(torch.log(p[idx])) for idx in this_idx]
states_list.append(new_states)
return states_list
def calculate_branches(arg_idx, states):
# arg_idx: [0, 1, 2, 3], target is a new box only having values with these indexes
x = states['x']
target = x.select_from_index(1, arg_idx)
# index_mask is a boolean tensor
index_mask = select_argmax(target.c - target.delta, target.c + target.delta)
# no split of boxes/states, only use the volume based probability distribution
# branch: boolean tensor k-th colume represents the k-th branch,
# p_volume: real tensor, k-th column represents the probability to select the k-th branch(after samping)
branch, p_volume = extract_branch_probability_list(target, index_mask)
states_list = assign_states(states, branch, p_volume)
return states_list
class Skip(nn.Module):
def __init__(self):
super().__init__()
def forward(self, states):
return states
class Assign(nn.Module):
def __init__(self, target_idx, arg_idx: list(), f):
super().__init__()
self.f = f
self.target_idx = torch.tensor(target_idx)
self.arg_idx = torch.tensor(arg_idx)
if torch.cuda.is_available():
self.target_idx = self.target_idx.cuda()
self.arg_idx = self.arg_idx.cuda()
def forward(self, states):
res_states = calculate_states(self.target_idx, self.arg_idx, self.f, states)
return res_states
class IfElse(nn.Module):
def __init__(self, target_idx, test, f_test, body, orelse):
super().__init__()
self.target_idx = torch.tensor(target_idx)
self.test = test
self.f_test = f_test
self.body = body
self.orelse = orelse
if torch.cuda.is_available():
self.target_idx = self.target_idx.cuda()
def forward(self, states):
body_states, orelse_states = calculate_branch(self.target_idx, self.test, states)
if len(body_states) > 0:
body_states = self.body(body_states)
if len(orelse_states) > 0:
orelse_states = self.orelse(orelse_states)
# maintain the same number of components as the initial ones
res_states = concatenate_states(body_states, orelse_states)
return res_states
class ArgMax(nn.Module):
def __init__(self, arg_idx, branch_list):
super().__init__()
self.arg_idx = torch.tensor(arg_idx)
self.branch_list = branch_list
if torch.cuda.is_available():
self.arg_idx = self.arg_idx.cuda()
def forward(self, states):
res_states_list = list()
states_list = calculate_branches(self.arg_idx, states)
for idx, state in enumerate(states_list):
if len(state) > 0:
res_states_list.append(self.branch_list[idx](state))
res_states = concatenate_states_list(res_states_list)
return res_states
class While(nn.Module):
def __init__(self, target_idx, test, body):
super().__init__()
self.target_idx = torch.tensor(target_idx)
self.test = test
self.body = body
if torch.cuda.is_available():
self.target_idx = self.target_idx.cuda()
def forward(self, states):
i = 0
res_states = dict()
while(len(states) > 0):
body_states, orelse_states = calculate_branch(self.target_idx, self.test, states)
res_states = concatenate_states(res_states, orelse_states)
if len(body_states) == 0:
return res_states
states = self.body(body_states)
i += 1
if i > MAXIMUM_ITERATION:
break
res_states = concatenate_states(res_states, orelse_states)
res_states = concatenate_states(res_states, body_states)
return res_states
class Trajectory(nn.Module):
def __init__(self, target_idx):
super().__init__()
self.target_idx = torch.tensor(target_idx)
if torch.cuda.is_available():
self.target_idx = self.target_idx.cuda()
def forward(self, states):
x = states['x']
trajectories = states['trajectories']
B, D = x.c.shape
for x_idx in range(B):
cur_x_c, cur_x_delta = x.c[x_idx], x.delta[x_idx]
input_interval_list = list()
for idx in self.target_idx:
input = domain.Box(cur_x_c[idx], cur_x_delta[idx])
input_interval = input.getInterval()
assert input_interval.left.data.item() <= input_interval.right.data.item()
input_interval_list.append(input_interval)
trajectories[x_idx].append(input_interval_list)
states['trajectories'] = trajectories
return states