blob: d802b513fb19deb891b68a826c5da77aebb2d658 [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]
101
102
103def rewrite_tensor_npu_producer_cpu_consumers(
104 orig_tens, call_ps, startup_init_ps, npu_subgraph, cpu_subgraph, subgraph_for_pass
105):
106
James Ward93389782021-10-14 12:58:02 +0100107 new_tens = orig_tens.clone("")
108 orig_tens.name = orig_tens.name + "_cpu"
Tim Hall79d07d22020-04-27 18:20:16 +0100109 npu_subgraph.output_tensors.append(orig_tens)
110
111 call_ps.outputs.append(new_tens)
112 call_ps.primary_op.outputs.append(new_tens)
113 new_tens.ops = [call_ps.primary_op]
114
115 for op in list(orig_tens.consumers()):
116 if op is None:
117 continue # Subgraph consumers handled separately.
118 ps = op.scheduled_pass
119 if subgraph_for_pass[ps] != npu_subgraph:
120 switch_tensor_for_op(op, orig_tens, new_tens)
121 orig_tens.consumer_list.remove(op)
122 new_tens.consumer_list.append(op)
123
124 # Deal with output tensors for the CPU graph. These are special.
125 cpu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in cpu_subgraph.output_tensors]
126
127
128def extract_subgraph(nng, orig_sg, arch):
129 assert orig_sg.placement == PassPlacement.Cpu
130
131 passes = list(orig_sg.passes)
132 place_vec = np.array([ps.placement for ps in passes])
133 place_vec[
134 place_vec == PassPlacement.StartupInit
135 ] = PassPlacement.Cpu # Keep the startup init pass on the CPU, we'll make new ones to move onto NPU.
136
137 # MemoryOnly passes that are either squeezed between NPU passes or on the boundary of NPU and CPU
Fredrik Svedberg2b5939f2021-10-14 15:16:30 +0200138 # passes should be assigned to the NPU, unless they are assigned to run on CPU explicitly.
Tim Hall79d07d22020-04-27 18:20:16 +0100139
140 # Forward, then backwards
141 for is_reversed in range(2):
142 last_place = PassPlacement.Cpu
143 seq = enumerate(place_vec)
144 if is_reversed:
145 seq = reversed(list(seq))
146 for idx, place in seq:
Fredrik Svedberg2b5939f2021-10-14 15:16:30 +0200147 if place == PassPlacement.MemoryOnly and passes[idx].ops[0].run_on_npu:
Tim Hall79d07d22020-04-27 18:20:16 +0100148 if last_place == PassPlacement.Npu:
149 place = PassPlacement.Npu
150 place_vec[idx] = place
151
152 if place != PassPlacement.MemoryOnly:
153 last_place = place
154
155 # Anything left, assign to the CPU.
156 place_vec[place_vec == PassPlacement.MemoryOnly] = PassPlacement.Cpu
157
158 if np.all(place_vec == PassPlacement.Cpu):
159 return [] # Nothing to do
160
161 # Create the subgraphs and split passes between them
162
163 new_subgraphs = []
164 split_count = 0
165 subgraph_for_pass = {}
166 orig_sg.passes = []
167 call_pass = {}
168 startup_init_passes = {}
169
170 last_place = PassPlacement.Cpu
171 curr_sg = orig_sg
172
173 for idx, place in enumerate(place_vec):
174 if place != last_place:
175 if place == PassPlacement.Npu:
176 split_count += 1
177 curr_sg = Subgraph("%s_split_%d" % (orig_sg.name, split_count), PassPlacement.Npu)
178 new_subgraphs.append(curr_sg)
179 call_ps = make_npu_call_op_pass(curr_sg)
180 subgraph_for_pass[call_ps] = orig_sg
181 orig_sg.passes.append(call_ps)
182 call_pass[curr_sg] = call_ps
183
184 startup_init_ps = Pass(
185 curr_sg.name + "_startup_init", PassPlacement.StartupInit, False, NpuBlockType.Default
186 )
187 curr_sg.passes.append(startup_init_ps)
188 startup_init_passes[curr_sg] = startup_init_ps
189 subgraph_for_pass[startup_init_ps] = curr_sg
190
191 else:
192 curr_sg = orig_sg
193 last_place = place
194 ps = passes[idx]
195 subgraph_for_pass[ps] = curr_sg
196 curr_sg.passes.append(ps)
197
198 # Rewrite tensors to fix up graphs.
199
200 for curr_sg in new_subgraphs:
201 for ps in curr_sg.passes:
202 for tens in ps.inputs:
203 source_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.ops]
204 assert len(source_sgs) >= 0
205 producer_sg = source_sgs[0]
206 for sg in source_sgs:
207 assert sg == producer_sg # All need to be the same.
208
209 if producer_sg != curr_sg:
210 assert (
211 producer_sg == orig_sg
212 ) # Because we go in-order, all the producers must be the original graph.
213 rewrite_tensor_cpu_producer_npu_consumers(
214 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
215 )
216
217 for tens in ps.outputs:
218
219 dest_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.consumers() if op is not None]
220 need_rewrite = False
221 for sg in dest_sgs:
222 if sg != curr_sg:
223 need_rewrite = True
224 break
225 if tens in orig_sg.output_tensors:
226 need_rewrite = True
227
228 if need_rewrite:
229 rewrite_tensor_npu_producer_cpu_consumers(
230 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
231 )
232
233 return new_subgraphs
234
235
236def extract_npu_subgraphs(nng, arch):
237
238 nng.refresh_after_modification()
239
240 for sg in list(nng.subgraphs):
241 if sg.placement == PassPlacement.Cpu:
242 new_subgraphs = extract_subgraph(nng, sg, arch)
243 nng.subgraphs += new_subgraphs
244
245 nng.refresh_after_modification()
246 nng.prune_startup_init_pass()
247
248 for sg in nng.subgraphs:
249 sg.build_pass_links()