diff --git a/backends/nxp/backend/ir/converter/builder/model_builder.py b/backends/nxp/backend/ir/converter/builder/model_builder.py index 1ca46237814..4f036854138 100755 --- a/backends/nxp/backend/ir/converter/builder/model_builder.py +++ b/backends/nxp/backend/ir/converter/builder/model_builder.py @@ -412,6 +412,26 @@ def _make_outputs_channels_first(self): self.get_sub_graph().outputs.tmp_outputs = new_outputs + def _keep_one_empty_buffer(self): + """Create a single empty `Buffer` object and assign it to all tensors in the model that don't have static data.""" + empty_buffer = self.get_first_empty_buffer() + + for t in self.get_tensors().vector: + if tensor_has_data(t): + # The buffer of `t` is not empty. + continue + + if t.tmp_buffer == empty_buffer: + # Already optimized. + continue + + if t.is_variable: + # The data of the tensor will change at runtime, so it shouldn't share the buffer with other tensors. + continue + + # It's safe to replace the buffer. + t.tmp_buffer = empty_buffer + def finish(self) -> tflite_model.Model: """Finalize and optimize the converted TFLite model. Then return it. @@ -430,6 +450,8 @@ def finish(self) -> tflite_model.Model: self.conversion_config.optimization_blacklist, ) + self._keep_one_empty_buffer() + # Remove outputs, which are not produced by any node. Otherwise, there would be errors after inference. operator_outputs = [] for op in self.get_operators().vector: diff --git a/backends/nxp/backend/ir/tflite_optimizer/optimizations/keep_one_empty_buffer.py b/backends/nxp/backend/ir/tflite_optimizer/optimizations/keep_one_empty_buffer.py deleted file mode 100755 index 9809719fad4..00000000000 --- a/backends/nxp/backend/ir/tflite_optimizer/optimizations/keep_one_empty_buffer.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright 2024 NXP -# -# This source code is licensed under the BSD-style license found in the -# LICENSE file in the root directory of this source tree. - -from executorch.backends.nxp.backend.ir.converter.tensor_utils import tensor_has_data -from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.base_optimization import ( - BaseOptimization, -) - - -class KeepOneEmptyBuffer(BaseOptimization): - - def __call__(self) -> bool: - """Create a single empty `Buffer` object and assign it to all tensors in the model that don't have static data. - :return: True, if any tensors had their buffer changed. Otherwise, False. - """ - - made_changes = False - empty_buffer = self._builder.get_first_empty_buffer() - - for t in self._builder.get_tensors().vector: - if tensor_has_data(t): - # The buffer of `t` is not empty. - continue - - if t.tmp_buffer == empty_buffer: - # Already optimized. - continue - - if t.is_variable: - # The data of the tensor will change at runtime, so it shouldn't share the buffer with other tensors. - continue - - # It's safe to replace the buffer. - t.tmp_buffer = empty_buffer - made_changes = True - - return made_changes diff --git a/backends/nxp/backend/ir/tflite_optimizer/optimizer.py b/backends/nxp/backend/ir/tflite_optimizer/optimizer.py index eb4ce6a5992..d4a097ca76d 100755 --- a/backends/nxp/backend/ir/tflite_optimizer/optimizer.py +++ b/backends/nxp/backend/ir/tflite_optimizer/optimizer.py @@ -20,9 +20,6 @@ from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.fuse_fully_connected_and_add_operators import ( FuseFullyConnectedAndAddOperators, ) -from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.keep_one_empty_buffer import ( - KeepOneEmptyBuffer, -) from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.move_relu_before_concat import ( MoveActivationBeforeConcatenation, ) @@ -36,7 +33,6 @@ class Optimization(Enum): - KEEP_ONE_EMPTY_BUFFER = 0 FUSE_ACTIVATION_FUNCTIONS = 1 FUSE_FULLY_CONNECTED_AND_ADD = 2 @@ -76,9 +72,6 @@ def __init__( self._builder = builder self.optimization_map = { - Optimization.KEEP_ONE_EMPTY_BUFFER: KeepOneEmptyBuffer( - builder, conversion_config - ), Optimization.FUSE_ACTIVATION_FUNCTIONS: FuseActivationFunctions( builder, conversion_config ),