blob: 4f0d0107906fa41dc53fdef582b14ea1696e79a7 [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
27def rewrite_graph_pre_order(sg, arch, tensor_rewrite_list, op_rewrite_list, rewrite_unsupported=True):
28
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:
41 res = rewrite(res, arch)
42
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:
67 res = rewrite(res, arch)
68
69 tens_visit_dict[tens] = res
70 tens_visit_dict[res] = res
71
72 ops = res.ops
73 res.ops = []
74 for op in ops:
75 res.ops.append(visit_op(op))
76 return res
77
78 sg.output_tensors = [visit_tens(tens) for tens in sg.output_tensors]
79 sg.refresh_after_modification()
80
81 return sg
82
83
84def visit_graph_post_order(sg, arch, tensor_visit_list, op_visit_list):
85
86 op_visit_dict = dict()
87 tens_visit_dict = dict()
88
89 def visit_op(op):
90 if op in op_visit_dict:
91 return op_visit_dict[op]
92 op_visit_dict[op] = op
93
94 for tens in op.inputs:
95 visit_tens(tens)
96
97 for visit in op_visit_list:
98 visit(op, arch)
99
100 for tens in op.outputs:
101 visit_tens(tens)
102
103 return op
104
105 def visit_tens(tens):
106 if tens in tens_visit_dict:
107 return tens_visit_dict[tens]
108
109 tens_visit_dict[tens] = tens
110
111 for op in tens.ops:
112 visit_op(op)
113
114 for visit in tensor_visit_list:
115 visit(tens, arch)
116
117 return tens
118
119 for tens in sg.output_tensors:
120 visit_tens(tens)
121
122 sg.refresh_after_modification()
123
124 return sg
125
126
127def verify_graph_health(nng):
128
129 for sg in nng.subgraphs:
130 verify_subgraph_health(sg)
131
132 return True
133
134
135def verify_subgraph_health(sg):
136 op_visit_dict = dict()
137 tens_visit_dict = dict()
138
139 def visit_op(op):
140 if op in op_visit_dict:
141 return op_visit_dict[op]
142 op_visit_dict[op] = op
143
144 for tens in op.inputs:
145 assert op in tens.consumers()
146 visit_tens(tens)
147
148 for tens in op.outputs:
149 assert op in tens.ops
150 visit_tens(tens)
151
152 return op
153
154 def visit_tens(tens):
155 if tens in tens_visit_dict:
156 return tens_visit_dict[tens]
157
158 tens_visit_dict[tens] = tens
159
160 for op in tens.ops:
161 assert tens in op.outputs
162 visit_op(op)
163
164 return tens
165
166 for tens in sg.output_tensors:
167 visit_tens(tens)
168
169 return True