blob: c0430b5d131d592564146682e3bb7b1cf33ce9bf [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
28from .operation import NpuBlockType
29from .operation import Operation
Diego Russoea6111a2020-04-14 18:41:58 +010030
Tim Hall79d07d22020-04-27 18:20:16 +010031
32def make_npu_call_op_pass(npu_subgraph):
33 op = Operation("NpuOp", "call_" + npu_subgraph.name)
34 op.attrs["subgraph"] = npu_subgraph
35 ps = Pass(op.name, PassPlacement.MemoryOnly, False, NpuBlockType.Default)
36 ps.ops = [op]
37 ps.primary_op = op
38 op.attrs["npu_block_type"] = ps.npu_block_type
39 op.scheduled_pass = ps
40
41 # Inputs and outputs filled in later as we cut the graphs
42 return ps
43
44
45def switch_tensor_for_op(op, orig_tens, new_tens):
46
47 op.inputs = [new_tens if tens == orig_tens else tens for tens in op.inputs]
48 op.outputs = [new_tens if tens == orig_tens else tens for tens in op.outputs]
49
50 ps = op.scheduled_pass
51 if ps is None:
52 return
53
54 ps.inputs = [new_tens if tens == orig_tens else tens for tens in ps.inputs]
55 ps.outputs = [new_tens if tens == orig_tens else tens for tens in ps.outputs]
56
57 if ps.ifm_tensor == orig_tens:
58 ps.ifm_tensor = new_tens
59 if ps.ifm2_tensor == orig_tens:
60 ps.ifm2_tensor = new_tens
61 if ps.ofm_tensor == orig_tens:
62 ps.ofm_tensor = new_tens
63 if ps.weight_tensor == orig_tens:
64 ps.weight_tensor = new_tens
65 if ps.scale_tensor == orig_tens:
66 ps.scale_tensor = new_tens
67
68
69def rewrite_tensor_cpu_producer_npu_consumers(
70 orig_tens, call_ps, startup_init_ps, npu_subgraph, cpu_subgraph, subgraph_for_pass
71):
72 is_const = orig_tens.ops[0].type == "Const"
Tim Hall79d07d22020-04-27 18:20:16 +010073 new_tens = orig_tens.clone("_npu")
Tim Hall79d07d22020-04-27 18:20:16 +010074
75 op_type = "SubgraphInput"
76 if is_const:
77 op_type = "Const"
78 op = Operation(op_type, orig_tens.name + "_input")
79 op.attrs["npu_block_type"] = NpuBlockType.Default
Tim Hall79d07d22020-04-27 18:20:16 +010080 op.scheduled_pass = startup_init_ps
Michael McGeaghc5b549b2020-08-07 11:54:28 +010081 op.set_output_tensor(new_tens)
Tim Hall79d07d22020-04-27 18:20:16 +010082 startup_init_ps.ops.append(op)
83 startup_init_ps.outputs.append(new_tens)
84
85 if not is_const:
86 call_ps.inputs.append(orig_tens)
87 call_ps.primary_op.inputs.append(orig_tens)
88
89 for op in list(orig_tens.consumers()):
90 if op is None:
91 continue # Subgraph consumers handled separately.
92 ps = op.scheduled_pass
93 if subgraph_for_pass[ps] == npu_subgraph:
94 switch_tensor_for_op(op, orig_tens, new_tens)
95 orig_tens.consumer_list.remove(op)
96 new_tens.consumer_list.append(op)
97
98 # Deal with output tensors for the NPU graph. These are special.
99 npu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in npu_subgraph.output_tensors]
100
101
102def rewrite_tensor_npu_producer_cpu_consumers(
103 orig_tens, call_ps, startup_init_ps, npu_subgraph, cpu_subgraph, subgraph_for_pass
104):
105
106 new_tens = orig_tens.clone("_cpu")
Tim Hall79d07d22020-04-27 18:20:16 +0100107 npu_subgraph.output_tensors.append(orig_tens)
108
109 call_ps.outputs.append(new_tens)
110 call_ps.primary_op.outputs.append(new_tens)
111 new_tens.ops = [call_ps.primary_op]
112
113 for op in list(orig_tens.consumers()):
114 if op is None:
115 continue # Subgraph consumers handled separately.
116 ps = op.scheduled_pass
117 if subgraph_for_pass[ps] != npu_subgraph:
118 switch_tensor_for_op(op, orig_tens, new_tens)
119 orig_tens.consumer_list.remove(op)
120 new_tens.consumer_list.append(op)
121
122 # Deal with output tensors for the CPU graph. These are special.
123 cpu_subgraph.output_tensors = [new_tens if tens == orig_tens else tens for tens in cpu_subgraph.output_tensors]
124
125
126def extract_subgraph(nng, orig_sg, arch):
127 assert orig_sg.placement == PassPlacement.Cpu
128
129 passes = list(orig_sg.passes)
130 place_vec = np.array([ps.placement for ps in passes])
131 place_vec[
132 place_vec == PassPlacement.StartupInit
133 ] = PassPlacement.Cpu # Keep the startup init pass on the CPU, we'll make new ones to move onto NPU.
134
135 # MemoryOnly passes that are either squeezed between NPU passes or on the boundary of NPU and CPU
136 # passes should be assigned to the NPU.
137
138 # Forward, then backwards
139 for is_reversed in range(2):
140 last_place = PassPlacement.Cpu
141 seq = enumerate(place_vec)
142 if is_reversed:
143 seq = reversed(list(seq))
144 for idx, place in seq:
145 if place == PassPlacement.MemoryOnly:
146 if last_place == PassPlacement.Npu:
147 place = PassPlacement.Npu
148 place_vec[idx] = place
149
150 if place != PassPlacement.MemoryOnly:
151 last_place = place
152
153 # Anything left, assign to the CPU.
154 place_vec[place_vec == PassPlacement.MemoryOnly] = PassPlacement.Cpu
155
156 if np.all(place_vec == PassPlacement.Cpu):
157 return [] # Nothing to do
158
159 # Create the subgraphs and split passes between them
160
161 new_subgraphs = []
162 split_count = 0
163 subgraph_for_pass = {}
164 orig_sg.passes = []
165 call_pass = {}
166 startup_init_passes = {}
167
168 last_place = PassPlacement.Cpu
169 curr_sg = orig_sg
170
171 for idx, place in enumerate(place_vec):
172 if place != last_place:
173 if place == PassPlacement.Npu:
174 split_count += 1
175 curr_sg = Subgraph("%s_split_%d" % (orig_sg.name, split_count), PassPlacement.Npu)
176 new_subgraphs.append(curr_sg)
177 call_ps = make_npu_call_op_pass(curr_sg)
178 subgraph_for_pass[call_ps] = orig_sg
179 orig_sg.passes.append(call_ps)
180 call_pass[curr_sg] = call_ps
181
182 startup_init_ps = Pass(
183 curr_sg.name + "_startup_init", PassPlacement.StartupInit, False, NpuBlockType.Default
184 )
185 curr_sg.passes.append(startup_init_ps)
186 startup_init_passes[curr_sg] = startup_init_ps
187 subgraph_for_pass[startup_init_ps] = curr_sg
188
189 else:
190 curr_sg = orig_sg
191 last_place = place
192 ps = passes[idx]
193 subgraph_for_pass[ps] = curr_sg
194 curr_sg.passes.append(ps)
195
196 # Rewrite tensors to fix up graphs.
197
198 for curr_sg in new_subgraphs:
199 for ps in curr_sg.passes:
200 for tens in ps.inputs:
201 source_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.ops]
202 assert len(source_sgs) >= 0
203 producer_sg = source_sgs[0]
204 for sg in source_sgs:
205 assert sg == producer_sg # All need to be the same.
206
207 if producer_sg != curr_sg:
208 assert (
209 producer_sg == orig_sg
210 ) # Because we go in-order, all the producers must be the original graph.
211 rewrite_tensor_cpu_producer_npu_consumers(
212 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
213 )
214
215 for tens in ps.outputs:
216
217 dest_sgs = [subgraph_for_pass[op.scheduled_pass] for op in tens.consumers() if op is not None]
218 need_rewrite = False
219 for sg in dest_sgs:
220 if sg != curr_sg:
221 need_rewrite = True
222 break
223 if tens in orig_sg.output_tensors:
224 need_rewrite = True
225
226 if need_rewrite:
227 rewrite_tensor_npu_producer_cpu_consumers(
228 tens, call_pass[curr_sg], startup_init_passes[curr_sg], curr_sg, orig_sg, subgraph_for_pass
229 )
230
231 return new_subgraphs
232
233
234def extract_npu_subgraphs(nng, arch):
235
236 nng.refresh_after_modification()
237
238 for sg in list(nng.subgraphs):
239 if sg.placement == PassPlacement.Cpu:
240 new_subgraphs = extract_subgraph(nng, sg, arch)
241 nng.subgraphs += new_subgraphs
242
243 nng.refresh_after_modification()
244 nng.prune_startup_init_pass()
245
246 for sg in nng.subgraphs:
247 sg.build_pass_links()