blob: d4e24a208a154235d098ced23529f23752503921 [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 write to a TensorFlow Lite format file. Supports adding in file identifiers.
Tim Hall79d07d22020-04-27 18:20:16 +010019import flatbuffers
Diego Russoe8a10452020-04-21 17:39:10 +010020import flatbuffers.number_types as N
21import numpy as np
22from flatbuffers import encode
Diego Russoea6111a2020-04-14 18:41:58 +010023from flatbuffers.builder import UOffsetTFlags
24
William Isakssonea8c5372023-07-03 20:31:42 +000025from ._version import __version__
Michael McGeagh7a6f8432020-12-02 15:29:22 +000026from .errors import VelaError
Diego Russoe8a10452020-04-21 17:39:10 +010027from .nn_graph import PassPlacement
Louis Verhaardaee5d752020-09-30 09:01:52 +020028from .operation import Op
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +020029from .reader_util import align_inputs_indices
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020030from .tensor import MemType
Johan Alfvénb9f81592022-10-31 14:39:02 +010031from .tensor import shape_num_elements
Samuel Panijel6f4955a2021-06-10 13:40:03 +030032from .tensor import TensorPurpose
Tim Hall79d07d22020-04-27 18:20:16 +010033from .tflite import Buffer
34from .tflite import Metadata
Diego Russoe8a10452020-04-21 17:39:10 +010035from .tflite import Model
36from .tflite import Operator
37from .tflite import OperatorCode
38from .tflite import QuantizationParameters
39from .tflite import SubGraph
40from .tflite import Tensor
41from .tflite_mapping import builtin_operator_inv_map
42from .tflite_mapping import BuiltinOperator
Diego Russoe8a10452020-04-21 17:39:10 +010043from .tflite_mapping import datatype_inv_map
Tim Hall2180a172023-03-10 18:11:34 +000044from .tflite_mapping import optype_to_builtintype
Diego Russoe8a10452020-04-21 17:39:10 +010045
Tim Hallffe8e282021-06-24 18:29:53 +010046# the python flatbuffer interface is missing a method to add in file identifier. patching it in here:
Tim Hall79d07d22020-04-27 18:20:16 +010047
48tflite_version = 3
49tflite_file_identifier = "TFL" + str(tflite_version)
50
51
Tim Hall79d07d22020-04-27 18:20:16 +010052def FinishWithFileIdentifier(self, rootTable, fid):
53 if fid is None or len(fid) != 4:
Michael McGeagh7a6f8432020-12-02 15:29:22 +000054 raise VelaError("FileIdentifier must be 4 chars")
Tim Hall79d07d22020-04-27 18:20:16 +010055
56 flags = N.Uint8Flags
57 prepSize = 4
58 self.Prep(self.minalign, prepSize + len(fid))
59 for i in range(3, -1, -1):
60 self.head = self.head - flags.bytewidth
61 encode.Write(flags.packer_type, self.Bytes, self.Head(), ord(fid[i]))
62
63 return self.Finish(rootTable)
64
65
66flatbuffers.Builder.FinishWithFileIdentifier = FinishWithFileIdentifier
67
68
69def make_vector(v):
70 try:
71 len(v)
72 return v
73 except TypeError:
74 return [v]
75
76
77class TFLiteSerialiser:
Johan Alfvén673683b2022-09-05 09:39:47 +020078
Johan Alfven10706362023-04-13 12:20:55 +020079 # The 0th buffer is always by default an empty buffer that can be used by tensors
80 # without any constant data
81 BUF_IDX_ZERO = 0
82 BUF_IDX_START = 1
Johan Alfvén673683b2022-09-05 09:39:47 +020083
Tim Hall79d07d22020-04-27 18:20:16 +010084 def __init__(self, nng):
85 self.builder = flatbuffers.Builder(0)
86 self.nng = nng
87
Johan Alfvén673683b2022-09-05 09:39:47 +020088 self.buf_idx = TFLiteSerialiser.BUF_IDX_START
Tim Hall79d07d22020-04-27 18:20:16 +010089 self.buffers_to_write = [] # have an empty array there
Johan Alfvén673683b2022-09-05 09:39:47 +020090 self.tensor_map_all = [] # Keep track of all subgraphs
91 self.tensor_map_sg = [] # Keep track of one subgraph
Tim Hall79d07d22020-04-27 18:20:16 +010092
Michael McGeaghf3e3ad72020-12-02 12:39:03 +000093 self.ops_to_ignore = (Op.Const, Op.Placeholder, Op.SubgraphInput)
Tim Hall79d07d22020-04-27 18:20:16 +010094
Tim Hall79d07d22020-04-27 18:20:16 +010095 self.subgraphs_to_write = [sg for sg in self.nng.subgraphs if sg.placement == PassPlacement.Cpu]
96
97 all_ops = []
98 for sg in self.subgraphs_to_write:
99 for ps in sg.passes:
100 for op in ps.ops:
101 if op.type not in self.ops_to_ignore:
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200102 # swap from nng input indexing to TensorFlow Lite input indexing
103 self.align_nng_inputs_to_tflite(op)
Tim Hall79d07d22020-04-27 18:20:16 +0100104 all_ops.append(op)
Johan Alfvenc4268bf2023-04-13 10:13:56 +0200105 if op.type.is_conv2d_op() or op.type.is_depthwise_conv2d_op() or op.type == Op.FullyConnected:
Johan Alfvenc02eaa32023-08-22 11:02:47 +0200106 # Op is run on CPU, make sure the original weight and bias tensors are written back
Johan Alfvenc4268bf2023-04-13 10:13:56 +0200107 # instead of the cloned/reshaped (see tflite_reader)
Johan Alfven37dbca22023-12-21 12:37:17 +0100108 # Do nothing when values are None (dynamic weights)
109 if op.inputs[1].values is not None:
110 for idx, inp in enumerate(op.inputs):
111 if inp != op.ifm and inp is not None and inp.src_tensor is not None:
112 op.inputs[idx] = inp.src_tensor
Tim Hall79d07d22020-04-27 18:20:16 +0100113
wilisa010a7d5ee2023-04-13 17:05:09 +0000114 # list of tuple(Op, string, op.version); the custom code is only used for 3rd party custom operators
115 self.operator_codes = sorted(set((op.type, op.attrs.get("custom_code", ""), op.version) for op in all_ops))
Tim Hall79d07d22020-04-27 18:20:16 +0100116 self.operator_code_map = {}
117
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200118 def align_nng_inputs_to_tflite(self, op):
119 from_indices = op.type.info.indices
120 _, _, to_indices = builtin_operator_inv_map[op.type]
121 op.inputs = align_inputs_indices(from_indices, to_indices, op.inputs)
122
Tim Hall79d07d22020-04-27 18:20:16 +0100123 def write_byte_vector(self, v, alignment=1):
124 builder = self.builder
125 builder.StartVector(1, len(v), alignment)
126 for e in v[::-1]:
127 builder.PrependByte(e)
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200128 return builder.EndVector()
Tim Hall79d07d22020-04-27 18:20:16 +0100129
130 def write_int_vector(self, v):
131 builder = self.builder
132 builder.StartVector(4, len(v), 4)
133 for e in v[::-1]:
134 builder.PrependInt32(e)
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200135 return builder.EndVector()
Tim Hall79d07d22020-04-27 18:20:16 +0100136
137 def write_long_vector(self, v):
138 builder = self.builder
139 builder.StartVector(8, len(v), 8)
140 for e in v[::-1]:
141 builder.PrependInt64(e)
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200142 return builder.EndVector()
Tim Hall79d07d22020-04-27 18:20:16 +0100143
144 def write_float_vector(self, v):
145 builder = self.builder
146 builder.StartVector(4, len(v), 4)
147 for e in v[::-1]:
148 builder.PrependFloat32(e)
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200149 return builder.EndVector()
Tim Hall79d07d22020-04-27 18:20:16 +0100150
151 def write_offset_vector(self, v):
152 builder = self.builder
153 builder.StartVector(4, len(v), 4)
154 for e in v[::-1]:
155 builder.PrependUOffsetTRelative(e)
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200156 return builder.EndVector()
Tim Hall79d07d22020-04-27 18:20:16 +0100157
Tim Hallc8310b12020-06-17 14:53:11 +0100158 def assign_buffers_to_tensors(self, tensors, scratch_tensor):
159 if scratch_tensor is not None:
160 scratch_tensor_mem_area = scratch_tensor.mem_area
Tim Hall25f605c2020-05-18 18:04:26 +0100161 else:
162 scratch_tensor_mem_area = None # all tensors are initialised to MemArea.Unknown
163
Tim Hall79d07d22020-04-27 18:20:16 +0100164 buffer_map = {}
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200165
Tim Hall79d07d22020-04-27 18:20:16 +0100166 for tens in tensors:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200167 # Set buffer ids depending on allocation
Johan Alfven10706362023-04-13 12:20:55 +0200168 if tens.is_allocated_in_tensor_arena(scratch_tensor_mem_area) or tens.mem_type == MemType.Scratch_fast:
169 # Tensor allocated in the scratch areas, does not have any constant data and can
170 # therefore all point to the empty buffer (zero)
171 buffer_map[tens] = TFLiteSerialiser.BUF_IDX_ZERO
Tim Hall79d07d22020-04-27 18:20:16 +0100172 else:
Johan Alfvén673683b2022-09-05 09:39:47 +0200173 buffer_map[tens] = self.buf_idx
174 self.buf_idx += 1
Tim Hall79d07d22020-04-27 18:20:16 +0100175
Johan Alfvén673683b2022-09-05 09:39:47 +0200176 # Initialize/extend buffers_to_write to a length equal to number of buffers so
Tim Hall79d07d22020-04-27 18:20:16 +0100177 # they can be appended at the correct index during tensor serialization
Johan Alfvén673683b2022-09-05 09:39:47 +0200178 self.buffers_to_write += [None] * (self.buf_idx)
Tim Hall79d07d22020-04-27 18:20:16 +0100179
180 return buffer_map
181
wilisa010a7d5ee2023-04-13 17:05:09 +0000182 def serialise_operator_code(self, idx, op_type, custom_code, version):
Tim Hall79d07d22020-04-27 18:20:16 +0100183 builder = self.builder
184 custom_code_offset = None
Louis Verhaardaee5d752020-09-30 09:01:52 +0200185 if op_type == Op.Custom:
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200186 tf_code, opt_serializer, _ = builtin_operator_inv_map[op_type]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200187 custom_code_offset = builder.CreateString(custom_code)
Tim Hall79d07d22020-04-27 18:20:16 +0100188 else:
Tim Halle9194df2020-08-04 20:37:01 +0100189 assert (
Louis Verhaardaee5d752020-09-30 09:01:52 +0200190 op_type in builtin_operator_inv_map
191 ), "Vela does not contain a mapping to serialise {} operator to a TensorFlow Lite operator".format(op_type)
Patrik Gustavsson5e26eda2021-06-30 09:07:16 +0200192 tf_code, opt_serializer, _ = builtin_operator_inv_map[op_type]
Tim Hall79d07d22020-04-27 18:20:16 +0100193
Tim Hallb2183762021-01-25 21:42:56 +0000194 if op_type == Op.CustomNpuOp:
Tim Halle9194df2020-08-04 20:37:01 +0100195 assert (
Tim Hallb2183762021-01-25 21:42:56 +0000196 tf_code == BuiltinOperator.CUSTOM
Tim Halle9194df2020-08-04 20:37:01 +0100197 ), "Vela only supports serialising NpuOp operators as TensorFlow Lite Custom operators"
Tim Hall79d07d22020-04-27 18:20:16 +0100198 custom_code_offset = builder.CreateString("ethos-u")
199
Tim Hallb2183762021-01-25 21:42:56 +0000200 # there can be multiple different types of 3rd party custom operators (i.e. non-"ethos-u" ones). therefore we
201 # need to add an extra level of indirection to this particular entry in the operator_code_map to allow for the
202 # correct lookup later on
203 if op_type == Op.Custom:
204 if op_type not in self.operator_code_map:
205 self.operator_code_map[op_type] = {}
206 self.operator_code_map[op_type][custom_code] = (idx, tf_code, opt_serializer)
207 else:
208 self.operator_code_map[op_type] = (idx, tf_code, opt_serializer)
Tim Hall79d07d22020-04-27 18:20:16 +0100209
210 OperatorCode.OperatorCodeStart(builder)
Tim Hall42abec12021-02-04 21:31:57 +0000211 OperatorCode.OperatorCodeAddDeprecatedBuiltinCode(builder, tf_code if tf_code < 127 else 127)
Tim Hall79d07d22020-04-27 18:20:16 +0100212 OperatorCode.OperatorCodeAddBuiltinCode(builder, tf_code)
wilisa010a7d5ee2023-04-13 17:05:09 +0000213 OperatorCode.OperatorCodeAddVersion(builder, version)
Tim Hall79d07d22020-04-27 18:20:16 +0100214 if custom_code_offset is not None:
215 OperatorCode.OperatorCodeAddCustomCode(builder, custom_code_offset)
216
217 return OperatorCode.OperatorCodeEnd(builder)
218
219 def serialise_quantization_parameters(self, quant):
220 builder = self.builder
221
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100222 qp = None
Tim Hall79d07d22020-04-27 18:20:16 +0100223 min = None
224 max = None
225 scale = None
226 zero_point = None
227 if quant is not None:
228 if quant.min is not None:
229 min = self.write_float_vector(make_vector(quant.min))
230 if quant.max is not None:
231 max = self.write_float_vector(make_vector(quant.max))
232 if quant.scale_f32 is not None:
233 scale = self.write_float_vector(make_vector(quant.scale_f32))
234 if quant.zero_point is not None:
235 zero_point = self.write_long_vector(make_vector(quant.zero_point))
236
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100237 QuantizationParameters.QuantizationParametersStart(builder)
238 if min is not None:
239 QuantizationParameters.QuantizationParametersAddMin(builder, min)
240 if max is not None:
241 QuantizationParameters.QuantizationParametersAddMax(builder, max)
242 if scale is not None:
243 QuantizationParameters.QuantizationParametersAddScale(builder, scale)
244 if zero_point is not None:
245 QuantizationParameters.QuantizationParametersAddZeroPoint(builder, zero_point)
Fredrik Svedbergcc8569f2021-11-01 14:25:29 +0100246 if quant.quant_dim is not None:
247 QuantizationParameters.QuantizationParametersAddQuantizedDimension(builder, quant.quant_dim)
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100248 qp = QuantizationParameters.QuantizationParametersEnd(builder)
249
250 return qp
Tim Hall79d07d22020-04-27 18:20:16 +0100251
252 def serialise_tensor(self, tens):
253 builder = self.builder
Johan Alfvénb9f81592022-10-31 14:39:02 +0100254 if shape_num_elements(tens.original_shape) != shape_num_elements(tens.shape):
255 # shapes have changed size, therefore assume that the latest (modified) shape is correct
256 tens_shape = tens.shape
257 else:
258 # shapes have not changed size, therefore the original shape is valid
259 tens_shape = tens.original_shape
James Peet7519d502021-07-19 16:47:58 +0100260 values = tens.values
Tim Hall79d07d22020-04-27 18:20:16 +0100261
Tim Hall79d07d22020-04-27 18:20:16 +0100262 buf_id = self.buffer_map[tens]
Johan Alfven10706362023-04-13 12:20:55 +0200263 # Sanity check that if buffer 0 is used there must not be any data
264 assert not (buf_id == TFLiteSerialiser.BUF_IDX_ZERO and values is not None)
Tim Hall2f18e172023-04-06 21:01:58 +0100265 self.buffers_to_write[buf_id] = None if values is None else values.flatten().view(np.uint8)
Tim Hall79d07d22020-04-27 18:20:16 +0100266
267 shape = self.write_int_vector(tens_shape)
268
269 name = builder.CreateString(tens.name)
270 quant = self.serialise_quantization_parameters(tens.quantization)
271
272 Tensor.TensorStart(builder)
273 Tensor.TensorAddShape(builder, shape)
274 Tensor.TensorAddType(builder, datatype_inv_map[tens.dtype])
275 # All tensors must have a valid backing buffer, even if it is empty.
276 # Empty buffers should be kept unique for TensorFlow Lite Micro
277 Tensor.TensorAddBuffer(builder, buf_id)
278 Tensor.TensorAddName(builder, name)
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100279 if quant is not None:
280 Tensor.TensorAddQuantization(builder, quant)
281 Tensor.TensorAddIsVariable(builder, tens.is_variable)
Tim Hall79d07d22020-04-27 18:20:16 +0100282
283 res = Tensor.TensorEnd(builder)
284 return res
285
286 def serialise_operator(self, op):
287 builder = self.builder
288
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100289 inputs_offset = self.write_int_vector(
Johan Alfvén673683b2022-09-05 09:39:47 +0200290 [self.tensor_map_sg[tens] if tens in self.tensor_map_sg else -1 for tens in op.inputs]
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100291 )
Michael McGeaghbb1b09e2020-08-19 11:24:17 +0100292 outputs_offset = self.write_int_vector(
Johan Alfvén673683b2022-09-05 09:39:47 +0200293 [self.tensor_map_sg[tens] for tens in op.outputs if tens in self.tensor_map_sg]
Michael McGeaghbb1b09e2020-08-19 11:24:17 +0100294 )
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100295 intermediates_offset = self.write_int_vector(
Johan Alfvén673683b2022-09-05 09:39:47 +0200296 [self.tensor_map_sg[tens] for tens in op.intermediates if tens in self.tensor_map_sg]
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100297 )
Tim Hall79d07d22020-04-27 18:20:16 +0100298
Tim Hallb2183762021-01-25 21:42:56 +0000299 if op.type == Op.Custom:
300 op_idx, tflop, opt_serializer = self.operator_code_map[op.type][op.attrs.get("custom_code", "")]
301 else:
302 op_idx, tflop, opt_serializer = self.operator_code_map[op.type]
Tim Hall79d07d22020-04-27 18:20:16 +0100303
304 builtin_opt_offset = None
305 custom_opt_offset = None
306 if opt_serializer is not None:
307 attrs = dict(op.attrs)
Tim Hall2180a172023-03-10 18:11:34 +0000308 if op.run_on_npu:
309 if "strides" in attrs:
310 attrs["stride_h"] = attrs["strides"][1]
311 attrs["stride_w"] = attrs["strides"][2]
312 if "ksize" in attrs:
313 attrs["filter_height"] = attrs["ksize"][1]
314 attrs["filter_width"] = attrs["ksize"][2]
315 if "dilation" in attrs:
316 attrs["dilation_h_factor"] = attrs["dilation"][1]
317 attrs["dilation_w_factor"] = attrs["dilation"][2]
318 if "channel_multiplier" in attrs:
319 attrs["depth_multiplier"] = attrs["channel_multiplier"]
Tim Hall2180a172023-03-10 18:11:34 +0000320 attrs["fused_activation_function"] = op.activation.op_type if op.activation is not None else None
Tim Hall79d07d22020-04-27 18:20:16 +0100321
Johan Alfven0426fe92023-05-15 11:22:48 +0200322 # Serialize VarHandleOptions (only op that have attributes with type String)
323 if "container" in attrs:
324 attrs["container"] = builder.CreateString(attrs["container"])
325 if "shared_name" in attrs:
326 attrs["shared_name"] = builder.CreateString(attrs["shared_name"])
327
Tim Hall79d07d22020-04-27 18:20:16 +0100328 builtin_opt_offset, custom_opt_offset = opt_serializer.serialize(builder, attrs)
329
Tim Hall2180a172023-03-10 18:11:34 +0000330 # report any missing attributes that could not be written during serialize().
331 # operators that have been created internally (i.e. not created as part of reading an input network) may not
332 # have the write error attribute
333 attribute_write_error = attrs.get("attribute_write_error", [])
334 if len(attribute_write_error) != 0:
335 print(
336 f"Warning: Could not write the following attributes to {optype_to_builtintype(op.type)}"
337 f" '{op.name}' {opt_serializer.name} field: {', '.join(attribute_write_error)}"
338 )
339
Tim Hall79d07d22020-04-27 18:20:16 +0100340 mutating_variable_inputs_offset = self.write_byte_vector([])
341 Operator.OperatorStart(builder)
342 Operator.OperatorAddOpcodeIndex(builder, op_idx)
343 Operator.OperatorAddInputs(builder, inputs_offset)
344 Operator.OperatorAddOutputs(builder, outputs_offset)
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100345 Operator.OperatorAddIntermediates(builder, intermediates_offset)
Tim Hall79d07d22020-04-27 18:20:16 +0100346
347 if builtin_opt_offset is not None:
William Isakssonf4a511f2023-11-22 22:27:58 +0100348 if opt_serializer.builtin_opt_type < 127:
349 Operator.OperatorAddBuiltinOptionsType(builder, opt_serializer.builtin_opt_type)
350 Operator.OperatorAddBuiltinOptions(builder, builtin_opt_offset)
351 else:
352 Operator.OperatorAddBuiltinOptions2Type(builder, opt_serializer.builtin_opt_type % 127)
353 Operator.OperatorAddBuiltinOptions2(builder, builtin_opt_offset)
Tim Hall79d07d22020-04-27 18:20:16 +0100354 if custom_opt_offset is not None:
355 Operator.OperatorAddCustomOptions(builder, custom_opt_offset)
356 Operator.OperatorAddCustomOptionsFormat(builder, opt_serializer.custom_opt_format)
357
358 Operator.OperatorAddMutatingVariableInputs(builder, mutating_variable_inputs_offset)
359 return Operator.OperatorEnd(builder)
360
Johan Alfvén673683b2022-09-05 09:39:47 +0200361 def serialise_subgraph(self, sg, name):
Tim Hall79d07d22020-04-27 18:20:16 +0100362 builder = self.builder
Tim Hall79d07d22020-04-27 18:20:16 +0100363 all_ops = []
Michael McGeagh515c9562020-09-02 15:52:43 +0100364 placeholder_ops = []
365
Tim Hall79d07d22020-04-27 18:20:16 +0100366 for ps in sg.passes:
367 for op in ps.ops:
368 if op.type not in self.ops_to_ignore:
369 all_ops.append(op)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200370 elif op.type == Op.Placeholder:
Michael McGeagh515c9562020-09-02 15:52:43 +0100371 placeholder_ops.append(op)
Tim Hall79d07d22020-04-27 18:20:16 +0100372
Johan Alfvén673683b2022-09-05 09:39:47 +0200373 # Make sure all original tensors are written back, special case for Ops
374 # with connected subgraphs. Even though not all inputs are used,
375 # the reference kernel expects all inputs to be in the tflite file.
376 # Since we traverse the graph starting with all outputs they are
377 # always added but if an input is not referenced it will not be added
378 # to an op.
379 tensor_set = set(sg.original_inputs)
380
Johan Alfven9070f0f2023-02-07 13:01:03 +0100381 # Remove any virtual outputs since they are only used internally when
382 # traversing the graph.
383 for tens in sg.virtual_outputs:
384 tens.ops[0].outputs = []
385 if tens in sg.output_tensors:
386 sg.output_tensors.remove(tens)
387
Michael McGeagh515c9562020-09-02 15:52:43 +0100388 # Add the tensors from all valid ops, as well as the tensors from placeholder ops
389 # This allows us to serialise tensors which arent attached to any specific ops,
390 # e.g. due to an empty graph containing no ops
391 for op in all_ops + placeholder_ops:
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100392 for tens in op.inputs + op.outputs + op.intermediates:
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200393 if tens is not None:
394 tensor_set.add(tens)
Tim Hall79d07d22020-04-27 18:20:16 +0100395
396 all_tensors = [tens for nm, idx, tens in sorted((tens.name, idx, tens) for idx, tens in enumerate(tensor_set))]
397
Samuel Panijel6f4955a2021-06-10 13:40:03 +0300398 scratch_tensors = [tens for tens in all_tensors if tens.purpose is TensorPurpose.Scratch]
Patrik Gustavsson3ab94522020-06-29 17:36:55 +0200399
Tim Hallc8310b12020-06-17 14:53:11 +0100400 if len(scratch_tensors) == 0:
401 scratch_tensor = None
402 else:
403 assert len(scratch_tensors) == 1, "Multiple scratch tensors"
404 scratch_tensor = scratch_tensors[0]
405
Johan Alfvén673683b2022-09-05 09:39:47 +0200406 self.tensor_map_sg = {tens: idx for idx, tens in enumerate(all_tensors)}
Tim Hallc8310b12020-06-17 14:53:11 +0100407 self.buffer_map = self.assign_buffers_to_tensors(all_tensors, scratch_tensor)
Johan Alfvén673683b2022-09-05 09:39:47 +0200408 self.tensor_map_all.append(self.tensor_map_sg)
Tim Hall79d07d22020-04-27 18:20:16 +0100409
410 tensors_offset = self.write_offset_vector([self.serialise_tensor(tens) for tens in all_tensors])
411
Tim Hall79d07d22020-04-27 18:20:16 +0100412 # Make sure the input_tensors haven't been modified
413 assert all(inp in sg.original_inputs for inp in sg.input_tensors)
Johan Alfvén673683b2022-09-05 09:39:47 +0200414 inputs = [self.tensor_map_sg[tens] for tens in sg.original_inputs if tens in self.tensor_map_sg]
Tim Hallc8310b12020-06-17 14:53:11 +0100415
Tim Hallc8310b12020-06-17 14:53:11 +0100416 inputs_offset = self.write_int_vector(inputs)
Michael McGeaghbb1b09e2020-08-19 11:24:17 +0100417 outputs_offset = self.write_int_vector(
Johan Alfvén673683b2022-09-05 09:39:47 +0200418 [self.tensor_map_sg[tens] for tens in sg.output_tensors if tens in self.tensor_map_sg]
Michael McGeaghbb1b09e2020-08-19 11:24:17 +0100419 )
Tim Hall79d07d22020-04-27 18:20:16 +0100420
421 operators_offset = self.write_offset_vector([self.serialise_operator(op) for op in all_ops])
422
423 SubGraph.SubGraphStart(builder)
424 SubGraph.SubGraphAddTensors(builder, tensors_offset)
425 SubGraph.SubGraphAddInputs(builder, inputs_offset)
426 SubGraph.SubGraphAddOutputs(builder, outputs_offset)
427
428 SubGraph.SubGraphAddOperators(builder, operators_offset)
Johan Alfvén673683b2022-09-05 09:39:47 +0200429 SubGraph.SubGraphAddName(builder, name)
Tim Hall79d07d22020-04-27 18:20:16 +0100430
431 return SubGraph.SubGraphEnd(builder)
432
433 def write_aligned_bytes(self, buf):
434 builder = self.builder
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200435 builder.assertNotNested()
Tim Hall79d07d22020-04-27 18:20:16 +0100436 builder.nested = True
William Isakssonea8c5372023-07-03 20:31:42 +0000437 if isinstance(buf, str):
438 data = bytes(buf, "utf-8")
439 else:
440 data = bytes(buf)
Tim Hall79d07d22020-04-27 18:20:16 +0100441 length_bytes = UOffsetTFlags.py_type(len(data))
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200442 builder.vectorNumElems = length_bytes
Tim Hall79d07d22020-04-27 18:20:16 +0100443 builder.Prep(16, length_bytes) # Reserve aligned storage
444 builder.head = UOffsetTFlags.py_type(builder.Head() - length_bytes) # Update FlatBuffer internal pointer
445 builder.Bytes[builder.Head() : builder.Head() + length_bytes] = data # Assign bytes to aligned area
erik.andersson@arm.com61f05d92022-09-27 12:06:32 +0200446 return builder.EndVector()
Tim Hall79d07d22020-04-27 18:20:16 +0100447
448 def serialise_buffer(self, buf):
449 builder = self.builder
450 data = None
451 if buf is not None:
452 data = self.write_aligned_bytes(buf)
453 Buffer.BufferStart(builder)
454 if data is not None:
455 Buffer.BufferAddData(builder, data)
456 return Buffer.BufferEnd(builder)
457
458 def serialise_metadata(self, metadata):
459 builder = self.builder
460 name = builder.CreateString(metadata[0])
461
462 Metadata.MetadataStart(builder)
463 Metadata.MetadataAddName(builder, name)
464 Metadata.MetadataAddBuffer(builder, metadata[1])
465
466 return Metadata.MetadataEnd(builder)
467
468 def serialise_model(self):
469 builder = self.builder
470 operator_code_offset = self.write_offset_vector(
wilisa010a7d5ee2023-04-13 17:05:09 +0000471 [
472 self.serialise_operator_code(idx, optype, code, version)
473 for idx, (optype, code, version) in enumerate(self.operator_codes)
474 ]
Tim Hall79d07d22020-04-27 18:20:16 +0100475 )
476
William Isakssonea8c5372023-07-03 20:31:42 +0000477 description = builder.CreateString(f"Vela {__version__} Optimised")
478 self.nng.metadata.append(("vela_version", __version__))
Tim Hall79d07d22020-04-27 18:20:16 +0100479
Johan Alfvén673683b2022-09-05 09:39:47 +0200480 subgraph_offset = self.write_offset_vector(
481 [self.serialise_subgraph(sg, builder.CreateString(sg.name)) for sg in self.subgraphs_to_write]
482 )
Tim Hall79d07d22020-04-27 18:20:16 +0100483
484 # Fill the metadata buffer
485 version = np.int32(0)
Johan Alfvén673683b2022-09-05 09:39:47 +0200486 subgraph_idx = np.int32(len(self.subgraphs_to_write))
487
488 nbr_tensors_all = np.sum([len(tensor_map_sg) for tensor_map_sg in self.tensor_map_all], dtype=np.int32)
489
490 offlineAlloc = [version, subgraph_idx, nbr_tensors_all]
Tim Hall79d07d22020-04-27 18:20:16 +0100491
Fredrik Svedberge22ba8c2021-01-27 16:53:41 +0100492 if not any([name == b"OfflineMemoryAllocation" for name, _ in self.nng.metadata]):
Johan Alfvén673683b2022-09-05 09:39:47 +0200493 for tensor_map_sg in self.tensor_map_all:
494 nbr_tensors_sg = np.int32(len(tensor_map_sg))
495 # An offset of -1 indicates that the tensor will be allocated online by Tensorflow Lite Micro
496 offsets = [np.int32(-1)] * nbr_tensors_sg
497 # Ensure that the order of the offsets match the order of the tensors
498 for tens, idx in tensor_map_sg.items():
499 # Set offsets for tensor allocated in Tensor Arena or in the scratch_fast area
500 if tens.mem_type in (MemType.Scratch, MemType.Scratch_fast):
501 offsets[idx] = np.int32(tens.address) if tens.address is not None else np.int32(0)
Tim Hall79d07d22020-04-27 18:20:16 +0100502
Johan Alfvén673683b2022-09-05 09:39:47 +0200503 offlineAlloc += offsets
Tim Hall79d07d22020-04-27 18:20:16 +0100504
Johan Alfvén673683b2022-09-05 09:39:47 +0200505 self.nng.metadata.append(("OfflineMemoryAllocation", np.array(offlineAlloc)))
Michael McGeagh22f74e12020-08-07 16:21:03 +0100506
507 metadata_list = []
508 for name, buffer in self.nng.metadata:
509 self.buffers_to_write.append(buffer)
510 metadata_list.append((name, len(self.buffers_to_write) - 1))
Tim Hall79d07d22020-04-27 18:20:16 +0100511
512 buffers_offset = self.write_offset_vector([self.serialise_buffer(buf) for buf in self.buffers_to_write])
Tim Hall79d07d22020-04-27 18:20:16 +0100513 metadata_offset = self.write_offset_vector([self.serialise_metadata(metadata) for metadata in metadata_list])
514
515 Model.ModelStart(builder)
516 Model.ModelAddVersion(builder, tflite_version)
517 Model.ModelAddOperatorCodes(builder, operator_code_offset)
518 Model.ModelAddSubgraphs(builder, subgraph_offset)
519 Model.ModelAddDescription(builder, description)
520 Model.ModelAddBuffers(builder, buffers_offset)
521 Model.ModelAddMetadata(builder, metadata_offset)
522 return Model.ModelEnd(builder)
523
524 def serialise(self):
525
526 model = self.serialise_model()
527
528 self.builder.FinishWithFileIdentifier(model, tflite_file_identifier)
529
530 return self.builder.Output()
531
532 def write(self, filename):
533 with open(self.filename, "wb") as f:
534 f.write(self.serialised_buf)
535
536
537def write_tflite(nng, filename):
538 writer = TFLiteSerialiser(nng)
539 buf = writer.serialise()
540
541 with open(filename, "wb") as f:
542 f.write(buf)