blob: 0f5636edf37d07df2a45fca86f0aa79f021aba34 [file] [log] [blame]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +02001# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved.
Tim Hall79d07d22020-04-27 18:20:16 +01002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020017# Early optimisation of the network graph, using the rewrite_graph module to do the traversal of the graph.
Diego Russoea6111a2020-04-14 18:41:58 +010018from . import rewrite_graph
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020019from .graph_optimiser_util import check_format_restrictions
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +020020from .graph_optimiser_util import check_memory_only_removed
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020021from .graph_optimiser_util import record_optimised
22from .nn_graph import NetworkType
23from .tflite_graph_optimiser import tflite_optimise_graph
24from .tosa_graph_optimiser import tosa_optimise_graph
Tim Hall79d07d22020-04-27 18:20:16 +010025
Tim Hall79d07d22020-04-27 18:20:16 +010026
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020027def optimise_graph(nng, arch, network_type, verbose_graph=False):
Tim Hall79d07d22020-04-27 18:20:16 +010028 if verbose_graph:
Fredrik Svedbergc875aa62021-05-06 09:53:31 +020029 nng.print_graph("Before Graph Optimization")
Tim Hall79d07d22020-04-27 18:20:16 +010030
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020031 if network_type == NetworkType.TFLite:
32 # TensorFlow Lite graph optimization
33 nng = tflite_optimise_graph(nng, arch)
34 else:
35 # TOSA graph optimization
36 nng = tosa_optimise_graph(nng, arch)
Patrik Gustavssonee99bb12021-04-08 09:04:00 +020037
Patrik Gustavsson3a269202021-01-21 08:28:55 +010038 # Post-optimisation operator debug tracing, and checking that no undesired reshapes are left in the graph
Tim Halle6ccd872020-11-09 16:46:37 +000039 for sg in nng.subgraphs:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020040 rewrite_graph.visit_graph_post_order(
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +020041 sg.output_tensors, arch, [check_format_restrictions], [check_memory_only_removed, record_optimised]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020042 )
Tim Hall79d07d22020-04-27 18:20:16 +010043
44 if verbose_graph:
Fredrik Svedbergc875aa62021-05-06 09:53:31 +020045 nng.print_graph("After Graph Optimization")
Tim Hall79d07d22020-04-27 18:20:16 +010046 return nng