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