-
Notifications
You must be signed in to change notification settings - Fork 1
/
milp_encoder.py
434 lines (353 loc) · 15.3 KB
/
milp_encoder.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# ************
# File: milp_encoder.py
# Top contributors (to current version):
# Panagiotis Kouvaros (panagiotis.kouvaros@gmail.com)
# This file is part of the Venus project.
# Copyright: 2019-2021 by the authors listed in the AUTHORS file in the
# top-level directory.
# License: BSD 2-Clause (see the file LICENSE in the top-level directory).
# Description: Builds a Gurobi Model encoding a verification problem.
# ************
import torch
import numpy as np
from gurobipy import *
from venus.network.node import *
from venus.dependency.dependency_graph import DependencyGraph
from venus.dependency.dependency_type import DependencyType
from venus.common.utils import ReluState
from venus.verification.verification_problem import VerificationProblem
from venus.common.configuration import Config
from venus.common.logger import get_logger
from timeit import default_timer as timer
class MILPEncoder:
logger = None
def __init__(self, prob: VerificationProblem, config: Config):
"""
Arguments:
nn:
NeuralNetwork.
spec:
Specification.
config:
Configuration
"""
self.prob = prob
self.config = config
if MILPEncoder.logger is None:
MILPEncoder.logger = get_logger(__name__, config.LOGGER.LOGFILE)
self._idx_count = 0
def encode(self, linear_approx=False):
"""
Builds a Gurobi Model encoding the verification problem.
Arguments:
linear_approx:
whether to use linear approximation for Relu nodes.
Returns:
Gurobi Model.
"""
start = timer()
with gurobipy.Env(empty=True) as env:
env.setParam('OutputFlag', 0)
env.setParam('LogToConsole', 0)
env.start()
gmodel = Model(env=env)
self.add_node_vars(gmodel, linear_approx)
gmodel.update()
self.add_node_constrs(gmodel, linear_approx)
self.add_output_constrs(self.prob.nn.tail, gmodel)
deps_cond = linear_approx is not True and \
(
self.config.SOLVER.INTRA_DEP_CONSTRS is True or \
self.config.SOLVER.INTER_DEP_CONSTRS is True
)
if deps_cond is True:
self.add_dep_constrs(gmodel)
gmodel.update()
MILPEncoder.logger.info(
'Encoded verification problem {} into {}, time: {:.2f}'.format(
self.prob.id,
"LP" if linear_approx is True else "MILP",
timer() - start
)
)
return gmodel
def add_node_vars(self, gmodel: Model, linear_approx: bool=False):
"""
Assigns MILP variables for encoding each of the outputs of a given
node.
Arguments:
gmodel:
The gurobi model
linear_approx:
whether to use linear approximation for Relu nodes.
"""
self.add_output_vars(self.prob.spec.input_node, gmodel)
for i in range(self.prob.nn.tail.depth + 1):
nodes = self.prob.nn.get_node_by_depth(i)
for j in nodes:
if j.has_relu_activation() is True:
p_idx = j.from_node[-1].get_milp_var_indices()
j.set_milp_var_indices(out_start=p_idx[0], out_end=p_idx[1])
elif isinstance(j, Relu):
self.add_output_vars(j, gmodel)
if linear_approx is not True:
self.add_relu_delta_vars(j, gmodel)
elif type(j) in [Flatten, Slice, Unsqueeze, Reshape]:
j.out_vars = j.forward(j.from_node[0].out_vars)
p_idx = j.from_node[0].get_milp_var_indices()
j.set_milp_var_indices(out_start=p_idx[0], out_end=p_idx[1])
elif isinstance(j, Concat):
j.out_vars = j.forward([k.out_vars for k in j.from_node])
elif type(j) in [
Gemm, MatMul, Conv, ConvTranspose, Add, Sub, BatchNormalization,
MaxPool, AveragePool
]:
self.add_output_vars(j, gmodel)
else:
raise TypeError(f'The MILP encoding of node {j} is not supported')
def add_output_vars(self, node: Node, gmodel: Model):
"""
Creates a real-valued MILP variable for each of the outputs of a given
node.
Arguments:
node:
The node.
gmodel:
The gurobi model
"""
if node.bounds.size() > 0:
node.out_vars = np.array(
gmodel.addVars(
int(node.output_size),
lb=node.bounds.lower.flatten(),
ub=node.bounds.upper.flatten()
).values()
).reshape(node.output_shape)
else:
if isinstance(node, Relu):
node.out_vars = np.array(
gmodel.addVars(
(node.output_size.item(),), lb=0, ub=GRB.INFINITY
).values()
).reshape(node.output_shape)
else:
node.out_vars = np.array(
gmodel.addVars(
(node.output_size.item(),), lb=-GRB.INFINITY, ub=GRB.INFINITY
).values()
).reshape(node.output_shape)
new_idx = self._idx_count + int(node.output_size)
node.set_milp_var_indices(out_start=self._idx_count, out_end=new_idx)
self._idx_count = new_idx
def add_relu_delta_vars(self, node: Relu, gmodel: Model):
"""
Creates a binary MILP variable for encoding each of the units in a given
ReLU node. The variables are prioritised for branching according to the
depth of the node.
Arguments:
node:
The Relu node.
gmodel:
The gurobi model
"""
assert(isinstance(node, Relu)), "Cannot add delta variables to non-relu nodes."
node.delta_vars = np.empty(shape=node.output_size, dtype=Var)
if node.get_unstable_count() > 0:
node.delta_vars[node.get_unstable_flag().flatten()] = np.array(
gmodel.addVars(
node.get_unstable_count().item(), vtype=GRB.BINARY
).values()
)
node.delta_vars = node.delta_vars.reshape(node.output_shape)
new_idx = self._idx_count + node.get_unstable_count().item()
node.set_milp_var_indices(delta_start=self._idx_count, delta_end=new_idx)
self._idx_count = new_idx
def add_node_constrs(self, gmodel, linear_approx: bool=False):
"""
Computes the output constraints of a node given the MILP variables of its
inputs. It assumes that variables have already been added.
Arguments:
gmodel:
The gurobi model.
linear_approx:
whether to use linear approximation for Relu nodes.
"""
for i in range(self.prob.nn.tail.depth + 1):
nodes = self.prob.nn.get_node_by_depth(i)
for j in nodes:
if j.has_relu_activation() is True:
continue
elif isinstance(j, Relu):
self.add_relu_constrs(j, gmodel, linear_approx)
elif type(j) in [Flatten, Concat, Slice, Unsqueeze, Reshape]:
pass
elif type(j) in [
Gemm, Conv, ConvTranspose, MatMul, Sub, Add, BatchNormalization,
AveragePool
]:
self.add_affine_constrs(j, gmodel)
elif isinstance(j, MaxPool):
self.add_maxpool_constrs(j, gmodel)
else:
raise TypeError(f'The MILP encoding of node {j} is not supported')
def add_affine_constrs(self, node: Gemm, gmodel: Model):
"""
Computes the output constraints of an affine node given the MILP
variables of its inputs. It assumes that variables have already been
added.
Arguments:
node:
The node.
gmodel:
Gurobi model.
"""
if type(node) not in [Gemm, Conv, ConvTranspose, MatMul, Sub, Add, BatchNormalization]:
raise TypeError(f"Cannot compute affine onstraints for {type(node)} nodes.")
if type(node) in ['Sub', 'Add'] and node.const is not None:
output = node.forward(
node.from_node[0].out_vars, node.from_node[1].out_vars
)
else:
output = node.forward(node.from_node[0].out_vars)
for i in node.get_outputs():
gmodel.addConstr(node.out_vars[i] == output[i])
def add_relu_constrs(self, node: Relu, gmodel: Model, linear_approx=False):
"""
Computes the output constraints of a relu node given the MILP variables
of its inputs.
Arguments:
node:
Relu node.
gmodel:
Gurobi model.
"""
assert(isinstance(node, Relu)), "Cannot compute relu constraints for non-relu nodes."
if type(node.from_node[0]) in [Add, Sub] and \
node.from_node[0].const is None:
inp = node.from_node[0].forward(
node.from_node[0].from_node[0].out_vars,
node.from_node[0].from_node[1].out_vars
)
else:
inp = node.from_node[0].forward(node.from_node[0].from_node[0].out_vars)
out, delta = node.out_vars, node.delta_vars
l, u = node.from_node[0].bounds.lower, node.from_node[0].bounds.upper
# if self.test is True:
# gmodel.addConstrs(out[i] == inp[i] for i in node.get_active_indices())
# gmodel.addConstrs(out[i] == 0 for i in node.get_inactive_indices())
# gmodel.addConstrs(inp[i] <= 0 for i in node.get_deproot_indices())
# idx = node.get_unstable_indices()
# if linear_approx is True:
# gmodel.addConstrs(out[i] >= inp[i] for i in idx)
# gmodel.addConstrs(out[i] >= 0 for i in idx)
# gmodel.addConstrs(out[i] <= (u[i].item() / (u[i].item() - l[i].item())) * (inp[i] - l[i].item()) for i in idx)
# else:
# gmodel.addConstrs(out[i] >= inp[i] for i in idx)
# gmodel.addConstrs(out[i] <= inp[i] - l[i].item() * (1 - delta[i]) for i in idx)
# gmodel.addConstrs(out[i] <= u[i].item() * delta[i] for i in idx)
# return
for i in node.get_outputs():
if l[i] >= 0 or node.state[i] == ReluState.ACTIVE:
# active node as per bounds or as per branching
gmodel.addConstr(out[i] == inp[i])
elif u[i] <= 0:
# inactive node as per bounds
gmodel.addConstr(out[i] == 0)
elif node.dep_root[i] == False and node.state[i] == ReluState.INACTIVE:
# non-root inactive node as per branching
gmodel.addConstr(out[i] == 0)
elif node.dep_root[i] == True and node.state[i] == ReluState.INACTIVE:
# root inactive node as per branching
gmodel.addConstr(out[i] == 0)
gmodel.addConstr(inp[i] <= 0)
else:
l_i, u_i = l[i].item(), u[i].item()
# unstable node
if linear_approx is True:
gmodel.addConstr(out[i] >= inp[i])
gmodel.addConstr(out[i] >= 0)
gmodel.addConstr(out[i] <= (u_i / (u_i - l_i)) * (inp[i] - l_i))
else:
gmodel.addConstr(out[i] >= inp[i])
gmodel.addConstr(out[i] <= inp[i] - l_i * (1 - delta[i]))
gmodel.addConstr(out[i] <= u_i * delta[i])
def add_maxpool_constrs(self, node: MaxPool, gmodel: Model):
"""
Computes the output constraints of a maxpool node given the MILP variables
of its inputs.
Arguments:
node:
MaxPool node.
gmodel:
Gurobi model.
"""
assert(isinstance(node, MaxPool)), "Cannot compute maxpool constraints for non-maxpool nodes."
inp = node.from_node[0].out_vars
padded_inp = Conv.pad(inp, node.pads).reshape((node.in_ch(), 1) + inp.shape[-2:])
im2col = Conv.im2col(
padded_inp, node.kernel_shape, node.strides
)
idxs = np.arange(node.output_size).reshape(
node.output_shape_no_batch()
).transpose(1, 2, 0).reshape(-1, node.in_ch())
for i in itertools.product(*[range(j) for j in idxs.shape]):
gmodel.addConstr(
np.take(node.out_vars, idxs[i]) == max_(im2col[:, i[0], i[1]].tolist())
)
# for i in node.get_outputs():
# kernel, height, width = i[-3:]
# win = []
# for kh, kw in itertools.product(
# range(node.kernel_shape[0]), range(node.kernel_shape[1])
# ):
# index_h = height * node.kernel_shape[0] + kh
# index_w = width * node.kernel_shape[1] + kw
# if node.from_node[0].has_batch_dimension():
# index = (0, kernel, index_h, index_w)
# else:
# index = (kernel, index_h, index_w)
# win.append(inp[index])
# gmodel.addConstr(node.out_vars[i] == max_(win))
def add_output_constrs(self, node: Node, gmodel: Model):
"""
Creates MILP constraints for the output of the output layer.
Arguments:
node:
The output node.
gmodel:
The gurobi model.
"""
constrs = self.prob.spec.get_output_constrs(gmodel, node.out_vars.flatten())
for i in constrs:
gmodel.addConstr(i)
def add_dep_constrs(self, gmodel):
"""
Adds dependency constraints.
Arguments:
gmodel:
The gurobi model.
"""
dg = DependencyGraph(
self.prob.nn,
self.config.SOLVER.INTRA_DEP_CONSTRS,
self.config.SOLVER.INTER_DEP_CONSTRS,
self.config
)
dg.build()
for i in dg.nodes:
for j in dg.nodes[i].adjacent:
# get the nodes in the dependency
lhs_node, lhs_idx = dg.nodes[i].nodeid, dg.nodes[i].index
delta1 = self.prob.nn.node[lhs_node].delta_vars[lhs_idx]
rhs_node, rhs_idx = dg.nodes[j].nodeid, dg.nodes[j].index
delta2 = self.prob.nn.node[rhs_node].delta_vars[rhs_idx]
dep = dg.nodes[i].adjacent[j]
# add the constraint as per the type of the dependency
if dep == DependencyType.INACTIVE_INACTIVE:
gmodel.addConstr(delta2 <= delta1)
elif dep == DependencyType.INACTIVE_ACTIVE:
gmodel.addConstr(1 - delta2 <= delta1)
elif dep == DependencyType.ACTIVE_INACTIVE:
gmodel.addConstr(delta2 <= 1 - delta1)
elif dep == DependencyType.ACTIVE_ACTIVE:
gmodel.addConstr(1 - delta2 <= 1 - delta1)