-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnnsmith_valid.py
57 lines (44 loc) · 1.59 KB
/
nnsmith_valid.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
from argparse import ArgumentParser, Namespace
from sys import stdout
from numpy.random import Generator, PCG64
from onnx.shape_inference import InferenceError
from tqdm import tqdm
from tvm import TVMError
from nnsmith.materialize import Model
from nnsmith.narrow_spec import auto_opset
from nnsmith.relay_gen import nnsmith_gen_relay
args = Namespace()
def _parse_args():
global args
p = ArgumentParser()
p.add_argument('-n', '--number', type=int, help='Number of graphs')
p.add_argument('-s', '--seed', type=int, default=42, help='Random seed of graph generator.')
args = p.parse_args()
def main():
# Initialization
rng = Generator(PCG64(seed=args.seed))
opset = auto_opset(Model.init('onnx'))
# Generation loop
progress = tqdm(range(args.number), file=stdout)
native_invalid, relay_invalid = 0, 0
def display_count():
progress.set_postfix_str(f'{native_invalid}, {relay_invalid}')
for _ in progress:
# Generate Keras model
try:
nnsmith_gen_relay(opset, 32, rng)
except TVMError:
relay_invalid += 1
except InferenceError as err:
print(err)
relay_invalid += 1
except RuntimeError as err:
print(err)
pass
display_count()
print('Original pass rate: {:.3f}'.format(1 - native_invalid / args.number))
print('Failure rate of conversion: {:.3f}'.format(relay_invalid / args.number))
print('Relay pass rate: {:.3f}'.format(1 - (native_invalid + relay_invalid) / args.number))
if __name__ == '__main__':
_parse_args()
main()