blob: f46f031a7122f5d26626dfba2d487f788916bbd3 [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# Vela separates CPU operations and NPU operations into separate internal subgraphs. The CPU operations are left
18# untouched in the final output.
19#
20# Vela does this by identifying NPU passes and pulling them out from the main CPU graph into separate subgraphs, invoked
21# by NpuOp operations. Later, Vela generates command streams and compressed weight streams for the NPU subgraphs and
22# attaches them to the NpuOp. This encapsulates everything the NPU subgraph is supposed to do.
Tim Hall79d07d22020-04-27 18:20:16 +010023import numpy as np
24
Diego Russoe8a10452020-04-21 17:39:10 +010025from .nn_graph import Pass
26from .nn_graph import PassPlacement
27from .nn_graph import Subgraph
Louis Verhaardaee5d752020-09-30 09:01:52 +020028from .operation import CustomType
Diego Russoe8a10452020-04-21 17:39:10 +010029from .operation import NpuBlockType
Louis Verhaardaee5d752020-09-30 09:01:52 +020030from .operation import Op
Diego Russoe8a10452020-04-21 17:39:10 +010031from .operation import Operation
Diego Russoea6111a2020-04-14 18:41:58 +010032
Tim Hall79d07d22020-04-27 18:20:16 +010033
34def make_npu_call_op_pass(npu_subgraph):
Louis Verhaardaee5d752020-09-30 09:01:52 +020035 op = Operation(Op.CustomNpuOp, "call_" + npu_subgraph.name)
Tim Hall79d07d22020-04-27 18:20:16 +010036 op.attrs["subgraph"] = npu_subgraph
Louis Verhaardaee5d752020-09-30 09:01:52 +020037 op.attrs["custom_type"] = CustomType.NpuOp
Tim Hall79d07d22020-04-27 18:20:16 +010038 ps = Pass(op.name, PassPlacement.MemoryOnly, False, NpuBlockType.Default)
39 ps.ops = [op]
40 ps.primary_op = op
Tim Hall79d07d22020-04-27 18:20:16 +010041 op.scheduled_pass = ps
42
43 # Inputs and outputs filled in later as we cut the graphs
44 return ps
45
46
47def switch_tensor_for_op(op, orig_tens, new_tens):
48
49 op.inputs = [new_tens if tens == orig_tens else tens for tens in op.inputs]
50 op.outputs = [new_tens if tens == orig_tens else tens for tens in op.outputs]
51
52 ps = op.scheduled_pass
53 if ps is None:
54 return
55
56 ps.inputs = [new_tens if tens == orig_tens else tens for tens in ps.inputs]
57 ps.outputs = [new_tens if tens == orig_tens else tens for tens in ps.outputs]
58
59 if ps.ifm_tensor == orig_tens:
60 ps.ifm_tensor = new_tens
61 if ps.ifm2_tensor == orig_tens:
62 ps.ifm2_tensor = new_tens
63 if ps.ofm_tensor == orig_tens:
64 ps.ofm_tensor = new_tens
65 if ps.weight_tensor == orig_tens:
66 ps.weight_tensor = new_tens
67 if ps.scale_tensor == orig_tens:
68 ps.scale_tensor = new_tens
69
70
71def rewrite_tensor_cpu_producer_npu_consumers(
72 orig_tens, call_ps, startup_init_ps, npu_subgraph, cpu_subgraph, subgraph_for_pass
73):
Louis Verhaardaee5d752020-09-30 09:01:52 +020074 is_const = orig_tens.ops[0].type == Op.Const
Tim Hall79d07d22020-04-27 18:20:16 +010075 new_tens = orig_tens.clone("_npu")
Tim Hall79d07d22020-04-27 18:20:16 +010076
Louis Verhaardaee5d752020-09-30 09:01:52 +020077 op_type = Op.SubgraphInput
Tim Hall79d07d22020-04-27 18:20:16 +010078 if is_const:
Louis Verhaardaee5d752020-09-30 09:01:52 +020079 op_type = Op.Const
Tim Hall79d07d22020-04-27 18:20:16 +010080 op = Operation(op_type, orig_tens.name + "_input")
Tim Hall79d07d22020-04-27 18:20:16 +010081 op.scheduled_pass = startup_init_ps
Michael McGeaghc5b549b2020-08-07 11:54:28 +010082 op.set_output_tensor(new_tens)
Tim Hall79d07d22020-04-27 18:20:16 +010083 startup_init_ps.ops.append(op)
84 startup_init_ps.outputs.append(new_tens)
85
86 if not is_const:
87 call_ps.inputs.append(orig_tens)
88 call_ps.primary_op.inputs.append(orig_tens)
89
90 for op in list(orig_tens.consumers()):
91 if op is None:
92 continue # Subgraph consumers handled separately.
93 ps = op.scheduled_pass
94 if subgraph_for_pass[ps] == npu_subgraph:
95 switch_tensor_for_op(op, orig_tens, new_tens)
96 orig_tens.consumer_list.remove(op)
97 new_tens.consumer_list.append(op)
98
99 # Deal with output tensors for the NPU graph. These are special.
100 npu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in npu_subgraph.output_tensors]
Johan Alfvén628928d2022-01-27 06:47:26 +0100101 for tens in npu_subgraph.output_tensors:
102 # Enforce output tensor from NPU graph to use normal NHWC output
103 tens.needs_linear_format = True
Tim Hall79d07d22020-04-27 18:20:16 +0100104
105
106def rewrite_tensor_npu_producer_cpu_consumers(
107 orig_tens, call_ps, startup_init_ps, npu_subgraph, cpu_subgraph, subgraph_for_pass
108):
109
James Ward93389782021-10-14 12:58:02 +0100110 new_tens = orig_tens.clone("")
111 orig_tens.name = orig_tens.name + "_cpu"
Tim Hall79d07d22020-04-27 18:20:16 +0100112 npu_subgraph.output_tensors.append(orig_tens)
113
114 call_ps.outputs.append(new_tens)
115 call_ps.primary_op.outputs.append(new_tens)
116 new_tens.ops = [call_ps.primary_op]
117
118 for op in list(orig_tens.consumers()):
119 if op is None:
120 continue # Subgraph consumers handled separately.
121 ps = op.scheduled_pass
122 if subgraph_for_pass[ps] != npu_subgraph:
123 switch_tensor_for_op(op, orig_tens, new_tens)
124 orig_tens.consumer_list.remove(op)
125 new_tens.consumer_list.append(op)
126
127 # Deal with output tensors for the CPU graph. These are special.
128 cpu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in cpu_subgraph.output_tensors]
129
130
131def extract_subgraph(nng, orig_sg, arch):
132 assert orig_sg.placement == PassPlacement.Cpu
133
134 passes = list(orig_sg.passes)
135 place_vec = np.array([ps.placement for ps in passes])
136 place_vec[
137 place_vec == PassPlacement.StartupInit
138 ] = PassPlacement.Cpu # Keep the startup init pass on the CPU, we'll make new ones to move onto NPU.
139
140 # MemoryOnly passes that are either squeezed between NPU passes or on the boundary of NPU and CPU
Fredrik Svedberg2b5939f2021-10-14 15:16:30 +0200141 # passes should be assigned to the NPU, unless they are assigned to run on CPU explicitly.
Tim Hall79d07d22020-04-27 18:20:16 +0100142
143 # Forward, then backwards
144 for is_reversed in range(2):
145 last_place = PassPlacement.Cpu
146 seq = enumerate(place_vec)
147 if is_reversed:
148 seq = reversed(list(seq))
149 for idx, place in seq:
Fredrik Svedberg2b5939f2021-10-14 15:16:30 +0200150 if place == PassPlacement.MemoryOnly and passes[idx].ops[0].run_on_npu:
Tim Hall79d07d22020-04-27 18:20:16 +0100151 if last_place == PassPlacement.Npu:
152 place = PassPlacement.Npu
153 place_vec[idx] = place
154
155 if place != PassPlacement.MemoryOnly:
156 last_place = place
157
158 # Anything left, assign to the CPU.
159 place_vec[place_vec == PassPlacement.MemoryOnly] = PassPlacement.Cpu
160
161 if np.all(place_vec == PassPlacement.Cpu):
162 return [] # Nothing to do
163
164 # Create the subgraphs and split passes between them
165
166 new_subgraphs = []
167 split_count = 0
168 subgraph_for_pass = {}
169 orig_sg.passes = []
170 call_pass = {}
171 startup_init_passes = {}
172
173 last_place = PassPlacement.Cpu
174 curr_sg = orig_sg
175
176 for idx, place in enumerate(place_vec):
177 if place != last_place:
178 if place == PassPlacement.Npu:
179 split_count += 1
180 curr_sg = Subgraph("%s_split_%d" % (orig_sg.name, split_count), PassPlacement.Npu)
181 new_subgraphs.append(curr_sg)
182 call_ps = make_npu_call_op_pass(curr_sg)
183 subgraph_for_pass[call_ps] = orig_sg
184 orig_sg.passes.append(call_ps)
185 call_pass[curr_sg] = call_ps
186
187 startup_init_ps = Pass(
188 curr_sg.name + "_startup_init", PassPlacement.StartupInit, False, NpuBlockType.Default
189 )
190 curr_sg.passes.append(startup_init_ps)
191 startup_init_passes[curr_sg] = startup_init_ps
192 subgraph_for_pass[startup_init_ps] = curr_sg
193
194 else:
195 curr_sg = orig_sg
196 last_place = place
197 ps = passes[idx]
198 subgraph_for_pass[ps] = curr_sg
199 curr_sg.passes.append(ps)
200
201 # Rewrite tensors to fix up graphs.
202
203 for curr_sg in new_subgraphs:
204 for ps in curr_sg.passes:
205 for tens in ps.inputs:
206 source_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.ops]
207 assert len(source_sgs) >= 0
208 producer_sg = source_sgs[0]
209 for sg in source_sgs:
210 assert sg == producer_sg # All need to be the same.
211
212 if producer_sg != curr_sg:
213 assert (
214 producer_sg == orig_sg
215 ) # Because we go in-order, all the producers must be the original graph.
216 rewrite_tensor_cpu_producer_npu_consumers(
217 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
218 )
219
220 for tens in ps.outputs:
221
222 dest_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.consumers() if op is not None]
223 need_rewrite = False
224 for sg in dest_sgs:
225 if sg != curr_sg:
226 need_rewrite = True
227 break
228 if tens in orig_sg.output_tensors:
229 need_rewrite = True
230
231 if need_rewrite:
232 rewrite_tensor_npu_producer_cpu_consumers(
233 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
234 )
235
236 return new_subgraphs
237
238
239def extract_npu_subgraphs(nng, arch):
240
241 nng.refresh_after_modification()
242
243 for sg in list(nng.subgraphs):
244 if sg.placement == PassPlacement.Cpu:
245 new_subgraphs = extract_subgraph(nng, sg, arch)
246 nng.subgraphs += new_subgraphs
247
248 nng.refresh_after_modification()
249 nng.prune_startup_init_pass()
250
251 for sg in nng.subgraphs:
252 sg.build_pass_links()