Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved. |
| 2 | # |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | # not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # Internal representation of a Neural Network Tensor. |
Patrik Gustavsson | 6ae0e42 | 2020-11-04 12:43:50 +0100 | [diff] [blame] | 18 | import copy |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 19 | import enum |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 20 | import uuid |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 21 | from collections import defaultdict |
Diqing Zhong | f842b69 | 2020-12-11 13:07:37 +0100 | [diff] [blame] | 22 | from enum import auto |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 23 | from functools import lru_cache |
Louis Verhaard | 6c74c3b | 2020-12-17 13:54:09 +0100 | [diff] [blame] | 24 | from functools import total_ordering |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 25 | from typing import Dict |
| 26 | from typing import List |
| 27 | from typing import Optional |
| 28 | from typing import Tuple |
| 29 | from typing import Union |
| 30 | from uuid import UUID |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 31 | |
| 32 | import numpy as np |
| 33 | |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 34 | from . import errors # Import this way due to cyclic imports |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 35 | from . import numeric_util |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 36 | from .data_type import BaseType |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 37 | from .data_type import DataType |
Dwight Lidman | a9390f7 | 2020-05-13 12:00:08 +0200 | [diff] [blame] | 38 | from .ethos_u55_regs.ethos_u55_regs import resampling_mode |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 39 | from .operation import Op |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 40 | from .operation import Operation |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 41 | |
| 42 | Shape = List |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 43 | |
| 44 | |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 45 | class MemType(enum.IntFlag): |
| 46 | Unknown = 0 |
| 47 | Permanent_NPU = 1 |
| 48 | Permanent_CPU = 2 |
| 49 | Scratch = 3 |
| 50 | Scratch_fast = 4 |
| 51 | Size = Scratch_fast + 1 |
| 52 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 53 | def display_name(self) -> str: |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 54 | return ("Unknown", "Permanent_NPU", "Permanent_CPU", "Scratch", "Scratch_fast", "Size")[self.value] |
| 55 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 56 | def identifier_name(self) -> str: |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 57 | return ("unknown", "permanent_npu", "permanent_cpu", "scratch", "scratch_fast", "size")[self.value] |
| 58 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 59 | @staticmethod |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 60 | def all(): |
| 61 | return (MemType.Permanent_NPU, MemType.Permanent_CPU, MemType.Scratch, MemType.Scratch_fast) |
| 62 | |
| 63 | def __str__(self): |
| 64 | return self.name |
| 65 | |
| 66 | |
Diqing Zhong | f842b69 | 2020-12-11 13:07:37 +0100 | [diff] [blame] | 67 | class BandwidthDirection(enum.IntEnum): |
| 68 | Read = 0 |
| 69 | Write = auto() |
| 70 | Size = auto() |
| 71 | |
| 72 | def display_name(self): |
| 73 | return self.name |
| 74 | |
| 75 | def identifier_name(self): |
| 76 | return self.name.lower() |
| 77 | |
| 78 | @staticmethod |
| 79 | def all(): |
| 80 | return (BandwidthDirection.Read, BandwidthDirection.Write) |
| 81 | |
| 82 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 83 | class MemArea(enum.IntFlag): |
| 84 | Unknown = 0 |
| 85 | Sram = 1 |
| 86 | Dram = 2 |
| 87 | OnChipFlash = 3 |
| 88 | OffChipFlash = 4 |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 89 | Shram = 5 # for LUT |
| 90 | Size = Shram + 1 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 91 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 92 | def display_name(self) -> str: |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 93 | return ("Unknown", "SRAM", "DRAM", "On-chip Flash", "Off-chip Flash", "SHRAM", "Size")[self.value] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 94 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 95 | def identifier_name(self) -> str: |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 96 | return ("unknown", "sram", "dram", "on_chip_flash", "off_chip_flash", "shram", "size")[self.value] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 97 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 98 | @staticmethod |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 99 | def all(): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 100 | return (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash, MemArea.Shram) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 101 | |
| 102 | def __str__(self): |
| 103 | return self.name |
| 104 | |
| 105 | |
| 106 | class TensorPurpose(enum.IntFlag): |
| 107 | Unknown = 0 |
| 108 | Weights = 1 |
| 109 | FeatureMap = 2 |
| 110 | Scratch = 3 |
Fredrik Svedberg | a0c3624 | 2020-06-03 15:43:31 +0200 | [diff] [blame] | 111 | LUT = 4 |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 112 | FSBias = 5 |
| 113 | Size = 6 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 114 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 115 | def display_name(self) -> str: |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 116 | return ("Unknown", "Weights", "FeatureMap", "Scratch", "LUT", "FastStorageBias", "Size")[self.value] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 117 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 118 | def identifier_name(self) -> str: |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 119 | return ("unknown", "weights", "feature_map", "scratch", "lut", "fast_storage_bias", "size")[self.value] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 120 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 121 | @staticmethod |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 122 | def all(): |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 123 | return (TensorPurpose.Weights, TensorPurpose.FeatureMap, TensorPurpose.FSBias) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 124 | |
| 125 | |
| 126 | class TensorSubPurpose(enum.Enum): |
| 127 | Standard = 0 |
| 128 | DoubleBuffer = 1 |
| 129 | RollingBufferX = 2 |
| 130 | RollingBufferY = 3 |
| 131 | RollingBufferXY = 4 |
| 132 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 133 | def display_name(self) -> str: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 134 | return ("Standard", "Double Buffer", "Rolling Buffer X", "Rolling Buffer Y", "Rolling Buffer XY")[self.value] |
| 135 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 136 | def identifier_name(self) -> str: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 137 | return ("standard", "double_buffer", "rolling_buffer_x", "rolling_buffer_y", "rolling_buffer_xy")[self.value] |
| 138 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 139 | @staticmethod |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 140 | def all(): |
| 141 | return ( |
| 142 | TensorSubPurpose.Standard, |
| 143 | TensorSubPurpose.DoubleBuffer, |
| 144 | TensorSubPurpose.RollingBufferX, |
| 145 | TensorSubPurpose.RollingBufferY, |
| 146 | TensorSubPurpose.RollingBufferXY, |
| 147 | ) |
| 148 | |
| 149 | |
| 150 | class TensorFormat(enum.Flag): |
| 151 | Unknown = 0 |
| 152 | WeightsCompressed = 1 |
| 153 | NHWC = 2 |
| 154 | NHCWB16 = 3 |
| 155 | |
| 156 | def __str__(self): |
| 157 | return self.name |
| 158 | |
| 159 | |
| 160 | class TensorBlockTraversal(enum.Enum): |
| 161 | Default = 0 |
| 162 | DepthWise = 1 |
| 163 | DepthFirst = 2 |
| 164 | PartKernelFirst = 3 |
| 165 | |
| 166 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 167 | def shape_num_elements(shp: Shape) -> Optional[int]: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 168 | elems = 1 |
| 169 | if shp is None: |
| 170 | return None |
| 171 | for d in shp: |
| 172 | if d is None: |
| 173 | return None |
| 174 | elems *= d |
| 175 | return elems |
| 176 | |
| 177 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 178 | def shape_fully_defined(shp: Shape) -> bool: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 179 | if shp is None: |
| 180 | return False |
| 181 | for d in shp: |
| 182 | if d is None: |
| 183 | return False |
| 184 | return True |
| 185 | |
| 186 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 187 | def shape_round_to_quantum(shp: Shape, quantum: Tuple) -> Shape: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 188 | new_shp = list(shp) |
| 189 | |
| 190 | # Traverse backwards using length of shape since there may be more rounding quantums than shape elements |
| 191 | for i in range(-1, -len(shp) - 1, -1): |
| 192 | if new_shp[i] is not None: |
| 193 | new_shp[i] = numeric_util.round_up(new_shp[i], quantum[i]) |
| 194 | return new_shp |
| 195 | |
| 196 | |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 197 | @lru_cache(maxsize=None) |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 198 | def create_equivalence_id(key) -> UUID: |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 199 | # Generates equivalence_id based on the given key. |
| 200 | return uuid.uuid4() |
| 201 | |
| 202 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 203 | class QuantizationParameters: |
| 204 | __slots__ = "min", "max", "num_bits", "narrow_range", "scale_f32", "zero_point", "quant_min", "quant_max" |
| 205 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 206 | def __init__( |
| 207 | self, |
| 208 | min: Union[float, np.ndarray, None] = None, |
| 209 | max: Union[float, np.ndarray, None] = None, |
| 210 | num_bits=None, |
| 211 | narrow_range=None, |
| 212 | ): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 213 | self.min = min |
| 214 | self.max = max |
| 215 | |
| 216 | self.num_bits = num_bits |
| 217 | self.narrow_range = narrow_range |
| 218 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 219 | self.scale_f32: Union[float, np.ndarray, None] = None |
| 220 | self.zero_point: Union[int, np.ndarray, None] = None |
| 221 | self.quant_min: Optional[float] = None |
| 222 | self.quant_max: Optional[float] = None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 223 | |
| 224 | def __str__(self): |
| 225 | return "<nng.QuantizationParameters min=%s max=%s, num_bits=%s, scale=%s, zero_point=%s>" % ( |
| 226 | self.min, |
| 227 | self.max, |
| 228 | self.num_bits, |
| 229 | self.scale_f32, |
| 230 | self.zero_point, |
| 231 | ) |
| 232 | |
| 233 | __repr__ = __str__ |
| 234 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 235 | def clone(self) -> "QuantizationParameters": |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 236 | res = QuantizationParameters() |
| 237 | res.min = self.min |
| 238 | res.max = self.max |
| 239 | |
| 240 | res.num_bits = self.num_bits |
| 241 | res.narrow_range = self.narrow_range |
| 242 | |
| 243 | res.scale_f32 = self.scale_f32 |
| 244 | res.zero_point = self.zero_point |
| 245 | res.quant_min = self.quant_min |
| 246 | res.quant_max = self.quant_max |
| 247 | return res |
| 248 | |
| 249 | def dequantize(self, values): |
| 250 | if self.zero_point.size == 1 and self.scale_f32.size == 1: |
| 251 | # same scale is used for all values |
| 252 | res = (values.astype(np.float64) - self.zero_point) * self.scale_f32 |
| 253 | else: |
| 254 | # a different scale is used for different sets of values |
| 255 | values_as_float = values.astype(np.float64) |
| 256 | |
| 257 | # this is not compatible with the format of depthwise weights, |
| 258 | # where input is at index 3 (Output, Kh, Kw, Input) |
| 259 | # return the quantized values |
| 260 | return np.ndarray((values_as_float.shape)) |
| 261 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 262 | return res |
| 263 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 264 | def is_scaling_equal(self, other: Optional["QuantizationParameters"]) -> bool: |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 265 | # quantisation parameter scaling is not equal if 'other' is None because |
| 266 | # it implies that the tensor it belongs to is not quantised. otherwise, |
| 267 | # it depends upon whether the scale and zero point are equal |
| 268 | |
Tim Hall | 8956761 | 2020-10-27 11:57:57 +0000 | [diff] [blame] | 269 | if not isinstance(other, QuantizationParameters): |
Tim Hall | e3786ac | 2020-07-28 17:40:50 +0100 | [diff] [blame] | 270 | return False |
| 271 | |
| 272 | return self.scale_f32 == other.scale_f32 and self.zero_point == other.zero_point |
| 273 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 274 | def is_valid(self) -> bool: |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 275 | # quantisation parameters are consider valid if they have a scale and zero point |
| 276 | |
| 277 | return None not in (self.scale_f32, self.zero_point) |
| 278 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 279 | def is_per_axis(self) -> bool: |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 280 | """Returns True if either the scale, zero point, minimum or maximum values are arrays""" |
| 281 | for attr in ("scale_f32", "zero_point", "min", "max"): |
| 282 | if isinstance(getattr(self, attr), np.ndarray): |
| 283 | return True |
| 284 | return False |
| 285 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 286 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 287 | def create_const_tensor( |
| 288 | name: str, |
| 289 | shape: Shape, |
| 290 | dtype: DataType, |
| 291 | values: np.ndarray, |
| 292 | value_dtype: np.dtype = None, |
| 293 | purpose: TensorPurpose = TensorPurpose.Unknown, |
| 294 | quantization: QuantizationParameters = None, |
| 295 | ): |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 296 | # Tensor |
| 297 | const_tensor = Tensor(shape, dtype, name + "_0") |
| 298 | const_tensor.purpose = purpose |
| 299 | const_tensor.quantization = quantization |
| 300 | const_tensor.values = np.array(values, dtype=value_dtype) |
Jacob Bohlin | a41cd4d | 2020-08-26 18:21:28 +0200 | [diff] [blame] | 301 | const_tensor.quant_values = np.frombuffer(const_tensor.values.tobytes(), dtype=np.uint8) |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 302 | # Operator |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 303 | const_op = Operation(Op.Const, name) |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 304 | const_op.set_output_tensor(const_tensor) |
| 305 | return const_tensor |
| 306 | |
| 307 | |
| 308 | def create_reshape_tensor(tens, shape, ifm_reshape=True): |
| 309 | if shape == tens.shape: |
| 310 | return tens |
| 311 | # Tensors |
| 312 | name = tens.name + "_reshape" |
| 313 | reshape_ifm = tens |
| 314 | reshape_ofm = tens.clone("_reshaped") |
| 315 | reshape_ofm.set_all_shapes(shape) |
| 316 | if not ifm_reshape: |
| 317 | reshape_ifm, reshape_ofm = reshape_ofm, reshape_ifm |
| 318 | # Operator |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 319 | reshape_op = Operation(Op.Reshape, name) |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 320 | reshape_op.attrs["new_shape"] = shape |
| 321 | reshape_op.add_input_tensor(reshape_ifm) |
| 322 | reshape_op.add_input_tensor(create_const_tensor(name + "_shape", [1], DataType.int32, shape)) |
| 323 | reshape_op.set_output_tensor(reshape_ofm) |
| 324 | return reshape_ofm if ifm_reshape else reshape_ifm |
| 325 | |
| 326 | |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 327 | # class that keeps track of all tensor addresses in the different memory types |
| 328 | class TensorAddressMap: |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 329 | address_map: Dict = defaultdict(dict) # dict (tens.equivalence_id -> dict (mem_type -> address)) |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 330 | |
| 331 | @classmethod |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 332 | def get_address_for_tens(cls, tens_id: UUID, mem_type: MemType) -> int: |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 333 | return cls.address_map[tens_id].get(mem_type) |
| 334 | |
| 335 | @classmethod |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 336 | def set_address_for_tens(cls, tens_id: UUID, mem_type: MemType, address: int): |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 337 | # Check previous address if there is one |
| 338 | previous_address = cls.address_map[tens_id].get(mem_type) |
Louis Verhaard | 0b9c9a3 | 2020-09-15 14:05:38 +0200 | [diff] [blame] | 339 | if address is not None and previous_address is not None: |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 340 | assert previous_address == address, "Two different addresses cannot be assigned to the same tensor." |
| 341 | |
| 342 | # Set tensor's address for memory type |
| 343 | cls.address_map[tens_id][mem_type] = address |
| 344 | |
| 345 | |
Louis Verhaard | 6c74c3b | 2020-12-17 13:54:09 +0100 | [diff] [blame] | 346 | @total_ordering |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 347 | class Tensor: |
| 348 | __slots__ = ( |
| 349 | "shape", |
| 350 | "storage_shape", |
| 351 | "bandwidth_shape", |
| 352 | "dtype", |
| 353 | "name", |
| 354 | "ops", |
| 355 | "consumer_list", |
| 356 | "values", |
| 357 | "quant_values", |
| 358 | "compressed_values", |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 359 | "compressed_values_substream_offsets", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 360 | "mem_area", |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 361 | "mem_type", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 362 | "format", |
| 363 | "purpose", |
| 364 | "sub_purpose", |
| 365 | "alignment", |
| 366 | "weight_transpose_depthwise", |
| 367 | "storage_compression_scale", |
| 368 | "bandwidth_compression_scale", |
| 369 | "compression_scale_for_worst_weight_stream", |
| 370 | "weight_compression_scales", |
| 371 | "weight_compression_config", |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 372 | "value_id", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 373 | "storage_rounding_quantum", |
| 374 | "brick_size", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 375 | "quantization", |
| 376 | "weight_compressed_offsets", |
| 377 | "element_size_bytes", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 378 | "block_traversal", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 379 | "equivalence_id", |
Dwight Lidman | a9390f7 | 2020-05-13 12:00:08 +0200 | [diff] [blame] | 380 | "resampling_mode", |
Patrik Gustavsson | 458a208 | 2020-08-13 13:41:05 +0200 | [diff] [blame] | 381 | "avoid_NHCWB16", |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 382 | ) |
| 383 | AllocationQuantum = 16 |
| 384 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 385 | def __init__(self, shape: Shape, dtype: DataType, name: str): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 386 | self.shape = shape |
| 387 | self.storage_shape = shape |
| 388 | self.bandwidth_shape = shape |
| 389 | self.dtype = dtype |
| 390 | self.name = name |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 391 | self.equivalence_id: UUID = uuid.uuid4() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 392 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 393 | self.ops: List[Operation] = [] |
| 394 | self.consumer_list: List[Operation] = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 395 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 396 | self.values: Optional[np.ndarray] = None |
| 397 | self.quant_values: Optional[np.ndarray] = None |
| 398 | self.compressed_values: Optional[np.ndarray] = None |
| 399 | self.compressed_values_substream_offsets: Optional[List] = None |
| 400 | self.mem_area: MemArea = MemArea.Unknown |
| 401 | self.mem_type: MemType = MemType.Unknown |
| 402 | self.format: TensorFormat = TensorFormat.Unknown |
| 403 | self.purpose: TensorPurpose = TensorPurpose.Unknown |
| 404 | self.sub_purpose: TensorSubPurpose = TensorSubPurpose.Standard |
| 405 | self.alignment: int = Tensor.AllocationQuantum |
| 406 | self.weight_transpose_depthwise: bool = False |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 407 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 408 | self.storage_compression_scale: float = 1.0 |
| 409 | self.bandwidth_compression_scale: float = 1.0 |
| 410 | self.compression_scale_for_worst_weight_stream: float = 1.0 |
| 411 | self.weight_compression_scales: Optional[np.ndarray] = None |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 412 | # if two tensors have the same weight_compression_config, then they have the same compressed values |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 413 | self.weight_compression_config = None |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 414 | # if two tensors have the same value_id, then they have the same values |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 415 | self.value_id: UUID = uuid.uuid4() |
| 416 | self.weight_compressed_offsets: List = [] |
| 417 | self.storage_rounding_quantum: Tuple = (1, 1, 1, 1) |
| 418 | self.brick_size: Tuple = (1, 1, 1, 1) |
| 419 | self.element_size_bytes: int = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 420 | |
| 421 | # quantization parameters |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 422 | self.quantization: Optional[QuantizationParameters] = None |
| 423 | self.block_traversal: TensorBlockTraversal = TensorBlockTraversal.Default |
| 424 | self.resampling_mode: resampling_mode = resampling_mode.NONE |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 425 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 426 | self.avoid_NHCWB16: bool = False |
Patrik Gustavsson | 458a208 | 2020-08-13 13:41:05 +0200 | [diff] [blame] | 427 | |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 428 | @property |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 429 | def address(self) -> int: |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 430 | return TensorAddressMap.get_address_for_tens(self.equivalence_id, self.mem_type) |
| 431 | |
| 432 | @address.setter |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 433 | def address(self, address: int): |
Jacob Bohlin | 1a66697 | 2020-09-11 10:04:15 +0200 | [diff] [blame] | 434 | TensorAddressMap.set_address_for_tens(self.equivalence_id, self.mem_type, address) |
| 435 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 436 | def element_size(self) -> int: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 437 | if self.element_size_bytes == 0: |
| 438 | return self.dtype.size_in_bits() / 8 |
| 439 | return self.element_size_bytes |
| 440 | |
Patrik Gustavsson | 6ae0e42 | 2020-11-04 12:43:50 +0100 | [diff] [blame] | 441 | # Returns a copy, renamed to self.name + suffix |
| 442 | # The references to Operators will be empty when returned |
| 443 | # Depending on set_unique, the copy is shallow, or deep |
| 444 | # For set_unique==True, a new equivalence_id will be set |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 445 | def clone(self, suffix="_clone", set_unique: bool = False) -> "Tensor": |
Patrik Gustavsson | 6ae0e42 | 2020-11-04 12:43:50 +0100 | [diff] [blame] | 446 | if set_unique: |
| 447 | res = copy.deepcopy(self) |
| 448 | res.equivalence_id = uuid.uuid4() |
| 449 | else: |
| 450 | res = copy.copy(self) |
| 451 | res.storage_shape = list(self.storage_shape) |
| 452 | res.bandwidth_shape = list(self.bandwidth_shape) |
| 453 | if self.quantization is not None: |
| 454 | res.quantization = self.quantization.clone() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 455 | |
Patrik Gustavsson | 6ae0e42 | 2020-11-04 12:43:50 +0100 | [diff] [blame] | 456 | res.name = res.name + suffix |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 457 | res.ops = [] |
| 458 | res.consumer_list = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 459 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 460 | return res |
| 461 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 462 | def clone_into_fast_storage(self, arch) -> "Tensor": |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 463 | res = self.clone(suffix="_fast_storage") |
| 464 | res.mem_area = arch.fast_storage_mem_area |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 465 | res.mem_type = MemType.Scratch_fast |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 466 | return res |
| 467 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 468 | def copy_compressed_weight_info(self, src_tens: "Tensor"): |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 469 | # Copies compressed values + all related weight compression info from the given tensor |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 470 | self.equivalence_id = src_tens.equivalence_id |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 471 | self.compressed_values = src_tens.compressed_values |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 472 | self.compressed_values_substream_offsets = src_tens.compressed_values_substream_offsets |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 473 | self.storage_shape = src_tens.storage_shape |
| 474 | self.brick_size = src_tens.brick_size |
| 475 | self.weight_compression_scales = src_tens.weight_compression_scales |
| 476 | self.weight_compressed_offsets = src_tens.weight_compressed_offsets |
| 477 | self.weight_transpose_depthwise = src_tens.weight_transpose_depthwise |
| 478 | self.compression_scale_for_worst_weight_stream = src_tens.compression_scale_for_worst_weight_stream |
| 479 | self.storage_compression_scale = src_tens.storage_compression_scale |
Diqing Zhong | 7e1d1d1 | 2020-10-30 15:10:46 +0100 | [diff] [blame] | 480 | self.bandwidth_compression_scale = src_tens.bandwidth_compression_scale |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 481 | self.block_traversal = src_tens.block_traversal |
| 482 | self.weight_compression_config = src_tens.weight_compression_config |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 483 | self.value_id = src_tens.value_id |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 484 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 485 | def set_format(self, fmt: TensorFormat, arch): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 486 | self.format = fmt |
| 487 | shape_len = 0 |
| 488 | try: |
| 489 | shape_len = len(self.shape) |
| 490 | except TypeError: |
| 491 | pass |
| 492 | |
Louis Verhaard | 0411edb | 2020-11-16 16:37:11 +0100 | [diff] [blame] | 493 | if shape_len > 4: |
| 494 | return |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 495 | self.storage_rounding_quantum = arch.storage_rounding_quantums[self.format] |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 496 | self.storage_rounding_quantum = tuple(self.storage_rounding_quantum[-shape_len:]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 497 | self.brick_size = arch.brick_sizes[self.format] |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 498 | self.brick_size = tuple(self.brick_size[-shape_len:]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 499 | if self.shape is None: |
| 500 | return |
| 501 | |
| 502 | self.bandwidth_shape = shape_round_to_quantum(self.shape, self.brick_size) |
| 503 | self.storage_shape = shape_round_to_quantum(self.shape, self.storage_rounding_quantum) |
| 504 | |
| 505 | if fmt == TensorFormat.WeightsCompressed: |
| 506 | compression_ratio = 5 / 8 |
| 507 | self.storage_compression_scale = compression_ratio |
| 508 | self.bandwidth_compression_scale = compression_ratio |
| 509 | self.compression_scale_for_worst_weight_stream = compression_ratio |
| 510 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 511 | def storage_elements(self) -> int: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 512 | elems = shape_num_elements(self.storage_shape) |
| 513 | if elems is None: |
| 514 | return 0 |
| 515 | return elems |
| 516 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 517 | def elements(self) -> int: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 518 | elems = shape_num_elements(self.shape) |
| 519 | if elems is None: |
| 520 | return 0 |
| 521 | return elems |
| 522 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 523 | def has_fully_defined_shape(self) -> bool: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 524 | return shape_fully_defined(self.shape) |
| 525 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 526 | def storage_size(self, scale: float = 1.0) -> int: |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 527 | raw_size = self.storage_elements() * self.element_size() * scale |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 528 | if raw_size == 0: |
| 529 | raw_size = 1 # force it to take up space |
| 530 | rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment) |
| 531 | return rounded_size |
| 532 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 533 | def storage_size_for_sub_purpose( |
| 534 | self, arch, sub_purpose: TensorSubPurpose, param_a: Optional[int] = None, param_b: Optional[int] = None |
| 535 | ) -> int: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 536 | alt_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b) |
| 537 | elems = shape_num_elements(alt_shape) |
| 538 | if elems is None: |
| 539 | return 0 |
| 540 | if sub_purpose == TensorSubPurpose.DoubleBuffer: |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 541 | raw_size = ( |
| 542 | elems |
| 543 | * self.element_size() |
| 544 | * self.compression_scale_for_worst_weight_stream |
| 545 | * arch.weight_estimation_scaling |
| 546 | ) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 547 | else: |
Patrik Gustavsson | 9baa4c3 | 2020-08-20 13:59:01 +0200 | [diff] [blame] | 548 | # Rolling buffers are used for intermediate data in ifm streaming |
| 549 | # These will all use the NHCWB16 format, and need to be aligned to 16 in the C-dimension |
| 550 | if alt_shape[-1] % 16 != 0: |
| 551 | nhcwb16_shape = alt_shape[0:-1] + [numeric_util.round_up(alt_shape[-1], 16)] |
| 552 | elems = shape_num_elements(nhcwb16_shape) |
| 553 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 554 | raw_size = elems * self.element_size() * self.storage_compression_scale |
| 555 | rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment) |
| 556 | return rounded_size |
| 557 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 558 | def storage_shape_for_sub_purpose( |
| 559 | self, sub_purpose: TensorSubPurpose, param_a: Optional[int], param_b: Optional[int] |
| 560 | ) -> Shape: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 561 | if sub_purpose == TensorSubPurpose.DoubleBuffer: |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 562 | shp = list(self.shape) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 563 | assert len(shp) >= 2 |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 564 | assert param_a is not None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 565 | shp[-1] = min(shp[-1], param_a * 2) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 566 | else: |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 567 | shp = list(self.storage_shape) |
| 568 | if sub_purpose == TensorSubPurpose.RollingBufferX: |
| 569 | assert len(shp) == 4 |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 570 | assert param_a is not None |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 571 | shp[0] = 1 |
| 572 | shp[2] = min(shp[2], param_a) |
| 573 | elif sub_purpose == TensorSubPurpose.RollingBufferY: |
| 574 | assert len(shp) == 4 |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 575 | assert param_a is not None |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 576 | shp[0] = 1 |
| 577 | shp[1] = min(shp[1], param_a) |
| 578 | elif sub_purpose == TensorSubPurpose.RollingBufferXY: |
| 579 | assert len(shp) == 4 |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 580 | assert param_a is not None |
| 581 | assert param_b is not None |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 582 | shp[0] = 1 |
| 583 | shp[2] = min(shp[2], param_a) |
| 584 | shp[1] = min(shp[1], param_b) |
| 585 | elif sub_purpose == TensorSubPurpose.Standard: |
| 586 | pass |
| 587 | else: |
| 588 | assert 0, "did not expect new sub purpose %s" % (sub_purpose,) |
| 589 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 590 | return shp |
| 591 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 592 | def set_new_sub_purpose(self, sub_purpose: TensorSubPurpose, param_a=None, param_b=None): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 593 | self.storage_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b) |
| 594 | self.sub_purpose = sub_purpose |
| 595 | if sub_purpose == TensorSubPurpose.DoubleBuffer: |
| 596 | self.storage_compression_scale = self.compression_scale_for_worst_weight_stream |
| 597 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 598 | def bandwidth(self) -> float: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 599 | elems = shape_num_elements(self.bandwidth_shape) |
| 600 | if elems is None: |
| 601 | return 0 |
| 602 | return elems * self.element_size() * self.bandwidth_compression_scale |
| 603 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 604 | def consumers(self) -> List[Operation]: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 605 | return self.consumer_list |
| 606 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 607 | def addresses_for_rolling_buffer(self, start_coord: Shape, end_coord: Shape) -> Tuple: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 608 | # returns ( box_height0, box_height1, box_width, [address_tl, address_tr, address_bl, address_br] ) |
| 609 | |
| 610 | if len(start_coord) < 4: |
| 611 | box_height0 = 1 |
| 612 | box_width = 1 |
| 613 | |
| 614 | if len(start_coord) >= 2: |
| 615 | box_width = end_coord[-2] - start_coord[-2] |
| 616 | |
| 617 | return box_height0, box_height0, box_width, [self.address_for_coordinate(start_coord), None, None, None] |
| 618 | |
| 619 | crossing_y = numeric_util.round_up(start_coord[1] + 1, self.storage_shape[1]) |
| 620 | crossing_x = numeric_util.round_up(start_coord[2] + 1, self.storage_shape[2]) |
| 621 | |
| 622 | crossing_y = min(crossing_y, end_coord[1]) |
| 623 | crossing_x = min(crossing_x, end_coord[2]) |
| 624 | |
| 625 | box_height0 = crossing_y - start_coord[1] |
| 626 | box_width = crossing_x - start_coord[2] |
| 627 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 628 | addresses: List = [None] * 4 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 629 | addresses[0] = self.address_for_coordinate(start_coord) |
| 630 | |
| 631 | if end_coord[2] > crossing_x: |
| 632 | addresses[1] = self.address_for_coordinate([start_coord[0], start_coord[1], crossing_x, start_coord[3]]) |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 633 | raise errors.UnsupportedFeatureError("Striping in vertical direction is not supported") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 634 | if end_coord[1] > crossing_y: |
| 635 | addresses[2] = self.address_for_coordinate([start_coord[0], crossing_y, start_coord[2], start_coord[3]]) |
| 636 | if end_coord[1] > crossing_y and end_coord[2] > crossing_x: |
| 637 | addresses[3] = self.address_for_coordinate([start_coord[0], crossing_y, crossing_x, start_coord[3]]) |
| 638 | |
| 639 | return box_height0, box_height0, box_width, addresses |
| 640 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 641 | def address_for_coordinate(self, coord: Shape, is_top_box: bool = False) -> int: |
| 642 | offset = self.address_offset_for_coordinate(coord, is_top_box) |
| 643 | assert offset is not None |
| 644 | return self.address + offset |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 645 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 646 | def get_strides_and_coord(self, coord: Optional[Shape] = None) -> Tuple[Optional[Shape], Optional[Shape]]: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 647 | if coord is None: |
| 648 | coord = [0] * len(self.storage_shape) |
| 649 | |
| 650 | augmented_coord = coord |
| 651 | augmented_shape = self.storage_shape |
| 652 | while len(augmented_shape) < 4: |
| 653 | augmented_shape = [1] + augmented_shape |
| 654 | |
| 655 | while len(augmented_coord) < 4: |
| 656 | augmented_coord = [0] + augmented_coord |
| 657 | |
| 658 | assert len(augmented_coord) == len(augmented_shape) |
| 659 | |
| 660 | if self.format == TensorFormat.NHWC: |
| 661 | augmented_shape = [augmented_shape[0], augmented_shape[3]] + augmented_shape[1:3] + [1] |
| 662 | augmented_coord = [augmented_coord[0], augmented_coord[3]] + augmented_coord[1:3] + [0] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 663 | |
| 664 | elif self.format == TensorFormat.NHCWB16: |
Patrik Gustavsson | 2213e90 | 2020-05-05 17:49:35 +0200 | [diff] [blame] | 665 | channel_divisor = 16 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 666 | augmented_shape = augmented_shape[0:4] + [1] |
| 667 | augmented_coord = ( |
| 668 | [augmented_coord[0], augmented_coord[3] // channel_divisor] |
| 669 | + augmented_coord[1:3] |
| 670 | + [augmented_coord[3] % channel_divisor] |
| 671 | ) |
| 672 | |
| 673 | if augmented_shape[1] == 0: |
| 674 | augmented_shape[1] = 1 |
| 675 | |
| 676 | else: |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 677 | assert self.format in (TensorFormat.Unknown, TensorFormat.WeightsCompressed) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 678 | return None, None |
| 679 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 680 | strides: List = [0] * len(augmented_shape) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 681 | stride = self.element_size() * self.storage_compression_scale |
| 682 | |
| 683 | if self.format != TensorFormat.NHCWB16: |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 684 | stride_order = [4, 1, 3, 2, 0] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 685 | for i in stride_order: |
| 686 | strides[i] = stride |
| 687 | stride *= augmented_shape[i] |
| 688 | else: |
| 689 | assert len(strides) == 5 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 690 | strides[4] = stride |
Patrik Gustavsson | 2213e90 | 2020-05-05 17:49:35 +0200 | [diff] [blame] | 691 | strides[3] = 16 * stride # STRIDE_X |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 692 | strides[1] = strides[3] * augmented_shape[2] # STRIDE_C |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 693 | strides[2] = augmented_shape[2] * augmented_shape[3] * stride # STRIDE_Y |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 694 | strides[0] = strides[2] * augmented_shape[1] # STRIDE_N |
| 695 | |
| 696 | return strides, augmented_coord |
| 697 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 698 | def get_strides(self) -> Shape: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 699 | strides, _ = self.get_strides_and_coord() |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 700 | assert strides is not None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 701 | return strides |
| 702 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 703 | def needs_dma(self) -> bool: |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 704 | return len(self.ops) == 1 and self.ops[0].type == Op.DMA |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 705 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 706 | def get_dma_src_tensor(self) -> "Optional[Tensor]": |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 707 | # For weight tensors that need DMA: returns the source tensor in Flash, else None |
| 708 | # Note: for DMA ops, Pass.weight_tensor is referring to the SRAM weight tensor |
| 709 | return self.ops[0].inputs[0] if self.needs_dma() else None |
| 710 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 711 | def find_npu_op(self) -> Optional[Operation]: |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 712 | # Returns the NPU operator that uses this tensor, excluding DMA operators. |
| 713 | for op in self.consumers(): |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 714 | if op.type == Op.DMA: |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 715 | return op.outputs[0].find_npu_op() |
Dwight Lidman | 940fdee | 2020-08-13 13:11:48 +0200 | [diff] [blame] | 716 | if op.run_on_npu: |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 717 | return op |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 718 | return None |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 719 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 720 | def compressed_stream_index_from_coord(self, coord: Shape) -> int: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 721 | assert self.format == TensorFormat.WeightsCompressed |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 722 | assert self.compressed_values is not None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 723 | assert len(self.compressed_values) > 0 |
| 724 | assert len(self.compressed_values) + 1 == len(self.weight_compressed_offsets) |
| 725 | |
| 726 | depth = coord[-1] |
| 727 | brick_depth = self.brick_size[-1] |
| 728 | # Clamp position at final element index |
| 729 | if depth > self.shape[-1]: |
| 730 | depth = self.shape[-1] |
| 731 | |
| 732 | # Always round up to next boundary |
Michael McGeagh | 8d3216f | 2020-08-10 11:35:57 +0100 | [diff] [blame] | 733 | index = numeric_util.round_up_divide(depth, brick_depth) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 734 | |
| 735 | # Check boundaries on all but last weight set (which may be shorter |
| 736 | # than the brick we divided it up into) |
| 737 | if index < len(self.weight_compressed_offsets) - 1: |
| 738 | # There are no half-way points in the weights |
| 739 | if (depth % brick_depth) != 0: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 740 | raise errors.UnsupportedFeatureError("Offset into weights must be aligned to a brick") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 741 | |
| 742 | return index |
| 743 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 744 | def size_of_compressed_stream(self, index: int) -> int: |
| 745 | assert self.compressed_values is not None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 746 | assert 0 <= index < len(self.compressed_values) |
| 747 | return len(self.compressed_values[index]) |
| 748 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 749 | def is_last_index_in_compressed_stream(self, index: int) -> bool: |
| 750 | assert self.compressed_values is not None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 751 | assert 0 <= index < len(self.compressed_values) |
| 752 | return index == len(self.compressed_values) - 1 |
| 753 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 754 | def address_offset_for_coordinate(self, orig_coord: Shape, is_top_box: bool = False) -> Optional[int]: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 755 | address_offset = 0 |
| 756 | coord = orig_coord |
| 757 | |
| 758 | coord = coord[-len(self.storage_shape) :] |
| 759 | |
| 760 | if self.sub_purpose == TensorSubPurpose.Standard: |
| 761 | for idx, c in enumerate(coord): |
| 762 | if is_top_box: |
| 763 | assert c > 0 and c <= self.shape[idx] |
| 764 | else: |
| 765 | assert c >= 0 and c < self.shape[idx] |
| 766 | |
| 767 | if self.format == TensorFormat.WeightsCompressed: |
| 768 | if len(self.weight_compressed_offsets) == 0: |
| 769 | return 0 |
| 770 | |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 771 | if self.needs_dma() and self.sub_purpose == TensorSubPurpose.DoubleBuffer: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 772 | depth = orig_coord[-1] |
| 773 | brick_depth = self.brick_size[-1] |
| 774 | # Clamp position at final element index |
| 775 | if depth > self.shape[-1]: |
| 776 | depth = self.shape[-1] |
| 777 | |
| 778 | # Always round up to next boundary |
Michael McGeagh | 8d3216f | 2020-08-10 11:35:57 +0100 | [diff] [blame] | 779 | index = numeric_util.round_up_divide(depth, brick_depth) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 780 | index = index % 2 |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 781 | assert self.compressed_values is not None |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 782 | |
| 783 | if len(self.compressed_values) <= 2: |
| 784 | if is_top_box and index == 0: |
| 785 | for cv in self.compressed_values: |
| 786 | address_offset += len(cv) |
| 787 | else: |
| 788 | address_offset = index * len(self.compressed_values[0]) |
| 789 | else: |
| 790 | if is_top_box and index == 0: |
| 791 | address_offset = self.storage_shape[-1] |
| 792 | else: |
| 793 | address_offset = index * (self.storage_shape[-1] // 2) |
| 794 | else: |
| 795 | index = self.compressed_stream_index_from_coord(orig_coord) |
| 796 | assert index < len(self.weight_compressed_offsets) |
| 797 | address_offset = self.weight_compressed_offsets[index] |
| 798 | else: |
| 799 | if is_top_box: |
| 800 | coord = [c - 1 for c in coord] |
| 801 | |
| 802 | # handle wraparound for partial buffers. make sure to do this after subtracting top box: |
| 803 | coord = [c % self.storage_shape[idx] for idx, c in enumerate(coord)] |
| 804 | |
| 805 | strides, augmented_coord = self.get_strides_and_coord(coord) |
| 806 | if strides is None: |
| 807 | return None |
| 808 | |
| 809 | if is_top_box: |
| 810 | address_offset += 1 * strides[-1] # one element |
| 811 | |
| 812 | address_offset += np.dot(augmented_coord, strides) |
| 813 | |
| 814 | assert address_offset >= 0 |
| 815 | assert address_offset <= self.storage_size() |
| 816 | return address_offset |
| 817 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 818 | def is_allocated_in_tensor_arena(self, scratch_tensor_mem_area: MemArea) -> bool: |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 819 | return (self.mem_area == scratch_tensor_mem_area) and (self.mem_type in (MemType.Scratch, MemType.Scratch_fast)) |
Patrik Gustavsson | eca2e95 | 2020-05-27 09:15:11 +0200 | [diff] [blame] | 820 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 821 | def equivalent(self, tens: "Tensor") -> bool: |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 822 | return self.equivalence_id == tens.equivalence_id |
| 823 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 824 | def set_all_shapes(self, shape: Shape): |
Michael McGeagh | 6a8d424 | 2020-07-28 12:17:59 +0100 | [diff] [blame] | 825 | self.shape = shape |
| 826 | self.storage_shape = shape |
| 827 | self.bandwidth_shape = shape |
| 828 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 829 | def get_full_shape(self) -> Shape: |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 830 | d = len(self.shape) |
| 831 | if d in (1, 3): |
Michael McGeagh | 8d3216f | 2020-08-10 11:35:57 +0100 | [diff] [blame] | 832 | return numeric_util.full_shape(4, self.shape, 1) |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 833 | elif d == 2: |
| 834 | return [self.shape[0], 1, 1, self.shape[1]] |
| 835 | else: |
Fredrik Svedberg | 835d8e1 | 2020-09-04 09:46:17 +0200 | [diff] [blame] | 836 | return self.shape.copy() |
Michael McGeagh | 5778ffd | 2020-08-06 17:31:02 +0100 | [diff] [blame] | 837 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 838 | def is_quantized(self) -> bool: |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 839 | # a tensor is quantized if it has an integral type and it contains valid quantization params |
| 840 | |
Tim Hall | 8956761 | 2020-10-27 11:57:57 +0000 | [diff] [blame] | 841 | if not isinstance(self.quantization, QuantizationParameters): |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 842 | return False |
| 843 | |
Tim Hall | 8956761 | 2020-10-27 11:57:57 +0000 | [diff] [blame] | 844 | return (self.dtype.type & BaseType.Int) != 0 and self.quantization.is_valid() |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 845 | |
Louis Verhaard | 6c74c3b | 2020-12-17 13:54:09 +0100 | [diff] [blame] | 846 | def __lt__(self, other: "Tensor") -> bool: |
| 847 | return self.equivalence_id < other.equivalence_id |
| 848 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 849 | def __str__(self): |
| 850 | return "<nng.Tensor '%s' shape=%s dtype=%s>" % (self.name, self.shape, self.dtype) |
| 851 | |
| 852 | __repr__ = __str__ |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 853 | |
| 854 | |
Louis Verhaard | 93719a9 | 2020-12-08 10:02:31 +0100 | [diff] [blame] | 855 | def check_quantized_tens_scaling_equal(tens_a: Tensor, tens_b: Tensor) -> bool: |
Tim Hall | 9358296 | 2020-09-09 21:58:15 +0100 | [diff] [blame] | 856 | # checks that the scaling of two quantized tensors are equal |
| 857 | |
Tim Hall | 8956761 | 2020-10-27 11:57:57 +0000 | [diff] [blame] | 858 | return tens_a.is_quantized() and tens_b.is_quantized() and tens_a.quantization.is_scaling_equal(tens_b.quantization) |