blob: a5a58e8a10439fbe1affb7780a89cb5874465016 [file] [log] [blame]
Louis Verhaardebf4af62021-01-27 15:57:57 +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# Internal representation of a Neural Network Operation.
Louis Verhaarde8a5a782020-11-02 18:04:27 +010018import copy
Louis Verhaardaee5d752020-09-30 09:01:52 +020019from collections import namedtuple
20from enum import Enum
Dwight Lidman9b43f842020-12-08 17:56:44 +010021from typing import Any
22from typing import Dict
23from typing import List
Louis Verhaarde8a5a782020-11-02 18:04:27 +010024from typing import Optional
Louis Verhaardebf4af62021-01-27 15:57:57 +010025from typing import Tuple
Dwight Lidman9b43f842020-12-08 17:56:44 +010026from typing import TYPE_CHECKING
Tim Hall79d07d22020-04-27 18:20:16 +010027
Louis Verhaard1a92f782021-02-09 16:08:26 +010028from .api import NpuRoundingMode
Michael McGeagh528a56d2020-12-16 11:33:21 +000029from .errors import VelaError
Tim Hall4ed38bc2020-10-20 18:54:20 +010030from .numeric_util import full_shape
patrik.gustavssoneeb85152020-12-21 17:10:40 +000031from .shape4d import Shape4D
Tim Hall4ed38bc2020-10-20 18:54:20 +010032
Patrik Gustavsson2349d422020-12-01 16:02:29 +010033
Dwight Lidman9b43f842020-12-08 17:56:44 +010034if TYPE_CHECKING:
35 from .tensor import Tensor
36
Tim Hall4ed38bc2020-10-20 18:54:20 +010037PointXY = namedtuple("PointXY", "x y")
38PointXYZ = namedtuple("PointXYZ", "x y z")
39
Tim Hall79d07d22020-04-27 18:20:16 +010040
Louis Verhaardaee5d752020-09-30 09:01:52 +020041class NpuBlockType(Enum):
Tim Hall79d07d22020-04-27 18:20:16 +010042 Default = 0
43 ConvolutionMxN = 1
44 VectorProduct = 2
45 Pooling = 3
46 ConvolutionDepthWise = 4
47 ElementWise = 5
Fredrik Svedberga0c36242020-06-03 15:43:31 +020048 ReduceSum = 6
Tim Hall79d07d22020-04-27 18:20:16 +010049
50
Tim Hall4ed38bc2020-10-20 18:54:20 +010051class Kernel:
Louis Verhaarde8a5a782020-11-02 18:04:27 +010052 """
53 Kernel information for NPU operations
54 """
55
56 def __init__(self, w: int, h: int, stride_x: int = 1, stride_y: int = 1, dilation_x: int = 1, dilation_y: int = 1):
57 assert stride_x > 0 and stride_y > 0
58 assert dilation_x > 0 and dilation_y > 0
Tim Hall4ed38bc2020-10-20 18:54:20 +010059 self.width = w
60 self.height = h
Louis Verhaarde8a5a782020-11-02 18:04:27 +010061 self.stride = PointXY(stride_x, stride_y)
62 self.dilation = PointXY(dilation_x, dilation_y)
Tim Hall4ed38bc2020-10-20 18:54:20 +010063
Louis Verhaarde8a5a782020-11-02 18:04:27 +010064 def elements_wh(self) -> int:
Tim Hall4ed38bc2020-10-20 18:54:20 +010065 return self.width * self.height
66
Louis Verhaarde8a5a782020-11-02 18:04:27 +010067 def area_width(self) -> int:
Tim Hall4ed38bc2020-10-20 18:54:20 +010068 return (self.width - 1) * self.dilation.x + 1
69
Louis Verhaarde8a5a782020-11-02 18:04:27 +010070 def area_height(self) -> int:
Tim Hall4ed38bc2020-10-20 18:54:20 +010071 return (self.height - 1) * self.dilation.y + 1
72
Louis Verhaardebf4af62021-01-27 15:57:57 +010073 def dilated_wh(self) -> Tuple[int, int]:
74 """Returns the dilated kernel width/height"""
75 return self.dilation.x * (self.width - 1) + 1, self.dilation.y * (self.height - 1) + 1
76
Louis Verhaarde8a5a782020-11-02 18:04:27 +010077 def __str__(self):
78 return f"w={self.width}, h={self.height}, stride={tuple(self.stride)}, dilation={tuple(self.dilation)}"
79
Tim Hall4ed38bc2020-10-20 18:54:20 +010080
Louis Verhaardaee5d752020-09-30 09:01:52 +020081# Classifies operators of type Custom
82class CustomType(Enum):
83 ThirdPartyOp = 0 # Third party custom op
84 NpuOp = 1 # NPU op
85 ExistingNpuOp = 2 # NPU op that was part of the input network
86
87
88TensorIndices = namedtuple("TensorIndices", ["ifms", "weights", "biases"])
89
90NO_INDICES = TensorIndices([], [], [])
91IFM_INDICES = TensorIndices([0], [], [])
92IFM_WEIGHTS_INDICES = TensorIndices([0], [1], [])
93IFM_WEIGHTS_BIAS_INDICES = TensorIndices([0], [1], [2])
94IFM_IFM2_INDICES = TensorIndices([0, 1], [], [])
95CONV2D_BACKPROP_INDICES = TensorIndices([2], [1], [3])
96TRANSPOSE_CONV_INDICES = TensorIndices([0], [1], [3])
97CONCAT_INDICES = TensorIndices([1, 2], [], [])
98SPLIT_IFM_INDICES = TensorIndices([1], [], [])
99BLOCK_LSTM_INDICES = TensorIndices([3], [4], [])
100
101
102# Static information related to operation codes
103class OperatorInfo:
104 __slots__ = ("id", "block_type", "indices", "is_unary")
105 _id = 0
106
107 def __init__(self, block_type=NpuBlockType.Default, indices=NO_INDICES, is_unary=False):
108 OperatorInfo._id += 1
109 self.id = OperatorInfo._id
110 self.block_type = block_type
111 self.indices = indices # Indices of the different tensor purposes
112 self.is_unary = is_unary # Classifies elementwise operators
113
114
115# Internally used operation codes
116class Op(Enum):
117 Abs = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_INDICES, is_unary=True)
118 Add = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES)
119 AddN = OperatorInfo()
120 Any = OperatorInfo()
121 ArgMax = OperatorInfo()
122 ArgMin = OperatorInfo()
123 AvgPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=IFM_INDICES)
124 BatchMatMul = OperatorInfo()
125 BatchToSpaceND = OperatorInfo()
126 BidirectionalSequenceLstm = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
127 BidirectionalSequenceRnn = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
128 BlockLSTM = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=BLOCK_LSTM_INDICES)
129
130 CLZ = OperatorInfo(
131 block_type=NpuBlockType.ElementWise, indices=IFM_INDICES, is_unary=True
132 ) # NPU specific operation
133 Call = OperatorInfo()
134 Cast = OperatorInfo()
135 Ceil = OperatorInfo()
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100136 Clip = OperatorInfo() # NPU specific fused activation function for clipping between activation.min/max
Louis Verhaardaee5d752020-09-30 09:01:52 +0200137 Concat = OperatorInfo(indices=CONCAT_INDICES)
138 ConcatEmbeddings = OperatorInfo()
139 ConcatSliceWrite = OperatorInfo(indices=IFM_INDICES)
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100140 ConcatTFLite = OperatorInfo(indices=CONCAT_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200141 Const = OperatorInfo() # Constant tensor, only used in CPU subgraphs
142 Conv2D = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=IFM_WEIGHTS_INDICES)
143 Conv2DBackpropInput = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=CONV2D_BACKPROP_INDICES)
144 Conv2DBackpropInputSwitchedBias = OperatorInfo(
145 block_type=NpuBlockType.ConvolutionMxN, indices=TRANSPOSE_CONV_INDICES
146 )
147 Conv2DBias = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=IFM_WEIGHTS_BIAS_INDICES)
148 Cos = OperatorInfo()
Tim Hall42abec12021-02-04 21:31:57 +0000149 Cumsum = OperatorInfo()
Louis Verhaardaee5d752020-09-30 09:01:52 +0200150 Custom = OperatorInfo() # Custom 3rd party operator, only used in CPU subgraphs
151 CustomNpuOp = OperatorInfo() # NPU custom operator, only used in CPU subgraphs
152 DMA = OperatorInfo()
153 Delegate = OperatorInfo()
154 Densify = OperatorInfo()
155 DepthToSpace = OperatorInfo()
156 DepthwiseConv2DBias = OperatorInfo(block_type=NpuBlockType.ConvolutionDepthWise, indices=IFM_WEIGHTS_BIAS_INDICES)
Louis Verhaard04f8c002020-10-09 11:40:21 +0200157 Dequantize = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200158 Div = OperatorInfo()
159 Elu = OperatorInfo()
160 EmbeddingLookup = OperatorInfo()
161 EmbeddingLookupSparse = OperatorInfo()
162 Equal = OperatorInfo()
163 Exp = OperatorInfo()
164 ExpandDims = OperatorInfo(indices=IFM_INDICES)
165 FakeQuantWithMinMaxArgs = OperatorInfo()
166 Fill = OperatorInfo()
167 Floor = OperatorInfo()
168 FloorDiv = OperatorInfo()
169 FloorMod = OperatorInfo()
170 FullyConnected = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_BIAS_INDICES)
171 GatherNd = OperatorInfo()
172 GatherV2 = OperatorInfo()
173 Greater = OperatorInfo()
174 GreaterEqual = OperatorInfo()
Diqing Zhong189f7482021-01-26 12:12:51 +0100175 HardSwish = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200176 HashtableLookup = OperatorInfo()
177 Identity = OperatorInfo()
178 If = OperatorInfo()
179 L2Norm = OperatorInfo()
180 L2Pool2D = OperatorInfo()
181 LRN = OperatorInfo()
182 LSHProjection = OperatorInfo()
183 LeakyRelu = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_INDICES, is_unary=True)
184 Less = OperatorInfo()
185 LessEqual = OperatorInfo()
186 Log = OperatorInfo()
187 LogSoftmax = OperatorInfo()
188 LogicalAnd = OperatorInfo()
189 LogicalNot = OperatorInfo()
190 LogicalOr = OperatorInfo()
191 Lstm = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
192 LUT = OperatorInfo() # NPU specific, operator has LUT, only used in fused activation functions
193 MatMul = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
194 MatrixDiag = OperatorInfo()
195 MatrixSetDiag = OperatorInfo()
196 Max = OperatorInfo()
197 MaxPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=IFM_INDICES)
198 Maximum = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES)
Dwight Lidman4f728c02020-12-17 15:14:45 +0100199 Mean = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200200 Min = OperatorInfo()
201 Minimum = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES)
202 MirrorPad = OperatorInfo()
203 Mul = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES)
204 Neg = OperatorInfo()
205 NonMaxSuppressionV4 = OperatorInfo()
206 NonMaxSuppressionV5 = OperatorInfo()
207 NotEqual = OperatorInfo()
208 OneHot = OperatorInfo()
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100209 Pack = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200210 PackReshaped = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardae2d5532020-12-11 17:19:54 +0100211 Pad = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200212 PadV2 = OperatorInfo()
213 Placeholder = OperatorInfo() # Only used in CPU subgraphs
214 Pow = OperatorInfo()
215 Prelu = OperatorInfo()
216 Prod = OperatorInfo()
Louis Verhaard04f8c002020-10-09 11:40:21 +0200217 Quantize = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200218 QuantizedAvgPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=IFM_INDICES)
219 QuantizedConv2D = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=IFM_WEIGHTS_INDICES)
220 QuantizedMatMul = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
221 QuantizedMaxPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=IFM_INDICES)
222 QuantizedReshape = OperatorInfo(indices=IFM_INDICES)
223 Range = OperatorInfo()
224 Rank = OperatorInfo()
225 ReduceSum = OperatorInfo(block_type=NpuBlockType.ReduceSum, indices=IFM_INDICES)
226 Relu = OperatorInfo(indices=IFM_INDICES)
227 Relu6 = OperatorInfo(indices=IFM_INDICES)
228 ReluN1To1 = OperatorInfo(indices=IFM_INDICES)
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100229 RescaleAdd = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200230 Reshape = OperatorInfo(indices=IFM_INDICES)
231 ResizeBilinear = OperatorInfo(block_type=NpuBlockType.Pooling, indices=IFM_INDICES)
232 ResizeNearestNeighbor = OperatorInfo()
233 ReverseSequence = OperatorInfo()
234 ReverseV2 = OperatorInfo()
235 Rnn = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
236 Round = OperatorInfo()
237 Rsqrt = OperatorInfo()
238 SHL = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES) # NPU specific operation
239 SHR = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES) # NPU specific operation
240 ScatterNd = OperatorInfo()
241 SegmentSum = OperatorInfo()
242 Select = OperatorInfo()
243 SelectV2 = OperatorInfo()
244 Shape = OperatorInfo()
245 Sigmoid = OperatorInfo(indices=IFM_INDICES)
246 SignBit = OperatorInfo()
247 Sin = OperatorInfo()
248 SkipGram = OperatorInfo()
249 Slice = OperatorInfo(indices=IFM_INDICES)
Michael McGeagh65fd9982020-10-20 11:49:28 +0100250 Softmax = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200251 SpaceToBatchND = OperatorInfo()
252 SpaceToDepth = OperatorInfo()
253 SparseToDense = OperatorInfo()
254 Split = OperatorInfo(indices=SPLIT_IFM_INDICES)
255 SplitSliceRead = OperatorInfo(indices=IFM_INDICES)
Jacob Bohline3de4e52020-11-27 14:52:06 +0100256 SplitV = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200257 Sqrt = OperatorInfo()
258 Square = OperatorInfo()
259 SquaredDifference = OperatorInfo()
260 Squeeze = OperatorInfo(indices=IFM_INDICES)
261 StridedSlice = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200262 Sub = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=IFM_IFM2_INDICES)
263 SubgraphInput = OperatorInfo() # Only used in CPU subgraphs
264 Sum = OperatorInfo()
265 Svdf = OperatorInfo()
266 Tanh = OperatorInfo(indices=IFM_INDICES)
267 Tile = OperatorInfo()
268 TopKV2 = OperatorInfo()
269 Transpose = OperatorInfo()
270 UnidirectionalSequenceLstm = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
271 UnidirectionalSequenceRnn = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=IFM_WEIGHTS_INDICES)
272 Unique = OperatorInfo()
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100273 Unpack = OperatorInfo(indices=IFM_INDICES)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200274 UnpackReshaped = OperatorInfo(indices=IFM_INDICES)
275 Where = OperatorInfo()
276 While = OperatorInfo()
277 ZerosLike = OperatorInfo()
278
279 @property
280 def info(self):
281 return self.value
282
283 @property
284 def npu_block_type(self):
285 return self.info.block_type
286
287 def is_conv2d_op(self):
288 return self.info.block_type == NpuBlockType.ConvolutionMxN
289
290 def is_depthwise_conv2d_op(self):
291 return self.info.block_type == NpuBlockType.ConvolutionDepthWise
292
293 def is_pool_op(self):
294 return self.info.block_type == NpuBlockType.Pooling
295
296 def is_maxpool_op(self):
297 return self in (Op.MaxPool, Op.QuantizedMaxPool)
298
299 def is_avgpool_op(self):
300 return self in (Op.QuantizedAvgPool, Op.AvgPool)
301
302 def is_elementwise_op(self):
303 return self.info.block_type == NpuBlockType.ElementWise
304
305 def is_unary_elementwise_op(self):
306 return self.info.block_type == NpuBlockType.ElementWise and self.info.is_unary
307
308 def is_binary_elementwise_op(self):
309 return self.info.block_type == NpuBlockType.ElementWise and not self.info.is_unary
310
311 def is_relu_op(self):
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100312 return self in (Op.Relu, Op.Relu6, Op.ReluN1To1, Op.Clip)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200313
314 def is_activation_op(self):
Diqing Zhong189f7482021-01-26 12:12:51 +0100315 return self.is_relu_op() or self in (Op.Tanh, Op.Sigmoid, Op.Softmax, Op.LUT, Op.HardSwish)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200316
317 def is_split_op(self):
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100318 return self in (Op.Split, Op.SplitV, Op.StridedSlice, Op.Slice, Op.UnpackReshaped, Op.Unpack)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200319
320 def is_concat_op(self):
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100321 return self in (Op.Concat, Op.ConcatTFLite, Op.PackReshaped, Op.Pack)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200322
323 def needs_bias(self):
324 return bool(self.info.indices.biases)
325
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100326 def needs_shapes(self):
327 return bool(self.info.indices.ifms)
328
Louis Verhaardaee5d752020-09-30 09:01:52 +0200329 @classmethod
330 def op_set(cls, predicate):
331 # Returns the set of all operator codes that fulfill the given predicate
332 return {op_type for op_type in Op if predicate(op_type)}
333
334 def __str__(self):
335 return self.name
336
337 __repr__ = __str__
338
339 def __lt__(self, other):
340 return self.value.id < other.value.id
341
342
Michael McGeagh16895482020-12-14 15:51:20 +0000343class Padding(Enum):
344 SAME = 0
345 VALID = 1
Louis Verhaardae2d5532020-12-11 17:19:54 +0100346 EXPLICIT = 2 # Padding is specified in a PAD operation (only used for NPU operations)
Michael McGeagh16895482020-12-14 15:51:20 +0000347
348
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100349class ActivationFunction:
350 """Fused activation function"""
351
352 def __init__(self, op_type: Op):
353 self.op_type = op_type # The activation operation to be performed
354 # min/max are optional; if present they are non-quantized values
355 self.min: Optional[float] = None
356 self.max: Optional[float] = None
357 # Table lookup index, only applicable for Op.LUT activation, 0-7
358 self.lut_index: int = 0
359
360 def clone(self):
361 res = copy.copy(self)
362 return res
363
364
365def create_activation_function(op_type: Op) -> ActivationFunction:
366 """Creates activation function with min/max depending on op_type"""
367 act = ActivationFunction(op_type)
368 if op_type == Op.Relu:
369 act.min = 0.0
370 elif op_type == Op.Relu6:
371 act.min = 0.0
372 act.max = 6.0
373 elif op_type == Op.ReluN1To1:
374 act.min = -1.0
375 act.max = 1.0
376 elif op_type == Op.Tanh:
377 act.min = -1.0
378 act.max = 1.0
379 elif op_type == Op.Sigmoid:
380 act.min = 0.0
381 act.max = 1.0
Diqing Zhong189f7482021-01-26 12:12:51 +0100382 elif op_type == Op.HardSwish:
383 act.min = 0.0
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100384 return act
385
386
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000387def get_slice_offsets(input_shape: List[int], offset_tens: int, offset_mask: int, is_begin: bool = True):
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200388 # For strided slice operator: get start or end offsets
389 offsets = len(input_shape) * [0] if is_begin else input_shape[:]
390 for idx in range(len(input_shape)):
391 # If the i:th bit in the mask is set then the value on offset_tens[i] should be ignored
392 if (offset_mask & (1 << idx)) == 0:
393 offsets[idx] = offset_tens.values[idx]
394 if offsets[idx] < 0:
395 # Convert offset to positive value
396 offsets[idx] += input_shape[idx]
397 return offsets
398
399
Tim Hall79d07d22020-04-27 18:20:16 +0100400class Operation:
401 """Class representing a Neural Network operation. Has a name, a type,
Dwight Lidmanc6ac1942020-10-02 14:55:45 +0200402 input and output tensors, as well as an attribute dictionary."""
Tim Hall79d07d22020-04-27 18:20:16 +0100403
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200404 __slots__ = (
405 "type",
406 "name",
407 "op_index",
408 "attrs",
409 "inputs",
410 "outputs",
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100411 "intermediates",
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200412 "flops",
413 "scheduled_pass",
414 "run_on_npu",
Louis Verhaardaee5d752020-09-30 09:01:52 +0200415 "activation",
416 "memory_function",
Dwight Lidman4f728c02020-12-17 15:14:45 +0100417 "forced_input_quantization",
Louis Verhaardaee5d752020-09-30 09:01:52 +0200418 "forced_output_quantization",
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200419 "activation_lut",
Tim Hall4ed38bc2020-10-20 18:54:20 +0100420 "_kernel",
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100421 "ifm_shapes",
422 "ofm_shapes",
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100423 "rescale",
Patrik Gustavssone3b1b912021-02-09 15:38:46 +0100424 "read_offsets",
Louis Verhaard1a92f782021-02-09 16:08:26 +0100425 "rounding_mode",
Dwight Lidman4f728c02020-12-17 15:14:45 +0100426 "low_precision_scaling",
Louis Verhaardc822d622021-03-11 14:59:06 +0100427 "write_offset",
428 "write_shape",
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200429 )
Tim Hall79d07d22020-04-27 18:20:16 +0100430
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100431 def __init__(self, op_type: Op, name: str):
Tim Hall79d07d22020-04-27 18:20:16 +0100432 self.type = op_type
433 self.name = name
Dwight Lidman9b43f842020-12-08 17:56:44 +0100434 self.attrs: Dict[str, Any] = {}
435 self.inputs: List[Tensor] = []
436 self.outputs: List[Tensor] = []
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100437 self.intermediates: List[Tensor] = []
Tim Hall79d07d22020-04-27 18:20:16 +0100438 self.flops = 0
439 self.run_on_npu = True
Louis Verhaardaee5d752020-09-30 09:01:52 +0200440 # Fused activation function. If not none: operator code.
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100441 self.activation: Optional[ActivationFunction] = None
Louis Verhaardaee5d752020-09-30 09:01:52 +0200442 # Fused memory function, if not None: operator code
Louis Verhaardc822d622021-03-11 14:59:06 +0100443 self.memory_function: Optional[Op] = None
Louis Verhaardaee5d752020-09-30 09:01:52 +0200444 # If not none: contains QuantizationParameters to be used as output quantization
445 # (which overrides the ofm tensor's quantization), used in LUT
Dwight Lidman4f728c02020-12-17 15:14:45 +0100446 self.forced_input_quantization = None
Louis Verhaardaee5d752020-09-30 09:01:52 +0200447 self.forced_output_quantization = None
Tim Hall79d07d22020-04-27 18:20:16 +0100448 self.scheduled_pass = None
Tim Hallc8310b12020-06-17 14:53:11 +0100449 self.op_index = None # input network operator index
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200450 self.activation_lut = None
Tim Hall4ed38bc2020-10-20 18:54:20 +0100451 self._kernel = None
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000452 self.ifm_shapes: List[Shape4D] = []
453 self.ofm_shapes: List[Shape4D] = []
Fredrik Svedberge82be7c2021-01-18 15:21:03 +0100454 # If not none: contains rescale to be used as output scaling
455 # (which overrides the ofm tensor's scale)
456 self.rescale = None
Patrik Gustavssone3b1b912021-02-09 15:38:46 +0100457 self.read_offsets: List[Shape4D] = [None, None] # offset for [ifm, ifm2]
Louis Verhaard1a92f782021-02-09 16:08:26 +0100458 self.rounding_mode: Optional[NpuRoundingMode] = None
Dwight Lidman4f728c02020-12-17 15:14:45 +0100459 # The Mean operator (implemented as a depthwise convolution) requires scaling
460 # to be calculated differently in one case. In that case, this is set to True.
461 self.low_precision_scaling = False
Louis Verhaardc822d622021-03-11 14:59:06 +0100462 # Write offset, for operations that only produce a part of the OFM
463 self.write_offset: Optional[Shape4D] = None
464 # The amount of OFM that is produced by the operation (only if write_offset is not None).
465 # E.g. an operation that only fills the bottom row of an OFM of size 1x10x8x1 would have
466 # write_offset 0,9,0,0, write_shape 1,1,8,1
467 self.write_shape: Optional[Shape4D] = None
Tim Hall79d07d22020-04-27 18:20:16 +0100468
469 def clone(self, suffix="_clone"):
470 res = Operation(self.type, self.name + suffix)
471
472 res.attrs = dict(self.attrs)
473 res.inputs = list(self.inputs)
474 res.outputs = list(self.outputs)
Fredrik Svedberg8d0f4892021-02-16 21:59:50 +0100475 res.intermediates = list(self.intermediates)
Tim Hall79d07d22020-04-27 18:20:16 +0100476 res.flops = self.flops
Louis Verhaardaee5d752020-09-30 09:01:52 +0200477 res.run_on_npu = self.run_on_npu
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100478 res.activation = None if self.activation is None else self.activation.clone()
Louis Verhaardaee5d752020-09-30 09:01:52 +0200479 res.memory_function = self.memory_function
Dwight Lidman4f728c02020-12-17 15:14:45 +0100480 res.forced_input_quantization = self.forced_input_quantization
Louis Verhaardaee5d752020-09-30 09:01:52 +0200481 res.forced_output_quantization = self.forced_output_quantization
Tim Hall79d07d22020-04-27 18:20:16 +0100482 res.scheduled_pass = self.scheduled_pass
Tim Hallc8310b12020-06-17 14:53:11 +0100483 res.op_index = None # not relevant as not part of input network
Patrik Gustavssone3b1b912021-02-09 15:38:46 +0100484 res.read_offsets = list(self.read_offsets)
Louis Verhaard1a92f782021-02-09 16:08:26 +0100485 res.rounding_mode = self.rounding_mode
Dwight Lidman4f728c02020-12-17 15:14:45 +0100486 res.low_precision_scaling = self.low_precision_scaling
Tim Hall79d07d22020-04-27 18:20:16 +0100487
488 return res
489
490 def __str__(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200491 return "<nng.Operation '{}' type={}>".format(self.name, self.type)
Tim Hall79d07d22020-04-27 18:20:16 +0100492
493 __repr__ = __str__
494
Michael McGeagh65fd9982020-10-20 11:49:28 +0100495 def get_kernel_size(self):
Tim Hall4ed38bc2020-10-20 18:54:20 +0100496 weights = self.weights
497 if weights and self.type.npu_block_type in (NpuBlockType.ConvolutionDepthWise, NpuBlockType.ConvolutionMxN):
498 weight_shape = full_shape(4, weights.shape, 1)
Michael McGeagh65fd9982020-10-20 11:49:28 +0100499 h = weight_shape[-4]
500 w = weight_shape[-3]
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100501 elif self.type.npu_block_type in (NpuBlockType.Pooling, NpuBlockType.ReduceSum) and "ksize" in self.attrs:
502 h, w = self.attrs["ksize"][1:3]
Tim Hall4ed38bc2020-10-20 18:54:20 +0100503 else:
Michael McGeagh65fd9982020-10-20 11:49:28 +0100504 h = self.attrs.get("filter_height", 1)
505 w = self.attrs.get("filter_width", 1)
506 return w, h
507
508 def get_kernel_stride(self):
509 if "strides" in self.attrs:
510 _, h, w, _ = self.attrs["strides"]
511 else:
512 h = self.attrs.get("stride_h", 1)
513 w = self.attrs.get("stride_w", 1)
514 return w, h
515
516 def get_kernel_dilation(self):
517 if "dilation" in self.attrs:
518 _, h, w, _ = self.attrs["dilation"]
519 else:
520 h = self.attrs.get("dilation_h_factor", 1)
521 w = self.attrs.get("dilation_w_factor", 1)
522 return w, h
523
524 @property
525 def kernel(self):
526 k_w, k_h = self.get_kernel_size()
527 s_w, s_h = self.get_kernel_stride()
528 d_w, d_h = self.get_kernel_dilation()
529 self._kernel = Kernel(k_w, k_h, s_w, s_h, d_w, d_h)
Tim Hall4ed38bc2020-10-20 18:54:20 +0100530 return self._kernel
531
Tim Hall79d07d22020-04-27 18:20:16 +0100532 def get_ifm_ifm2_weights_ofm(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200533 return self.ifm, self.ifm2, self.weights, self.ofm
Tim Hall79d07d22020-04-27 18:20:16 +0100534
535 def get_ifm_weights_biases_ofm(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200536 return self.ifm, self.weights, self.bias, self.ofm
Tim Hall79d07d22020-04-27 18:20:16 +0100537
Jacob Bohlin49d92122020-08-19 14:36:46 +0200538 def get_ifm_ifm2_weights_biases_ofm(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200539 return self.ifm, self.ifm2, self.weights, self.bias, self.ofm
Jacob Bohlin49d92122020-08-19 14:36:46 +0200540
Louis Verhaardaee5d752020-09-30 09:01:52 +0200541 def get_ifm_ofm(self):
542 return self.ifm, self.ofm
Jacob Bohlin49d92122020-08-19 14:36:46 +0200543
Louis Verhaardaee5d752020-09-30 09:01:52 +0200544 @property
545 def ifm(self):
546 # Gets the IFM tensor, or None if not applicable
547 return self.get_input(self.type.info.indices.ifms, 0)
Jacob Bohlin49d92122020-08-19 14:36:46 +0200548
Louis Verhaardaee5d752020-09-30 09:01:52 +0200549 @property
550 def ifm2(self):
551 # Gets the IFM2 tensor, or None if not applicable
552 return self.get_input(self.type.info.indices.ifms, 1)
Louis Verhaard98a34992020-09-01 10:39:04 +0200553
Louis Verhaardaee5d752020-09-30 09:01:52 +0200554 @property
555 def bias(self):
556 # Gets the bias tensor, or None if not applicable
557 return self.get_input(self.type.info.indices.biases, 0)
558
559 @property
560 def weights(self):
561 # Gets the weight tensor, or None if not applicable
562 return self.get_input(self.type.info.indices.weights, 0)
563
564 def get_ifm_tensors(self):
565 # Gets the IFM tensors, or empty list if not applicable
566 return self._index_list_to_tensors(self.type.info.indices.ifms)
567
568 def get_weight_tensors(self):
569 # Gets the weight tensors, or empty list if not applicable
570 return self._index_list_to_tensors(self.type.info.indices.weights)
571
572 def get_bias_tensors(self):
573 # Gets the bias tensors, or empty list if not applicable
574 return self._index_list_to_tensors(self.type.info.indices.biases)
575
576 def _index_list_to_tensors(self, index_list):
577 return [self.inputs[ix] for ix in index_list if ix < len(self.inputs)]
578
579 def get_input(self, index_list, ix):
580 if ix >= len(index_list):
581 return None
582 if index_list[ix] >= len(self.inputs):
583 return None
584 return self.inputs[index_list[ix]]
585
586 @property
587 def ofm(self):
588 # Gets the OFM tensor, or None if not applicable
589 return self.outputs[0] if self.outputs else None
Tim Hall79d07d22020-04-27 18:20:16 +0100590
591 def get_concat_inputs_axis(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200592 assert self.type.is_concat_op()
Tim Hall79d07d22020-04-27 18:20:16 +0100593
Louis Verhaardaee5d752020-09-30 09:01:52 +0200594 if self.type == Op.Concat:
Tim Hall79d07d22020-04-27 18:20:16 +0100595 axis_tensor = self.inputs[0]
596 inputs = self.inputs[1:]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200597 elif self.type == Op.ConcatTFLite:
Tim Hall79d07d22020-04-27 18:20:16 +0100598 inputs = self.inputs
599 axis = self.attrs["axis"]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200600 elif self.type == Op.PackReshaped:
Tim Hall79d07d22020-04-27 18:20:16 +0100601 # Requires fixup_pack_input to be called before this point
602 inputs = self.inputs
603 axis = self.attrs["axis"]
604 assert len(self.inputs) == self.attrs["values_count"]
605 else:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200606 assert len(axis_tensor.ops) == 1 and axis_tensor.ops[0].type == Op.Const
Tim Hall79d07d22020-04-27 18:20:16 +0100607 axis = int(axis_tensor.values)
608
609 return inputs, axis
610
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200611 def get_dilation_h_w(self):
612 _, dilation_h, dilation_w, _ = self.attrs.get("dilation", (1, 1, 1, 1))
613 return dilation_h, dilation_w
614
Tim Hall79d07d22020-04-27 18:20:16 +0100615 def get_split_inputs_axis(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200616 assert self.type.is_split_op()
Tim Hall79d07d22020-04-27 18:20:16 +0100617
618 offset_start = None
619 offset_end = None
620 axis = None
Louis Verhaardaee5d752020-09-30 09:01:52 +0200621 if self.type == Op.Split:
Tim Hall79d07d22020-04-27 18:20:16 +0100622 num_splits = self.attrs.get("num_splits")
623 axis_tens = self.inputs[0]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200624 assert len(axis_tens.ops) == 1 and axis_tens.ops[0].type == Op.Const
Tim Hall79d07d22020-04-27 18:20:16 +0100625 axis = int(axis_tens.values)
626 input_tens = self.inputs[1]
627 outputs = self.outputs
628 assert num_splits == len(outputs)
629
Louis Verhaardaee5d752020-09-30 09:01:52 +0200630 elif self.type == Op.SplitV:
Charles Xu53d47522020-05-04 11:32:05 +0200631 num_splits = self.attrs.get("num_splits")
632 input_tens = self.inputs[0]
633 size_tens = self.inputs[1]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200634 assert len(size_tens.ops) == 1 and size_tens.ops[0].type == Op.Const
Charles Xu53d47522020-05-04 11:32:05 +0200635 sizes = size_tens.values
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200636
Charles Xu53d47522020-05-04 11:32:05 +0200637 axis_tens = self.inputs[2]
Louis Verhaardaee5d752020-09-30 09:01:52 +0200638 assert len(axis_tens.ops) == 1 and axis_tens.ops[0].type == Op.Const
Charles Xu53d47522020-05-04 11:32:05 +0200639 axis = int(axis_tens.values)
Patrik Gustavsson271ddc32020-09-01 09:15:27 +0200640
641 for idx, size in enumerate(sizes):
642 # One but only one size might be set to -1, indicating that size should be inferred
643 if size == -1:
644 sizes[idx] = input_tens.shape[axis] - (sum(sizes) + 1)
645 break
646
Charles Xu53d47522020-05-04 11:32:05 +0200647 outputs = self.outputs
648 assert num_splits == len(outputs)
649 assert sum(sizes) == input_tens.shape[axis]
650
Louis Verhaardaee5d752020-09-30 09:01:52 +0200651 elif self.type == Op.Slice:
Tim Hall79d07d22020-04-27 18:20:16 +0100652 input_tens, begin_tens, size_tens = self.inputs
653 outputs = self.outputs
654 offset_start = [0] * len(input_tens.shape)
655 offset_end = [0] * len(input_tens.shape)
656
657 for idx in range(len(begin_tens.values)):
658 # Check if the op should slice in dimension idx
659 if size_tens.values[idx] != input_tens.shape[idx]:
660 offset_start[idx] = begin_tens.values[idx]
661 offset_end[idx] = size_tens.values[idx] + offset_start[idx]
662
Louis Verhaardaee5d752020-09-30 09:01:52 +0200663 elif self.type == Op.StridedSlice:
Tim Hall79d07d22020-04-27 18:20:16 +0100664 input_tens, begin_tens, end_tens, strides_tens = self.inputs
665 outputs = self.outputs
Tim Hall79d07d22020-04-27 18:20:16 +0100666
667 # Extract masks
668 begin_mask = self.attrs["begin_mask"]
669 ellipsis_mask = self.attrs["ellipsis_mask"]
670 end_mask = self.attrs["end_mask"]
671 new_axis_mask = self.attrs["new_axis_mask"]
672 shrink_axis_mask = self.attrs["shrink_axis_mask"]
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200673
674 # shrink_axis_mask/new_axis_mask/ellipsis_mask is not supported by the Operation class but the operation
Tim Hall79d07d22020-04-27 18:20:16 +0100675 # may have the attribute modified and handled in the graph optimization phase.
Patrik Gustavssoncf728902020-04-30 08:57:23 +0200676 assert shrink_axis_mask == new_axis_mask == ellipsis_mask == 0
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200677 offset_start = get_slice_offsets(input_tens.shape, begin_tens, begin_mask, is_begin=True)
678 offset_end = get_slice_offsets(input_tens.shape, end_tens, end_mask, is_begin=False)
Louis Verhaardaee5d752020-09-30 09:01:52 +0200679 elif self.type == Op.UnpackReshaped:
Tim Hall79d07d22020-04-27 18:20:16 +0100680 # Requires fixup_unpack_output to be called before this point
681 input_tens = self.inputs[0]
682 outputs = self.outputs
683 axis = self.attrs["axis"]
684 num_splits = self.attrs["num"]
685 # Number of outputs have to equal the value of the dimension to unpack
686 assert num_splits == len(outputs) == input_tens.shape[axis]
687 else:
688 assert False
689
690 return input_tens, outputs, axis, offset_start, offset_end
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200691
692 def set_activation_lut(self, lut_tensor):
Louis Verhaarde8a5a782020-11-02 18:04:27 +0100693 self.activation = ActivationFunction(Op.LUT)
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200694 self.activation_lut = lut_tensor
Michael McGeaghc5b549b2020-08-07 11:54:28 +0100695 self.add_input_tensor(lut_tensor)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100696
697 def add_input_tensor(self, tens):
698 self.inputs.append(tens)
699 if self not in tens.consumer_list:
700 tens.consumer_list.append(self)
701
Jacob Bohlin67e0d8f2020-08-20 10:53:02 +0200702 def set_input_tensor(self, tens, idx):
703 tens_to_remove = self.inputs[idx]
704 if tens_to_remove in tens.consumer_list:
705 tens.consumer_list.remove(tens_to_remove)
706
707 self.inputs[idx] = tens
708 if self not in tens.consumer_list:
709 tens.consumer_list.append(self)
710
Dwight Lidman4f728c02020-12-17 15:14:45 +0100711 def get_input_quantization(self):
712 if self.forced_input_quantization is not None:
713 return self.forced_input_quantization
714 return self.ifm.quantization
715
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100716 def set_output_tensor(self, tens):
717 tens.ops = [self]
718 self.outputs = [tens]
Jacob Bohlina41cd4d2020-08-26 18:21:28 +0200719
Louis Verhaard98a34992020-09-01 10:39:04 +0200720 def get_output_quantization(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200721 if self.forced_output_quantization is not None:
722 return self.forced_output_quantization
723 return self.ofm.quantization
Michael McGeagh528a56d2020-12-16 11:33:21 +0000724
725 def error(self, msg):
726 """
727 Raises a VelaError exception for errors encountered when parsing an Operation
728
729 :param self: Operation object that resulted in the error
730 :param msg: str object that contains a description of the specific error encountered
731 """
732
733 def _print_tensors(tensors):
734 lines = []
735 for idx, tens in enumerate(tensors):
736 tens_name = getattr(tens, "name", "Not a Tensor")
737 lines.append(f" {idx} = {tens_name}")
738 return lines
739
740 if self.op_index is None:
741 lines = [f"Invalid {self.type} (name = {self.name}) operator in the internal representation. {msg}"]
742 else:
743 lines = [f"Invalid {self.type} (op_index = {self.op_index}) operator in the input network. {msg}"]
744
745 lines += [" Input tensors:"]
746 lines += _print_tensors(self.inputs)
747
748 lines += [" Output tensors:"]
749 lines += _print_tensors(self.outputs)
750
751 raise VelaError("\n".join(lines))
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100752
753 def set_ifm_ofm_shapes(self):
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000754 self.ifm_shapes = []
755 self.ofm_shapes = []
756
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100757 ifm_tensor, ifm2_tensor, weight_tensor, ofm_tensor = self.get_ifm_ifm2_weights_ofm()
758
759 # set all shapes to op, as 4D
760 if self.type == Op.FullyConnected:
Patrik Gustavsson2c2522d2021-01-29 11:51:31 +0100761 if len(self.ifm.shape) == 2:
762 self.ifm_shapes.append(Shape4D([self.ifm.shape[0], 1, 1, self.ifm.shape[1]]))
763 else:
764 # Special case, handled in graph optimization
765 self.ifm_shapes.append(Shape4D(ifm_tensor.get_full_shape()))
766 if len(self.ofm.shape) == 2:
767 self.ofm_shapes.append(Shape4D([self.ofm.shape[0], 1, 1, self.ofm.shape[1]]))
768 else:
769 self.ofm_shapes.append(Shape4D(ofm_tensor.get_full_shape()))
770 if self.type == Op.Softmax:
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000771 self.ifm_shapes.append(Shape4D(ifm_tensor.get_full_shape()))
772 self.ofm_shapes.append(Shape4D(ofm_tensor.get_full_shape()))
Patrik Gustavssonda2b0032021-02-04 16:28:29 +0100773 elif self.type.is_split_op() or self.type.is_concat_op():
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100774 for inp in self.inputs:
775 if inp is not None:
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000776 self.ifm_shapes.append(Shape4D(full_shape(4, inp.shape, 1)))
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100777 else:
778 self.ifm_shapes.append(None)
779 for out in self.outputs:
780 if out is not None:
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000781 self.ofm_shapes.append(Shape4D(full_shape(4, out.shape, 1)))
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100782 else:
783 self.ofm_shapes.append(None)
784 else:
Patrik Gustavssonda2b0032021-02-04 16:28:29 +0100785 if ifm_tensor is not None:
786 self.ifm_shapes.append(Shape4D(full_shape(4, ifm_tensor.shape, 1)))
Patrik Gustavsson2349d422020-12-01 16:02:29 +0100787 if ifm2_tensor is not None:
patrik.gustavssoneeb85152020-12-21 17:10:40 +0000788 self.ifm_shapes.append(Shape4D(full_shape(4, ifm2_tensor.shape, 1)))
Patrik Gustavssonda2b0032021-02-04 16:28:29 +0100789 if ofm_tensor is not None:
790 self.ofm_shapes.append(Shape4D(full_shape(4, ofm_tensor.shape, 1)))