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