Louis Verhaard | ebf4af6 | 2021-01-27 15:57:57 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # Internal representation of a Neural Network Operation. |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 18 | import copy |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 19 | from collections import namedtuple |
| 20 | from enum import Enum |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 21 | from typing import Any |
| 22 | from typing import Dict |
| 23 | from typing import List |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 24 | from typing import Optional |
Louis Verhaard | ebf4af6 | 2021-01-27 15:57:57 +0100 | [diff] [blame] | 25 | from typing import Tuple |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 26 | from typing import TYPE_CHECKING |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 27 | |
Louis Verhaard | 1a92f78 | 2021-02-09 16:08:26 +0100 | [diff] [blame] | 28 | from .api import NpuRoundingMode |
Michael McGeagh | 528a56d | 2020-12-16 11:33:21 +0000 | [diff] [blame] | 29 | from .errors import VelaError |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 30 | from .numeric_util import full_shape |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 31 | from .shape4d import Shape4D |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 32 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 33 | |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 34 | if TYPE_CHECKING: |
| 35 | from .tensor import Tensor |
| 36 | |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 37 | PointXY = namedtuple("PointXY", "x y") |
| 38 | PointXYZ = namedtuple("PointXYZ", "x y z") |
| 39 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 40 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 41 | class NpuBlockType(Enum): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 42 | Default = 0 |
| 43 | ConvolutionMxN = 1 |
| 44 | VectorProduct = 2 |
| 45 | Pooling = 3 |
| 46 | ConvolutionDepthWise = 4 |
| 47 | ElementWise = 5 |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 48 | ReduceSum = 6 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 49 | |
| 50 | |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 51 | class Kernel: |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 52 | """ |
| 53 | Kernel information for NPU operations |
| 54 | """ |
| 55 | |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 56 | def __init__( |
| 57 | self, |
| 58 | w: int, |
| 59 | h: int, |
| 60 | stride_x: int = 1, |
| 61 | stride_y: int = 1, |
| 62 | dilation_x: int = 1, |
| 63 | dilation_y: int = 1, |
| 64 | valid_padding=False, |
| 65 | ): |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 66 | assert stride_x > 0 and stride_y > 0 |
| 67 | assert dilation_x > 0 and dilation_y > 0 |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 68 | self.width = w |
| 69 | self.height = h |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 70 | self.stride = PointXY(stride_x, stride_y) |
| 71 | self.dilation = PointXY(dilation_x, dilation_y) |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 72 | self.valid_padding = valid_padding |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 73 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 74 | def elements_wh(self) -> int: |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 75 | return self.width * self.height |
| 76 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 77 | def area_width(self) -> int: |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 78 | return (self.width - 1) * self.dilation.x + 1 |
| 79 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 80 | def area_height(self) -> int: |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 81 | return (self.height - 1) * self.dilation.y + 1 |
| 82 | |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 83 | def dilation(self) -> PointXY: |
| 84 | return self.dilation |
| 85 | |
Louis Verhaard | ebf4af6 | 2021-01-27 15:57:57 +0100 | [diff] [blame] | 86 | def dilated_wh(self) -> Tuple[int, int]: |
| 87 | """Returns the dilated kernel width/height""" |
| 88 | return self.dilation.x * (self.width - 1) + 1, self.dilation.y * (self.height - 1) + 1 |
| 89 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 90 | def __str__(self): |
| 91 | return f"w={self.width}, h={self.height}, stride={tuple(self.stride)}, dilation={tuple(self.dilation)}" |
| 92 | |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 93 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 94 | # Classifies operators of type Custom |
| 95 | class CustomType(Enum): |
| 96 | ThirdPartyOp = 0 # Third party custom op |
| 97 | NpuOp = 1 # NPU op |
| 98 | ExistingNpuOp = 2 # NPU op that was part of the input network |
| 99 | |
| 100 | |
| 101 | TensorIndices = namedtuple("TensorIndices", ["ifms", "weights", "biases"]) |
| 102 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 103 | NNG_NO_INDICES = TensorIndices([], [], []) |
| 104 | NNG_IFM_INDICES = TensorIndices([0], [], []) |
| 105 | NNG_IFM_WEIGHTS_INDICES = TensorIndices([0], [1], []) |
| 106 | NNG_IFM_WEIGHTS_BIAS_INDICES = TensorIndices([0], [1], [2]) |
| 107 | NNG_IFM_IFM2_INDICES = TensorIndices([0, 1], [], []) |
| 108 | NNG_CONV2D_BACKPROP_INDICES = TensorIndices([2], [1], [3]) |
| 109 | NNG_TRANSPOSE_CONV_INDICES = TensorIndices([0], [1], [3]) |
| 110 | NNG_CONCAT_INDICES = TensorIndices([1, 2], [], []) |
| 111 | NNG_SPLIT_IFM_INDICES = TensorIndices([1], [], []) |
| 112 | NNG_BLOCK_LSTM_INDICES = TensorIndices([3], [4], []) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 113 | |
| 114 | |
| 115 | # Static information related to operation codes |
| 116 | class OperatorInfo: |
| 117 | __slots__ = ("id", "block_type", "indices", "is_unary") |
| 118 | _id = 0 |
| 119 | |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 120 | def __init__(self, block_type=NpuBlockType.Default, indices=NNG_NO_INDICES, is_unary=False): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 121 | OperatorInfo._id += 1 |
| 122 | self.id = OperatorInfo._id |
| 123 | self.block_type = block_type |
| 124 | self.indices = indices # Indices of the different tensor purposes |
| 125 | self.is_unary = is_unary # Classifies elementwise operators |
| 126 | |
| 127 | |
| 128 | # Internally used operation codes |
| 129 | class Op(Enum): |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 130 | Abs = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_INDICES, is_unary=True) |
| 131 | Add = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 132 | AddN = OperatorInfo() |
| 133 | Any = OperatorInfo() |
| 134 | ArgMax = OperatorInfo() |
| 135 | ArgMin = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 136 | AvgPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 137 | BatchMatMul = OperatorInfo() |
| 138 | BatchToSpaceND = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 139 | BidirectionalSequenceLstm = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
| 140 | BidirectionalSequenceRnn = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
| 141 | BlockLSTM = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_BLOCK_LSTM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 142 | |
| 143 | CLZ = OperatorInfo( |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 144 | block_type=NpuBlockType.ElementWise, indices=NNG_IFM_INDICES, is_unary=True |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 145 | ) # NPU specific operation |
| 146 | Call = OperatorInfo() |
| 147 | Cast = OperatorInfo() |
| 148 | Ceil = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 149 | Clamp = OperatorInfo(indices=NNG_IFM_INDICES) # TOSA specific |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 150 | Clip = OperatorInfo() # NPU specific fused activation function for clipping between activation.min/max |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 151 | Concat = OperatorInfo(indices=NNG_CONCAT_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 152 | ConcatEmbeddings = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 153 | ConcatSliceWrite = OperatorInfo(indices=NNG_IFM_INDICES) |
| 154 | ConcatTFLite = OperatorInfo(indices=NNG_CONCAT_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 155 | Const = OperatorInfo() # Constant tensor, only used in CPU subgraphs |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 156 | Conv2D = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=NNG_IFM_WEIGHTS_INDICES) |
| 157 | Conv2DBackpropInput = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=NNG_CONV2D_BACKPROP_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 158 | Conv2DBackpropInputSwitchedBias = OperatorInfo( |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 159 | block_type=NpuBlockType.ConvolutionMxN, indices=NNG_TRANSPOSE_CONV_INDICES |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 160 | ) |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 161 | Conv2DBias = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=NNG_IFM_WEIGHTS_BIAS_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 162 | Cos = OperatorInfo() |
Tim Hall | 42abec1 | 2021-02-04 21:31:57 +0000 | [diff] [blame] | 163 | Cumsum = OperatorInfo() |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 164 | Custom = OperatorInfo() # Custom 3rd party operator, only used in CPU subgraphs |
| 165 | CustomNpuOp = OperatorInfo() # NPU custom operator, only used in CPU subgraphs |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 166 | Delegate = OperatorInfo() |
| 167 | Densify = OperatorInfo() |
| 168 | DepthToSpace = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 169 | DepthwiseConv2DBias = OperatorInfo( |
| 170 | block_type=NpuBlockType.ConvolutionDepthWise, indices=NNG_IFM_WEIGHTS_BIAS_INDICES |
| 171 | ) |
| 172 | Dequantize = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 173 | Div = OperatorInfo() |
| 174 | Elu = OperatorInfo() |
| 175 | EmbeddingLookup = OperatorInfo() |
| 176 | EmbeddingLookupSparse = OperatorInfo() |
| 177 | Equal = OperatorInfo() |
| 178 | Exp = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 179 | ExpandDims = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 180 | FakeQuantWithMinMaxArgs = OperatorInfo() |
| 181 | Fill = OperatorInfo() |
| 182 | Floor = OperatorInfo() |
| 183 | FloorDiv = OperatorInfo() |
| 184 | FloorMod = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 185 | FullyConnected = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_BIAS_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 186 | GatherNd = OperatorInfo() |
| 187 | GatherV2 = OperatorInfo() |
| 188 | Greater = OperatorInfo() |
| 189 | GreaterEqual = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 190 | HardSwish = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 191 | HashtableLookup = OperatorInfo() |
| 192 | Identity = OperatorInfo() |
| 193 | If = OperatorInfo() |
| 194 | L2Norm = OperatorInfo() |
| 195 | L2Pool2D = OperatorInfo() |
| 196 | LRN = OperatorInfo() |
| 197 | LSHProjection = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 198 | LeakyRelu = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_INDICES, is_unary=True) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 199 | Less = OperatorInfo() |
| 200 | LessEqual = OperatorInfo() |
| 201 | Log = OperatorInfo() |
| 202 | LogSoftmax = OperatorInfo() |
| 203 | LogicalAnd = OperatorInfo() |
| 204 | LogicalNot = OperatorInfo() |
| 205 | LogicalOr = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 206 | Lstm = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 207 | LUT = OperatorInfo() # NPU specific, operator has LUT, only used in fused activation functions |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 208 | MatMul = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 209 | MatrixDiag = OperatorInfo() |
| 210 | MatrixSetDiag = OperatorInfo() |
| 211 | Max = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 212 | MaxPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=NNG_IFM_INDICES) |
| 213 | Maximum = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) |
| 214 | Mean = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 215 | Min = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 216 | Minimum = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 217 | MirrorPad = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 218 | Mul = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 219 | Neg = OperatorInfo() |
| 220 | NonMaxSuppressionV4 = OperatorInfo() |
| 221 | NonMaxSuppressionV5 = OperatorInfo() |
| 222 | NotEqual = OperatorInfo() |
| 223 | OneHot = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 224 | Pack = OperatorInfo(indices=NNG_IFM_INDICES) |
| 225 | PackReshaped = OperatorInfo(indices=NNG_IFM_INDICES) |
| 226 | Pad = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 227 | PadV2 = OperatorInfo() |
| 228 | Placeholder = OperatorInfo() # Only used in CPU subgraphs |
| 229 | Pow = OperatorInfo() |
| 230 | Prelu = OperatorInfo() |
| 231 | Prod = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 232 | Quantize = OperatorInfo(indices=NNG_IFM_INDICES) |
| 233 | QuantizedAvgPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=NNG_IFM_INDICES) |
| 234 | QuantizedConv2D = OperatorInfo(block_type=NpuBlockType.ConvolutionMxN, indices=NNG_IFM_WEIGHTS_INDICES) |
| 235 | QuantizedMatMul = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
| 236 | QuantizedMaxPool = OperatorInfo(block_type=NpuBlockType.Pooling, indices=NNG_IFM_INDICES) |
| 237 | QuantizedReshape = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 238 | Range = OperatorInfo() |
| 239 | Rank = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 240 | ReduceSum = OperatorInfo(block_type=NpuBlockType.ReduceSum, indices=NNG_IFM_INDICES) |
| 241 | Relu = OperatorInfo(indices=NNG_IFM_INDICES) |
| 242 | Relu6 = OperatorInfo(indices=NNG_IFM_INDICES) |
| 243 | ReluN1To1 = OperatorInfo(indices=NNG_IFM_INDICES) |
| 244 | ReluN = OperatorInfo(indices=NNG_IFM_INDICES) # TOSA specific |
| 245 | Rescale = OperatorInfo(indices=NNG_IFM_INDICES) # TOSA specific |
| 246 | RescaleAdd = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) |
| 247 | Reshape = OperatorInfo(indices=NNG_IFM_INDICES) |
| 248 | ResizeBilinear = OperatorInfo(block_type=NpuBlockType.Pooling, indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 249 | ResizeNearestNeighbor = OperatorInfo() |
| 250 | ReverseSequence = OperatorInfo() |
| 251 | ReverseV2 = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 252 | Rnn = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 253 | Round = OperatorInfo() |
| 254 | Rsqrt = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 255 | SHL = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) # NPU specific operation |
| 256 | SHR = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) # NPU specific operation |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 257 | ScatterNd = OperatorInfo() |
| 258 | SegmentSum = OperatorInfo() |
| 259 | Select = OperatorInfo() |
| 260 | SelectV2 = OperatorInfo() |
| 261 | Shape = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 262 | Sigmoid = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 263 | SignBit = OperatorInfo() |
| 264 | Sin = OperatorInfo() |
| 265 | SkipGram = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 266 | Slice = OperatorInfo(indices=NNG_IFM_INDICES) |
| 267 | Softmax = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 268 | SpaceToBatchND = OperatorInfo() |
| 269 | SpaceToDepth = OperatorInfo() |
| 270 | SparseToDense = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 271 | Split = OperatorInfo(indices=NNG_SPLIT_IFM_INDICES) |
| 272 | SplitSliceRead = OperatorInfo(indices=NNG_IFM_INDICES) |
| 273 | SplitV = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 274 | Sqrt = OperatorInfo() |
| 275 | Square = OperatorInfo() |
| 276 | SquaredDifference = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 277 | Squeeze = OperatorInfo(indices=NNG_IFM_INDICES) |
| 278 | StridedSlice = OperatorInfo(indices=NNG_IFM_INDICES) |
| 279 | Sub = OperatorInfo(block_type=NpuBlockType.ElementWise, indices=NNG_IFM_IFM2_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 280 | SubgraphInput = OperatorInfo() # Only used in CPU subgraphs |
| 281 | Sum = OperatorInfo() |
| 282 | Svdf = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 283 | Tanh = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 284 | Tile = OperatorInfo() |
| 285 | TopKV2 = OperatorInfo() |
| 286 | Transpose = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 287 | UnidirectionalSequenceLstm = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
| 288 | UnidirectionalSequenceRnn = OperatorInfo(block_type=NpuBlockType.VectorProduct, indices=NNG_IFM_WEIGHTS_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 289 | Unique = OperatorInfo() |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 290 | Unpack = OperatorInfo(indices=NNG_IFM_INDICES) |
| 291 | UnpackReshaped = OperatorInfo(indices=NNG_IFM_INDICES) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 292 | Where = OperatorInfo() |
| 293 | While = OperatorInfo() |
| 294 | ZerosLike = OperatorInfo() |
| 295 | |
| 296 | @property |
| 297 | def info(self): |
| 298 | return self.value |
| 299 | |
| 300 | @property |
| 301 | def npu_block_type(self): |
| 302 | return self.info.block_type |
| 303 | |
| 304 | def is_conv2d_op(self): |
| 305 | return self.info.block_type == NpuBlockType.ConvolutionMxN |
| 306 | |
| 307 | def is_depthwise_conv2d_op(self): |
| 308 | return self.info.block_type == NpuBlockType.ConvolutionDepthWise |
| 309 | |
| 310 | def is_pool_op(self): |
| 311 | return self.info.block_type == NpuBlockType.Pooling |
| 312 | |
| 313 | def is_maxpool_op(self): |
| 314 | return self in (Op.MaxPool, Op.QuantizedMaxPool) |
| 315 | |
| 316 | def is_avgpool_op(self): |
| 317 | return self in (Op.QuantizedAvgPool, Op.AvgPool) |
| 318 | |
| 319 | def is_elementwise_op(self): |
| 320 | return self.info.block_type == NpuBlockType.ElementWise |
| 321 | |
| 322 | def is_unary_elementwise_op(self): |
| 323 | return self.info.block_type == NpuBlockType.ElementWise and self.info.is_unary |
| 324 | |
| 325 | def is_binary_elementwise_op(self): |
| 326 | return self.info.block_type == NpuBlockType.ElementWise and not self.info.is_unary |
| 327 | |
| 328 | def is_relu_op(self): |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 329 | return self in (Op.Relu, Op.Relu6, Op.ReluN1To1, Op.ReluN, Op.Clip, Op.Clamp) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 330 | |
| 331 | def is_activation_op(self): |
Diqing Zhong | 189f748 | 2021-01-26 12:12:51 +0100 | [diff] [blame] | 332 | return self.is_relu_op() or self in (Op.Tanh, Op.Sigmoid, Op.Softmax, Op.LUT, Op.HardSwish) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 333 | |
| 334 | def is_split_op(self): |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 335 | return self in (Op.Split, Op.SplitV, Op.StridedSlice, Op.Slice, Op.UnpackReshaped, Op.Unpack) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 336 | |
| 337 | def is_concat_op(self): |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 338 | return self in (Op.Concat, Op.ConcatTFLite, Op.PackReshaped, Op.Pack) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 339 | |
| 340 | def needs_bias(self): |
| 341 | return bool(self.info.indices.biases) |
| 342 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 343 | def needs_shapes(self): |
| 344 | return bool(self.info.indices.ifms) |
| 345 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 346 | @classmethod |
| 347 | def op_set(cls, predicate): |
| 348 | # Returns the set of all operator codes that fulfill the given predicate |
| 349 | return {op_type for op_type in Op if predicate(op_type)} |
| 350 | |
| 351 | def __str__(self): |
| 352 | return self.name |
| 353 | |
| 354 | __repr__ = __str__ |
| 355 | |
| 356 | def __lt__(self, other): |
| 357 | return self.value.id < other.value.id |
| 358 | |
| 359 | |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 360 | class Padding(Enum): |
| 361 | SAME = 0 |
| 362 | VALID = 1 |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 363 | EXPLICIT = 2 # Padding is specified in a PAD operation (only used for NPU operations) |
Michael McGeagh | 1689548 | 2020-12-14 15:51:20 +0000 | [diff] [blame] | 364 | |
| 365 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 366 | class ActivationFunction: |
| 367 | """Fused activation function""" |
| 368 | |
| 369 | def __init__(self, op_type: Op): |
| 370 | self.op_type = op_type # The activation operation to be performed |
| 371 | # min/max are optional; if present they are non-quantized values |
| 372 | self.min: Optional[float] = None |
| 373 | self.max: Optional[float] = None |
| 374 | # Table lookup index, only applicable for Op.LUT activation, 0-7 |
| 375 | self.lut_index: int = 0 |
| 376 | |
| 377 | def clone(self): |
| 378 | res = copy.copy(self) |
| 379 | return res |
| 380 | |
| 381 | |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 382 | class ExplicitScaling: |
| 383 | """Explicit scaling parameters""" |
| 384 | |
| 385 | def __init__(self, per_channel, shift, multiplier): |
| 386 | self.per_channel = per_channel |
| 387 | self.shift = shift |
| 388 | self.multiplier = multiplier |
| 389 | |
| 390 | def clone(self): |
| 391 | res = copy.copy(self) |
| 392 | return res |
| 393 | |
| 394 | |
| 395 | def create_activation_function(op_type: Op, min=None, max=None) -> ActivationFunction: |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 396 | """Creates activation function with min/max depending on op_type""" |
| 397 | act = ActivationFunction(op_type) |
| 398 | if op_type == Op.Relu: |
| 399 | act.min = 0.0 |
| 400 | elif op_type == Op.Relu6: |
| 401 | act.min = 0.0 |
| 402 | act.max = 6.0 |
| 403 | elif op_type == Op.ReluN1To1: |
| 404 | act.min = -1.0 |
| 405 | act.max = 1.0 |
| 406 | elif op_type == Op.Tanh: |
| 407 | act.min = -1.0 |
| 408 | act.max = 1.0 |
| 409 | elif op_type == Op.Sigmoid: |
| 410 | act.min = 0.0 |
| 411 | act.max = 1.0 |
Diqing Zhong | 189f748 | 2021-01-26 12:12:51 +0100 | [diff] [blame] | 412 | elif op_type == Op.HardSwish: |
| 413 | act.min = 0.0 |
Patrik Gustavsson | 5e26eda | 2021-06-30 09:07:16 +0200 | [diff] [blame^] | 414 | if op_type == Op.Clamp: |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 415 | assert min is not None and max is not None |
| 416 | act.min = min |
| 417 | act.max = max |
| 418 | elif op_type == Op.ReluN: |
| 419 | assert max is not None |
| 420 | act.min = 0.0 |
| 421 | act.max = max |
| 422 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 423 | return act |
| 424 | |
| 425 | |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 426 | def get_slice_offsets(input_shape: List[int], offset_tens: int, offset_mask: int, is_begin: bool = True): |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 427 | # For strided slice operator: get start or end offsets |
| 428 | offsets = len(input_shape) * [0] if is_begin else input_shape[:] |
| 429 | for idx in range(len(input_shape)): |
| 430 | # If the i:th bit in the mask is set then the value on offset_tens[i] should be ignored |
| 431 | if (offset_mask & (1 << idx)) == 0: |
| 432 | offsets[idx] = offset_tens.values[idx] |
| 433 | if offsets[idx] < 0: |
| 434 | # Convert offset to positive value |
| 435 | offsets[idx] += input_shape[idx] |
| 436 | return offsets |
| 437 | |
| 438 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 439 | class Operation: |
| 440 | """Class representing a Neural Network operation. Has a name, a type, |
Dwight Lidman | c6ac194 | 2020-10-02 14:55:45 +0200 | [diff] [blame] | 441 | input and output tensors, as well as an attribute dictionary.""" |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 442 | |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 443 | __slots__ = ( |
| 444 | "type", |
| 445 | "name", |
| 446 | "op_index", |
| 447 | "attrs", |
| 448 | "inputs", |
| 449 | "outputs", |
Fredrik Svedberg | 8d0f489 | 2021-02-16 21:59:50 +0100 | [diff] [blame] | 450 | "intermediates", |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 451 | "flops", |
| 452 | "scheduled_pass", |
| 453 | "run_on_npu", |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 454 | "activation", |
| 455 | "memory_function", |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 456 | "forced_input_quantization", |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 457 | "forced_output_quantization", |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 458 | "activation_lut", |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 459 | "_kernel", |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 460 | "ifm_shapes", |
| 461 | "ofm_shapes", |
Fredrik Svedberg | e82be7c | 2021-01-18 15:21:03 +0100 | [diff] [blame] | 462 | "rescale", |
Patrik Gustavsson | e3b1b91 | 2021-02-09 15:38:46 +0100 | [diff] [blame] | 463 | "read_offsets", |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 464 | "read_shapes", |
Louis Verhaard | 1a92f78 | 2021-02-09 16:08:26 +0100 | [diff] [blame] | 465 | "rounding_mode", |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 466 | "explicit_scaling", |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 467 | "low_precision_scaling", |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 468 | "write_offset", |
| 469 | "write_shape", |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 470 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 471 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 472 | def __init__(self, op_type: Op, name: str): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 473 | self.type = op_type |
| 474 | self.name = name |
Dwight Lidman | 9b43f84 | 2020-12-08 17:56:44 +0100 | [diff] [blame] | 475 | self.attrs: Dict[str, Any] = {} |
| 476 | self.inputs: List[Tensor] = [] |
| 477 | self.outputs: List[Tensor] = [] |
Fredrik Svedberg | 8d0f489 | 2021-02-16 21:59:50 +0100 | [diff] [blame] | 478 | self.intermediates: List[Tensor] = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 479 | self.flops = 0 |
| 480 | self.run_on_npu = True |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 481 | # Fused activation function. If not none: operator code. |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 482 | self.activation: Optional[ActivationFunction] = None |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 483 | # Fused memory function, if not None: operator code |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 484 | self.memory_function: Optional[Op] = None |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 485 | # If not none: contains QuantizationParameters to be used as output quantization |
| 486 | # (which overrides the ofm tensor's quantization), used in LUT |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 487 | self.forced_input_quantization = None |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 488 | self.forced_output_quantization = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 489 | self.scheduled_pass = None |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 490 | self.op_index = None # input network operator index |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 491 | self.activation_lut = None |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 492 | self._kernel = None |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 493 | self.ifm_shapes: List[Shape4D] = [] |
| 494 | self.ofm_shapes: List[Shape4D] = [] |
Fredrik Svedberg | e82be7c | 2021-01-18 15:21:03 +0100 | [diff] [blame] | 495 | # If not none: contains rescale to be used as output scaling |
| 496 | # (which overrides the ofm tensor's scale) |
| 497 | self.rescale = None |
Patrik Gustavsson | e3b1b91 | 2021-02-09 15:38:46 +0100 | [diff] [blame] | 498 | self.read_offsets: List[Shape4D] = [None, None] # offset for [ifm, ifm2] |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 499 | self.read_shapes: List[Shape4D] = [None, None] # read shape for [ifm, ifm2] |
Louis Verhaard | 1a92f78 | 2021-02-09 16:08:26 +0100 | [diff] [blame] | 500 | self.rounding_mode: Optional[NpuRoundingMode] = None |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 501 | # Rescale op in TOSA supplies explicit multiplier and shift values |
| 502 | self.explicit_scaling: Optional[ExplicitScaling] = None |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 503 | # The Mean operator (implemented as a depthwise convolution) requires scaling |
| 504 | # to be calculated differently in one case. In that case, this is set to True. |
| 505 | self.low_precision_scaling = False |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 506 | # Write offset, for operations that only produce a part of the OFM |
| 507 | self.write_offset: Optional[Shape4D] = None |
| 508 | # The amount of OFM that is produced by the operation (only if write_offset is not None). |
| 509 | # E.g. an operation that only fills the bottom row of an OFM of size 1x10x8x1 would have |
| 510 | # write_offset 0,9,0,0, write_shape 1,1,8,1 |
| 511 | self.write_shape: Optional[Shape4D] = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 512 | |
| 513 | def clone(self, suffix="_clone"): |
| 514 | res = Operation(self.type, self.name + suffix) |
| 515 | |
| 516 | res.attrs = dict(self.attrs) |
| 517 | res.inputs = list(self.inputs) |
| 518 | res.outputs = list(self.outputs) |
Fredrik Svedberg | 8d0f489 | 2021-02-16 21:59:50 +0100 | [diff] [blame] | 519 | res.intermediates = list(self.intermediates) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 520 | res.flops = self.flops |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 521 | res.run_on_npu = self.run_on_npu |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 522 | res.activation = None if self.activation is None else self.activation.clone() |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 523 | res.memory_function = self.memory_function |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 524 | res.forced_input_quantization = self.forced_input_quantization |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 525 | res.forced_output_quantization = self.forced_output_quantization |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 526 | res.scheduled_pass = self.scheduled_pass |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 527 | res.op_index = None # not relevant as not part of input network |
Patrik Gustavsson | e3b1b91 | 2021-02-09 15:38:46 +0100 | [diff] [blame] | 528 | res.read_offsets = list(self.read_offsets) |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 529 | res.read_shapes = list(self.read_shapes) |
Louis Verhaard | 1a92f78 | 2021-02-09 16:08:26 +0100 | [diff] [blame] | 530 | res.rounding_mode = self.rounding_mode |
Patrik Gustavsson | 8f1f9aa | 2021-06-28 07:41:58 +0200 | [diff] [blame] | 531 | res.explicit_scaling = self.explicit_scaling |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 532 | res.low_precision_scaling = self.low_precision_scaling |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 533 | |
| 534 | return res |
| 535 | |
| 536 | def __str__(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 537 | return "<nng.Operation '{}' type={}>".format(self.name, self.type) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 538 | |
| 539 | __repr__ = __str__ |
| 540 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 541 | def get_kernel_size(self): |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 542 | weights = self.weights |
| 543 | if weights and self.type.npu_block_type in (NpuBlockType.ConvolutionDepthWise, NpuBlockType.ConvolutionMxN): |
| 544 | weight_shape = full_shape(4, weights.shape, 1) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 545 | h = weight_shape[-4] |
| 546 | w = weight_shape[-3] |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 547 | elif self.type.npu_block_type in (NpuBlockType.Pooling, NpuBlockType.ReduceSum) and "ksize" in self.attrs: |
| 548 | h, w = self.attrs["ksize"][1:3] |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 549 | else: |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 550 | h = self.attrs.get("filter_height", 1) |
| 551 | w = self.attrs.get("filter_width", 1) |
| 552 | return w, h |
| 553 | |
| 554 | def get_kernel_stride(self): |
| 555 | if "strides" in self.attrs: |
| 556 | _, h, w, _ = self.attrs["strides"] |
| 557 | else: |
| 558 | h = self.attrs.get("stride_h", 1) |
| 559 | w = self.attrs.get("stride_w", 1) |
| 560 | return w, h |
| 561 | |
| 562 | def get_kernel_dilation(self): |
| 563 | if "dilation" in self.attrs: |
| 564 | _, h, w, _ = self.attrs["dilation"] |
| 565 | else: |
| 566 | h = self.attrs.get("dilation_h_factor", 1) |
| 567 | w = self.attrs.get("dilation_w_factor", 1) |
| 568 | return w, h |
| 569 | |
| 570 | @property |
| 571 | def kernel(self): |
| 572 | k_w, k_h = self.get_kernel_size() |
| 573 | s_w, s_h = self.get_kernel_stride() |
| 574 | d_w, d_h = self.get_kernel_dilation() |
| 575 | self._kernel = Kernel(k_w, k_h, s_w, s_h, d_w, d_h) |
Tim Hall | 4ed38bc | 2020-10-20 18:54:20 +0100 | [diff] [blame] | 576 | return self._kernel |
| 577 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 578 | def get_ifm_ifm2_weights_ofm(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 579 | return self.ifm, self.ifm2, self.weights, self.ofm |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 580 | |
| 581 | def get_ifm_weights_biases_ofm(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 582 | return self.ifm, self.weights, self.bias, self.ofm |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 583 | |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 584 | def get_ifm_ifm2_weights_biases_ofm(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 585 | return self.ifm, self.ifm2, self.weights, self.bias, self.ofm |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 586 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 587 | def get_ifm_ofm(self): |
| 588 | return self.ifm, self.ofm |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 589 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 590 | @property |
| 591 | def ifm(self): |
| 592 | # Gets the IFM tensor, or None if not applicable |
| 593 | return self.get_input(self.type.info.indices.ifms, 0) |
Jacob Bohlin | 49d9212 | 2020-08-19 14:36:46 +0200 | [diff] [blame] | 594 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 595 | @property |
| 596 | def ifm2(self): |
| 597 | # Gets the IFM2 tensor, or None if not applicable |
| 598 | return self.get_input(self.type.info.indices.ifms, 1) |
Louis Verhaard | 98a3499 | 2020-09-01 10:39:04 +0200 | [diff] [blame] | 599 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 600 | @property |
| 601 | def bias(self): |
| 602 | # Gets the bias tensor, or None if not applicable |
| 603 | return self.get_input(self.type.info.indices.biases, 0) |
| 604 | |
| 605 | @property |
| 606 | def weights(self): |
| 607 | # Gets the weight tensor, or None if not applicable |
| 608 | return self.get_input(self.type.info.indices.weights, 0) |
| 609 | |
| 610 | def get_ifm_tensors(self): |
| 611 | # Gets the IFM tensors, or empty list if not applicable |
| 612 | return self._index_list_to_tensors(self.type.info.indices.ifms) |
| 613 | |
| 614 | def get_weight_tensors(self): |
| 615 | # Gets the weight tensors, or empty list if not applicable |
| 616 | return self._index_list_to_tensors(self.type.info.indices.weights) |
| 617 | |
| 618 | def get_bias_tensors(self): |
| 619 | # Gets the bias tensors, or empty list if not applicable |
| 620 | return self._index_list_to_tensors(self.type.info.indices.biases) |
| 621 | |
| 622 | def _index_list_to_tensors(self, index_list): |
| 623 | return [self.inputs[ix] for ix in index_list if ix < len(self.inputs)] |
| 624 | |
| 625 | def get_input(self, index_list, ix): |
| 626 | if ix >= len(index_list): |
| 627 | return None |
| 628 | if index_list[ix] >= len(self.inputs): |
| 629 | return None |
| 630 | return self.inputs[index_list[ix]] |
| 631 | |
| 632 | @property |
| 633 | def ofm(self): |
| 634 | # Gets the OFM tensor, or None if not applicable |
| 635 | return self.outputs[0] if self.outputs else None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 636 | |
| 637 | def get_concat_inputs_axis(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 638 | assert self.type.is_concat_op() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 639 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 640 | if self.type == Op.Concat: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 641 | axis_tensor = self.inputs[0] |
| 642 | inputs = self.inputs[1:] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 643 | elif self.type == Op.ConcatTFLite: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 644 | inputs = self.inputs |
| 645 | axis = self.attrs["axis"] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 646 | elif self.type == Op.PackReshaped: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 647 | # Requires fixup_pack_input to be called before this point |
| 648 | inputs = self.inputs |
| 649 | axis = self.attrs["axis"] |
| 650 | assert len(self.inputs) == self.attrs["values_count"] |
| 651 | else: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 652 | assert len(axis_tensor.ops) == 1 and axis_tensor.ops[0].type == Op.Const |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 653 | axis = int(axis_tensor.values) |
| 654 | |
| 655 | return inputs, axis |
| 656 | |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 657 | def get_dilation_h_w(self): |
| 658 | _, dilation_h, dilation_w, _ = self.attrs.get("dilation", (1, 1, 1, 1)) |
| 659 | return dilation_h, dilation_w |
| 660 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 661 | def get_split_inputs_axis(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 662 | assert self.type.is_split_op() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 663 | |
| 664 | offset_start = None |
| 665 | offset_end = None |
| 666 | axis = None |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 667 | if self.type == Op.Split: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 668 | num_splits = self.attrs.get("num_splits") |
| 669 | axis_tens = self.inputs[0] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 670 | assert len(axis_tens.ops) == 1 and axis_tens.ops[0].type == Op.Const |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 671 | axis = int(axis_tens.values) |
| 672 | input_tens = self.inputs[1] |
| 673 | outputs = self.outputs |
| 674 | assert num_splits == len(outputs) |
| 675 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 676 | elif self.type == Op.SplitV: |
Charles Xu | 53d4752 | 2020-05-04 11:32:05 +0200 | [diff] [blame] | 677 | num_splits = self.attrs.get("num_splits") |
| 678 | input_tens = self.inputs[0] |
| 679 | size_tens = self.inputs[1] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 680 | assert len(size_tens.ops) == 1 and size_tens.ops[0].type == Op.Const |
Charles Xu | 53d4752 | 2020-05-04 11:32:05 +0200 | [diff] [blame] | 681 | sizes = size_tens.values |
Patrik Gustavsson | 271ddc3 | 2020-09-01 09:15:27 +0200 | [diff] [blame] | 682 | |
Charles Xu | 53d4752 | 2020-05-04 11:32:05 +0200 | [diff] [blame] | 683 | axis_tens = self.inputs[2] |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 684 | assert len(axis_tens.ops) == 1 and axis_tens.ops[0].type == Op.Const |
Charles Xu | 53d4752 | 2020-05-04 11:32:05 +0200 | [diff] [blame] | 685 | axis = int(axis_tens.values) |
Patrik Gustavsson | 271ddc3 | 2020-09-01 09:15:27 +0200 | [diff] [blame] | 686 | |
| 687 | for idx, size in enumerate(sizes): |
| 688 | # One but only one size might be set to -1, indicating that size should be inferred |
| 689 | if size == -1: |
| 690 | sizes[idx] = input_tens.shape[axis] - (sum(sizes) + 1) |
| 691 | break |
| 692 | |
Charles Xu | 53d4752 | 2020-05-04 11:32:05 +0200 | [diff] [blame] | 693 | outputs = self.outputs |
| 694 | assert num_splits == len(outputs) |
| 695 | assert sum(sizes) == input_tens.shape[axis] |
| 696 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 697 | elif self.type == Op.Slice: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 698 | input_tens, begin_tens, size_tens = self.inputs |
| 699 | outputs = self.outputs |
| 700 | offset_start = [0] * len(input_tens.shape) |
| 701 | offset_end = [0] * len(input_tens.shape) |
| 702 | |
| 703 | for idx in range(len(begin_tens.values)): |
| 704 | # Check if the op should slice in dimension idx |
| 705 | if size_tens.values[idx] != input_tens.shape[idx]: |
| 706 | offset_start[idx] = begin_tens.values[idx] |
| 707 | offset_end[idx] = size_tens.values[idx] + offset_start[idx] |
| 708 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 709 | elif self.type == Op.StridedSlice: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 710 | input_tens, begin_tens, end_tens, strides_tens = self.inputs |
| 711 | outputs = self.outputs |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 712 | |
| 713 | # Extract masks |
| 714 | begin_mask = self.attrs["begin_mask"] |
| 715 | ellipsis_mask = self.attrs["ellipsis_mask"] |
| 716 | end_mask = self.attrs["end_mask"] |
| 717 | new_axis_mask = self.attrs["new_axis_mask"] |
| 718 | shrink_axis_mask = self.attrs["shrink_axis_mask"] |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 719 | |
| 720 | # shrink_axis_mask/new_axis_mask/ellipsis_mask is not supported by the Operation class but the operation |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 721 | # may have the attribute modified and handled in the graph optimization phase. |
Patrik Gustavsson | cf72890 | 2020-04-30 08:57:23 +0200 | [diff] [blame] | 722 | assert shrink_axis_mask == new_axis_mask == ellipsis_mask == 0 |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 723 | offset_start = get_slice_offsets(input_tens.shape, begin_tens, begin_mask, is_begin=True) |
| 724 | offset_end = get_slice_offsets(input_tens.shape, end_tens, end_mask, is_begin=False) |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 725 | elif self.type == Op.UnpackReshaped: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 726 | # Requires fixup_unpack_output to be called before this point |
| 727 | input_tens = self.inputs[0] |
| 728 | outputs = self.outputs |
| 729 | axis = self.attrs["axis"] |
| 730 | num_splits = self.attrs["num"] |
| 731 | # Number of outputs have to equal the value of the dimension to unpack |
| 732 | assert num_splits == len(outputs) == input_tens.shape[axis] |
| 733 | else: |
| 734 | assert False |
| 735 | |
| 736 | return input_tens, outputs, axis, offset_start, offset_end |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 737 | |
| 738 | def set_activation_lut(self, lut_tensor): |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 739 | self.activation = ActivationFunction(Op.LUT) |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 740 | self.activation_lut = lut_tensor |
Michael McGeagh | c5b549b | 2020-08-07 11:54:28 +0100 | [diff] [blame] | 741 | self.add_input_tensor(lut_tensor) |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 742 | |
| 743 | def add_input_tensor(self, tens): |
| 744 | self.inputs.append(tens) |
| 745 | if self not in tens.consumer_list: |
| 746 | tens.consumer_list.append(self) |
| 747 | |
Jacob Bohlin | 67e0d8f | 2020-08-20 10:53:02 +0200 | [diff] [blame] | 748 | def set_input_tensor(self, tens, idx): |
| 749 | tens_to_remove = self.inputs[idx] |
| 750 | if tens_to_remove in tens.consumer_list: |
| 751 | tens.consumer_list.remove(tens_to_remove) |
| 752 | |
| 753 | self.inputs[idx] = tens |
| 754 | if self not in tens.consumer_list: |
| 755 | tens.consumer_list.append(self) |
| 756 | |
Dwight Lidman | 4f728c0 | 2020-12-17 15:14:45 +0100 | [diff] [blame] | 757 | def get_input_quantization(self): |
| 758 | if self.forced_input_quantization is not None: |
| 759 | return self.forced_input_quantization |
| 760 | return self.ifm.quantization |
| 761 | |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 762 | def set_output_tensor(self, tens): |
| 763 | tens.ops = [self] |
| 764 | self.outputs = [tens] |
Jacob Bohlin | a41cd4d | 2020-08-26 18:21:28 +0200 | [diff] [blame] | 765 | |
Louis Verhaard | 98a3499 | 2020-09-01 10:39:04 +0200 | [diff] [blame] | 766 | def get_output_quantization(self): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 767 | if self.forced_output_quantization is not None: |
| 768 | return self.forced_output_quantization |
| 769 | return self.ofm.quantization |
Michael McGeagh | 528a56d | 2020-12-16 11:33:21 +0000 | [diff] [blame] | 770 | |
| 771 | def error(self, msg): |
| 772 | """ |
| 773 | Raises a VelaError exception for errors encountered when parsing an Operation |
| 774 | |
| 775 | :param self: Operation object that resulted in the error |
| 776 | :param msg: str object that contains a description of the specific error encountered |
| 777 | """ |
| 778 | |
| 779 | def _print_tensors(tensors): |
| 780 | lines = [] |
| 781 | for idx, tens in enumerate(tensors): |
| 782 | tens_name = getattr(tens, "name", "Not a Tensor") |
| 783 | lines.append(f" {idx} = {tens_name}") |
| 784 | return lines |
| 785 | |
| 786 | if self.op_index is None: |
| 787 | lines = [f"Invalid {self.type} (name = {self.name}) operator in the internal representation. {msg}"] |
| 788 | else: |
| 789 | lines = [f"Invalid {self.type} (op_index = {self.op_index}) operator in the input network. {msg}"] |
| 790 | |
| 791 | lines += [" Input tensors:"] |
| 792 | lines += _print_tensors(self.inputs) |
| 793 | |
| 794 | lines += [" Output tensors:"] |
| 795 | lines += _print_tensors(self.outputs) |
| 796 | |
| 797 | raise VelaError("\n".join(lines)) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 798 | |
| 799 | def set_ifm_ofm_shapes(self): |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 800 | self.ifm_shapes = [] |
| 801 | self.ofm_shapes = [] |
| 802 | |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 803 | ifm_tensor, ifm2_tensor, weight_tensor, ofm_tensor = self.get_ifm_ifm2_weights_ofm() |
| 804 | |
| 805 | # set all shapes to op, as 4D |
| 806 | if self.type == Op.FullyConnected: |
Patrik Gustavsson | 2c2522d | 2021-01-29 11:51:31 +0100 | [diff] [blame] | 807 | if len(self.ifm.shape) == 2: |
| 808 | self.ifm_shapes.append(Shape4D([self.ifm.shape[0], 1, 1, self.ifm.shape[1]])) |
| 809 | else: |
| 810 | # Special case, handled in graph optimization |
| 811 | self.ifm_shapes.append(Shape4D(ifm_tensor.get_full_shape())) |
| 812 | if len(self.ofm.shape) == 2: |
| 813 | self.ofm_shapes.append(Shape4D([self.ofm.shape[0], 1, 1, self.ofm.shape[1]])) |
| 814 | else: |
| 815 | self.ofm_shapes.append(Shape4D(ofm_tensor.get_full_shape())) |
| 816 | if self.type == Op.Softmax: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 817 | self.ifm_shapes.append(Shape4D(ifm_tensor.get_full_shape())) |
| 818 | self.ofm_shapes.append(Shape4D(ofm_tensor.get_full_shape())) |
Patrik Gustavsson | da2b003 | 2021-02-04 16:28:29 +0100 | [diff] [blame] | 819 | elif self.type.is_split_op() or self.type.is_concat_op(): |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 820 | for inp in self.inputs: |
| 821 | if inp is not None: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 822 | self.ifm_shapes.append(Shape4D(full_shape(4, inp.shape, 1))) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 823 | else: |
| 824 | self.ifm_shapes.append(None) |
| 825 | for out in self.outputs: |
| 826 | if out is not None: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 827 | self.ofm_shapes.append(Shape4D(full_shape(4, out.shape, 1))) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 828 | else: |
| 829 | self.ofm_shapes.append(None) |
| 830 | else: |
Patrik Gustavsson | da2b003 | 2021-02-04 16:28:29 +0100 | [diff] [blame] | 831 | if ifm_tensor is not None: |
| 832 | self.ifm_shapes.append(Shape4D(full_shape(4, ifm_tensor.shape, 1))) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 833 | if ifm2_tensor is not None: |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 834 | self.ifm_shapes.append(Shape4D(full_shape(4, ifm2_tensor.shape, 1))) |
Patrik Gustavsson | da2b003 | 2021-02-04 16:28:29 +0100 | [diff] [blame] | 835 | if ofm_tensor is not None: |
| 836 | self.ofm_shapes.append(Shape4D(full_shape(4, ofm_tensor.shape, 1))) |
Tim Hall | d8339a7 | 2021-05-27 18:49:40 +0100 | [diff] [blame] | 837 | |
| 838 | def has_scaling(self): |
| 839 | scaled = True |
| 840 | for tensor in [self.ifm, self.ifm2, self.ofm]: |
| 841 | if tensor is not None: |
| 842 | if tensor.quantization is None: |
| 843 | scaled = False |
| 844 | break |
| 845 | |
| 846 | return scaled |