From 2f18e172a3d727d7c610855e93d8aeccd2fca9b1 Mon Sep 17 00:00:00 2001 From: Tim Hall Date: Thu, 6 Apr 2023 21:01:58 +0100 Subject: MLBEDSW-7373: Vela sometimes write empty buffers in incorrect format - Fixed an issue whereby a zero length buffer was written out instead of an empty buffer - Added a warning message to highlight when this type of semantically incorrect empty buffer is read from an input network Change-Id: Iac3bc71a2dbfda53737bbeb6e7f895552f0f13d0 Signed-off-by: Tim Hall --- ethosu/vela/tflite_reader.py | 20 +++++++++++++++++--- ethosu/vela/tflite_writer.py | 5 +---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ethosu/vela/tflite_reader.py b/ethosu/vela/tflite_reader.py index 2f3192b7..18b61e75 100644 --- a/ethosu/vela/tflite_reader.py +++ b/ethosu/vela/tflite_reader.py @@ -255,9 +255,23 @@ class TFLiteGraph: parsing_step = "parsing buffers length" self.buffers = [] - for idx in range(model.BuffersLength()): - parsing_step = f"parsing buffer {idx}" - self.buffers.append(self.parse_buffer(model.Buffers(idx))) + if not model.BuffersIsNone(): + for idx in range(model.BuffersLength()): + parsing_step = f"parsing buffer {idx}" + buffer = model.Buffers(idx) + buffer_data = self.parse_buffer(buffer) + # buffers can be either; empty, or contain no data (zero length), or contain data (non-zero length). + # when a buffer is None it means that it is either empty or zero length, and an empty buffer + # will have DataIsNone() equal to true. + # we should detect zero length buffers and report a warning because the TFLite semantics for these + # types of buffers changed in TensorFlow 2.11, whereby they could result in runtime errors + if buffer_data is None and not buffer.DataIsNone(): + print( + f"Warning: Input TensorFlow Lite network contains a zero length buffer (index = {idx})" + f" which is semantically not empty. However, it will be treated as an empty buffer." + ) + + self.buffers.append(buffer_data) parsing_step = "parsing operator codes length" self.operator_codes = [] diff --git a/ethosu/vela/tflite_writer.py b/ethosu/vela/tflite_writer.py index 8d44774b..625cf7cc 100644 --- a/ethosu/vela/tflite_writer.py +++ b/ethosu/vela/tflite_writer.py @@ -259,16 +259,13 @@ class TFLiteSerialiser: tens_shape = tens.original_shape values = tens.values - if values is None: - values = np.empty(shape=(0), dtype=np.uint8) - if tens in self.tensors_to_reshape: reorder = self.tensors_to_reshape[tens] tens_shape = [tens_shape[idx] for idx in reorder] values = values.transpose(reorder) buf_id = self.buffer_map[tens] - self.buffers_to_write[buf_id] = values.flatten().view(np.uint8) + self.buffers_to_write[buf_id] = None if values is None else values.flatten().view(np.uint8) shape = self.write_int_vector(tens_shape) -- cgit v1.2.1