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