From 6fa0bbadaa34a0ca4a86bd4de784216c4e4d2dfe Mon Sep 17 00:00:00 2001 From: Jacob Bohlin Date: Thu, 15 May 2025 09:19:10 +0100 Subject: [PATCH] MLBEDSW-10776 Don't serialize intermediates vector if empty Most operations do not have any intermediate tensors. In this case there is no need to create a vector in the flatbuffer file as even empty vectors contain a 4 byte length header. With this patch the intermediates vector is only created if any intermediate tensors are present. Change-Id: I505ebe8c17a577eee2361050d18715207555409d Signed-off-by: Jacob Bohlin --- ethosu/regor/tflite/tflite_writer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ethosu/regor/tflite/tflite_writer.cpp b/ethosu/regor/tflite/tflite_writer.cpp index 19eebc06..a0e58769 100644 --- a/ethosu/regor/tflite/tflite_writer.cpp +++ b/ethosu/regor/tflite/tflite_writer.cpp @@ -222,11 +222,14 @@ std::unique_ptr TfLiteWriter::SerialiseImpl(const std::vector(inputs); - auto serialised_intermediates = _flatbuffer.CreateVector(intermediates); auto serialised_outputs = _flatbuffer.CreateVector(outputs); auto serialised_options = SerialiseOptions(operation, type); auto serialised_options2 = SerialiseOptions2(operation, type); + // Flatbuffer vectors have a length prefix before the payload. If the op doesn't have any intermediates + // the field can be omitted entirely by setting below variable to 0 instead of creating an empty vector. + auto serialised_intermediates = !intermediates.empty() ? _flatbuffer.CreateVector(intermediates) : 0; + _serialised_operations.push_back(tflite::CreateOperator(_flatbuffer, opcode_index, serialised_inputs, serialised_outputs, builtin_options_type, serialised_options, custom_options, custom_options_format, mvi, serialised_intermediates, large_custom_options_offset, large_custom_options_size, builtin_options_2_type, serialised_options2)); -- GitLab