blob: e71b228a12475352044c46f3d94e4b8223c24609 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
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:
17# Functions for abstracting out the traversal and rewriting of graphs so that the optimisation passes can focus on the
18# correct operation.
19#
20# Requires two lists, one of functions that rewrite Tensors, and one of functions that rewrite Operations.
21#
22# Pre-order traversal, this supports rewrites. Therefore, functions can return something other than the original value.
23#
24# Post-order traversal, this does not support rewrites. Therefore, functions must return the original value.
25
26
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +020027def rewrite_graph_pre_order(nng, sg, arch, tensor_rewrite_list, op_rewrite_list, rewrite_unsupported=True):
Tim Hall79d07d22020-04-27 18:20:16 +010028
29 op_visit_dict = dict()
30 tens_visit_dict = dict()
31
32 def visit_op(op):
33 if op in op_visit_dict:
34 return op_visit_dict[op]
35 res = op
36 prev_res = None
37 while prev_res != res:
38 prev_res = res
39 for rewrite in op_rewrite_list:
40 if res.run_on_npu or rewrite_unsupported:
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +020041 res = rewrite(res, arch, nng)
Tim Hall79d07d22020-04-27 18:20:16 +010042
43 op_visit_dict[op] = res
44 op_visit_dict[res] = res
45
46 inputs = res.inputs
47 res.inputs = []
48 for tens in inputs:
49 res.inputs.append(visit_tens(tens))
50
51 outputs = res.outputs
52 res.outputs = []
53 for tens in outputs:
54 res.outputs.append(visit_tens(tens))
55
56 return res
57
58 def visit_tens(tens):
59 if tens in tens_visit_dict:
60 return tens_visit_dict[tens]
61
62 res = tens
63 prev_res = None
64 while prev_res != res:
65 prev_res = res
66 for rewrite in tensor_rewrite_list:
Patrik Gustavsson3010d9b2020-10-01 08:22:10 +020067 res = rewrite(res, arch, nng)
Tim Hall79d07d22020-04-27 18:20:16 +010068
69 tens_visit_dict[tens] = res
70 tens_visit_dict[res] = res
71
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +020072 if res:
73 ops = res.ops
74 res.ops = []
75 for op in ops:
76 res.ops.append(visit_op(op))
Tim Hall79d07d22020-04-27 18:20:16 +010077 return res
78
79 sg.output_tensors = [visit_tens(tens) for tens in sg.output_tensors]
80 sg.refresh_after_modification()
81
82 return sg
83
84
85def visit_graph_post_order(sg, arch, tensor_visit_list, op_visit_list):
86
87 op_visit_dict = dict()
88 tens_visit_dict = dict()
89
90 def visit_op(op):
91 if op in op_visit_dict:
92 return op_visit_dict[op]
93 op_visit_dict[op] = op
94
95 for tens in op.inputs:
96 visit_tens(tens)
97
98 for visit in op_visit_list:
99 visit(op, arch)
100
101 for tens in op.outputs:
102 visit_tens(tens)
103
104 return op
105
106 def visit_tens(tens):
107 if tens in tens_visit_dict:
108 return tens_visit_dict[tens]
109
110 tens_visit_dict[tens] = tens
111
112 for op in tens.ops:
113 visit_op(op)
114
115 for visit in tensor_visit_list:
116 visit(tens, arch)
117
118 return tens
119
120 for tens in sg.output_tensors:
121 visit_tens(tens)
122
123 sg.refresh_after_modification()
124
125 return sg
126
127
128def verify_graph_health(nng):
129
130 for sg in nng.subgraphs:
131 verify_subgraph_health(sg)
132
133 return True
134
135
136def verify_subgraph_health(sg):
137 op_visit_dict = dict()
138 tens_visit_dict = dict()
139
140 def visit_op(op):
141 if op in op_visit_dict:
142 return op_visit_dict[op]
143 op_visit_dict[op] = op
144
145 for tens in op.inputs:
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200146 if not tens:
147 continue
Tim Hall79d07d22020-04-27 18:20:16 +0100148 assert op in tens.consumers()
149 visit_tens(tens)
150
151 for tens in op.outputs:
152 assert op in tens.ops
153 visit_tens(tens)
154
155 return op
156
157 def visit_tens(tens):
158 if tens in tens_visit_dict:
159 return tens_visit_dict[tens]
160
161 tens_visit_dict[tens] = tens
162
163 for op in tens.ops:
164 assert tens in op.outputs
165 visit_op(op)
166
167 return tens
168
169 for tens in sg.output_tensors:
170 visit_tens(tens)
171
172 return True