-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprobfuzz.py
executable file
·171 lines (134 loc) · 5.56 KB
/
probfuzz.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
#!/usr/bin/python
import subprocess as sp
import time
import os
import sys
import threading
import Queue as queue
from utils.utils import *
from language.templateparser import TemplatePopulator
# region global config
_TOOL = 'tool'
_ENABLED = 'enabled'
_ALGORITHM = 'algorithm'
_TIMEOUT = 'timeout'
_MODELTYPE = 'model_type'
_CURRENT_MODEL_TYPE = 'LR' # LR
_STAN_GENERATE_QUANTS = False
_COMPARE = True
# choose fuzzer type
set_fuzzer_type('STR')
generate_special_data = False
generate_special_params = False
# endregion global config
class InferenceEngineRunner(threading.Thread):
def __init__(self, id, queue, runconfigs):
threading.Thread.__init__(self)
self.queue = queue
self.id = id
self.runconfigs = runconfigs
@staticmethod
def _runTool(config, prog_id, target_directory):
from backends.stan import Stan
from backends.edward import Edward
from backends.pyro import Pyro
if not config[_ENABLED]:
return
if config[_TOOL] == 'stan':
stan = Stan(target_directory, None, None, None, None, None, None)
stan.run(config[_ALGORITHM], config[_TIMEOUT], prog_id, config['python'])
elif config[_TOOL] == 'edward':
edward = Edward(target_directory, None, None, None, None, None, None, config.get("name", "edward"))
edward.run(config[_ALGORITHM], config[_TIMEOUT], prog_id, config['python'])
elif config[_TOOL] == 'pyro':
pyro = Pyro(target_directory, None, None, None, None, None, None)
pyro.run(config[_ALGORITHM], config[_TIMEOUT], prog_id, config['python'])
def run(self):
while not self.queue.empty():
args = self.queue.get(block=False)
for config in self.runconfigs:
self._runTool(config, args[0] + 1, args[1])
def filterCommonDistributions(models, runconfigurations):
filteredModels = []
for model in models:
i = 1
for config in runconfigurations:
if config[_ENABLED] and (config[_TOOL] not in model.keys()):
i = 0
break
if i == 1:
filteredModels.append(model)
return filteredModels
def generate_programs_from_template():
from backends.stan import Stan
from backends.edward import Edward
from backends.pyro import Pyro
config = read_config()
max_thread = config['max_threads']
if len(sys.argv) > 1:
progs = int(sys.argv[1])
if progs is None:
print("Err: Program count should be integer")
exit(-1)
else:
print("Err: Missing program count")
exit(-1)
# flag to indicate whether to use domain info for filtering
structured = config['structured']
current_template = config['templates'][config['current_template']]
# directory for output
dirname = config['output_dir'] + "/progs" + str(time.strftime("%Y%m%d-%H%M%S"))
# initialize queues
queues = [queue.Queue() for _ in range(0, max_thread)]
# select models
models = parse_models()
all_models = filterCommonDistributions(models, config['runConfigurations'])
# generate programs using template
for i in range(0, progs):
# create program directory
file_dir_name = dirname + '/prob_rand_' + str(i + 1)
if not os.path.exists(file_dir_name):
os.makedirs(file_dir_name)
template_populator = TemplatePopulator(file_dir_name, current_template, all_models)
data, priors, distmap, constmap = template_populator.populate(structured)
# check if model is valid if structured check is enabled
if structured:
while not template_populator.validModel:
data, priors, distmap, constmap = template_populator.populate()
for toolconfig in config['runConfigurations']:
if toolconfig[_ENABLED]:
if toolconfig[_TOOL] == 'stan':
stan = Stan(file_dir_name, data, priors, distmap, constmap, current_template, config)
stan.create_program()
# add the driver
sp.Popen(['cp', 'driver.py', file_dir_name], stdout=sp.PIPE).communicate()
elif toolconfig[_TOOL] == 'edward':
edward = Edward(file_dir_name, data, priors, distmap, constmap, current_template, config,
toolconfig.get('name', 'edward'))
edward.create_program()
elif toolconfig[_TOOL] == 'pyro':
pyro = Pyro(file_dir_name, data, priors, distmap, constmap, current_template, config)
pyro.create_program()
# enqueue threads
queues[i % max_thread].put((i, file_dir_name))
print("Output directory : " + dirname)
threads = []
for i in range(0, max_thread):
print("Starting thread .. " + str(i + 1))
thread = InferenceEngineRunner(i, queues[i], config['runConfigurations'])
thread.start()
threads.append(thread)
for i in range(0 , max_thread):
threads[i].join()
metric = config['metric']
printSummary(dirname, metric)
def printSummary(dir, metric):
print("Printing summary")
process = sp.Popen("./summary.sh -m {0} -d {1} | column -t -s,".format(metric, dir), stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
dataout, dataerr = process.communicate()
with open(dir + '/summary.csv', 'w') as summaryfile:
summaryfile.write(dataerr)
summaryfile.write(dataout)
print("Summary written in summary.csv")
if __name__ == "__main__":
generate_programs_from_template()