File tree Expand file tree Collapse file tree 3 files changed +22
-46
lines changed Expand file tree Collapse file tree 3 files changed +22
-46
lines changed Original file line number Diff line number Diff 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 :
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 2020from 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- )
2623from executorch .backends .nxp .backend .ir .tflite_optimizer .optimizations .move_relu_before_concat import (
2724 MoveActivationBeforeConcatenation ,
2825)
3633
3734
3835class 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 ),
You can’t perform that action at this time.
0 commit comments