blob: 530ad78e73a92346004e323999b01ecf51999fcd [file] [log] [blame]
Patrik Gustavssone3b1b912021-02-09 15:38:46 +01001# Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved.
Tim Hall79d07d22020-04-27 18:20:16 +01002#
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# Packs a subgraph with Neural Network Operations into Passes. Each Pass has one or more Operations.
Diego Russoea6111a2020-04-14 18:41:58 +010018import collections
Diego Russoe8a10452020-04-21 17:39:10 +010019import enum
Diego Russoea6111a2020-04-14 18:41:58 +010020
Tim Halle6ccd872020-11-09 16:46:37 +000021from .debug_database import DebugDatabase
Diego Russoe8a10452020-04-21 17:39:10 +010022from .nn_graph import Pass
23from .nn_graph import PassPlacement
24from .operation import NpuBlockType
Louis Verhaardaee5d752020-09-30 09:01:52 +020025from .operation import Op
Fredrik Svedbergd9c2c422020-12-01 16:33:45 +010026from .operation_util import create_avgpool_nop
Diego Russoea6111a2020-04-14 18:41:58 +010027from .tensor import TensorPurpose
Tim Hall79d07d22020-04-27 18:20:16 +010028
29
30class PassFlags(enum.Flag):
31 Empty = 0
Patrik Gustavsson56b6c712021-02-16 12:57:03 +010032 Main = 1
33 Post = 2
34 Mac = 4
Tim Halld8339a72021-05-27 18:49:40 +010035 ElementWise = 8
36 Npu = 16
37 Cpu = 32
38 StartupInit = 64
39 MemoryOnly = 128
40 PostFusingLimited = 256
Tim Hall79d07d22020-04-27 18:20:16 +010041
42
Tim Hall79d07d22020-04-27 18:20:16 +010043mac_main_ops = set(
44 (
45 # convolutions
Louis Verhaardaee5d752020-09-30 09:01:52 +020046 Op.Conv2DBias,
47 Op.Conv2D,
48 Op.QuantizedConv2D,
49 Op.Conv2DBackpropInputSwitchedBias,
Tim Hall79d07d22020-04-27 18:20:16 +010050 # depth-wise convolutions
Louis Verhaardaee5d752020-09-30 09:01:52 +020051 Op.DepthwiseConv2DBias,
Tim Hall79d07d22020-04-27 18:20:16 +010052 # FC layers
Louis Verhaardaee5d752020-09-30 09:01:52 +020053 Op.QuantizedMatMul,
54 Op.MatMul,
55 Op.FullyConnected,
Tim Hall79d07d22020-04-27 18:20:16 +010056 # RNN/LSTM/GRU
Louis Verhaardaee5d752020-09-30 09:01:52 +020057 Op.BlockLSTM,
Tim Hall79d07d22020-04-27 18:20:16 +010058 # pooling
Louis Verhaardaee5d752020-09-30 09:01:52 +020059 Op.QuantizedMaxPool,
60 Op.QuantizedAvgPool,
61 Op.AvgPool,
62 Op.MaxPool,
63 Op.ReduceSum,
Dwight Lidman3ec04ac2020-04-30 11:54:48 +020064 # deconvolution
Louis Verhaardaee5d752020-09-30 09:01:52 +020065 Op.ResizeBilinear,
Tim Hall79d07d22020-04-27 18:20:16 +010066 )
67)
68
Louis Verhaardaee5d752020-09-30 09:01:52 +020069binary_elem_wise_main_ops = Op.op_set(Op.is_binary_elementwise_op)
Tim Hall79d07d22020-04-27 18:20:16 +010070
Michael McGeaghf3e3ad72020-12-02 12:39:03 +000071unary_elem_wise_main_ops = Op.op_set(Op.is_unary_elementwise_op)
Tim Hall79d07d22020-04-27 18:20:16 +010072
73elem_wise_main_ops = binary_elem_wise_main_ops | unary_elem_wise_main_ops
74
Louis Verhaardaee5d752020-09-30 09:01:52 +020075activation_ops = Op.op_set(Op.is_relu_op)
76npu_post_ops = activation_ops
Tim Hall79d07d22020-04-27 18:20:16 +010077
78npu_post_fuse_limited_ops = set(
79 # Set of post operators that should not be fused with main/elementwise ops
Patrik Gustavsson138d47f2021-02-08 10:13:48 +010080 (Op.Sigmoid, Op.Tanh, Op.Quantize)
Tim Hall79d07d22020-04-27 18:20:16 +010081)
82
Louis Verhaardaee5d752020-09-30 09:01:52 +020083elem_wise_ops = elem_wise_main_ops | activation_ops | set((Op.Sigmoid, Op.Tanh))
Tim Hall79d07d22020-04-27 18:20:16 +010084
85
Louis Verhaardaee5d752020-09-30 09:01:52 +020086quantization_ops = set((Op.Dequantize, Op.Max, Op.Min))
87cpu_ops = set((Op.Softmax, Op.LRN, Op.Shape, Op.Pad, Op.AddN)) | quantization_ops
Tim Hall79d07d22020-04-27 18:20:16 +010088
patrik.gustavsson10683622020-10-14 10:57:46 +000089startup_init_ops = set((Op.Const, Op.Placeholder, Op.SubgraphInput))
Louis Verhaardaee5d752020-09-30 09:01:52 +020090memory_only_ops = set((Op.Squeeze, Op.Reshape, Op.QuantizedReshape, Op.ExpandDims,))
Tim Hall79d07d22020-04-27 18:20:16 +010091
92
93test_sequence = [
94 (
95 # ops_set
96 npu_post_ops,
97 # incompatible_pack_flags
Patrik Gustavsson56b6c712021-02-16 12:57:03 +010098 PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.Main,
Tim Hall79d07d22020-04-27 18:20:16 +010099 # flags_to_set
100 PassFlags.Npu | PassFlags.Post,
101 # flags_to_clear
102 PassFlags.Empty,
103 ),
104 (
105 # ops_set
106 npu_post_fuse_limited_ops,
107 # incompatible_pack_flags
Tim Hallb1a9a922021-10-29 12:51:53 +0100108 PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.Main | PassFlags.PostFusingLimited,
Tim Hall79d07d22020-04-27 18:20:16 +0100109 # flags_to_set
110 PassFlags.Npu | PassFlags.PostFusingLimited,
111 # flags_to_clear
112 PassFlags.Empty,
113 ),
114 (
115 # ops_set
116 mac_main_ops,
117 # incompatible_pack_flags
Patrik Gustavsson56b6c712021-02-16 12:57:03 +0100118 PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.ElementWise | PassFlags.Main | PassFlags.PostFusingLimited,
Tim Hall79d07d22020-04-27 18:20:16 +0100119 # flags_to_set
120 PassFlags.Npu | PassFlags.Mac | PassFlags.Main,
121 # flags_to_clear
122 PassFlags.Empty,
123 ),
124 (
125 # ops_set
126 elem_wise_main_ops,
127 # incompatible_pack_flags
Patrik Gustavsson56b6c712021-02-16 12:57:03 +0100128 PassFlags.Cpu | PassFlags.MemoryOnly | PassFlags.Mac | PassFlags.Main | PassFlags.PostFusingLimited,
Tim Hall79d07d22020-04-27 18:20:16 +0100129 # flags_to_set
130 PassFlags.Npu | PassFlags.ElementWise | PassFlags.Main,
131 # flags_to_clear
132 PassFlags.Empty,
133 ),
134 (
135 # ops_set
Tim Hall79d07d22020-04-27 18:20:16 +0100136 startup_init_ops,
137 # incompatible_pack_flags
138 PassFlags.Npu | PassFlags.Cpu | PassFlags.MemoryOnly,
139 # flags_to_set
140 PassFlags.StartupInit | PassFlags.Main,
141 # flags_to_clear
142 PassFlags.Empty,
143 ),
144 (
145 # ops_set
146 memory_only_ops,
147 # incompatible_pack_flags
148 PassFlags.Npu | PassFlags.Cpu,
149 # flags_to_set
150 PassFlags.MemoryOnly | PassFlags.Main,
151 # flags_to_clear
Diego Russoea6111a2020-04-14 18:41:58 +0100152 PassFlags.Empty,
Tim Hall79d07d22020-04-27 18:20:16 +0100153 ),
154 (
155 # ops_set
156 cpu_ops,
157 # incompatible_pack_flags
158 PassFlags.Npu | PassFlags.MemoryOnly | PassFlags.Main,
159 # flags_to_set
160 PassFlags.Cpu | PassFlags.Main,
161 # flags_to_clear
Diego Russoea6111a2020-04-14 18:41:58 +0100162 PassFlags.Empty,
Tim Hall79d07d22020-04-27 18:20:16 +0100163 ),
Diego Russoea6111a2020-04-14 18:41:58 +0100164 ( # This last one is a fallback for unrecognised operations
Tim Hall79d07d22020-04-27 18:20:16 +0100165 # ops_set
166 None,
167 # incompatible_pack_flags
168 PassFlags.Npu | PassFlags.MemoryOnly | PassFlags.Main,
169 # flags_to_set
170 PassFlags.Cpu | PassFlags.Main,
171 # flags_to_clear
Diego Russoea6111a2020-04-14 18:41:58 +0100172 PassFlags.Empty,
Tim Hall79d07d22020-04-27 18:20:16 +0100173 ),
174]
175
176# Some sanity checking
177for (operation_set, incompatible_pack_flags, flags_to_set, flags_to_clear) in test_sequence:
178 assert not flags_to_clear & flags_to_set
179
Tim Hall79d07d22020-04-27 18:20:16 +0100180
181def pack_into_passes(nng, arch, verbose_packing=False):
182 def visit_op(op, ignored):
183 visit_op_refcount[op] += 1
184
185 if visit_op_refcount[op] == 1: # First-time visit, go and fix up unused output tensors
186 for tens in op.outputs:
187 if len(tens.consumers()) == 0:
188 visit_op_refcount[op] += 1
189
190 assert visit_op_refcount[op] <= len(op.outputs)
191 if visit_op_refcount[op] == len(op.outputs):
192
193 if op.type in startup_init_ops:
194 startup_list.append(op)
195 else:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200196 ofm_tensor = op.ofm
Tim Hall79d07d22020-04-27 18:20:16 +0100197 if ofm_tensor is None:
198 ofm_tensor = op.outputs[0]
Tim Hall73e843f2021-02-04 22:47:46 +0000199 ofm_shape = op.ofm_shapes[0] if op.run_on_npu else None
Tim Hall79d07d22020-04-27 18:20:16 +0100200
Patrik Gustavsson6bb8f672020-12-21 14:49:13 +0100201 build_pass((op,), ofm_tensor, ofm_shape)
202
203 def build_pass(start_ops_to_process, ofm_tensor=None, ofm_shape=None):
Tim Hall79d07d22020-04-27 18:20:16 +0100204 reverse_ops_list = []
205 curr_flags = PassFlags.Empty
206 npu_block_type = NpuBlockType.Default
207
208 reverse_intermediates = []
209 input_set = set()
210 ifm_tensor = None
211 primary_op = None
Patrik Gustavsson224e99b2021-01-14 10:55:43 +0100212 ifm_shapes = None
Tim Hall79d07d22020-04-27 18:20:16 +0100213
214 to_process = collections.deque()
215 for start_op in start_ops_to_process:
216 to_process.append((start_op, None))
217
218 while to_process:
219 curr_op, tens = to_process.popleft()
220
221 if curr_op in reverse_ops_list:
222 continue
223
224 for operation_set, incompatible_pack_flags, flags_to_set, flags_to_clear in test_sequence:
225 if operation_set is None or curr_op.type in operation_set:
226 if not (curr_flags & incompatible_pack_flags):
227 if flags_to_set & PassFlags.Npu:
228 if not curr_op.run_on_npu:
229 continue
230
231 reverse_ops_list.append(curr_op)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200232 new_block_type = curr_op.type.npu_block_type
Tim Hall79d07d22020-04-27 18:20:16 +0100233 if new_block_type != NpuBlockType.Default:
234 assert npu_block_type == NpuBlockType.Default
235 npu_block_type = new_block_type # Only one major block type per pass
236 assert primary_op is None
237 primary_op = curr_op
238
239 curr_flags &= ~flags_to_clear
240 curr_flags |= flags_to_set
241
242 if flags_to_set & PassFlags.Npu:
243 if flags_to_set & (
244 PassFlags.Mac | PassFlags.ElementWise | PassFlags.Post | PassFlags.PostFusingLimited
245 ):
246 assert len(curr_op.inputs) >= 1
Louis Verhaardaee5d752020-09-30 09:01:52 +0200247 ifm_tensor = curr_op.ifm
Patrik Gustavsson224e99b2021-01-14 10:55:43 +0100248 ifm_shapes = curr_op.ifm_shapes.copy()
Louis Verhaard04f8c002020-10-09 11:40:21 +0200249 assert ifm_tensor is not None, "IFM missing in {}".format(curr_op)
Tim Hall79d07d22020-04-27 18:20:16 +0100250 assert ifm_tensor.purpose == TensorPurpose.FeatureMap
251
Tim Hall79d07d22020-04-27 18:20:16 +0100252 if operation_set is None:
253 print("Warning:", curr_op.type, "operation is unknown or unsupported, placing on CPU")
254
Charles Xu600351a2020-05-18 08:54:47 +0200255 for inp in reversed(curr_op.inputs):
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200256 if inp is None:
257 continue
Patrik Gustavssonfcb1a002021-02-03 09:13:57 +0100258 if can_pack(inp, curr_op):
259 to_process.append((inp.ops[0], inp))
Tim Hall79d07d22020-04-27 18:20:16 +0100260 else:
Tim Hall79d07d22020-04-27 18:20:16 +0100261 input_set.add(inp)
262
263 break
264
265 else:
266 # This operation is not compatible with already packed operations, just register the tensor as an input
267 assert tens is not None
268 input_set.add(tens)
269
270 if curr_flags & PassFlags.Npu and not curr_flags & (PassFlags.ElementWise | PassFlags.Mac):
271 # Make the choice that if we don't have a mac operation, the ambidextrous operations go on the
272 # element wise unit
273 curr_flags |= PassFlags.ElementWise
274
275 is_element_wise = True
276 for op in reverse_ops_list:
Tim Halld8339a72021-05-27 18:49:40 +0100277 if op.type not in elem_wise_ops and op.type:
Tim Hall79d07d22020-04-27 18:20:16 +0100278 is_element_wise = False
279 break
280
281 placement = PassPlacement.Unknown
282 if curr_flags & PassFlags.Npu:
283 assert placement == PassPlacement.Unknown
284 placement = PassPlacement.Npu
285 if curr_flags & PassFlags.Cpu:
286 assert placement == PassPlacement.Unknown
287 placement = PassPlacement.Cpu
288 if curr_flags & PassFlags.MemoryOnly:
289 assert placement == PassPlacement.Unknown
290 placement = PassPlacement.MemoryOnly
291 if curr_flags & PassFlags.StartupInit:
292 assert placement == PassPlacement.Unknown
293 placement = PassPlacement.StartupInit
294 assert placement != PassPlacement.Unknown
295
296 ops_list = list(reversed(reverse_ops_list))
297 intermediates = list(reversed(reverse_intermediates))
298
Diego Russoea6111a2020-04-14 18:41:58 +0100299 if primary_op is None:
Tim Hall79d07d22020-04-27 18:20:16 +0100300 primary_op = create_primary_op(ops_list)
Diego Russoea6111a2020-04-14 18:41:58 +0100301 if primary_op is not None:
Tim Hall79d07d22020-04-27 18:20:16 +0100302 visit_tensor_refcount[primary_op.inputs[0]] += 1
Louis Verhaardaee5d752020-09-30 09:01:52 +0200303 npu_block_type = primary_op.type.npu_block_type
Tim Hall79d07d22020-04-27 18:20:16 +0100304 for input_tens in primary_op.inputs:
305 if input_tens not in input_set:
306 input_set.add(input_tens)
307
308 ordered_input_list = []
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200309 # Keep LUT-s in a separate list and add as inputs at the end
310 # to avoid that they would accidentally be assigned as ifm or ifm2
311 lut_list = []
Tim Hall79d07d22020-04-27 18:20:16 +0100312 input_refcounts = collections.defaultdict(int)
Diqing Zhong2abd3dd2020-08-25 10:40:36 +0200313 input_ops_list = ops_list.copy()
314
315 # Check primary_op first
316 if primary_op is not None:
317 for inp in primary_op.inputs:
Andreas Nevalainend8c032d2020-09-11 10:25:09 +0200318 if inp is None:
319 continue
Diqing Zhong2abd3dd2020-08-25 10:40:36 +0200320 add_input_list(inp, input_set, input_refcounts, lut_list, ordered_input_list)
321 input_ops_list.remove(primary_op)
322
323 # Check rest of the list
324 for op in input_ops_list:
Tim Hall79d07d22020-04-27 18:20:16 +0100325 for inp in op.inputs:
Diqing Zhong2abd3dd2020-08-25 10:40:36 +0200326 add_input_list(inp, input_set, input_refcounts, lut_list, ordered_input_list)
Tim Hall79d07d22020-04-27 18:20:16 +0100327
328 name = ops_list[0].name
Tim Hall79d07d22020-04-27 18:20:16 +0100329 ps = Pass(name, placement, is_element_wise, npu_block_type)
330 ps.ops = ops_list
331 ps.primary_op = primary_op
332 ps.inputs = ordered_input_list
333 ps.intermediates = intermediates
334 ps.outputs = list(ops_list[-1].outputs)
Tim Hall79d07d22020-04-27 18:20:16 +0100335
336 # ElementWise operation, 2 IFMs
337 if ps.primary_op and ps.primary_op.type in binary_elem_wise_main_ops:
Tim Hall79d07d22020-04-27 18:20:16 +0100338 ps.ifm_tensor = ps.inputs[0]
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200339 ps.ifm2_tensor = ps.inputs[-1]
Tim Hall79d07d22020-04-27 18:20:16 +0100340
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200341 if len(ps.inputs) > 2:
342 ps.ifm_tensor = ps.inputs[-2]
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100343
344 # Get the corresponding ifm_shapes
345 for op in input_ops_list + [primary_op]:
Patrik Gustavsson0a261cd2020-12-23 08:50:44 +0100346 if op.run_on_npu:
347 if ps.ifm_tensor == op.ifm:
348 ps.ifm_shapes.append(op.ifm_shapes[0])
349 elif ps.ifm_tensor == op.ifm2:
350 ps.ifm_shapes.append(op.ifm_shapes[1])
Tim Hallffe8e282021-06-24 18:29:53 +0100351
Patrik Gustavsson0a261cd2020-12-23 08:50:44 +0100352 if ps.ifm2_tensor == op.ifm:
353 ps.ifm_shapes.append(op.ifm_shapes[0])
354 elif ps.ifm2_tensor == op.ifm2:
355 ps.ifm_shapes.append(op.ifm_shapes[1])
Tim Hall79d07d22020-04-27 18:20:16 +0100356 else:
357 ps.ifm_tensor = ifm_tensor
358 ps.ifm2_tensor = None
Patrik Gustavssoncc6915c2020-12-22 09:16:50 +0100359 if ps.primary_op is not None and ps.primary_op.run_on_npu:
Patrik Gustavsson224e99b2021-01-14 10:55:43 +0100360 ps.ifm_shapes.append(ifm_shapes[0])
Tim Hall79d07d22020-04-27 18:20:16 +0100361
362 ps.ofm_tensor = ofm_tensor
Patrik Gustavsson6bb8f672020-12-21 14:49:13 +0100363 ps.ofm_shapes.append(ofm_shape)
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100364
Tim Hall79d07d22020-04-27 18:20:16 +0100365 assert ps.placement != PassPlacement.Npu or ps.ofm_tensor is not None
366 ps.weight_tensor = ps.get_primary_op_ifm_weights()[1]
367 ps.scale_tensor = ps.get_primary_op_ifm_weights_biases_ofm()[2]
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200368 ps.lut_tensor = ps.get_primary_op_lut()
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200369 ps.inputs.extend(lut_list)
Tim Hall79d07d22020-04-27 18:20:16 +0100370
371 for op in ps.ops:
372 op.scheduled_pass = ps
373
374 reverse_pass_list.append(ps)
375
376 for inp, refcount in input_refcounts.items():
377 for _ in range(refcount):
378 visit_tensor(inp)
379
380 return ps
381
382 def visit_tensor(tens):
383 visit_tensor_refcount[tens] += 1
384 assert visit_tensor_refcount[tens] <= len(tens.consumers())
385 if visit_tensor_refcount[tens] == len(tens.consumers()):
386 for op in reversed(tens.ops):
387 visit_op(op, tens)
388
Jacob Bohlinfb858732020-08-17 09:42:35 +0200389 def create_primary_op(op_list):
Patrik Gustavssone3b1b912021-02-09 15:38:46 +0100390 if any(op.type in (npu_post_ops | npu_post_fuse_limited_ops) and op.run_on_npu for op in op_list):
Tim Hall79d07d22020-04-27 18:20:16 +0100391 # Configure a 1x1 AvgPool and attach the op onto it
Jacob Bohlinfb858732020-08-17 09:42:35 +0200392 op = op_list[0]
Tim Hall79d07d22020-04-27 18:20:16 +0100393 inp = op.inputs[0]
Michael McGeagh8dbf8cf2020-09-08 11:09:48 +0100394 avgpool_op = create_avgpool_nop(op.name + "_avgpool")
395 avgpool_op.add_input_tensor(inp)
Tim Hall79d07d22020-04-27 18:20:16 +0100396 avgpool_out = inp.clone("_avgpooled")
397 avgpool_out.consumer_list.append(op)
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100398 avgpool_op.set_output_tensor(avgpool_out)
Patrik Gustavsson3a269202021-01-21 08:28:55 +0100399 avgpool_op.ifm_shapes = op.ifm_shapes.copy()
400 avgpool_op.ofm_shapes = op.ofm_shapes.copy()
Patrik Gustavsson56b6c712021-02-16 12:57:03 +0100401 avgpool_op.read_offsets = op.read_offsets.copy()
Tim Hall79d07d22020-04-27 18:20:16 +0100402
403 op.inputs[0] = avgpool_out
Jacob Bohlinfb858732020-08-17 09:42:35 +0200404 op_list.insert(0, avgpool_op)
Tim Hall79d07d22020-04-27 18:20:16 +0100405
Tim Halle6ccd872020-11-09 16:46:37 +0000406 DebugDatabase.add_optimised(op, avgpool_op)
Tim Hall79d07d22020-04-27 18:20:16 +0100407 return avgpool_op
408
409 return None
410
Patrik Gustavssonfcb1a002021-02-03 09:13:57 +0100411 def can_pack(inp, curr_op):
412 if len(inp.ops) == 1:
413 next_op = inp.ops[0]
414 for outp in next_op.outputs:
415 consumers = outp.consumers()
416 if len(consumers) > 1 or (len(consumers) == 1 and consumers[0] != curr_op):
417 return False
418
419 # There cannot be any reshaping between next_op ofm and corresponding curr_op ifm
420 if len(curr_op.ifm_shapes) != 0 and len(next_op.ofm_shapes) != 0:
421 if inp == curr_op.ifm and next_op.ofm_shapes[0] != curr_op.ifm_shapes[0]:
422 return False
423 elif (
424 curr_op.ifm2 is not None and inp == curr_op.ifm2 and next_op.ofm_shapes[0] != curr_op.ifm_shapes[1]
425 ):
426 return False
427 else:
428 return False
429
430 return True
431
Diqing Zhong2abd3dd2020-08-25 10:40:36 +0200432 def add_input_list(inp_to_add, inp_set, inp_refcnts, lut_list, ordered_inp_list):
433 if inp_to_add in inp_set:
434 if inp_refcnts[inp_to_add] == 0:
435 if inp_to_add.purpose == TensorPurpose.LUT:
436 lut_list.append(inp_to_add)
437 else:
438 ordered_inp_list.append(inp_to_add)
439 inp_refcnts[inp_to_add] += 1
440
Tim Hall79d07d22020-04-27 18:20:16 +0100441 for sg in nng.subgraphs:
442 reverse_pass_list = []
443 visit_op_refcount = collections.defaultdict(int)
444 visit_tensor_refcount = collections.defaultdict(int)
445
446 startup_list = []
447
448 for tens in sg.output_tensors:
449 visit_tensor(tens)
450
451 if startup_list:
452 startup_ps = build_pass(startup_list)
453 startup_ps.outputs = [op.outputs[0] for op in startup_list] # Need to fixup the outputs
454 startup_ps.name = "startup_weight_initialisation"
455
456 sg.passes = list(reversed(reverse_pass_list))
457 sg.build_pass_links()
458
459 if verbose_packing:
460 nng.print_passes()
461
462 return nng