blob: 015634c857fea8d298af7cc9d98135eedb9f9fac [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
Johan Alfvén8d57aaa2022-02-04 11:19:17 +010090 # Elementwise op can not overwrite ifm if input is used by many consumers
91 if orig_tens in cpu_subgraph.input_tensors and len(orig_tens.consumers()) > 1:
92 new_tens.ifm_write_protected = True
93
94 # Elementwise op can not overwrite ifm if tensor is used as output from sub graph
95 if orig_tens in cpu_subgraph.output_tensors:
96 new_tens.ifm_write_protected = True
97
Tim Hall79d07d22020-04-27 18:20:16 +010098 for op in list(orig_tens.consumers()):
99 if op is None:
100 continue # Subgraph consumers handled separately.
101 ps = op.scheduled_pass
102 if subgraph_for_pass[ps] == npu_subgraph:
103 switch_tensor_for_op(op, orig_tens, new_tens)
104 orig_tens.consumer_list.remove(op)
105 new_tens.consumer_list.append(op)
106
107 # Deal with output tensors for the NPU graph. These are special.
108 npu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in npu_subgraph.output_tensors]
109
110
111def rewrite_tensor_npu_producer_cpu_consumers(
112 orig_tens, call_ps, startup_init_ps, npu_subgraph, cpu_subgraph, subgraph_for_pass
113):
114
James Ward93389782021-10-14 12:58:02 +0100115 new_tens = orig_tens.clone("")
116 orig_tens.name = orig_tens.name + "_cpu"
Tim Hall79d07d22020-04-27 18:20:16 +0100117 npu_subgraph.output_tensors.append(orig_tens)
118
119 call_ps.outputs.append(new_tens)
120 call_ps.primary_op.outputs.append(new_tens)
121 new_tens.ops = [call_ps.primary_op]
122
123 for op in list(orig_tens.consumers()):
124 if op is None:
125 continue # Subgraph consumers handled separately.
126 ps = op.scheduled_pass
127 if subgraph_for_pass[ps] != npu_subgraph:
128 switch_tensor_for_op(op, orig_tens, new_tens)
129 orig_tens.consumer_list.remove(op)
130 new_tens.consumer_list.append(op)
131
132 # Deal with output tensors for the CPU graph. These are special.
133 cpu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in cpu_subgraph.output_tensors]
134
135
136def extract_subgraph(nng, orig_sg, arch):
137 assert orig_sg.placement == PassPlacement.Cpu
138
139 passes = list(orig_sg.passes)
140 place_vec = np.array([ps.placement for ps in passes])
141 place_vec[
142 place_vec == PassPlacement.StartupInit
143 ] = PassPlacement.Cpu # Keep the startup init pass on the CPU, we'll make new ones to move onto NPU.
144
145 # MemoryOnly passes that are either squeezed between NPU passes or on the boundary of NPU and CPU
Fredrik Svedberg2b5939f2021-10-14 15:16:30 +0200146 # passes should be assigned to the NPU, unless they are assigned to run on CPU explicitly.
Tim Hall79d07d22020-04-27 18:20:16 +0100147
148 # Forward, then backwards
149 for is_reversed in range(2):
150 last_place = PassPlacement.Cpu
151 seq = enumerate(place_vec)
152 if is_reversed:
153 seq = reversed(list(seq))
154 for idx, place in seq:
Fredrik Svedberg2b5939f2021-10-14 15:16:30 +0200155 if place == PassPlacement.MemoryOnly and passes[idx].ops[0].run_on_npu:
Tim Hall79d07d22020-04-27 18:20:16 +0100156 if last_place == PassPlacement.Npu:
157 place = PassPlacement.Npu
158 place_vec[idx] = place
159
160 if place != PassPlacement.MemoryOnly:
161 last_place = place
162
163 # Anything left, assign to the CPU.
164 place_vec[place_vec == PassPlacement.MemoryOnly] = PassPlacement.Cpu
165
166 if np.all(place_vec == PassPlacement.Cpu):
167 return [] # Nothing to do
168
169 # Create the subgraphs and split passes between them
170
171 new_subgraphs = []
172 split_count = 0
173 subgraph_for_pass = {}
174 orig_sg.passes = []
175 call_pass = {}
176 startup_init_passes = {}
177
178 last_place = PassPlacement.Cpu
179 curr_sg = orig_sg
180
181 for idx, place in enumerate(place_vec):
182 if place != last_place:
183 if place == PassPlacement.Npu:
184 split_count += 1
185 curr_sg = Subgraph("%s_split_%d" % (orig_sg.name, split_count), PassPlacement.Npu)
186 new_subgraphs.append(curr_sg)
187 call_ps = make_npu_call_op_pass(curr_sg)
188 subgraph_for_pass[call_ps] = orig_sg
189 orig_sg.passes.append(call_ps)
190 call_pass[curr_sg] = call_ps
191
192 startup_init_ps = Pass(
193 curr_sg.name + "_startup_init", PassPlacement.StartupInit, False, NpuBlockType.Default
194 )
195 curr_sg.passes.append(startup_init_ps)
196 startup_init_passes[curr_sg] = startup_init_ps
197 subgraph_for_pass[startup_init_ps] = curr_sg
198
199 else:
200 curr_sg = orig_sg
201 last_place = place
202 ps = passes[idx]
203 subgraph_for_pass[ps] = curr_sg
204 curr_sg.passes.append(ps)
205
206 # Rewrite tensors to fix up graphs.
207
208 for curr_sg in new_subgraphs:
209 for ps in curr_sg.passes:
210 for tens in ps.inputs:
211 source_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.ops]
212 assert len(source_sgs) >= 0
213 producer_sg = source_sgs[0]
214 for sg in source_sgs:
215 assert sg == producer_sg # All need to be the same.
216
217 if producer_sg != curr_sg:
218 assert (
219 producer_sg == orig_sg
220 ) # Because we go in-order, all the producers must be the original graph.
221 rewrite_tensor_cpu_producer_npu_consumers(
222 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
223 )
224
225 for tens in ps.outputs:
226
227 dest_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.consumers() if op is not None]
228 need_rewrite = False
229 for sg in dest_sgs:
230 if sg != curr_sg:
231 need_rewrite = True
232 break
233 if tens in orig_sg.output_tensors:
234 need_rewrite = True
235
236 if need_rewrite:
237 rewrite_tensor_npu_producer_cpu_consumers(
238 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
239 )
240
Johan Alfvén211165a2022-02-06 15:30:07 +0100241 for tens in curr_sg.output_tensors:
242 # ofm can depend on multiple ops. These ops can be divided into different NPU
243 # nodes due to CPU nodes. If that is the case the ofm must be NHWC.
244 tens.needs_linear_format = True
245
Tim Hall79d07d22020-04-27 18:20:16 +0100246 return new_subgraphs
247
248
249def extract_npu_subgraphs(nng, arch):
250
251 nng.refresh_after_modification()
252
253 for sg in list(nng.subgraphs):
254 if sg.placement == PassPlacement.Cpu:
255 new_subgraphs = extract_subgraph(nng, sg, arch)
256 nng.subgraphs += new_subgraphs
257
258 nng.refresh_after_modification()
259 nng.prune_startup_init_pass()
260
261 for sg in nng.subgraphs:
262 sg.build_pass_links()