Skip to content

Commit aeef112

Browse files
roman-janik-nxprobert-kalmar
authored andcommitted
Move optimization in keep_one_empty_buffer.py to Model builder
1 parent 6ed10e5 commit aeef112

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,27 @@ 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+
"""
418+
empty_buffer = self.get_first_empty_buffer()
419+
420+
for t in self.get_tensors().vector:
421+
if tensor_has_data(t):
422+
# The buffer of `t` is not empty.
423+
continue
424+
425+
if t.tmp_buffer == empty_buffer:
426+
# Already optimized.
427+
continue
428+
429+
if t.is_variable:
430+
# The data of the tensor will change at runtime, so it shouldn't share the buffer with other tensors.
431+
continue
432+
433+
# It's safe to replace the buffer.
434+
t.tmp_buffer = empty_buffer
435+
415436
def finish(self) -> tflite_model.Model:
416437
"""Finalize and optimize the converted TFLite model. Then return it.
417438
@@ -430,6 +451,8 @@ def finish(self) -> tflite_model.Model:
430451
self.conversion_config.optimization_blacklist,
431452
)
432453

454+
self._keep_one_empty_buffer()
455+
433456
# Remove outputs, which are not produced by any node. Otherwise, there would be errors after inference.
434457
operator_outputs = []
435458
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)