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