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