ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci_eval_verifier_ref.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2import numpy as np
3import subprocess
4import argparse
5import traceback
6import os
7
8#
9# This script compares the execution result of luci-interpreter with that from ref_model path
10#
11# Basic usage:
12# luci_eval_verifier_ref.py --driver build/compiler/luci-eval-driver/luci_eval_driver
13# --ref_model ref_model_path --model this_model_path
14# Assumption:
15# these file exist with its purpose
16# - ref_model_path.circle; circle model
17# - ref_model_path.circle.inputN; N'th input numpy data
18# - ref_model_path.circle.inputN.dtype; N'th input data type in text
19# - ref_model_path.circle.inputN.shape; N'th input data shape in CSV
20# - ref_model_path.circle.outputN; N'th output numpy data
21# - ref_model_path.circle.outputN.dtype; N'th output data type in text
22# - ref_model_path.circle.outputN.shape; N'th output data shape in CSV
23
24
25def dtype_from_file(file_path):
26 with open(file_path, 'r') as dtype_file:
27 dtype_str = dtype_file.read()
28 if dtype_str == "float32":
29 return np.float32
30 if dtype_str == "uint8":
31 return np.uint8
32 if dtype_str == "int16":
33 return np.int16
34 if dtype_str == "int32":
35 return np.int32
36 if dtype_str == "int64":
37 return np.int64
38 if dtype_str == "bool":
39 return np.bool_
40 raise SystemExit("Unsupported dtype from file", dtype_str)
41
42
43parser = argparse.ArgumentParser()
44parser.add_argument('--driver', type=str, required=True)
45parser.add_argument('--model_ref', type=str, required=True)
46parser.add_argument('--work_path', type=str, required=True)
47parser.add_argument('--rtolf32', type=str, required=False)
48parser.add_argument('--atolf32', type=str, required=False)
49args = parser.parse_args()
50
51driver = args.driver
52circle_model_ref = args.model_ref + ".circle"
53circle_model = args.work_path + ".circle"
54# circle_model is used as to follow existing luci_eval_verifier.py
55
56rtolf32 = 1e-5
57atolf32 = 1e-5
58# NOTE reuse f32 value as int value too
59rtolint = 0
60atolint = 0
61try:
62 if args.rtolf32 != None:
63 rtolf32 = float(args.rtolf32)
64 rtolint = int(rtolf32)
65 if args.atolf32 != None:
66 atolf32 = float(args.atolf32)
67 atolint = int(atolf32)
68except ValueError:
69 print("rtolf32 or atolf32 is not a number")
70 quit(128)
71
72# get num of inputs by checking existance of model.inputN
73check_input = 0
74while True:
75 input_file_path = circle_model_ref + ".input" + str(check_input)
76 if not os.path.isfile(input_file_path):
77 num_inputs = check_input
78 break
79 check_input = check_input + 1
80
81if num_inputs == 0:
82 print("input file not exist for", circle_model_ref)
83 quit(128)
84
85# get num of outputs by checking existance of model.outputN
86check_output = 0
87while True:
88 output_file_path = circle_model_ref + ".output" + str(check_output)
89 if not os.path.isfile(output_file_path):
90 num_outputs = check_output
91 break
92 check_output = check_output + 1
93
94if num_outputs == 0:
95 print("output file not exist for", circle_model_ref)
96 quit(128)
97
98# Execute luci interpreter with reference input
99subprocess.run([
100 driver, circle_model_ref,
101 str(num_inputs), circle_model_ref + ".input", circle_model + ".output"
102],
103 check=True)
104
105# Compare the results.
106for idx in range(num_outputs):
107 output_dtype = dtype_from_file(circle_model_ref + ".output" + str(idx) + ".dtype")
108 shape_file = open(circle_model_ref + ".output" + str(idx) + ".shape", 'r')
109 output_shape = [int(i) for i in shape_file.read().split(',')]
110
111 output_data_ref = np.fromfile(circle_model_ref + ".output" + str(idx), output_dtype)
112 luci_output_data_ref = np.reshape(output_data_ref, output_shape)
113
114 output_data = np.fromfile(circle_model + ".output" + str(idx), output_dtype)
115 luci_output_data = np.reshape(output_data, output_shape)
116
117 try:
118 if output_dtype == np.uint8:
119 if np.allclose(luci_output_data,
120 luci_output_data_ref,
121 rtol=rtolint,
122 atol=atolint) == False:
123 print("luci_output_data_ref", luci_output_data_ref)
124 print("luci_output_data", luci_output_data)
125 raise SystemExit("Execution result of " + circle_model_ref +
126 " does not match with " + circle_model)
127 elif output_dtype == np.float32:
128 if np.allclose(luci_output_data,
129 luci_output_data_ref,
130 rtol=rtolf32,
131 atol=atolf32) == False:
132 print("luci_output_data_ref", luci_output_data_ref)
133 print("luci_output_data", luci_output_data)
134 raise SystemExit("Execution result of " + circle_model_ref +
135 " does not match with " + circle_model)
136 elif output_dtype == np.int64:
137 if np.allclose(luci_output_data,
138 luci_output_data_ref,
139 rtol=rtolint,
140 atol=atolint) == False:
141 print("luci_output_data_ref", luci_output_data_ref)
142 print("luci_output_data", luci_output_data)
143 raise SystemExit("Execution result of " + circle_model_ref +
144 " does not match with " + circle_model)
145 elif output_dtype == np.int32:
146 if np.allclose(luci_output_data,
147 luci_output_data_ref,
148 rtol=rtolint,
149 atol=atolint) == False:
150 print("luci_output_data_ref", luci_output_data_ref)
151 print("luci_output_data", luci_output_data)
152 raise SystemExit("Execution result of " + circle_model_ref +
153 " does not match with " + circle_model)
154 elif output_dtype == np.int16:
155 if np.allclose(luci_output_data,
156 luci_output_data_ref,
157 rtol=rtolint,
158 atol=atolint) == False:
159 print("luci_output_data_ref", luci_output_data_ref)
160 print("luci_output_data", luci_output_data)
161 raise SystemExit("Execution result of " + circle_model_ref +
162 " does not match with " + circle_model)
163 elif output_dtype == np.bool_:
164 if np.allclose(luci_output_data, luci_output_data_ref, rtol=0,
165 atol=0) == False:
166 raise SystemExit("Execution result of " + circle_model_ref +
167 " does not match with " + circle_model)
168 else:
169 raise SystemExit("Unsupported data type: ", output_dtype)
170 except:
171 print(traceback.format_exc())
172 quit(255)
173
174quit(0)