Skip to content

Commit dfd7f2a

Browse files
NXP backend: Move optimization in keep_one_empty_buffer.py to Model builder (#14163)
### Summary Move optimization in keep_one_empty_buffer.py to IR Model builder. This functionality is not an optimization, but a required housekeeping step to build a valid IR model for Neutron Converter. ### Test plan All tests where a subgraph is delegated to Neutron.
1 parent 59008b5 commit dfd7f2a

File tree

3 files changed

+22
-46
lines changed

3 files changed

+22
-46
lines changed

backends/nxp/backend/ir/converter/builder/model_builder.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ def _make_outputs_channels_first(self):
412412

413413
self.get_sub_graph().outputs.tmp_outputs = new_outputs
414414

415+
def _keep_one_empty_buffer(self):
416+
"""Create a single empty `Buffer` object and assign it to all tensors in the model that don't have static data."""
417+
empty_buffer = self.get_first_empty_buffer()
418+
419+
for t in self.get_tensors().vector:
420+
if tensor_has_data(t):
421+
# The buffer of `t` is not empty.
422+
continue
423+
424+
if t.tmp_buffer == empty_buffer:
425+
# Already optimized.
426+
continue
427+
428+
if t.is_variable:
429+
# The data of the tensor will change at runtime, so it shouldn't share the buffer with other tensors.
430+
continue
431+
432+
# It's safe to replace the buffer.
433+
t.tmp_buffer = empty_buffer
434+
415435
def finish(self) -> tflite_model.Model:
416436
"""Finalize and optimize the converted TFLite model. Then return it.
417437
@@ -430,6 +450,8 @@ def finish(self) -> tflite_model.Model:
430450
self.conversion_config.optimization_blacklist,
431451
)
432452

453+
self._keep_one_empty_buffer()
454+
433455
# Remove outputs, which are not produced by any node. Otherwise, there would be errors after inference.
434456
operator_outputs = []
435457
for op in self.get_operators().vector:

backends/nxp/backend/ir/tflite_optimizer/optimizations/keep_one_empty_buffer.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

backends/nxp/backend/ir/tflite_optimizer/optimizer.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.fuse_fully_connected_and_add_operators import (
2121
FuseFullyConnectedAndAddOperators,
2222
)
23-
from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.keep_one_empty_buffer import (
24-
KeepOneEmptyBuffer,
25-
)
2623
from executorch.backends.nxp.backend.ir.tflite_optimizer.optimizations.move_relu_before_concat import (
2724
MoveActivationBeforeConcatenation,
2825
)
@@ -36,7 +33,6 @@
3633

3734

3835
class Optimization(Enum):
39-
KEEP_ONE_EMPTY_BUFFER = 0
4036
FUSE_ACTIVATION_FUNCTIONS = 1
4137
FUSE_FULLY_CONNECTED_AND_ADD = 2
4238

@@ -76,9 +72,6 @@ def __init__(
7672
self._builder = builder
7773

7874
self.optimization_map = {
79-
Optimization.KEEP_ONE_EMPTY_BUFFER: KeepOneEmptyBuffer(
80-
builder, conversion_config
81-
),
8275
Optimization.FUSE_ACTIVATION_FUNCTIONS: FuseActivationFunctions(
8376
builder, conversion_config
8477
),

0 commit comments

Comments
 (0)