blob: e732f19d9e85b22abbda3ac5e1a243641985f6aa [file] [log] [blame]
Johan Alfven9070f0f2023-02-07 13:01:03 +01001# SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Tim Hall79d07d22020-04-27 18:20:16 +01002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Tim Hall79d07d22020-04-27 18:20:16 +010017# Description:
18# Functions used to read from a TensorFlow Lite format file.
Diego Russoea6111a2020-04-14 18:41:58 +010019import os.path
Henrik G Olssonea9b23c2021-03-23 17:34:49 +010020import struct
21import sys
Tim Hall79d07d22020-04-27 18:20:16 +010022
23import numpy as np
Tim Hall79d07d22020-04-27 18:20:16 +010024
Louis Verhaard678645b2020-06-15 15:22:47 +020025from .errors import InputFileError
Diego Russoe8a10452020-04-21 17:39:10 +010026from .nn_graph import Graph
27from .nn_graph import Subgraph
Louis Verhaarde8a5a782020-11-02 18:04:27 +010028from .operation import create_activation_function
Louis Verhaardaee5d752020-09-30 09:01:52 +020029from .operation import Op
Diego Russoea6111a2020-04-14 18:41:58 +010030from .operation import Operation
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020031from .reader_util import align_tensor_indices_to_nng
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020032from .reader_util import clone_and_reshape_tensor
33from .reader_util import decode_str
34from .reader_util import fixup_tensors
Johan Alfven9070f0f2023-02-07 13:01:03 +010035from .tensor import create_virtual_tensor
Diego Russoe8a10452020-04-21 17:39:10 +010036from .tensor import QuantizationParameters
37from .tensor import Tensor
38from .tflite.BuiltinOperator import BuiltinOperator
39from .tflite.Model import Model
40from .tflite_mapping import builtin_operator_map
41from .tflite_mapping import DataType
42from .tflite_mapping import datatype_map
43from .tflite_mapping import datatype_map_numpy
Tim Hall2180a172023-03-10 18:11:34 +000044from .tflite_mapping import optype_to_builtintype
Tim Hall79d07d22020-04-27 18:20:16 +010045
46
Tim Hall79d07d22020-04-27 18:20:16 +010047class TFLiteSubgraph:
48 def __init__(self, graph, subgraph):
49 self.graph = graph
50 self.name = decode_str(subgraph.Name())
51
52 self.tensors = []
53 for idx in range(subgraph.TensorsLength()):
54 self.tensors.append(self.parse_tensor(subgraph.Tensors(idx)))
55
Johan Alfven9070f0f2023-02-07 13:01:03 +010056 self.virtual_outputs = []
Tim Hall79d07d22020-04-27 18:20:16 +010057 for idx in range(subgraph.OperatorsLength()):
Tim Hallc8310b12020-06-17 14:53:11 +010058 self.parse_operator(idx, subgraph.Operators(idx))
Tim Hall79d07d22020-04-27 18:20:16 +010059
Tim Hallc8310b12020-06-17 14:53:11 +010060 self.outputs = self.get_tensors_from_indices_remove_duplicates(subgraph.OutputsAsNumpy(), "output")
61 self.inputs = self.get_tensors_from_indices_remove_duplicates(subgraph.InputsAsNumpy(), "input")
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020062 fixup_tensors(self.inputs, self.tensors)
Tim Hall79d07d22020-04-27 18:20:16 +010063
Johan Alfven9070f0f2023-02-07 13:01:03 +010064 self.outputs.extend(self.virtual_outputs)
65
Tim Hallc8310b12020-06-17 14:53:11 +010066 def get_tensors_from_indices_remove_duplicates(self, indices, warning_str):
67 tensors = []
68 for idx in indices:
69 tensor = self.tensors[idx]
70 if tensor not in tensors:
71 tensors.append(tensor)
72 else:
73 print(
74 "Warning: Subgraph {0} tensor ({1}) with idx = {2} already seen. Removing the duplicate.".format(
75 warning_str, tensor, idx
76 )
77 )
78
79 return tensors
80
Tim Hall79d07d22020-04-27 18:20:16 +010081 def parse_tensor(self, tens_data):
82 np_shape = tens_data.ShapeAsNumpy()
83 shape = list(np_shape) if type(np_shape) is np.ndarray else []
84 name = decode_str(tens_data.Name())
Dwight Lidmane05de452020-11-05 15:56:08 +010085 tens_dtype = tens_data.Type()
86 dtype = datatype_map[tens_dtype]
Tim Hall79d07d22020-04-27 18:20:16 +010087 tens = Tensor(shape, dtype, name)
Tim Hall79d07d22020-04-27 18:20:16 +010088 quant = tens_data.Quantization()
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +010089 tens.is_variable = tens_data.IsVariable()
Tim Hall79d07d22020-04-27 18:20:16 +010090
Tim Hall79d07d22020-04-27 18:20:16 +010091 tens.quantization = QuantizationParameters()
Tim Halle4e58e12020-05-08 09:50:21 +010092 if quant is not None:
Diego Russod0eee262020-04-23 18:14:37 +010093 tens.quantization.min = self.len1_array_to_scalar(quant.MinAsNumpy())
94 tens.quantization.max = self.len1_array_to_scalar(quant.MaxAsNumpy())
95 tens.quantization.scale_f32 = self.len1_array_to_scalar(quant.ScaleAsNumpy())
96 tens.quantization.zero_point = self.len1_array_to_scalar(quant.ZeroPointAsNumpy())
Fredrik Svedbergcc8569f2021-11-01 14:25:29 +010097 tens.quantization.quant_dim = quant.QuantizedDimension()
Tim Hall79d07d22020-04-27 18:20:16 +010098
99 if dtype == DataType.uint8:
100 tens.quantization.quant_min = 0
101 tens.quantization.quant_max = (1 << dtype.bits) - 1
Michael McGeaghf3e3ad72020-12-02 12:39:03 +0000102 elif dtype in (DataType.int8, DataType.int16, DataType.int32, DataType.int64):
Tim Hall79d07d22020-04-27 18:20:16 +0100103 tens.quantization.quant_min = -(1 << (dtype.bits - 1))
104 tens.quantization.quant_max = (1 << (dtype.bits - 1)) - 1
Tim Hall79d07d22020-04-27 18:20:16 +0100105
106 if tens.quantization.scale_f32 is None and tens.quantization.zero_point is None:
107 tens.quantization = None
108
109 tens.values = None
110 buf = self.graph.buffers[tens_data.Buffer()]
Louis Verhaardf4e12be2020-12-18 14:23:06 +0100111 if buf is not None:
112 np_dtype = datatype_map_numpy[tens_dtype]
113 if dtype == DataType.string:
114 tens.values = np.array(buf.view(np_dtype))
115 else:
116 tens.values = np.array(buf.view(np_dtype).reshape(shape))
Tim Hall79d07d22020-04-27 18:20:16 +0100117 return tens
118
Tim Hallc8310b12020-06-17 14:53:11 +0100119 def parse_operator(self, op_index, op_data):
wilisa010a7d5ee2023-04-13 17:05:09 +0000120 op_type, opt_serializer, custom_code, indices, version = self.graph.operator_codes[op_data.OpcodeIndex()]
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200121 inputs = [self.tensors[idx] if idx != -1 else None for idx in op_data.InputsAsNumpy()]
122 outputs = [self.tensors[idx] if idx != -1 else None for idx in op_data.OutputsAsNumpy()]
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100123 intermediates = []
124 if op_data.IntermediatesLength():
125 intermediates = [self.tensors[idx] if idx != -1 else None for idx in op_data.IntermediatesAsNumpy()]
126
Tim Hall79d07d22020-04-27 18:20:16 +0100127 name = "unknown_op_name"
128 if len(outputs):
129 name = outputs[0].name
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200130 inputs = align_tensor_indices_to_nng(op_type, indices, inputs)
Tim Hall79d07d22020-04-27 18:20:16 +0100131 op = Operation(op_type, name)
Tim Hallc8310b12020-06-17 14:53:11 +0100132 op.op_index = op_index
wilisa010a7d5ee2023-04-13 17:05:09 +0000133 op.version = version
Tim Hall79d07d22020-04-27 18:20:16 +0100134 op.inputs = inputs
135 op.outputs = outputs
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100136 op.intermediates = intermediates
Tim Hall79d07d22020-04-27 18:20:16 +0100137 for out in op.outputs:
138 out.ops = [op]
139
Johan Alfven9070f0f2023-02-07 13:01:03 +0100140 if op_type in (Op.AssignVariable, Op.CallOnce):
141 # All graph traversals are based on depth-first and the starting
142 # points are the subgraph output tensors. Because of this, operators
143 # like AssignVariable and CallOnce will not be visit when the
144 # graph is traversed and the ops are never handled. In order to
145 # fix that, the code base will have to be changed in several places.
146 # Until then this workaround is applied. A virtual output is added
147 # both to the operator and to the subgraph. By doing this the full
148 # graph is traversed correctly. The tensor is not used for anything
149 # else.
150 op.name = f"{op_type}_{op_index}"
151 tens = create_virtual_tensor(op.name)
152 op.set_output_tensor(tens)
153 self.virtual_outputs.append(tens)
154
Louis Verhaardaee5d752020-09-30 09:01:52 +0200155 if op.type.is_depthwise_conv2d_op() or op.type.is_conv2d_op() or op.type == Op.FullyConnected:
Johan Alfven37dbca22023-12-21 12:37:17 +0100156 # Reshape and add bias for ops with constant weights
157 # Do not modify ops with dynamic data since they will run on CPU
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200158 if inputs[1].values is not None:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200159 if op.type == Op.FullyConnected:
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100160 inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 0), False)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200161 else:
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100162 inputs[1] = clone_and_reshape_tensor(inputs[1], (1, 2, 3, 0), False)
Johan Alfven37dbca22023-12-21 12:37:17 +0100163
164 if op.type.needs_bias() and len(inputs) <= op_type.info.indices.biases[0]:
165 # No Bias tensor
166 inputs.append(None)
167 if inputs[-1] and inputs[-1].values is not None:
168 # Since bias tensor is used for both bias and scale,
169 # a clone with a unique equivalence_id is needed.
170 inputs[-1] = clone_and_reshape_tensor(inputs[-1], None, True)
Tim Hall79d07d22020-04-27 18:20:16 +0100171
172 if opt_serializer is not None:
Tim Hallc8310b12020-06-17 14:53:11 +0100173 op.attrs = opt_serializer.deserialize(op_data)
Tim Hall79d07d22020-04-27 18:20:16 +0100174
Johan Alfvén673683b2022-09-05 09:39:47 +0200175 if op_type == Op.While:
176 # Attach the actual nng subgraphs to the op
177 cond_subgraph_index = op.attrs["cond_subgraph_index"]
178 body_subgraph_index = op.attrs["body_subgraph_index"]
179 op.attrs["subgraph"] = (
180 self.graph.nng.subgraphs[cond_subgraph_index],
181 self.graph.nng.subgraphs[body_subgraph_index],
182 )
Johan Alfven9070f0f2023-02-07 13:01:03 +0100183 if op_type == Op.CallOnce:
184 # Attach the actual nng subgraphs to the op
185 init_subgraph_index = op.attrs["init_subgraph_index"]
186 op.attrs["subgraph"] = (self.graph.nng.subgraphs[init_subgraph_index],)
Johan Alfvén673683b2022-09-05 09:39:47 +0200187
Tim Hall2180a172023-03-10 18:11:34 +0000188 if op_type == Op.Reshape:
189 if "new_shape" in op.attrs["attribute_read_error"] and len(inputs) > 1:
190 # the "new_shape" attribute is optional if the new_shape tensor (inputs[1]) is specified. therefore,
191 # remove the attribute read error
192 op.attrs["attribute_read_error"].remove("new_shape")
Michael McGeagh7b245fd2020-07-31 12:50:57 +0100193
Louis Verhaardaee5d752020-09-30 09:01:52 +0200194 if op_type == Op.Cast:
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200195 # Cast op should have "in/out_data_type" attribs add if missing
196 if "in_data_type" not in op.attrs:
197 op.attrs["in_data_type"] = inputs[0].dtype
198 if "out_data_type" not in op.attrs:
199 op.attrs["out_data_type"] = outputs[0].dtype
200
Tim Hall79d07d22020-04-27 18:20:16 +0100201 if "stride_w" in op.attrs:
202 op.attrs["strides"] = (1, op.attrs["stride_h"], op.attrs["stride_w"], 1)
203 if "filter_width" in op.attrs:
204 op.attrs["ksize"] = (1, op.attrs["filter_height"], op.attrs["filter_width"], 1)
205 if "dilation_w_factor" in op.attrs:
206 op.attrs["dilation"] = (1, op.attrs["dilation_h_factor"], op.attrs["dilation_w_factor"], 1)
207 if "depth_multiplier" in op.attrs:
208 op.attrs["channel_multiplier"] = op.attrs["depth_multiplier"]
209
Fredrik Svedbergbdf09f92020-11-18 11:30:21 +0100210 if op_type == Op.DepthwiseConv2DBias and op.attrs["depth_multiplier"] == 0:
211 # The depth multiplier is implicit and is calculated as weight channels / ifm channels
212 # Note however that the weights have been reshaped above.
213 # The original value is cached above in channel_multiplier
214 op.attrs["depth_multiplier"] = op.weights.shape[2] // op.ifm.shape[-1]
215
Johan Alfven301ca602023-04-13 11:49:11 +0200216 # The fused_activation_function attribute needs to be retained so that the
217 # tflite_writer can correctly pass through operators that run on the CPU.
218 # This is because the operator activation attribute is later converted to an
219 # NpuActivation which treats None and ReLU the same, thereby making it difficult
220 # for the tflite_writer to recover the original activation function.
221 faf = op.attrs.get("fused_activation_function", None)
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100222 if faf is not None:
223 op.activation = create_activation_function(faf)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200224 if custom_code is not None:
225 op.attrs["custom_code"] = custom_code
Tim Hall79d07d22020-04-27 18:20:16 +0100226
Tim Hall2180a172023-03-10 18:11:34 +0000227 # finally, report any missing attributes that could not be read during deserialize()
228 attribute_read_error = op.attrs["attribute_read_error"]
229 if len(attribute_read_error) != 0:
230 print(
231 f"Warning: Could not read the following attributes from {optype_to_builtintype(op.type)}"
232 f" '{op.name}' {opt_serializer.name} field: {', '.join(attribute_read_error)}"
233 )
234
Diego Russod0eee262020-04-23 18:14:37 +0100235 @staticmethod
236 def len1_array_to_scalar(arr):
237 # The following flatbuffer quantisation fields all return a scalar value of 0 if they are not definied in
238 # the input buffer. This is represented in Vela by using None.
239 # Otherwise, the fields returned are a single or multi-element array. In which case, single element arrays
240 # are converted to scalars
241 if isinstance(arr, int) and arr == 0:
242 return None
243 if len(arr) == 1:
244 return arr[0]
245 return arr
246
Tim Hall79d07d22020-04-27 18:20:16 +0100247
248class TFLiteGraph:
Michael McGeagh6f725262020-12-03 15:21:36 +0000249 def __init__(self, filename, batch_size, feed_dict, output_node_names, initialisation_nodes):
Tim Hall79d07d22020-04-27 18:20:16 +0100250
251 self.op_times = {}
252 if batch_size is None:
253 batch_size = 1
254 self.batch_size = batch_size
255 self.name = os.path.splitext(os.path.basename(filename))[0]
256 self.initialisation_nodes = initialisation_nodes
257
258 with open(filename, "rb") as f:
259 buf = bytearray(f.read())
260
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100261 try:
262 parsing_step = "parsing root"
263 model = Model.GetRootAsModel(buf, 0)
Tim Hall79d07d22020-04-27 18:20:16 +0100264
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100265 parsing_step = "parsing buffers length"
266 self.buffers = []
Tim Hall2f18e172023-04-06 21:01:58 +0100267 if not model.BuffersIsNone():
268 for idx in range(model.BuffersLength()):
269 parsing_step = f"parsing buffer {idx}"
270 buffer = model.Buffers(idx)
271 buffer_data = self.parse_buffer(buffer)
272 # buffers can be either; empty, or contain no data (zero length), or contain data (non-zero length).
273 # when a buffer is None it means that it is either empty or zero length, and an empty buffer
274 # will have DataIsNone() equal to true.
275 # we should detect zero length buffers and report a warning because the TFLite semantics for these
276 # types of buffers changed in TensorFlow 2.11, whereby they could result in runtime errors
277 if buffer_data is None and not buffer.DataIsNone():
278 print(
279 f"Warning: Input TensorFlow Lite network contains a zero length buffer (index = {idx})"
280 f" which is semantically not empty. However, it will be treated as an empty buffer."
281 )
282
283 self.buffers.append(buffer_data)
Tim Hall79d07d22020-04-27 18:20:16 +0100284
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100285 parsing_step = "parsing operator codes length"
286 self.operator_codes = []
287 for idx in range(model.OperatorCodesLength()):
288 parsing_step = f"parsing operator code {idx}"
289 self.operator_codes.append(self.parse_operator_code(model.OperatorCodes(idx)))
Tim Hall79d07d22020-04-27 18:20:16 +0100290
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100291 parsing_step = "parsing subgraphs length"
292 self.subgraphs = []
Johan Alfvén673683b2022-09-05 09:39:47 +0200293
294 # Pre-allocate nng subgraphs - needed when parsing an operator and the operator
295 # has subgraph attributes.
296 self.nng = Graph(self.name, self.batch_size)
297 for idx in range(model.SubgraphsLength()):
298 sg = Subgraph()
299 self.nng.subgraphs.append(sg)
300
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100301 for idx in range(model.SubgraphsLength()):
302 parsing_step = f"parsing subgraph {idx}"
303 self.subgraphs.append(TFLiteSubgraph(self, model.Subgraphs(idx)))
Tim Hall79d07d22020-04-27 18:20:16 +0100304
Johan Alfvén673683b2022-09-05 09:39:47 +0200305 for idx, tflite_sg in enumerate(self.subgraphs):
306 sg = self.nng.subgraphs[idx]
307 sg.name = tflite_sg.name
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100308 sg.original_inputs = tflite_sg.inputs # Preserve the original input order
309 sg.output_tensors = tflite_sg.outputs
Johan Alfven9070f0f2023-02-07 13:01:03 +0100310 sg.virtual_outputs = tflite_sg.virtual_outputs
Tim Hall79d07d22020-04-27 18:20:16 +0100311
Henrik G Olssonea9b23c2021-03-23 17:34:49 +0100312 parsing_step = "parsing metadata length"
313 # Preserve the original metadata
314 for idx in range(model.MetadataLength()):
315 parsing_step = f"parsing metadata {idx}"
316 meta = model.Metadata(idx)
317 parsing_step = f"parsing metadata name of metadata {idx}"
318 name = meta.Name()
319 if name is not None:
320 parsing_step = f"parsing metadata {idx} ({name})"
321 buf_data = self.buffers[meta.Buffer()]
322 self.nng.metadata.append((name, buf_data))
323 except (struct.error, TypeError, RuntimeError) as e:
324 print(f'Error: Invalid tflite file. Got "{e}" while {parsing_step}.')
325 sys.exit(1)
Michael McGeagh22f74e12020-08-07 16:21:03 +0100326
Tim Hall79d07d22020-04-27 18:20:16 +0100327 def parse_buffer(self, buf_data):
328 if buf_data.DataLength() == 0:
329 return None
330 data = buf_data.DataAsNumpy()
331 return data
332
333 def parse_operator_code(self, code):
334 c = code.BuiltinCode()
Tim Hall42abec12021-02-04 21:31:57 +0000335 if c == 0:
336 c = code.DeprecatedBuiltinCode()
Tim Hallc30f4952020-06-15 20:47:35 +0100337 if c not in builtin_operator_map:
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000338 raise InputFileError(
339 self.name, f"The input file contains operator code '{c}' which is currently not supported"
340 )
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200341 op_type, ser, indices = builtin_operator_map[c]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200342 custom_code = None
Tim Hall79d07d22020-04-27 18:20:16 +0100343 if c == BuiltinOperator.CUSTOM:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200344 custom_code = decode_str(code.CustomCode())
wilisa010a7d5ee2023-04-13 17:05:09 +0000345 return op_type, ser, custom_code, indices, code.Version()
Tim Hall79d07d22020-04-27 18:20:16 +0100346
347
Michael McGeagh6f725262020-12-03 15:21:36 +0000348def read_tflite(filename, batch_size, feed_dict, output_node_names, initialisation_nodes):
Diego Russoea6111a2020-04-14 18:41:58 +0100349 tflite_graph = TFLiteGraph(filename, batch_size, feed_dict, output_node_names, initialisation_nodes)
Tim Hall79d07d22020-04-27 18:20:16 +0100350 nng = tflite_graph.nng
351 nng.refresh_after_modification()
352 return nng