Lines Matching refs:self

105     def __init__(self,  argument
134 self.tensor_flow_reference_version = (
147self.tensor_flow_reference_version += ("// Interpreter from {} version {} and revision {}.\n".form…
151 self.mins = randmin
152 self.maxs = randmax
154 self.bias_mins = bias_min
155 self.bias_maxs = bias_max
157 self.input_ch = in_ch
158 self.output_ch = out_ch
159 self.x_input = x_in
160 self.y_input = y_in
161 self.filter_x = w_x
162 self.filter_y = w_y
163 self.stride_x = stride_x
164 self.stride_y = stride_y
165 self.dilation_x = dilation_x
166 self.dilation_y = dilation_y
167 self.batches = batches
168 self.test_type = testtype
169 self.has_padding = pad
171 self.is_int16xint8 = int16xint8
174 self.out_activation_max = 6
175 self.out_activation_min = 0
178 self.out_activation_min = out_activation_min
180 self.out_activation_min = INT16_MIN if self.is_int16xint8 else INT8_MIN
182 self.out_activation_max = out_activation_max
184 self.out_activation_max = INT16_MAX if self.is_int16xint8 else INT8_MAX
187 self.generate_bias = generate_bias
189 self.generated_header_files = []
190 self.pregenerated_data_dir = self.PREGEN
192 self.config_data = "config_data.h"
194 self.testdataset = dataset
196 self.kernel_table_file = self.pregenerated_data_dir + self.testdataset + '/' + 'kernel.txt'
197 self.inputs_table_file = self.pregenerated_data_dir + self.testdataset + '/' + 'input.txt'
198 self.bias_table_file = self.pregenerated_data_dir + self.testdataset + '/' + 'bias.txt'
200 if self.has_padding:
201 self.padding = 'SAME'
203 self.padding = 'VALID'
205 self.regenerate_new_weights = regenerate_weights
206 self.regenerate_new_input = regenerate_input
207 self.regenerate_new_bias = regenerate_biases
208 self.schema_file = schema_file
210 self.headers_dir = self.OUTDIR + self.testdataset + '/'
211 os.makedirs(self.headers_dir, exist_ok=True)
213 self.model_path = "{}model_{}".format(self.headers_dir, self.testdataset)
214 self.model_path_tflite = self.model_path + '.tflite'
216 self.input_data_file_prefix = "input"
217 self.weight_data_file_prefix = "weights"
218 self.bias_data_file_prefix = "biases"
219 self.output_data_file_prefix = "output_ref"
221 def save_multiple_dim_array_in_txt(self, file, data): argument
225 def load_multiple_dim_array_from_txt(self, file): argument
231 def convert_tensor_np(self, tensor_in, converter, *qminmax): argument
242 def convert_tensor(self, tensor_in, converter, *qminmax): argument
260 … def get_randomized_data(self, dims, npfile, regenerate, decimals=0, minrange=None, maxrange=None): argument
262 minrange = self.mins
264 maxrange = self.maxs
277 self.save_multiple_dim_array_in_txt(npfile, data.numpy())
280 data = tf.convert_to_tensor(self.load_multiple_dim_array_from_txt(npfile))
283 def get_randomized_input_data(self, input_data, input_shape=None): argument
286 input_shape = [self.batches, self.y_input, self.x_input, self.input_ch]
290 input_data = self.get_randomized_data(input_shape,
291 self.inputs_table_file,
292 regenerate=self.regenerate_new_input)
295 def get_randomized_bias_data(self, biases): argument
297 if not self.generate_bias:
298 biases = tf.reshape(np.full([self.output_ch], 0), [self.output_ch])
300 biases = tf.reshape(biases, [self.output_ch])
302 biases = self.get_randomized_data([self.output_ch],
303 self.bias_table_file,
304 regenerate=self.regenerate_new_bias,
305 minrange=self.bias_mins,
306 maxrange=self.bias_maxs)
309 def format_output_file(self, file): argument
320 def write_c_header_wrapper(self): argument
322 filepath = self.headers_dir + filename
326 f.write(self.tensor_flow_reference_version)
327 while len(self.generated_header_files) > 0:
328 f.write('#include "{}"\n'.format(self.generated_header_files.pop()))
329 self.format_output_file(filepath)
331 def write_common_config(self, f, prefix): argument
335 f.write("#define {}_FILTER_X {}\n".format(prefix, self.filter_x))
336 f.write("#define {}_FILTER_Y {}\n".format(prefix, self.filter_y))
337 f.write("#define {}_STRIDE_X {}\n".format(prefix, self.stride_x))
338 f.write("#define {}_STRIDE_Y {}\n".format(prefix, self.stride_y))
339 f.write("#define {}_PAD_X {}\n".format(prefix, self.pad_x))
340 f.write("#define {}_PAD_Y {}\n".format(prefix, self.pad_y))
341 f.write("#define {}_OUTPUT_W {}\n".format(prefix, self.x_output))
342 f.write("#define {}_OUTPUT_H {}\n".format(prefix, self.y_output))
344 def write_c_common_header(self, f): argument
345 f.write(self.tensor_flow_reference_version)
348 def write_c_config_header(self, write_common_parameters=True) -> None: argument
349 filename = self.config_data
351 self.generated_header_files.append(filename)
352 filepath = self.headers_dir + filename
354 prefix = self.testdataset.upper()
358 self.write_c_common_header(f)
360 f.write("#define {}_OUT_CH {}\n".format(prefix, self.output_ch))
361 f.write("#define {}_IN_CH {}\n".format(prefix, self.input_ch))
362 f.write("#define {}_INPUT_W {}\n".format(prefix, self.x_input))
363 f.write("#define {}_INPUT_H {}\n".format(prefix, self.y_input))
364 … f.write("#define {}_DST_SIZE {}\n".format(prefix, self.x_output * self.y_output * self.output_ch *
365 self.batches))
366 … f.write("#define {}_INPUT_SIZE {}\n".format(prefix, self.x_input * self.y_input * self.input_ch))
367 … f.write("#define {}_OUT_ACTIVATION_MIN {}\n".format(prefix, self.out_activation_min))
368 … f.write("#define {}_OUT_ACTIVATION_MAX {}\n".format(prefix, self.out_activation_max))
369 f.write("#define {}_INPUT_BATCHES {}\n".format(prefix, self.batches))
370 self.format_output_file(filepath)
372 def get_data_file_name_info(self, name_prefix) -> (str, str): argument
374 filepath = self.headers_dir + filename
377 def generate_c_array(self, name, array, datatype="int8_t", const="const ") -> None: argument
392 filename, filepath = self.get_data_file_name_info(name)
393 self.generated_header_files.append(filename)
397 self.write_c_common_header(f)
400 … f.write(const + datatype + " " + self.testdataset + '_' + name + "[%d] =\n{\n" % size)
406 f.write(const + datatype + " *" + self.testdataset + '_' + name + " = NULL;\n")
407 self.format_output_file(filepath)
409 def set_output_dims_and_padding(self, output_x, output_y): argument
410 self.x_output = output_x
411 self.y_output = output_y
412 if self.has_padding:
414 filter_x = (self.filter_x - 1) * self.dilation_x + 1
415 filter_y = (self.filter_y - 1) * self.dilation_y + 1
417 pad_along_width = max((self.x_output - 1) * self.stride_x + filter_x - self.x_input, 0)
418 pad_along_height = max((self.y_output - 1) * self.stride_y + filter_y - self.y_input, 0)
421 self.pad_x = pad_left
422 self.pad_y = pad_top
424 self.pad_x = 0
425 self.pad_y = 0
428 def generate_data(self, input_data=None, weights=None, biases=None) -> None: argument
431 def quantize_scale(self, scale): argument
436 def get_calib_data_func(self, n_inputs, shape): argument
446 self.test_type))
450 …def convert_and_interpret(self, model, inttype, input_data=None, dataset_shape=None) -> Interprete… argument
462 representative_dataset_shape = (self.batches, self.y_input, self.x_input, self.input_ch)
466 representative_dataset = self.get_calib_data_func(n_inputs, representative_dataset_shape)
470 if self.is_int16xint8:
480 os.makedirs(os.path.dirname(self.model_path_tflite), exist_ok=True)
481 with open(self.model_path_tflite, "wb") as model:
484 interpreter = Interpreter(model_path=str(self.model_path_tflite),
489 (self.output_scale, self.output_zero_point) = output_details[0]['quantization']
493 (self.input_scale, self.input_zero_point) = input_details[0]['quantization']
500 def generate_json_from_template(self, argument
508 generated_json_file = self.model_path + '.json'
510 with open(self.json_template, 'r') as in_file, open(generated_json_file, 'w') as out_file:
513 for item, to_replace in self.json_replacements.items():
521 … data["buffers"][w_1_buffer_index]["data"] = self.to_bytes(weights_feature_data.numpy().ravel(), 1)
525 … data["buffers"][w_2_buffer_index]["data"] = self.to_bytes(weights_time_data.numpy().ravel(), 1)
527 … data["buffers"][w_2_buffer_index]["data"] = self.to_bytes(weights_time_data.numpy().ravel(), 2)
530 … data["buffers"][bias_buffer_index]["data"] = self.to_bytes(bias_data.numpy().ravel(), 4)
536 def flatc_generate_tflite(self, json_input, schema): argument
540 command = "{} -o {} -c -b {} {}".format(flatc, self.headers_dir, schema, json_input)
550 def to_bytes(self, tensor_data, type_size) -> bytes: argument
571 def __init__(self, argument
627 self.scaling_factors = []
629 if self.test_type == 'depthwise_conv':
630 self.channel_multiplier = self.output_ch // self.input_ch
631 if self.output_ch % self.input_ch != 0:
634 def write_c_config_header(self) -> None: argument
637 filename = self.config_data
638 filepath = self.headers_dir + filename
639 prefix = self.testdataset.upper()
642 self.write_common_config(f, prefix)
643 if self.test_type == 'depthwise_conv':
644 f.write("#define {}_CH_MULT {}\n".format(prefix, self.channel_multiplier))
645 f.write("#define {}_INPUT_OFFSET {}\n".format(prefix, -self.input_zero_point))
646 f.write("#define {}_OUTPUT_OFFSET {}\n".format(prefix, self.output_zero_point))
647 f.write("#define {}_DILATION_X {}\n".format(prefix, self.dilation_x))
648 f.write("#define {}_DILATION_Y {}\n".format(prefix, self.dilation_y))
650 def generate_quantize_per_channel_multiplier(self): argument
651 num_channels = self.output_ch
655 if len(self.scaling_factors) != num_channels:
659 effective_output_scale = self.input_scale * self.scaling_factors[i] / self.output_scale
660 (quantized_multiplier, shift) = self.quantize_scale(effective_output_scale)
667 def generate_data(self, input_data=None, weights=None, biases=None) -> None: argument
668 if self.is_int16xint8:
677 input_data = self.get_randomized_input_data(input_data)
679 if self.test_type == 'conv':
680 out_channel = self.output_ch
681 elif self.test_type == 'depthwise_conv':
682 out_channel = self.channel_multiplier
685 … weights = tf.reshape(weights, [self.filter_y, self.filter_x, self.input_ch, out_channel])
687 … weights = self.get_randomized_data([self.filter_y, self.filter_x, self.input_ch, out_channel],
688 self.kernel_table_file,
692 regenerate=self.regenerate_new_weights)
694 biases = self.get_randomized_bias_data(biases)
698 input_shape = (self.batches, self.y_input, self.x_input, self.input_ch)
699 model.add(tf.keras.layers.InputLayer(input_shape=input_shape[1:], batch_size=self.batches))
700 if self.test_type == 'conv':
701 conv_layer = tf.keras.layers.Conv2D(self.output_ch,
702 kernel_size=(self.filter_y, self.filter_x),
703 strides=(self.stride_y, self.stride_x),
704 padding=self.padding,
706 dilation_rate=(self.dilation_y, self.dilation_x))
709 elif self.test_type == 'depthwise_conv':
710 … depthwise_layer = tf.keras.layers.DepthwiseConv2D(kernel_size=(self.filter_y, self.filter_x),
711 … strides=(self.stride_y, self.stride_x),
712 padding=self.padding,
713 … depth_multiplier=self.channel_multiplier,
715 … dilation_rate=(self.dilation_y, self.dilation_x))
718 interpreter = self.convert_and_interpret(model, inttype, input_data)
724 … (self.generate_bias and biases.numpy().size != interpreter.get_tensor(bias_layer['index']).size):
728self.set_output_dims_and_padding(output_details[0]['shape'][2], output_details[0]['shape'][1])
730 self.generate_c_array(self.input_data_file_prefix, input_data, datatype=datatype)
731self.generate_c_array(self.weight_data_file_prefix, interpreter.get_tensor(filter_layer['index']))
733 self.scaling_factors = filter_layer['quantization_parameters']['scales']
734 per_channel_multiplier, per_channel_shift = self.generate_quantize_per_channel_multiplier()
735 self.generate_c_array("output_mult", per_channel_multiplier, datatype='int32_t')
736 self.generate_c_array("output_shift", per_channel_shift, datatype='int32_t')
738self.generate_c_array(self.bias_data_file_prefix, interpreter.get_tensor(bias_layer['index']), bia…
743 self.generate_c_array(self.output_data_file_prefix,
744 … np.clip(output_data, self.out_activation_min, self.out_activation_max),
747 self.write_c_config_header()
748 self.write_c_header_wrapper()
753 def __init__(self, argument
799 def generate_data(self, input_data=None) -> None: argument
800 if self.is_int16xint8:
807 input_data = self.get_randomized_input_data(input_data)
808 self.generate_c_array(self.input_data_file_prefix, input_data, datatype=datatype)
814 input_shape = (self.batches, self.y_input, self.x_input, self.input_ch)
815 model.add(tf.keras.layers.InputLayer(input_shape=input_shape[1:], batch_size=self.batches))
816 if self.test_type == 'avgpool':
818 tf.keras.layers.AveragePooling2D(pool_size=(self.filter_y, self.filter_x),
819 strides=(self.stride_y, self.stride_x),
820 padding=self.padding,
822 elif self.test_type == 'maxpool':
824 tf.keras.layers.MaxPooling2D(pool_size=(self.filter_y, self.filter_x),
825 strides=(self.stride_y, self.stride_x),
826 padding=self.padding,
831 interpreter = self.convert_and_interpret(model, inttype, input_data)
834self.set_output_dims_and_padding(output_details[0]['shape'][2], output_details[0]['shape'][1])
839 self.generate_c_array(self.output_data_file_prefix,
840 … np.clip(output_data, self.out_activation_min, self.out_activation_max),
843 self.write_c_config_header()
844 self.write_c_header_wrapper()
846 def write_c_config_header(self) -> None: argument
849 filename = self.config_data
850 filepath = self.headers_dir + filename
851 prefix = self.testdataset.upper()
854 self.write_common_config(f, prefix)
859 def __init__(self, argument
909 def write_c_config_header(self) -> None: argument
912 filename = self.config_data
913 filepath = self.headers_dir + filename
914 prefix = self.testdataset.upper()
917 f.write("#define {}_OUTPUT_MULTIPLIER {}\n".format(prefix, self.quantized_multiplier))
918 f.write("#define {}_OUTPUT_SHIFT {}\n".format(prefix, self.quantized_shift))
919 ….write("#define {}_ACCUMULATION_DEPTH {}\n".format(prefix, self.input_ch * self.x_input * self.y_i…
920 f.write("#define {}_INPUT_OFFSET {}\n".format(prefix, -self.input_zero_point))
921 f.write("#define {}_OUTPUT_OFFSET {}\n".format(prefix, self.output_zero_point))
923 def quantize_multiplier(self): argument
924 input_product_scale = self.input_scale * self.weights_scale
927 real_multipler = input_product_scale / self.output_scale
928 (self.quantized_multiplier, self.quantized_shift) = self.quantize_scale(real_multipler)
930 def generate_data(self, input_data=None, weights=None, biases=None) -> None: argument
931 input_data = self.get_randomized_input_data(input_data,
932 … [self.batches, self.input_ch * self.x_input * self.y_input])
934 if self.is_int16xint8:
943 fc_weights_format = [self.input_ch * self.y_input * self.x_input, self.output_ch]
948 weights = self.get_randomized_data(fc_weights_format,
949 self.kernel_table_file,
952 regenerate=self.regenerate_new_weights)
954 biases = self.get_randomized_bias_data(biases)
959 tf.keras.layers.InputLayer(input_shape=(self.y_input * self.x_input * self.input_ch, ),
960 batch_size=self.batches))
961 fully_connected_layer = tf.keras.layers.Dense(self.output_ch, activation=None)
965 interpreter = self.convert_and_interpret(model, inttype, input_data)
968 if self.generate_bias:
974 … (self.generate_bias and biases.numpy().size != interpreter.get_tensor(bias_layer['index']).size):
979 self.x_output = 1
980 self.y_output = 1
982 …if self.output_ch != output_details[0]['shape'][1] or self.batches != output_details[0]['shape'][0…
985 self.weights_scale = filter_layer['quantization_parameters']['scales'][0]
986 self.quantize_multiplier()
988 self.generate_c_array(self.input_data_file_prefix, input_data, datatype=datatype)
989self.generate_c_array(self.weight_data_file_prefix, interpreter.get_tensor(filter_layer['index']))
991 if self.generate_bias:
992self.generate_c_array(self.bias_data_file_prefix, interpreter.get_tensor(bias_layer['index']),
995 self.generate_c_array(self.bias_data_file_prefix, biases, bias_datatype)
1000 self.generate_c_array(self.output_data_file_prefix,
1001 … np.clip(output_data, self.out_activation_min, self.out_activation_max),
1004 self.write_c_config_header()
1005 self.write_c_header_wrapper()
1011 def __init__(self, argument
1044 self.x_input = self.x_output = x_in
1045 self.y_input = self.y_output = y_in
1046 self.inInt8outInt16 = inInt8outInt16
1048 if self.inInt8outInt16 and self.is_int16xint8:
1051 if self.inInt8outInt16:
1052 self.input_scale = input_scale
1053 self.json_template = "TestCases/Common/Softmax/softmax_int8_to_int16_template.json"
1054 self.json_replacements = {
1055 "num_rows": self.y_input,
1056 "row_size": self.x_input,
1061 def calc_softmax_params(self): argument
1062 if self.is_int16xint8:
1063 input_scale_beta_rescale = self.input_scale / (10.0 / 65535.0)
1064 … (self.input_multiplier, self.input_left_shift) = self.quantize_scale(input_scale_beta_rescale)
1066 …input_real_multiplier = min(self.input_scale * (1 << (31 - self.softmax_input_integer_bits)), (1 <…
1067 … (self.input_multiplier, self.input_left_shift) = self.quantize_scale(input_real_multiplier)
1069 self.diff_min = ((1 << self.softmax_input_integer_bits) - 1) * \
1070 (1 << (31 - self.softmax_input_integer_bits)) / \
1071 (1 << self.input_left_shift)
1072 self.diff_min = math.floor(self.diff_min)
1074 def write_c_config_header(self) -> None: argument
1077 filename = self.config_data
1078 filepath = self.headers_dir + filename
1079 prefix = self.testdataset.upper()
1082 f.write("#define {}_NUM_ROWS {}\n".format(prefix, self.y_input))
1083 f.write("#define {}_ROW_SIZE {}\n".format(prefix, self.x_input))
1084 f.write("#define {}_INPUT_MULT {}\n".format(prefix, self.input_multiplier))
1085 f.write("#define {}_INPUT_LEFT_SHIFT {}\n".format(prefix, self.input_left_shift))
1086 if not self.is_int16xint8:
1087 f.write("#define {}_DIFF_MIN {}\n".format(prefix, -self.diff_min))
1088 f.write("#define {}_DST_SIZE {}\n".format(prefix, self.x_output * self.y_output))
1090 def get_softmax_randomized_input_data(self, input_data, input_shape): argument
1095 input_data = self.get_randomized_data(input_shape,
1096 self.inputs_table_file,
1097 regenerate=self.regenerate_new_input)
1100 def generate_data(self, input_data=None, weights=None, biases=None) -> None: argument
1101 … input_data = self.get_softmax_randomized_input_data(input_data, [self.y_input, self.x_input])
1103 if self.is_int16xint8:
1110 self.generate_c_array(self.input_data_file_prefix, input_data, datatype=datatype)
1113 if self.inInt8outInt16:
1119 generated_json = self.generate_json_from_template()
1120 self.flatc_generate_tflite(generated_json, self.schema_file)
1122 interpreter = Interpreter(model_path=str(self.model_path_tflite),
1135 input_shape = (self.y_input, self.x_input)
1138 … interpreter = self.convert_and_interpret(model, inttype, tf.expand_dims(input_data, axis=0))
1143 self.calc_softmax_params()
1144 self.generate_c_array(self.output_data_file_prefix, output_data, datatype=datatype)
1146 self.write_c_config_header()
1147 self.write_c_header_wrapper()
1152 def __init__(self, argument
1199 self.batches = batches
1200 self.number_units = number_units
1201 self.input_size = input_size
1202 self.memory_size = memory_size
1203 self.rank = rank
1204 self.number_filters = self.number_units * self.rank
1205 self.time_table_file = self.pregenerated_data_dir + self.testdataset + '/' + 'time_data.txt'
1207 self.number_inputs = number_inputs
1208 self.input_sequence_length = self.number_inputs * self.input_size * self.batches
1210 self.in_activation_max = INT16_MAX
1211 self.in_activation_min = INT16_MIN
1213 self.int8_time_weights = int8_time_weights
1215 if self.int8_time_weights:
1216 self.json_template = "TestCases/Common/svdf_s8_weights_template.json"
1218 self.json_template = "TestCases/Common/svdf_template.json"
1220 self.json_replacements = {
1221 "memory_sizeXnumber_filters": self.memory_size * self.number_filters,
1222 "batches": self.batches,
1223 "input_size": self.input_size,
1224 "number_filters": self.number_filters,
1225 "memory_size": self.memory_size,
1226 "number_units": self.number_units,
1227 "rank_value": self.rank,
1242 …def calc_multipliers_and_shifts(self, input_scale, weights_1_scale, weights_2_scale, state_scale, … argument
1245 (self.multiplier_in, self.shift_1) = self.quantize_scale(effective_scale_1)
1246 (self.multiplier_out, self.shift_2) = self.quantize_scale(effective_scale_2)
1248 def write_c_config_header(self) -> None: argument
1251 filename = self.config_data
1252 filepath = self.headers_dir + filename
1253 prefix = self.testdataset.upper()
1256 f.write("#define {}_MULTIPLIER_IN {}\n".format(prefix, self.multiplier_in))
1257 f.write("#define {}_MULTIPLIER_OUT {}\n".format(prefix, self.multiplier_out))
1258 f.write("#define {}_SHIFT_1 {}\n".format(prefix, self.shift_1))
1259 f.write("#define {}_SHIFT_2 {}\n".format(prefix, self.shift_2))
1260 f.write("#define {}_IN_ACTIVATION_MIN {}\n".format(prefix, self.in_activation_min))
1261 f.write("#define {}_IN_ACTIVATION_MAX {}\n".format(prefix, self.in_activation_max))
1262 f.write("#define {}_RANK {}\n".format(prefix, self.rank))
1263 f.write("#define {}_FEATURE_BATCHES {}\n".format(prefix, self.number_filters))
1264 f.write("#define {}_TIME_BATCHES {}\n".format(prefix, self.memory_size))
1265 f.write("#define {}_INPUT_SIZE {}\n".format(prefix, self.input_size))
1266 f.write("#define {}_DST_SIZE {}\n".format(prefix, self.number_units * self.batches))
1267 f.write("#define {}_OUT_ACTIVATION_MIN {}\n".format(prefix, self.out_activation_min))
1268 f.write("#define {}_OUT_ACTIVATION_MAX {}\n".format(prefix, self.out_activation_max))
1269 f.write("#define {}_INPUT_BATCHES {}\n".format(prefix, self.batches))
1270 f.write("#define {}_INPUT_OFFSET {}\n".format(prefix, self.input_zero_point))
1271 f.write("#define {}_OUTPUT_OFFSET {}\n".format(prefix, self.output_zero_point))
1273 …def generate_data(self, input_data=None, weights=None, biases=None, time_data=None, state_data=Non… argument
1275 input_data = tf.reshape(input_data, [self.input_sequence_length])
1277 input_data = self.get_randomized_data([self.input_sequence_length],
1278 self.inputs_table_file,
1279 regenerate=self.regenerate_new_input)
1280 self.generate_c_array("input_sequence", input_data)
1283 weights_feature_data = tf.reshape(weights, [self.number_filters, self.input_size])
1285 weights_feature_data = self.get_randomized_data([self.number_filters, self.input_size],
1286 self.kernel_table_file,
1287 regenerate=self.regenerate_new_weights)
1290 weights_time_data = tf.reshape(time_data, [self.number_filters, self.memory_size])
1292 weights_time_data = self.get_randomized_data([self.number_filters, self.memory_size],
1293 self.time_table_file,
1294 regenerate=self.regenerate_new_weights)
1296 if not self.generate_bias:
1297 biases = [0] * self.number_units
1299 biases = tf.reshape(biases, [self.number_units])
1301 biases = self.get_randomized_data([self.number_units],
1302 self.bias_table_file,
1303 regenerate=self.regenerate_new_weights)
1306 generated_json = self.generate_json_from_template(weights_feature_data,
1309 self.int8_time_weights)
1310 self.flatc_generate_tflite(generated_json, self.schema_file)
1313 interpreter = Interpreter(model_path=str(self.model_path_tflite),
1325 (input_scale, self.input_zero_point) = self.get_scale_and_zp(input_layer)
1326 (weights_1_scale, zero_point) = self.get_scale_and_zp(weights_1_layer)
1327 (weights_2_scale, zero_point) = self.get_scale_and_zp(weights_2_layer)
1328 (bias_scale, zero_point) = self.get_scale_and_zp(bias_layer)
1329 (state_scale, zero_point) = self.get_scale_and_zp(state_layer)
1330 (output_scale, self.output_zero_point) = self.get_scale_and_zp(output_layer)
1332self.calc_multipliers_and_shifts(input_scale, weights_1_scale, weights_2_scale, state_scale, outpu…
1335 self.generate_c_array("weights_feature", interpreter.get_tensor(weights_1_layer['index']))
1336self.generate_c_array(self.bias_data_file_prefix, interpreter.get_tensor(bias_layer['index']), "in…
1338 if self.int8_time_weights:
1339self.generate_c_array("weights_time", interpreter.get_tensor(weights_2_layer['index']), datatype='…
1340 self.generate_c_array("state", interpreter.get_tensor(state_layer['index']), "int8_t")
1342self.generate_c_array("weights_time", interpreter.get_tensor(weights_2_layer['index']), datatype='…
1343 self.generate_c_array("state", interpreter.get_tensor(state_layer['index']), "int16_t")
1346 if self.int8_time_weights:
1347 self.write_c_config_header()
1348 self.write_c_header_wrapper()
1353 for i in range(self.number_inputs):
1354 start = i * self.input_size * self.batches
1355 end = i * self.input_size * self.batches + self.input_size * self.batches
1357 input_sequence = tf.reshape(input_sequence, [self.batches, self.input_size])
1361 self.generate_c_array(self.output_data_file_prefix, svdf_ref)
1363 self.write_c_config_header()
1364 self.write_c_header_wrapper()
1366 def get_scale_and_zp(self, layer): argument
1372 def __init__(self, argument
1412 self.x_input = self.x_output = x_in
1413 self.y_input = self.y_output = y_in
1414 self.decimal_input = decimal_input
1416 self.left_shift = 15 if self.is_int16xint8 else 20
1418 def generate_data(self, input_data1=None, input_data2=None) -> None: argument
1419 input_shape = (1, self.y_input, self.x_input, self.input_ch)
1421 input_data1 = self.get_randomized_data(list(input_shape),
1422 self.inputs_table_file,
1423 regenerate=self.regenerate_new_input,
1424 decimals=self.decimal_input)
1425 input_data2 = self.get_randomized_data(list(input_shape),
1426 self.kernel_table_file,
1427 regenerate=self.regenerate_new_weights,
1428 decimals=self.decimal_input)
1430 if self.is_int16xint8:
1440 if self.test_type == 'add':
1442 elif self.test_type == 'mul':
1449 interpreter = self.convert_and_interpret(model, inttype_tf)
1456 (input1_scale, self.input1_zero_point) = input_details[0]['quantization']
1457 (input2_scale, self.input2_zero_point) = input_details[1]['quantization']
1458 self.input1_zero_point = -self.input1_zero_point
1459 self.input2_zero_point = -self.input2_zero_point
1461 … (self.input1_mult, self.input1_shift) = self.quantize_scale(input1_scale / double_max_input_scale)
1462 … (self.input2_mult, self.input2_shift) = self.quantize_scale(input2_scale / double_max_input_scale)
1464 if self.test_type == 'add':
1465 … actual_output_scale = double_max_input_scale / ((1 << self.left_shift) * self.output_scale)
1466 elif self.test_type == 'mul':
1467 actual_output_scale = input1_scale * input2_scale / self.output_scale
1468 (self.output_mult, self.output_shift) = self.quantize_scale(actual_output_scale)
1474 self.generate_c_array("input1", input_data1, datatype=inttype)
1475 self.generate_c_array("input2", input_data2, datatype=inttype)
1476 self.generate_c_array(self.output_data_file_prefix,
1477 … np.clip(output_data, self.out_activation_min, self.out_activation_max),
1480 self.write_c_config_header()
1481 self.write_c_header_wrapper()
1483 def write_c_config_header(self) -> None: argument
1486 filename = self.config_data
1487 filepath = self.headers_dir + filename
1488 prefix = self.testdataset.upper()
1492self.batches * self.y_input * self.x_input * self.input_ch))
1493 f.write("#define {}_OUT_ACTIVATION_MIN {}\n".format(prefix, self.out_activation_min))
1494 f.write("#define {}_OUT_ACTIVATION_MAX {}\n".format(prefix, self.out_activation_max))
1495 f.write("#define {}_INPUT1_OFFSET {}\n".format(prefix, self.input1_zero_point))
1496 f.write("#define {}_INPUT2_OFFSET {}\n".format(prefix, self.input2_zero_point))
1497 f.write("#define {}_OUTPUT_MULT {}\n".format(prefix, self.output_mult))
1498 f.write("#define {}_OUTPUT_SHIFT {}\n".format(prefix, self.output_shift))
1499 f.write("#define {}_OUTPUT_OFFSET {}\n".format(prefix, self.output_zero_point))
1500 if self.test_type == 'add':
1501 f.write("#define {}_LEFT_SHIFT {}\n".format(prefix, self.left_shift))
1502 f.write("#define {}_INPUT1_SHIFT {}\n".format(prefix, self.input1_shift))
1503 f.write("#define {}_INPUT2_SHIFT {}\n".format(prefix, self.input2_shift))
1504 f.write("#define {}_INPUT1_MULT {}\n".format(prefix, self.input1_mult))
1505 f.write("#define {}_INPUT2_MULT {}\n".format(prefix, self.input2_mult))
1510 def __init__(self, argument
1544 self.batches = batches
1545 self.time_steps = time_steps
1546 self.number_units = number_units
1547 self.number_inputs = number_inputs
1549self.kernel_hidden_table_file = self.pregenerated_data_dir + self.testdataset + '/' + 'kernel_hidd…
1551 self.time_major = time_major
1553 self.in_activation_max = INT16_MAX
1554 self.in_activation_min = INT16_MIN
1556 self.lstm_scales = []
1559 self.output_gate_bias_index = 1
1560 self.cell_gate_bias_index = 2
1561 self.forget_gate_bias_index = 3
1562 self.input_gate_bias_index = 4
1563 self.recurrent_input_to_output_w_index = 5
1564 self.recurrent_input_to_cell_w_index = 6
1565 self.recurrent_input_to_forget_w_index = 7
1566 self.recurrent_input_to_input_w_index = 8
1567 self.input_to_output_w_index = 9
1568 self.input_to_cell_w_index = 10
1569 self.input_to_forget_w_index = 11
1570 self.input_to_input_w_index = 12
1571 self.output_state_index = 13
1572 self.cell_state_index = 14
1573 self.input_norm_coeff_index = 15
1574 self.forget_norm_coeff_index = 16
1575 self.cell_norm_coeff_index = 17
1576 self.output_norm_coeff_index = 18
1577 self.effective_hidden_scale_intermediate_index = 20
1579 … def generate_data(self, input_data=None, weights=None, hidden_weights=None, biases=None) -> None: argument
1581 input_dims = [self.batches, self.time_steps, self.number_inputs]
1585 input_data = self.get_randomized_data(input_dims,
1586 self.inputs_table_file,
1587 regenerate=self.regenerate_new_input)
1590 number_cells = self.number_units
1596 weights = tf.reshape(weights, [self.number_inputs, number_cells * number_w_b])
1598 weights = self.get_randomized_data([self.number_inputs, number_cells * number_w_b],
1599 self.kernel_table_file,
1600 regenerate=self.regenerate_new_weights,
1608 hidden_weights = self.get_randomized_data([number_cells, number_cells * number_w_b],
1609 self.kernel_hidden_table_file,
1610 regenerate=self.regenerate_new_weights,
1614 if not self.generate_bias:
1619 biases = self.get_randomized_data([number_cells * number_w_b],
1620 self.bias_table_file,
1621 regenerate=self.regenerate_new_bias,
1627 input_layer = tf.keras.layers.Input(shape=(self.time_steps, self.number_inputs),
1628 batch_size=self.batches,
1630 if self.time_major:
1632 lstm_layer = tf.keras.layers.LSTM(units=self.number_units,
1633 time_major=self.time_major,
1636 lstm_layer = tf.keras.layers.LSTM(units=self.number_units,
1637 time_major=self.time_major,
1641 if self.time_major:
1643 shape = (self.time_steps, self.batches, self.number_inputs)
1646 shape = (self.batches, self.time_steps, self.number_inputs)
1656 interpreter = self.convert_and_interpret(model, tf.int8, input_data, dataset_shape=shape)
1661 self.lstm_scales.append(i['quantization_parameters']['scales'])
1665 input_gate_bias = all_layers_details[self.input_gate_bias_index + time_major_offset]
1666 forget_gate_bias = all_layers_details[self.forget_gate_bias_index + time_major_offset]
1667 cell_gate_bias = all_layers_details[self.cell_gate_bias_index + time_major_offset]
1668 output_gate_bias = all_layers_details[self.output_gate_bias_index + time_major_offset]
1670 input_to_input_w = all_layers_details[self.input_to_input_w_index + time_major_offset]
1671 input_to_forget_w = all_layers_details[self.input_to_forget_w_index + time_major_offset]
1672 input_to_cell_w = all_layers_details[self.input_to_cell_w_index + time_major_offset]
1673 input_to_output_w = all_layers_details[self.input_to_output_w_index + time_major_offset]
1675 …recurrent_input_to_input_w = all_layers_details[self.recurrent_input_to_input_w_index + time_major…
1676 …recurrent_input_to_forget_w = all_layers_details[self.recurrent_input_to_forget_w_index + time_maj…
1677 …recurrent_input_to_cell_w = all_layers_details[self.recurrent_input_to_cell_w_index + time_major_o…
1678 …recurrent_input_to_output_w = all_layers_details[self.recurrent_input_to_output_w_index + time_maj…
1680 if self.time_major:
1683 output_state = all_layers_details[self.output_state_index + time_major_offset]
1684 cell_state = all_layers_details[self.cell_state_index + time_major_offset]
1686 input_norm_coeff = all_layers_details[self.input_norm_coeff_index + time_major_offset]
1687 forget_norm_coeff = all_layers_details[self.forget_norm_coeff_index + time_major_offset]
1688 cell_norm_coeff = all_layers_details[self.cell_norm_coeff_index + time_major_offset]
1689 output_norm_coeff = all_layers_details[self.output_norm_coeff_index + time_major_offset]
1692 …effective_hidden_scale_intermediate = all_layers_details[self.effective_hidden_scale_intermediate_…
1702self.generate_c_array(self.input_data_file_prefix, interpreter.get_tensor(input_data_for_index['in…
1703 self.generate_c_array("input_to_input_w", interpreter.get_tensor(input_to_input_w['index']))
1704self.generate_c_array("input_to_forget_w", interpreter.get_tensor(input_to_forget_w['index']))
1705 self.generate_c_array("input_to_cell_w", interpreter.get_tensor(input_to_cell_w['index']))
1706self.generate_c_array("input_to_output_w", interpreter.get_tensor(input_to_output_w['index']))
1707self.generate_c_array("recurrent_input_to_input_w", interpreter.get_tensor(recurrent_input_to_inpu…
1708 self.generate_c_array("recurrent_input_to_forget_w",
1710self.generate_c_array("recurrent_input_to_cell_w", interpreter.get_tensor(recurrent_input_to_cell_…
1711 self.generate_c_array("recurrent_input_to_output_w",
1715 self.generate_c_array("cell_to_input", [], datatype='int16_t')
1716 self.generate_c_array("cell_to_forget", [], datatype='int16_t')
1717 self.generate_c_array("cell_to_output", [], datatype='int16_t')
1719self.generate_c_array("input_gate_bias", interpreter.get_tensor(input_gate_bias['index']), datatyp…
1720self.generate_c_array("cell_gate_bias", interpreter.get_tensor(cell_gate_bias['index']), datatype=…
1721self.generate_c_array("forget_gate_bias", interpreter.get_tensor(forget_gate_bias['index']), datat…
1722self.generate_c_array("output_gate_bias", interpreter.get_tensor(output_gate_bias['index']), datat…
1725 self.generate_c_array("projection_weights", [])
1726 self.generate_c_array("projection_bias", [], datatype='int32_t')
1728self.generate_c_array("output_state", interpreter.get_tensor(output_state['index']), const="")
1729self.generate_c_array("cell_state", interpreter.get_tensor(cell_state['index']), datatype='int16_t…
1731 self.generate_c_array("input_norm_coeff", interpreter.get_tensor(input_norm_coeff['index']))
1732self.generate_c_array("forget_norm_coeff", interpreter.get_tensor(forget_norm_coeff['index']))
1733 self.generate_c_array("cell_norm_coeff", interpreter.get_tensor(cell_norm_coeff['index']))
1734self.generate_c_array("output_norm_coeff", interpreter.get_tensor(output_norm_coeff['index']))
1742self.hidden_zp = effective_hidden_scale_intermediate['quantization_parameters']['zero_points'][0]
1743 self.output_state_offset = output_state_zp
1746 self.cell_state_shift = int(round(tmp))
1748 self.calc_scales(input_scale, output_state_scale)
1754 …input_to_forget_eff_bias = self.calc_effective_bias(interpreter, input_zp, input_to_forget_w, forg…
1755 recurrent_to_forget_eff_bias = self.calc_effective_bias(interpreter, output_state_zp,
1757 …input_to_cell_eff_bias = self.calc_effective_bias(interpreter, input_zp, input_to_cell_w, cell_gat…
1758 …recurrent_to_cell_eff_bias = self.calc_effective_bias(interpreter, output_state_zp, recurrent_inpu…
1760 …input_to_output_eff_bias = self.calc_effective_bias(interpreter, input_zp, input_to_output_w, outp…
1761 recurrent_to_output_eff_bias = self.calc_effective_bias(interpreter, output_state_zp,
1763 …input_to_input_eff_bias = self.calc_effective_bias(interpreter, input_zp, input_to_input_w, input_…
1765 …recurrent_to_input_eff_bias = self.calc_effective_bias(interpreter, output_state_zp, recurrent_inp…
1768self.generate_c_array("input_to_input_eff_bias", input_to_input_eff_bias, datatype='int32_t')
1769self.generate_c_array("input_to_forget_eff_bias", input_to_forget_eff_bias, datatype='int32_t')
1770 self.generate_c_array("input_to_cell_eff_bias", input_to_cell_eff_bias, datatype='int32_t')
1771self.generate_c_array("input_to_output_eff_bias", input_to_output_eff_bias, datatype='int32_t')
1772self.generate_c_array("recurrent_to_input_eff_bias", recurrent_to_input_eff_bias, datatype='int32_…
1773self.generate_c_array("recurrent_to_cell_eff_bias", recurrent_to_cell_eff_bias, datatype='int32_t')
1774self.generate_c_array("recurrent_to_forget_eff_bias", recurrent_to_forget_eff_bias, datatype='int3…
1775self.generate_c_array("recurrent_to_output_eff_bias", recurrent_to_output_eff_bias, datatype='int3…
1780 self.generate_c_array(self.output_data_file_prefix, output_data, datatype='int8_t')
1782 self.write_c_config_header()
1783 self.write_c_header_wrapper()
1785 def calc_scales(self, input_scale, output_state_scale): argument
1788 if self.time_major:
1793 self.effective_hidden_scale = pow(2, -15) / output_state_scale * pow(2, -15)
1795self.i2i_effective_scale = input_scale * self.lstm_scales[self.input_to_input_w_index + time_major…
1797self.i2f_effective_scale = input_scale * self.lstm_scales[self.input_to_forget_w_index + time_majo…
1799self.i2c_effective_scale = input_scale * self.lstm_scales[self.input_to_cell_w_index + time_major_…
1801self.i2o_effective_scale = input_scale * self.lstm_scales[self.input_to_output_w_index + time_majo…
1804self.r2i_effective_scale = output_state_scale * self.lstm_scales[self.recurrent_input_to_input_w_i…
1806self.r2f_effective_scale = output_state_scale * self.lstm_scales[self.recurrent_input_to_forget_w_…
1808self.r2c_effective_scale = output_state_scale * self.lstm_scales[self.recurrent_input_to_cell_w_in…
1810self.r2o_effective_scale = output_state_scale * self.lstm_scales[self.recurrent_input_to_output_w_…
1813 …def calc_effective_bias(self, interpreter, zero_point, weight_tensor, bias_tensor, has_bias=True) … argument
1834 def write_c_config_header(self) -> None: argument
1837 filename = self.config_data
1838 filepath = self.headers_dir + filename
1839 prefix = self.testdataset.upper()
1842 f.write("#define {}_BUFFER_SIZE {}\n".format(prefix, self.batches * self.number_units))
1843 f.write("#define {}_INPUT_BATCHES {}\n".format(prefix, self.batches))
1844 …f.write("#define {}_DST_SIZE {}\n".format(prefix, self.batches * self.time_steps * self.number_uni…
1845 f.write("#define {}_TIME_STEPS {}\n".format(prefix, self.time_steps))
1846 f.write("#define {}_NUMBER_UNITS {}\n".format(prefix, self.number_units))
1847 f.write("#define {}_NUMBER_INPUTS {}\n".format(prefix, self.number_inputs))
1848 f.write("#define {}_TIME_MAJOR {}\n".format(prefix, int(self.time_major)))
1849 f.write("#define {}_IN_ACTIVATION_MIN {}\n".format(prefix, self.in_activation_min))
1850 f.write("#define {}_IN_ACTIVATION_MAX {}\n".format(prefix, self.in_activation_max))
1852 (multiplier, shift) = self.quantize_scale(self.i2i_effective_scale)
1855 (multiplier, shift) = self.quantize_scale(self.i2f_effective_scale)
1858 (multiplier, shift) = self.quantize_scale(self.i2c_effective_scale)
1861 (multiplier, shift) = self.quantize_scale(self.i2o_effective_scale)
1865 (multiplier, shift) = self.quantize_scale(self.r2i_effective_scale)
1868 (multiplier, shift) = self.quantize_scale(self.r2f_effective_scale)
1871 (multiplier, shift) = self.quantize_scale(self.r2c_effective_scale)
1874 (multiplier, shift) = self.quantize_scale(self.r2o_effective_scale)
1878 (multiplier, shift) = self.quantize_scale(self.effective_hidden_scale)
1882 f.write("#define {}_HIDDEN_OFFSET {}\n".format(prefix, self.hidden_zp))
1884 f.write("#define {}_OUTPUT_STATE_OFFSET {}\n".format(prefix, self.output_state_offset))
1885 f.write("#define {}_CELL_STATE_SHIFT {}\n".format(prefix, self.cell_state_shift))
1887 for i in range(len(self.lstm_scales)):
1888 if len(self.lstm_scales[i]) == 0:
1890 (multiplier, shift) = self.quantize_scale(self.lstm_scales[i][0])