blob: 4505cf5835e2cb1aada4eb34fcc6927d10d21a2b [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2020-2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
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.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Tim Hall79d07d22020-04-27 18:20:16 +010017# Description:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020018# 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 +010019from . import rewrite_graph
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020020from .graph_optimiser_util import check_format_restrictions
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +020021from .graph_optimiser_util import check_memory_only_removed
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020022from .graph_optimiser_util import record_optimised
23from .nn_graph import NetworkType
24from .tflite_graph_optimiser import tflite_optimise_graph
25from .tosa_graph_optimiser import tosa_optimise_graph
Tim Hall79d07d22020-04-27 18:20:16 +010026
Tim Hall79d07d22020-04-27 18:20:16 +010027
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020028def optimise_graph(nng, arch, network_type, verbose_graph=False):
Tim Hall79d07d22020-04-27 18:20:16 +010029 if verbose_graph:
Fredrik Svedbergc875aa62021-05-06 09:53:31 +020030 nng.print_graph("Before Graph Optimization")
Tim Hall79d07d22020-04-27 18:20:16 +010031
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020032 if network_type == NetworkType.TFLite:
33 # TensorFlow Lite graph optimization
34 nng = tflite_optimise_graph(nng, arch)
35 else:
36 # TOSA graph optimization
37 nng = tosa_optimise_graph(nng, arch)
Patrik Gustavssonee99bb12021-04-08 09:04:00 +020038
Patrik Gustavsson3a269202021-01-21 08:28:55 +010039 # Post-optimisation operator debug tracing, and checking that no undesired reshapes are left in the graph
Tim Halle6ccd872020-11-09 16:46:37 +000040 for sg in nng.subgraphs:
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020041 rewrite_graph.visit_graph_post_order(
Jonas Ohlsson0957e3e2021-09-01 15:57:21 +020042 sg.output_tensors, arch, [check_format_restrictions], [check_memory_only_removed, record_optimised]
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020043 )
Tim Hall79d07d22020-04-27 18:20:16 +010044
45 if verbose_graph:
Fredrik Svedbergc875aa62021-05-06 09:53:31 +020046 nng.print_graph("After Graph Optimization")
Tim Hall79d07d22020-04-27 18:20:16 +010047 return nng