ONE - On-device Neural Engine
Loading...
Searching...
No Matches
compare_tensors_all Namespace Reference

Functions

 compare_fake_quantization (tensor, tensor_name, expect_dir)
 
 compare_record_minmax (tensor, tensor_name, expect_dir)
 
 compare_quantization (tensor, tensor_name, expect_dir)
 
 chunks (lst, n)
 

Variables

 parser = argparse.ArgumentParser()
 
 type
 
 required
 
 nargs
 
 help
 
 str
 
 True
 
 args = parser.parse_args()
 
dict modes_to_expected_folder
 
dict modes_to_input_h5_suffix
 
 test_param = args.test_param
 
 bin_dir = args.bin_dir
 
 source_dir = args.source_dir
 
 mode = args.mode
 
str log_format = '%(levelname)s: %(message)s'
 
 formatter = logging.Formatter(log_format)
 
 streamer = StringIO()
 
 stream_handler = logging.StreamHandler(stream=streamer)
 
 handlers
 
 failed_log = dict()
 
 inputs = test_param[0].split()
 
int PARAM_SET_SIZE = 3
 
 model_name = inputs[idx][0]
 
 granularity = inputs[idx][1]
 
 dtype = inputs[idx][2]
 
str testcase = f'{model_name}.{granularity}.{dtype}'
 
 test_result_file = os.path.join(bin_dir, testcase)
 
str input_h5 = f'{test_result_file}.{modes_to_input_h5_suffix[mode]}'
 
str expect_dir = f'{source_dir}/expected_outputs/{model_name}/{granularity}/{dtype}/{modes_to_expected_folder[mode]}'
 
bool test_result = False
 
 failed_number = len(failed_log)
 

Function Documentation

◆ chunks()

compare_tensors_all.chunks (   lst,
  n 
)
Yield successive n-sized chunks from the list

Definition at line 148 of file compare_tensors_all.py.

148def chunks(lst, n):
149 """Yield successive n-sized chunks from the list"""
150 for i in range(0, len(lst), n):
151 yield lst[i:i + n]
152
153

◆ compare_fake_quantization()

compare_tensors_all.compare_fake_quantization (   tensor,
  tensor_name,
  expect_dir 
)

Definition at line 73 of file compare_tensors_all.py.

73def compare_fake_quantization(tensor, tensor_name, expect_dir):
74 with open(expect_dir + "/" + tensor_name + ".json", "r") as expect_file:
75 json_load = json.load(expect_file)
76 expected_weights = np.array(json_load["weights"])
77 input_weights = tensor["weights"][:]
78 if np.allclose(input_weights, expected_weights, rtol=1.e-5, atol=1.e-5) == False:
79 logging.error("Fake-quantized weights of " + tensor_name + " (" +
80 str(input_weights) + ") do not match with expected value (" +
81 str(expected_weights) + ").")
82 return False
83 return True
84
85

References str.

◆ compare_quantization()

compare_tensors_all.compare_quantization (   tensor,
  tensor_name,
  expect_dir 
)

Definition at line 105 of file compare_tensors_all.py.

105def compare_quantization(tensor, tensor_name, expect_dir):
106 test_result = True
107 with open(expect_dir + "/" + tensor_name + ".json", "r") as expect_file:
108 json_load = json.load(expect_file)
109 for key in json_load:
110 if key == "weights":
111 expected_weights = np.array(json_load["weights"])
112 input_weights = tensor["weights"][()]
113 abs_tolerance = 1
114 # We use higher tolerance for int64 data (bias of int16-quantized model)
115 if tensor["weights"].dtype == 'int64':
116 abs_tolerance = 5
117
118 if np.allclose(input_weights, expected_weights, rtol=0,
119 atol=abs_tolerance) == False:
120 logging.error("Quantized weights of " + tensor_name + " (" +
121 str(input_weights) +
122 ") do not match with expected value (" +
123 str(expected_weights) + ").")
124 test_result = False
125
126 if key == "scale":
127 expected_scale = np.array(json_load["scale"])
128 input_scale = tensor["scale"][:]
129 if np.allclose(input_scale, expected_scale, rtol=1.e-5, atol=1.e-5) == False:
130 logging.error("Quantized scale of " + tensor_name + " (" +
131 str(input_scale) + ") do not match with expected value (" +
132 str(expected_scale) + ").")
133 test_result = False
134
135 if key == "zero_point":
136 expected_zero_point = np.array(json_load["zero_point"])
137 input_zero_point = tensor["zero_point"][:]
138 if np.allclose(input_zero_point, expected_zero_point, rtol=0,
139 atol=1) == False:
140 logging.error("Quantized zero_point of " + tensor_name + " (" +
141 str(input_zero_point) +
142 ") do not match with expected value (" +
143 str(expected_zero_point) + ").")
144 test_result = False
145 return test_result
146
147

References str.

◆ compare_record_minmax()

compare_tensors_all.compare_record_minmax (   tensor,
  tensor_name,
  expect_dir 
)

Definition at line 86 of file compare_tensors_all.py.

86def compare_record_minmax(tensor, tensor_name, expect_dir):
87 test_result = True
88 with open(expect_dir + "/" + tensor_name + ".json", "r") as expect_file:
89 json_load = json.load(expect_file)
90 expected_min = np.array(json_load["min"])
91 expected_max = np.array(json_load["max"])
92 input_min = tensor["min"][:]
93 input_max = tensor["max"][:]
94 if np.allclose(input_min, expected_min, rtol=1.e-5, atol=1.e-5) == False:
95 logging.error("Recorded min of " + tensor_name + " (" + str(input_min) +
96 ") does not match with expected value (" + str(expected_min) + ").")
97 test_result = False
98 if np.allclose(input_max, expected_max, rtol=1.e-5, atol=1.e-5) == False:
99 logging.error("Recorded max of " + tensor_name + " (" + str(input_max) +
100 ") does not match with expected value (" + str(expected_max) + ").")
101 test_result = False
102 return test_result
103
104

References str.

Variable Documentation

◆ args

compare_tensors_all.args = parser.parse_args()

Definition at line 32 of file compare_tensors_all.py.

◆ bin_dir

compare_tensors_all.bin_dir = args.bin_dir

Definition at line 54 of file compare_tensors_all.py.

◆ dtype

compare_tensors_all.dtype ( void  ) = inputs[idx][2]

Definition at line 162 of file compare_tensors_all.py.

◆ expect_dir

str compare_tensors_all.expect_dir = f'{source_dir}/expected_outputs/{model_name}/{granularity}/{dtype}/{modes_to_expected_folder[mode]}'

Definition at line 169 of file compare_tensors_all.py.

◆ failed_log

compare_tensors_all.failed_log = dict()

Definition at line 154 of file compare_tensors_all.py.

◆ failed_number

compare_tensors_all.failed_number = len(failed_log)

Definition at line 195 of file compare_tensors_all.py.

◆ formatter

compare_tensors_all.formatter = logging.Formatter(log_format)

Definition at line 62 of file compare_tensors_all.py.

◆ granularity

compare_tensors_all.granularity = inputs[idx][1]

Definition at line 161 of file compare_tensors_all.py.

◆ handlers

compare_tensors_all.handlers

Definition at line 66 of file compare_tensors_all.py.

◆ help

compare_tensors_all.help

Definition at line 20 of file compare_tensors_all.py.

◆ input_h5

str compare_tensors_all.input_h5 = f'{test_result_file}.{modes_to_input_h5_suffix[mode]}'

Definition at line 166 of file compare_tensors_all.py.

◆ inputs

compare_tensors_all.inputs = test_param[0].split()

Definition at line 155 of file compare_tensors_all.py.

◆ log_format

str compare_tensors_all.log_format = '%(levelname)s: %(message)s'

Definition at line 61 of file compare_tensors_all.py.

◆ mode

compare_tensors_all.mode = args.mode

Definition at line 56 of file compare_tensors_all.py.

◆ model_name

compare_tensors_all.model_name = inputs[idx][0]

Definition at line 160 of file compare_tensors_all.py.

◆ modes_to_expected_folder

dict compare_tensors_all.modes_to_expected_folder
Initial value:
1= {
2 'fake_quantization': 'fake_quantization',
3 'mixed_fake_quantization': 'fake_quantization',
4 'record_minmax': 'record_minmax',
5 'parallel_record_minmax': 'record_minmax',
6 'quantization': 'quantization',
7 'mixed_quantization': 'quantization',
8 'weights_only_quantization': 'wo_quantization'
9}

Definition at line 34 of file compare_tensors_all.py.

◆ modes_to_input_h5_suffix

dict compare_tensors_all.modes_to_input_h5_suffix
Initial value:
1= {
2 'fake_quantization': 'fake_quantized.circle.h5',
3 'mixed_fake_quantization': 'fake_quantized.mixed.circle.h5',
4 'record_minmax': 'minmax_recorded.circle.h5',
5 'parallel_record_minmax': 'parallel_minmax_recorded.circle.h5',
6 'quantization': 'quantized.circle.h5',
7 'mixed_quantization': 'quantized.mixed.circle.h5',
8 'weights_only_quantization': 'wo_quantized.circle.h5'
9}

Definition at line 43 of file compare_tensors_all.py.

◆ nargs

compare_tensors_all.nargs

Definition at line 19 of file compare_tensors_all.py.

◆ PARAM_SET_SIZE

int compare_tensors_all.PARAM_SET_SIZE = 3

Definition at line 156 of file compare_tensors_all.py.

◆ parser

compare_tensors_all.parser = argparse.ArgumentParser()

Definition at line 14 of file compare_tensors_all.py.

◆ required

compare_tensors_all.required

Definition at line 18 of file compare_tensors_all.py.

◆ source_dir

compare_tensors_all.source_dir = args.source_dir

Definition at line 55 of file compare_tensors_all.py.

◆ str

compare_tensors_all.str

◆ stream_handler

compare_tensors_all.stream_handler = logging.StreamHandler(stream=streamer)

Definition at line 64 of file compare_tensors_all.py.

◆ streamer

compare_tensors_all.streamer = StringIO()

Definition at line 63 of file compare_tensors_all.py.

◆ test_param

compare_tensors_all.test_param = args.test_param

Definition at line 53 of file compare_tensors_all.py.

◆ test_result

compare_tensors_all.test_result = False

Definition at line 171 of file compare_tensors_all.py.

◆ test_result_file

compare_tensors_all.test_result_file = os.path.join(bin_dir, testcase)

Definition at line 165 of file compare_tensors_all.py.

◆ testcase

str compare_tensors_all.testcase = f'{model_name}.{granularity}.{dtype}'

Definition at line 164 of file compare_tensors_all.py.

◆ True

compare_tensors_all.True

Definition at line 31 of file compare_tensors_all.py.

◆ type

compare_tensors_all.type

Definition at line 17 of file compare_tensors_all.py.