-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFastBATLLNNClient.py
163 lines (136 loc) · 5.59 KB
/
FastBATLLNNClient.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
import sys
import requests
import json
import time
import numpy as np
serverURL = 'http://localhost:8000/'
availableVNNLIB = False
if len(sys.argv) >= 2 and sys.argv[1] == 'setProblem' or sys.argv[0][-min(20,len(sys.argv[0])):] != 'FastBATLLNNClient.py':
import TLLnetIO as TLLnet
import numpy as np
try:
from vnnlib import read_vnnlib_simple
availableVNNLIB = True
except ImportError:
print('Unable to import module vnnlib -- VNNLIB import will be disabled. Install vnnlib.py via nnenum https://github.com/stanleybak/nnenum to enable VNNLIB properties.')
def setProblem(onnxFile=None,tllFile=None,vnnlibFile=None,inputProperty=None,outputProperty=None):
if tllFile is not None:
if type(tllFile) is dict:
tllDict = tllFile
fileID = str(id(tllDict))
else:
tll = TLLnet.TLLnet.fromTLLFormat(tllFile)
tllDict = tll.save()
fileID = str(tllFile)
elif onnxFile is not None:
t = time.time()
tll = TLLnet.TLLnet.fromONNX(onnxFile,validateLayers=False)
tllDict = tll.save()
print(f'Used {time.time()-t} seconds to import ONNX file...')
fileID = onnxFile
n = tllDict['n']
if vnnlibFile is not None:
if availableVNNLIB:
spec = read_vnnlib_simple(vnnlibFile,2,1)
# print(spec)
# [
# (
# [[-2.0, 2.0], [-2.0, 2.0]],
# [
# (array([[1.]]), array([-1.74903255]))
# ]
# )
# ]
A_in = np.zeros((2*n,n))
np.fill_diagonal(A_in[:n], 1)
np.fill_diagonal(A_in[n:], -1)
b_in = np.ones((2*n,))
b_in[:n] = np.array([inputConst[0] for inputConst in spec[0][0]])
b_in[n:] = np.array([-inputConst[1] for inputConst in spec[0][0]])
tllDict['A_in'] = A_in.tolist()
tllDict['b_in'] = b_in.tolist()
A_out, b_out = spec[0][1][0]
tllDict['A_out'] = float(A_out)
tllDict['b_out'] = float(b_out)
propertyID = vnnlibFile
else:
raise ImportError('Attempted to set a VNNLIB property, but the vnnlib module was not imported. Please install vnnlib.py.')
elif inputProperty is not None and outputProperty is not None:
propertyID = str(inputProperty) + str(outputProperty)
else:
raise ValueError('Please supply both an input property and an output property')
tllDict['localLinearFns'] = localLinToPy(tllDict['localLinearFns'])
tllDict['selectorSets'] = selectorsToList(tllDict['selectorSets'])
tllDict['id'] = '[' + fileID + '][' + propertyID + ']'
tllDict['COMMAND'] = 'NEW_PROBLEM'
# print(tllDict)
r = requests.post(serverURL + 'post', json=tllDict)
return tllDict
def getResult(onnxFile=None,tllFile=None,vnnlibFile=None,inputProperty=None,outputProperty=None,timeout=300):
if tllFile is not None:
if type(tllFile) is dict:
fileID = str(id(tllDict))
else:
fileID = str(tllFile)
elif onnxFile is not None:
fileID = onnxFile
if vnnlibFile is not None:
propertyID = vnnlibFile
elif inputProperty is not None and outputProperty is not None:
propertyID = str(inputProperty) + str(outputProperty)
else:
raise ValueError('Please supply both an input property and an output property')
tllDict = {}
tllDict['timeout'] = timeout
tllDict['id'] = '[' + fileID + '][' + propertyID + ']'
tllDict['COMMAND'] = 'GO'
r = requests.post(serverURL + 'post', json=tllDict)
result = requests.get(serverURL)
return result
def shutdown():
tllDict = {}
tllDict['COMMAND'] = 'SHUTDOWN'
r = requests.post(serverURL + 'post', json=tllDict)
def localLinToPy(localLinearFns):
return [
[ x.tolist() for x in output ] for output in localLinearFns
]
def selectorsToList(selectorSets):
return [
[ [int(idx) for idx in x] for x in output ] for output in selectorSets
]
if __name__ == '__main__':
if len(sys.argv) <= 1:
sys.exit(1)
command = sys.argv[1]
if command == 'shutdown':
result = shutdown()
sys.exit()
elif command == 'getResult':
if len(sys.argv) >= 5:
result = getResult(onnxFile=sys.argv[2], vnnlibFile=sys.argv[3], timeout=int(sys.argv[4])).json()
if 'RESULT' in result:
if 'counterExample' in result and 'counterExampleVal' in result:
resultStr = '\n('
cePoint = np.array(result['counterExample']).flatten()
ceVal = np.array(result['counterExampleVal']).flatten()
for i in range(len(cePoint)):
resultStr += f'(X_{i} {cePoint[i]})\n'
for i in range(len(ceVal)):
resultStr += f'(Y_{i} {ceVal[i]})\n'
resultStr = resultStr[:-1] + ')'
else:
resultStr = ''
print(result['RESULT'] + resultStr)
sys.exit(0)
else:
sys.exit(1)
else:
sys.exit(1)
elif command == 'setProblem':
if len(sys.argv) >= 4:
result = setProblem(onnxFile=sys.argv[2], vnnlibFile=sys.argv[3])
else:
sys.exit(1)
else:
sys.exit(1)