blob: 69618d2c26f4b09a999a71138a8aa2a99c744213 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# 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 Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Internal representation of a Neural Network Tensor.
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +010018import copy
Tim Hall79d07d22020-04-27 18:20:16 +010019import enum
Tim Hall79d07d22020-04-27 18:20:16 +010020import uuid
Jacob Bohlin1a666972020-09-11 10:04:15 +020021from collections import defaultdict
Diqing Zhongf842b692020-12-11 13:07:37 +010022from enum import auto
Louis Verhaard9db529a2020-09-23 10:27:11 +020023from functools import lru_cache
Louis Verhaard6c74c3b2020-12-17 13:54:09 +010024from functools import total_ordering
Louis Verhaard93719a92020-12-08 10:02:31 +010025from typing import Dict
26from typing import List
27from typing import Optional
28from typing import Tuple
29from typing import Union
30from uuid import UUID
Diego Russoea6111a2020-04-14 18:41:58 +010031
32import numpy as np
33
34from . import numeric_util
Tim Hall93582962020-09-09 21:58:15 +010035from .data_type import BaseType
Michael McGeagh5778ffd2020-08-06 17:31:02 +010036from .data_type import DataType
Michael McGeagh528a56d2020-12-16 11:33:21 +000037from .errors import UnsupportedFeatureError
38from .errors import VelaError
Dwight Lidmana9390f72020-05-13 12:00:08 +020039from .ethos_u55_regs.ethos_u55_regs import resampling_mode
Louis Verhaardaee5d752020-09-30 09:01:52 +020040from .operation import Op
Michael McGeagh5778ffd2020-08-06 17:31:02 +010041from .operation import Operation
Louis Verhaard93719a92020-12-08 10:02:31 +010042
43Shape = List
Tim Hall79d07d22020-04-27 18:20:16 +010044
45
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020046class MemType(enum.IntFlag):
47 Unknown = 0
48 Permanent_NPU = 1
49 Permanent_CPU = 2
50 Scratch = 3
51 Scratch_fast = 4
52 Size = Scratch_fast + 1
53
Louis Verhaard93719a92020-12-08 10:02:31 +010054 def display_name(self) -> str:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020055 return ("Unknown", "Permanent_NPU", "Permanent_CPU", "Scratch", "Scratch_fast", "Size")[self.value]
56
Louis Verhaard93719a92020-12-08 10:02:31 +010057 def identifier_name(self) -> str:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020058 return ("unknown", "permanent_npu", "permanent_cpu", "scratch", "scratch_fast", "size")[self.value]
59
Louis Verhaard93719a92020-12-08 10:02:31 +010060 @staticmethod
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020061 def all():
62 return (MemType.Permanent_NPU, MemType.Permanent_CPU, MemType.Scratch, MemType.Scratch_fast)
63
64 def __str__(self):
65 return self.name
66
67
Diqing Zhongf842b692020-12-11 13:07:37 +010068class BandwidthDirection(enum.IntEnum):
69 Read = 0
70 Write = auto()
71 Size = auto()
72
73 def display_name(self):
74 return self.name
75
76 def identifier_name(self):
77 return self.name.lower()
78
79 @staticmethod
80 def all():
81 return (BandwidthDirection.Read, BandwidthDirection.Write)
82
83
Tim Hall79d07d22020-04-27 18:20:16 +010084class MemArea(enum.IntFlag):
85 Unknown = 0
86 Sram = 1
87 Dram = 2
88 OnChipFlash = 3
89 OffChipFlash = 4
Louis Verhaard0b8268a2020-08-05 16:11:29 +020090 Shram = 5 # for LUT
91 Size = Shram + 1
Tim Hall79d07d22020-04-27 18:20:16 +010092
Louis Verhaard93719a92020-12-08 10:02:31 +010093 def display_name(self) -> str:
Louis Verhaard0b8268a2020-08-05 16:11:29 +020094 return ("Unknown", "SRAM", "DRAM", "On-chip Flash", "Off-chip Flash", "SHRAM", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010095
Louis Verhaard93719a92020-12-08 10:02:31 +010096 def identifier_name(self) -> str:
Louis Verhaard0b8268a2020-08-05 16:11:29 +020097 return ("unknown", "sram", "dram", "on_chip_flash", "off_chip_flash", "shram", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010098
Louis Verhaard93719a92020-12-08 10:02:31 +010099 @staticmethod
Tim Hall79d07d22020-04-27 18:20:16 +0100100 def all():
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200101 return (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash, MemArea.Shram)
Tim Hall79d07d22020-04-27 18:20:16 +0100102
103 def __str__(self):
104 return self.name
105
106
107class TensorPurpose(enum.IntFlag):
108 Unknown = 0
109 Weights = 1
110 FeatureMap = 2
111 Scratch = 3
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200112 LUT = 4
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100113 FSBias = 5
114 Size = 6
Tim Hall79d07d22020-04-27 18:20:16 +0100115
Louis Verhaard93719a92020-12-08 10:02:31 +0100116 def display_name(self) -> str:
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100117 return ("Unknown", "Weights", "FeatureMap", "Scratch", "LUT", "FastStorageBias", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +0100118
Louis Verhaard93719a92020-12-08 10:02:31 +0100119 def identifier_name(self) -> str:
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100120 return ("unknown", "weights", "feature_map", "scratch", "lut", "fast_storage_bias", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +0100121
Louis Verhaard93719a92020-12-08 10:02:31 +0100122 @staticmethod
Tim Hall79d07d22020-04-27 18:20:16 +0100123 def all():
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100124 return (TensorPurpose.Weights, TensorPurpose.FeatureMap, TensorPurpose.FSBias)
Tim Hall79d07d22020-04-27 18:20:16 +0100125
126
127class TensorSubPurpose(enum.Enum):
128 Standard = 0
129 DoubleBuffer = 1
130 RollingBufferX = 2
131 RollingBufferY = 3
132 RollingBufferXY = 4
133
Louis Verhaard93719a92020-12-08 10:02:31 +0100134 def display_name(self) -> str:
Tim Hall79d07d22020-04-27 18:20:16 +0100135 return ("Standard", "Double Buffer", "Rolling Buffer X", "Rolling Buffer Y", "Rolling Buffer XY")[self.value]
136
Louis Verhaard93719a92020-12-08 10:02:31 +0100137 def identifier_name(self) -> str:
Tim Hall79d07d22020-04-27 18:20:16 +0100138 return ("standard", "double_buffer", "rolling_buffer_x", "rolling_buffer_y", "rolling_buffer_xy")[self.value]
139
Louis Verhaard93719a92020-12-08 10:02:31 +0100140 @staticmethod
Tim Hall79d07d22020-04-27 18:20:16 +0100141 def all():
142 return (
143 TensorSubPurpose.Standard,
144 TensorSubPurpose.DoubleBuffer,
145 TensorSubPurpose.RollingBufferX,
146 TensorSubPurpose.RollingBufferY,
147 TensorSubPurpose.RollingBufferXY,
148 )
149
150
151class TensorFormat(enum.Flag):
152 Unknown = 0
153 WeightsCompressed = 1
154 NHWC = 2
155 NHCWB16 = 3
156
157 def __str__(self):
158 return self.name
159
160
161class TensorBlockTraversal(enum.Enum):
162 Default = 0
163 DepthWise = 1
164 DepthFirst = 2
165 PartKernelFirst = 3
166
167
Louis Verhaard93719a92020-12-08 10:02:31 +0100168def shape_num_elements(shp: Shape) -> Optional[int]:
Tim Hall79d07d22020-04-27 18:20:16 +0100169 elems = 1
170 if shp is None:
171 return None
172 for d in shp:
173 if d is None:
174 return None
175 elems *= d
176 return elems
177
178
Louis Verhaard93719a92020-12-08 10:02:31 +0100179def shape_fully_defined(shp: Shape) -> bool:
Tim Hall79d07d22020-04-27 18:20:16 +0100180 if shp is None:
181 return False
182 for d in shp:
183 if d is None:
184 return False
185 return True
186
187
Louis Verhaard93719a92020-12-08 10:02:31 +0100188def shape_round_to_quantum(shp: Shape, quantum: Tuple) -> Shape:
Tim Hall79d07d22020-04-27 18:20:16 +0100189 new_shp = list(shp)
190
191 # Traverse backwards using length of shape since there may be more rounding quantums than shape elements
192 for i in range(-1, -len(shp) - 1, -1):
193 if new_shp[i] is not None:
194 new_shp[i] = numeric_util.round_up(new_shp[i], quantum[i])
195 return new_shp
196
197
Louis Verhaard9db529a2020-09-23 10:27:11 +0200198@lru_cache(maxsize=None)
Louis Verhaard93719a92020-12-08 10:02:31 +0100199def create_equivalence_id(key) -> UUID:
Louis Verhaard9db529a2020-09-23 10:27:11 +0200200 # Generates equivalence_id based on the given key.
201 return uuid.uuid4()
202
203
Tim Hall79d07d22020-04-27 18:20:16 +0100204class QuantizationParameters:
205 __slots__ = "min", "max", "num_bits", "narrow_range", "scale_f32", "zero_point", "quant_min", "quant_max"
206
Louis Verhaard93719a92020-12-08 10:02:31 +0100207 def __init__(
208 self,
209 min: Union[float, np.ndarray, None] = None,
210 max: Union[float, np.ndarray, None] = None,
211 num_bits=None,
212 narrow_range=None,
213 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100214 self.min = min
215 self.max = max
216
217 self.num_bits = num_bits
218 self.narrow_range = narrow_range
219
Louis Verhaard93719a92020-12-08 10:02:31 +0100220 self.scale_f32: Union[float, np.ndarray, None] = None
221 self.zero_point: Union[int, np.ndarray, None] = None
222 self.quant_min: Optional[float] = None
223 self.quant_max: Optional[float] = None
Tim Hall79d07d22020-04-27 18:20:16 +0100224
225 def __str__(self):
226 return "<nng.QuantizationParameters min=%s max=%s, num_bits=%s, scale=%s, zero_point=%s>" % (
227 self.min,
228 self.max,
229 self.num_bits,
230 self.scale_f32,
231 self.zero_point,
232 )
233
234 __repr__ = __str__
235
Louis Verhaard93719a92020-12-08 10:02:31 +0100236 def clone(self) -> "QuantizationParameters":
Tim Hall79d07d22020-04-27 18:20:16 +0100237 res = QuantizationParameters()
238 res.min = self.min
239 res.max = self.max
240
241 res.num_bits = self.num_bits
242 res.narrow_range = self.narrow_range
243
244 res.scale_f32 = self.scale_f32
245 res.zero_point = self.zero_point
246 res.quant_min = self.quant_min
247 res.quant_max = self.quant_max
248 return res
249
250 def dequantize(self, values):
251 if self.zero_point.size == 1 and self.scale_f32.size == 1:
252 # same scale is used for all values
253 res = (values.astype(np.float64) - self.zero_point) * self.scale_f32
254 else:
255 # a different scale is used for different sets of values
256 values_as_float = values.astype(np.float64)
257
258 # this is not compatible with the format of depthwise weights,
259 # where input is at index 3 (Output, Kh, Kw, Input)
260 # return the quantized values
261 return np.ndarray((values_as_float.shape))
262
Tim Hall79d07d22020-04-27 18:20:16 +0100263 return res
264
Louis Verhaard93719a92020-12-08 10:02:31 +0100265 def is_scaling_equal(self, other: Optional["QuantizationParameters"]) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100266 # quantisation parameter scaling is not equal if 'other' is None because
267 # it implies that the tensor it belongs to is not quantised. otherwise,
268 # it depends upon whether the scale and zero point are equal
269
Tim Hall89567612020-10-27 11:57:57 +0000270 if not isinstance(other, QuantizationParameters):
Tim Halle3786ac2020-07-28 17:40:50 +0100271 return False
272
273 return self.scale_f32 == other.scale_f32 and self.zero_point == other.zero_point
274
Louis Verhaard93719a92020-12-08 10:02:31 +0100275 def is_valid(self) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100276 # quantisation parameters are consider valid if they have a scale and zero point
277
278 return None not in (self.scale_f32, self.zero_point)
279
Louis Verhaard93719a92020-12-08 10:02:31 +0100280 def is_per_axis(self) -> bool:
Dwight Lidmanc7187432020-11-16 17:40:46 +0100281 """Returns True if either the scale, zero point, minimum or maximum values are arrays"""
282 for attr in ("scale_f32", "zero_point", "min", "max"):
283 if isinstance(getattr(self, attr), np.ndarray):
284 return True
285 return False
286
Tim Hall79d07d22020-04-27 18:20:16 +0100287
Louis Verhaard93719a92020-12-08 10:02:31 +0100288def create_const_tensor(
289 name: str,
290 shape: Shape,
291 dtype: DataType,
292 values: np.ndarray,
293 value_dtype: np.dtype = None,
294 purpose: TensorPurpose = TensorPurpose.Unknown,
295 quantization: QuantizationParameters = None,
296):
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100297 # Tensor
298 const_tensor = Tensor(shape, dtype, name + "_0")
299 const_tensor.purpose = purpose
300 const_tensor.quantization = quantization
301 const_tensor.values = np.array(values, dtype=value_dtype)
Jacob Bohlina41cd4d2020-08-26 18:21:28 +0200302 const_tensor.quant_values = np.frombuffer(const_tensor.values.tobytes(), dtype=np.uint8)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100303 # Operator
Louis Verhaardaee5d752020-09-30 09:01:52 +0200304 const_op = Operation(Op.Const, name)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100305 const_op.set_output_tensor(const_tensor)
306 return const_tensor
307
308
309def create_reshape_tensor(tens, shape, ifm_reshape=True):
310 if shape == tens.shape:
311 return tens
312 # Tensors
313 name = tens.name + "_reshape"
314 reshape_ifm = tens
315 reshape_ofm = tens.clone("_reshaped")
316 reshape_ofm.set_all_shapes(shape)
317 if not ifm_reshape:
318 reshape_ifm, reshape_ofm = reshape_ofm, reshape_ifm
319 # Operator
Louis Verhaardaee5d752020-09-30 09:01:52 +0200320 reshape_op = Operation(Op.Reshape, name)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100321 reshape_op.attrs["new_shape"] = shape
322 reshape_op.add_input_tensor(reshape_ifm)
323 reshape_op.add_input_tensor(create_const_tensor(name + "_shape", [1], DataType.int32, shape))
324 reshape_op.set_output_tensor(reshape_ofm)
325 return reshape_ofm if ifm_reshape else reshape_ifm
326
327
Jacob Bohlin1a666972020-09-11 10:04:15 +0200328# class that keeps track of all tensor addresses in the different memory types
329class TensorAddressMap:
Louis Verhaard93719a92020-12-08 10:02:31 +0100330 address_map: Dict = defaultdict(dict) # dict (tens.equivalence_id -> dict (mem_type -> address))
Jacob Bohlin1a666972020-09-11 10:04:15 +0200331
332 @classmethod
Louis Verhaard93719a92020-12-08 10:02:31 +0100333 def get_address_for_tens(cls, tens_id: UUID, mem_type: MemType) -> int:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200334 return cls.address_map[tens_id].get(mem_type)
335
336 @classmethod
Louis Verhaard93719a92020-12-08 10:02:31 +0100337 def set_address_for_tens(cls, tens_id: UUID, mem_type: MemType, address: int):
Jacob Bohlin1a666972020-09-11 10:04:15 +0200338 # Check previous address if there is one
339 previous_address = cls.address_map[tens_id].get(mem_type)
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200340 if address is not None and previous_address is not None:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200341 assert previous_address == address, "Two different addresses cannot be assigned to the same tensor."
342
343 # Set tensor's address for memory type
344 cls.address_map[tens_id][mem_type] = address
345
346
Louis Verhaard6c74c3b2020-12-17 13:54:09 +0100347@total_ordering
Tim Hall79d07d22020-04-27 18:20:16 +0100348class Tensor:
349 __slots__ = (
350 "shape",
351 "storage_shape",
352 "bandwidth_shape",
353 "dtype",
354 "name",
355 "ops",
356 "consumer_list",
357 "values",
358 "quant_values",
359 "compressed_values",
Tim Hallf7e810a2020-06-25 15:04:31 +0100360 "compressed_values_substream_offsets",
Tim Hall79d07d22020-04-27 18:20:16 +0100361 "mem_area",
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200362 "mem_type",
Tim Hall79d07d22020-04-27 18:20:16 +0100363 "format",
364 "purpose",
365 "sub_purpose",
366 "alignment",
367 "weight_transpose_depthwise",
368 "storage_compression_scale",
369 "bandwidth_compression_scale",
370 "compression_scale_for_worst_weight_stream",
371 "weight_compression_scales",
372 "weight_compression_config",
Louis Verhaard9db529a2020-09-23 10:27:11 +0200373 "value_id",
Tim Hall79d07d22020-04-27 18:20:16 +0100374 "storage_rounding_quantum",
375 "brick_size",
Tim Hall79d07d22020-04-27 18:20:16 +0100376 "quantization",
377 "weight_compressed_offsets",
378 "element_size_bytes",
Tim Hall79d07d22020-04-27 18:20:16 +0100379 "block_traversal",
Tim Hall79d07d22020-04-27 18:20:16 +0100380 "equivalence_id",
Dwight Lidmana9390f72020-05-13 12:00:08 +0200381 "resampling_mode",
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200382 "avoid_NHCWB16",
Tim Hall79d07d22020-04-27 18:20:16 +0100383 )
384 AllocationQuantum = 16
385
Louis Verhaard93719a92020-12-08 10:02:31 +0100386 def __init__(self, shape: Shape, dtype: DataType, name: str):
Tim Hall79d07d22020-04-27 18:20:16 +0100387 self.shape = shape
388 self.storage_shape = shape
389 self.bandwidth_shape = shape
390 self.dtype = dtype
391 self.name = name
Louis Verhaard93719a92020-12-08 10:02:31 +0100392 self.equivalence_id: UUID = uuid.uuid4()
Tim Hall79d07d22020-04-27 18:20:16 +0100393
Louis Verhaard93719a92020-12-08 10:02:31 +0100394 self.ops: List[Operation] = []
395 self.consumer_list: List[Operation] = []
Tim Hall79d07d22020-04-27 18:20:16 +0100396
Louis Verhaard93719a92020-12-08 10:02:31 +0100397 self.values: Optional[np.ndarray] = None
398 self.quant_values: Optional[np.ndarray] = None
399 self.compressed_values: Optional[np.ndarray] = None
400 self.compressed_values_substream_offsets: Optional[List] = None
401 self.mem_area: MemArea = MemArea.Unknown
402 self.mem_type: MemType = MemType.Unknown
403 self.format: TensorFormat = TensorFormat.Unknown
404 self.purpose: TensorPurpose = TensorPurpose.Unknown
405 self.sub_purpose: TensorSubPurpose = TensorSubPurpose.Standard
406 self.alignment: int = Tensor.AllocationQuantum
407 self.weight_transpose_depthwise: bool = False
Tim Hall79d07d22020-04-27 18:20:16 +0100408
Louis Verhaard93719a92020-12-08 10:02:31 +0100409 self.storage_compression_scale: float = 1.0
410 self.bandwidth_compression_scale: float = 1.0
411 self.compression_scale_for_worst_weight_stream: float = 1.0
412 self.weight_compression_scales: Optional[np.ndarray] = None
Louis Verhaard9db529a2020-09-23 10:27:11 +0200413 # if two tensors have the same weight_compression_config, then they have the same compressed values
Tim Hall79d07d22020-04-27 18:20:16 +0100414 self.weight_compression_config = None
Louis Verhaard9db529a2020-09-23 10:27:11 +0200415 # if two tensors have the same value_id, then they have the same values
Louis Verhaard93719a92020-12-08 10:02:31 +0100416 self.value_id: UUID = uuid.uuid4()
417 self.weight_compressed_offsets: List = []
418 self.storage_rounding_quantum: Tuple = (1, 1, 1, 1)
419 self.brick_size: Tuple = (1, 1, 1, 1)
420 self.element_size_bytes: int = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100421
422 # quantization parameters
Louis Verhaard93719a92020-12-08 10:02:31 +0100423 self.quantization: Optional[QuantizationParameters] = None
424 self.block_traversal: TensorBlockTraversal = TensorBlockTraversal.Default
425 self.resampling_mode: resampling_mode = resampling_mode.NONE
Tim Hall79d07d22020-04-27 18:20:16 +0100426
Louis Verhaard93719a92020-12-08 10:02:31 +0100427 self.avoid_NHCWB16: bool = False
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200428
Jacob Bohlin1a666972020-09-11 10:04:15 +0200429 @property
Louis Verhaard93719a92020-12-08 10:02:31 +0100430 def address(self) -> int:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200431 return TensorAddressMap.get_address_for_tens(self.equivalence_id, self.mem_type)
432
433 @address.setter
Louis Verhaard93719a92020-12-08 10:02:31 +0100434 def address(self, address: int):
Jacob Bohlin1a666972020-09-11 10:04:15 +0200435 TensorAddressMap.set_address_for_tens(self.equivalence_id, self.mem_type, address)
436
Louis Verhaard93719a92020-12-08 10:02:31 +0100437 def element_size(self) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100438 if self.element_size_bytes == 0:
439 return self.dtype.size_in_bits() / 8
440 return self.element_size_bytes
441
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100442 # Returns a copy, renamed to self.name + suffix
443 # The references to Operators will be empty when returned
444 # Depending on set_unique, the copy is shallow, or deep
445 # For set_unique==True, a new equivalence_id will be set
Louis Verhaard93719a92020-12-08 10:02:31 +0100446 def clone(self, suffix="_clone", set_unique: bool = False) -> "Tensor":
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100447 if set_unique:
448 res = copy.deepcopy(self)
449 res.equivalence_id = uuid.uuid4()
450 else:
451 res = copy.copy(self)
452 res.storage_shape = list(self.storage_shape)
453 res.bandwidth_shape = list(self.bandwidth_shape)
454 if self.quantization is not None:
455 res.quantization = self.quantization.clone()
Tim Hall79d07d22020-04-27 18:20:16 +0100456
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100457 res.name = res.name + suffix
Tim Hall79d07d22020-04-27 18:20:16 +0100458 res.ops = []
459 res.consumer_list = []
Tim Hall79d07d22020-04-27 18:20:16 +0100460
Tim Hall79d07d22020-04-27 18:20:16 +0100461 return res
462
Louis Verhaard93719a92020-12-08 10:02:31 +0100463 def clone_into_fast_storage(self, arch) -> "Tensor":
Tim Hall79d07d22020-04-27 18:20:16 +0100464 res = self.clone(suffix="_fast_storage")
465 res.mem_area = arch.fast_storage_mem_area
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200466 res.mem_type = MemType.Scratch_fast
Tim Hall79d07d22020-04-27 18:20:16 +0100467 return res
468
Louis Verhaard93719a92020-12-08 10:02:31 +0100469 def copy_compressed_weight_info(self, src_tens: "Tensor"):
Louis Verhaard3c07c972020-05-07 08:12:58 +0200470 # Copies compressed values + all related weight compression info from the given tensor
Louis Verhaard9db529a2020-09-23 10:27:11 +0200471 self.equivalence_id = src_tens.equivalence_id
Louis Verhaard3c07c972020-05-07 08:12:58 +0200472 self.compressed_values = src_tens.compressed_values
Tim Hallf7e810a2020-06-25 15:04:31 +0100473 self.compressed_values_substream_offsets = src_tens.compressed_values_substream_offsets
Louis Verhaard3c07c972020-05-07 08:12:58 +0200474 self.storage_shape = src_tens.storage_shape
475 self.brick_size = src_tens.brick_size
476 self.weight_compression_scales = src_tens.weight_compression_scales
477 self.weight_compressed_offsets = src_tens.weight_compressed_offsets
478 self.weight_transpose_depthwise = src_tens.weight_transpose_depthwise
479 self.compression_scale_for_worst_weight_stream = src_tens.compression_scale_for_worst_weight_stream
480 self.storage_compression_scale = src_tens.storage_compression_scale
Diqing Zhong7e1d1d12020-10-30 15:10:46 +0100481 self.bandwidth_compression_scale = src_tens.bandwidth_compression_scale
Louis Verhaard3c07c972020-05-07 08:12:58 +0200482 self.block_traversal = src_tens.block_traversal
483 self.weight_compression_config = src_tens.weight_compression_config
Louis Verhaard9db529a2020-09-23 10:27:11 +0200484 self.value_id = src_tens.value_id
Louis Verhaard3c07c972020-05-07 08:12:58 +0200485
Louis Verhaard93719a92020-12-08 10:02:31 +0100486 def set_format(self, fmt: TensorFormat, arch):
Tim Hall79d07d22020-04-27 18:20:16 +0100487 self.format = fmt
488 shape_len = 0
489 try:
490 shape_len = len(self.shape)
491 except TypeError:
492 pass
493
Louis Verhaard0411edb2020-11-16 16:37:11 +0100494 if shape_len > 4:
495 return
Tim Hall79d07d22020-04-27 18:20:16 +0100496 self.storage_rounding_quantum = arch.storage_rounding_quantums[self.format]
Louis Verhaard93719a92020-12-08 10:02:31 +0100497 self.storage_rounding_quantum = tuple(self.storage_rounding_quantum[-shape_len:])
Tim Hall79d07d22020-04-27 18:20:16 +0100498 self.brick_size = arch.brick_sizes[self.format]
Louis Verhaard93719a92020-12-08 10:02:31 +0100499 self.brick_size = tuple(self.brick_size[-shape_len:])
Tim Hall79d07d22020-04-27 18:20:16 +0100500 if self.shape is None:
501 return
502
503 self.bandwidth_shape = shape_round_to_quantum(self.shape, self.brick_size)
504 self.storage_shape = shape_round_to_quantum(self.shape, self.storage_rounding_quantum)
505
506 if fmt == TensorFormat.WeightsCompressed:
507 compression_ratio = 5 / 8
508 self.storage_compression_scale = compression_ratio
509 self.bandwidth_compression_scale = compression_ratio
510 self.compression_scale_for_worst_weight_stream = compression_ratio
511
Louis Verhaard93719a92020-12-08 10:02:31 +0100512 def storage_elements(self) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100513 elems = shape_num_elements(self.storage_shape)
514 if elems is None:
515 return 0
516 return elems
517
Louis Verhaard93719a92020-12-08 10:02:31 +0100518 def elements(self) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100519 elems = shape_num_elements(self.shape)
520 if elems is None:
521 return 0
522 return elems
523
Louis Verhaard93719a92020-12-08 10:02:31 +0100524 def has_fully_defined_shape(self) -> bool:
Tim Hall79d07d22020-04-27 18:20:16 +0100525 return shape_fully_defined(self.shape)
526
Louis Verhaard93719a92020-12-08 10:02:31 +0100527 def storage_size(self, scale: float = 1.0) -> int:
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200528 raw_size = self.storage_elements() * self.element_size() * scale
Tim Hall79d07d22020-04-27 18:20:16 +0100529 if raw_size == 0:
530 raw_size = 1 # force it to take up space
531 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
532 return rounded_size
533
Louis Verhaard93719a92020-12-08 10:02:31 +0100534 def storage_size_for_sub_purpose(
535 self, arch, sub_purpose: TensorSubPurpose, param_a: Optional[int] = None, param_b: Optional[int] = None
536 ) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100537 alt_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
538 elems = shape_num_elements(alt_shape)
539 if elems is None:
540 return 0
541 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200542 raw_size = (
543 elems
544 * self.element_size()
545 * self.compression_scale_for_worst_weight_stream
546 * arch.weight_estimation_scaling
547 )
Tim Hall79d07d22020-04-27 18:20:16 +0100548 else:
Patrik Gustavsson9baa4c32020-08-20 13:59:01 +0200549 # Rolling buffers are used for intermediate data in ifm streaming
550 # These will all use the NHCWB16 format, and need to be aligned to 16 in the C-dimension
551 if alt_shape[-1] % 16 != 0:
552 nhcwb16_shape = alt_shape[0:-1] + [numeric_util.round_up(alt_shape[-1], 16)]
553 elems = shape_num_elements(nhcwb16_shape)
554
Tim Hall79d07d22020-04-27 18:20:16 +0100555 raw_size = elems * self.element_size() * self.storage_compression_scale
556 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
557 return rounded_size
558
Louis Verhaard93719a92020-12-08 10:02:31 +0100559 def storage_shape_for_sub_purpose(
560 self, sub_purpose: TensorSubPurpose, param_a: Optional[int], param_b: Optional[int]
561 ) -> Shape:
Tim Hall79d07d22020-04-27 18:20:16 +0100562 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Jacob Bohline843d332020-06-23 12:12:56 +0200563 shp = list(self.shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100564 assert len(shp) >= 2
Louis Verhaard93719a92020-12-08 10:02:31 +0100565 assert param_a is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100566 shp[-1] = min(shp[-1], param_a * 2)
Tim Hall79d07d22020-04-27 18:20:16 +0100567 else:
Jacob Bohline843d332020-06-23 12:12:56 +0200568 shp = list(self.storage_shape)
569 if sub_purpose == TensorSubPurpose.RollingBufferX:
570 assert len(shp) == 4
Louis Verhaard93719a92020-12-08 10:02:31 +0100571 assert param_a is not None
Jacob Bohline843d332020-06-23 12:12:56 +0200572 shp[0] = 1
573 shp[2] = min(shp[2], param_a)
574 elif sub_purpose == TensorSubPurpose.RollingBufferY:
575 assert len(shp) == 4
Louis Verhaard93719a92020-12-08 10:02:31 +0100576 assert param_a is not None
Jacob Bohline843d332020-06-23 12:12:56 +0200577 shp[0] = 1
578 shp[1] = min(shp[1], param_a)
579 elif sub_purpose == TensorSubPurpose.RollingBufferXY:
580 assert len(shp) == 4
Louis Verhaard93719a92020-12-08 10:02:31 +0100581 assert param_a is not None
582 assert param_b is not None
Jacob Bohline843d332020-06-23 12:12:56 +0200583 shp[0] = 1
584 shp[2] = min(shp[2], param_a)
585 shp[1] = min(shp[1], param_b)
586 elif sub_purpose == TensorSubPurpose.Standard:
587 pass
588 else:
589 assert 0, "did not expect new sub purpose %s" % (sub_purpose,)
590
Tim Hall79d07d22020-04-27 18:20:16 +0100591 return shp
592
Louis Verhaard93719a92020-12-08 10:02:31 +0100593 def set_new_sub_purpose(self, sub_purpose: TensorSubPurpose, param_a=None, param_b=None):
Tim Hall79d07d22020-04-27 18:20:16 +0100594 self.storage_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
595 self.sub_purpose = sub_purpose
596 if sub_purpose == TensorSubPurpose.DoubleBuffer:
597 self.storage_compression_scale = self.compression_scale_for_worst_weight_stream
598
Louis Verhaard93719a92020-12-08 10:02:31 +0100599 def bandwidth(self) -> float:
Tim Hall79d07d22020-04-27 18:20:16 +0100600 elems = shape_num_elements(self.bandwidth_shape)
601 if elems is None:
602 return 0
603 return elems * self.element_size() * self.bandwidth_compression_scale
604
Louis Verhaard93719a92020-12-08 10:02:31 +0100605 def consumers(self) -> List[Operation]:
Tim Hall79d07d22020-04-27 18:20:16 +0100606 return self.consumer_list
607
Louis Verhaard93719a92020-12-08 10:02:31 +0100608 def addresses_for_rolling_buffer(self, start_coord: Shape, end_coord: Shape) -> Tuple:
Tim Hall79d07d22020-04-27 18:20:16 +0100609 # returns ( box_height0, box_height1, box_width, [address_tl, address_tr, address_bl, address_br] )
610
611 if len(start_coord) < 4:
612 box_height0 = 1
613 box_width = 1
614
615 if len(start_coord) >= 2:
616 box_width = end_coord[-2] - start_coord[-2]
617
618 return box_height0, box_height0, box_width, [self.address_for_coordinate(start_coord), None, None, None]
619
620 crossing_y = numeric_util.round_up(start_coord[1] + 1, self.storage_shape[1])
621 crossing_x = numeric_util.round_up(start_coord[2] + 1, self.storage_shape[2])
622
623 crossing_y = min(crossing_y, end_coord[1])
624 crossing_x = min(crossing_x, end_coord[2])
625
626 box_height0 = crossing_y - start_coord[1]
627 box_width = crossing_x - start_coord[2]
628
Louis Verhaard93719a92020-12-08 10:02:31 +0100629 addresses: List = [None] * 4
Tim Hall79d07d22020-04-27 18:20:16 +0100630 addresses[0] = self.address_for_coordinate(start_coord)
631
632 if end_coord[2] > crossing_x:
633 addresses[1] = self.address_for_coordinate([start_coord[0], start_coord[1], crossing_x, start_coord[3]])
Michael McGeagh528a56d2020-12-16 11:33:21 +0000634 raise UnsupportedFeatureError("Striping in vertical direction is not supported")
Tim Hall79d07d22020-04-27 18:20:16 +0100635 if end_coord[1] > crossing_y:
636 addresses[2] = self.address_for_coordinate([start_coord[0], crossing_y, start_coord[2], start_coord[3]])
637 if end_coord[1] > crossing_y and end_coord[2] > crossing_x:
638 addresses[3] = self.address_for_coordinate([start_coord[0], crossing_y, crossing_x, start_coord[3]])
639
640 return box_height0, box_height0, box_width, addresses
641
Louis Verhaard93719a92020-12-08 10:02:31 +0100642 def address_for_coordinate(self, coord: Shape, is_top_box: bool = False) -> int:
643 offset = self.address_offset_for_coordinate(coord, is_top_box)
644 assert offset is not None
645 return self.address + offset
Tim Hall79d07d22020-04-27 18:20:16 +0100646
Louis Verhaard93719a92020-12-08 10:02:31 +0100647 def get_strides_and_coord(self, coord: Optional[Shape] = None) -> Tuple[Optional[Shape], Optional[Shape]]:
Tim Hall79d07d22020-04-27 18:20:16 +0100648 if coord is None:
649 coord = [0] * len(self.storage_shape)
650
651 augmented_coord = coord
652 augmented_shape = self.storage_shape
653 while len(augmented_shape) < 4:
654 augmented_shape = [1] + augmented_shape
655
656 while len(augmented_coord) < 4:
657 augmented_coord = [0] + augmented_coord
658
659 assert len(augmented_coord) == len(augmented_shape)
660
661 if self.format == TensorFormat.NHWC:
662 augmented_shape = [augmented_shape[0], augmented_shape[3]] + augmented_shape[1:3] + [1]
663 augmented_coord = [augmented_coord[0], augmented_coord[3]] + augmented_coord[1:3] + [0]
Tim Hall79d07d22020-04-27 18:20:16 +0100664
665 elif self.format == TensorFormat.NHCWB16:
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200666 channel_divisor = 16
Tim Hall79d07d22020-04-27 18:20:16 +0100667 augmented_shape = augmented_shape[0:4] + [1]
668 augmented_coord = (
669 [augmented_coord[0], augmented_coord[3] // channel_divisor]
670 + augmented_coord[1:3]
671 + [augmented_coord[3] % channel_divisor]
672 )
673
674 if augmented_shape[1] == 0:
675 augmented_shape[1] = 1
676
677 else:
Michael McGeaghf3e3ad72020-12-02 12:39:03 +0000678 assert self.format in (TensorFormat.Unknown, TensorFormat.WeightsCompressed)
Tim Hall79d07d22020-04-27 18:20:16 +0100679 return None, None
680
Louis Verhaard93719a92020-12-08 10:02:31 +0100681 strides: List = [0] * len(augmented_shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100682 stride = self.element_size() * self.storage_compression_scale
683
684 if self.format != TensorFormat.NHCWB16:
Louis Verhaard93719a92020-12-08 10:02:31 +0100685 stride_order = [4, 1, 3, 2, 0]
Tim Hall79d07d22020-04-27 18:20:16 +0100686 for i in stride_order:
687 strides[i] = stride
688 stride *= augmented_shape[i]
689 else:
690 assert len(strides) == 5
Tim Hall79d07d22020-04-27 18:20:16 +0100691 strides[4] = stride
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200692 strides[3] = 16 * stride # STRIDE_X
Tim Hall79d07d22020-04-27 18:20:16 +0100693 strides[1] = strides[3] * augmented_shape[2] # STRIDE_C
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200694 strides[2] = augmented_shape[2] * augmented_shape[3] * stride # STRIDE_Y
Tim Hall79d07d22020-04-27 18:20:16 +0100695 strides[0] = strides[2] * augmented_shape[1] # STRIDE_N
696
697 return strides, augmented_coord
698
Louis Verhaard93719a92020-12-08 10:02:31 +0100699 def get_strides(self) -> Shape:
Tim Hall79d07d22020-04-27 18:20:16 +0100700 strides, _ = self.get_strides_and_coord()
Louis Verhaard93719a92020-12-08 10:02:31 +0100701 assert strides is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100702 return strides
703
Louis Verhaard93719a92020-12-08 10:02:31 +0100704 def needs_dma(self) -> bool:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200705 return len(self.ops) == 1 and self.ops[0].type == Op.DMA
Louis Verhaard3c07c972020-05-07 08:12:58 +0200706
Louis Verhaard93719a92020-12-08 10:02:31 +0100707 def get_dma_src_tensor(self) -> "Optional[Tensor]":
Louis Verhaard3c07c972020-05-07 08:12:58 +0200708 # For weight tensors that need DMA: returns the source tensor in Flash, else None
709 # Note: for DMA ops, Pass.weight_tensor is referring to the SRAM weight tensor
710 return self.ops[0].inputs[0] if self.needs_dma() else None
711
Louis Verhaard93719a92020-12-08 10:02:31 +0100712 def find_npu_op(self) -> Optional[Operation]:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200713 # Returns the NPU operator that uses this tensor, excluding DMA operators.
714 for op in self.consumers():
Louis Verhaardaee5d752020-09-30 09:01:52 +0200715 if op.type == Op.DMA:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200716 return op.outputs[0].find_npu_op()
Dwight Lidman940fdee2020-08-13 13:11:48 +0200717 if op.run_on_npu:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200718 return op
Louis Verhaard93719a92020-12-08 10:02:31 +0100719 return None
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200720
Louis Verhaard93719a92020-12-08 10:02:31 +0100721 def compressed_stream_index_from_coord(self, coord: Shape) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100722 assert self.format == TensorFormat.WeightsCompressed
Louis Verhaard93719a92020-12-08 10:02:31 +0100723 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100724 assert len(self.compressed_values) > 0
725 assert len(self.compressed_values) + 1 == len(self.weight_compressed_offsets)
726
727 depth = coord[-1]
728 brick_depth = self.brick_size[-1]
729 # Clamp position at final element index
730 if depth > self.shape[-1]:
731 depth = self.shape[-1]
732
733 # Always round up to next boundary
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100734 index = numeric_util.round_up_divide(depth, brick_depth)
Tim Hall79d07d22020-04-27 18:20:16 +0100735
736 # Check boundaries on all but last weight set (which may be shorter
737 # than the brick we divided it up into)
738 if index < len(self.weight_compressed_offsets) - 1:
739 # There are no half-way points in the weights
740 if (depth % brick_depth) != 0:
Michael McGeagh528a56d2020-12-16 11:33:21 +0000741 raise UnsupportedFeatureError("Offset into weights must be aligned to a brick")
Tim Hall79d07d22020-04-27 18:20:16 +0100742
743 return index
744
Louis Verhaard93719a92020-12-08 10:02:31 +0100745 def size_of_compressed_stream(self, index: int) -> int:
746 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100747 assert 0 <= index < len(self.compressed_values)
748 return len(self.compressed_values[index])
749
Louis Verhaard93719a92020-12-08 10:02:31 +0100750 def is_last_index_in_compressed_stream(self, index: int) -> bool:
751 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100752 assert 0 <= index < len(self.compressed_values)
753 return index == len(self.compressed_values) - 1
754
Louis Verhaard93719a92020-12-08 10:02:31 +0100755 def address_offset_for_coordinate(self, orig_coord: Shape, is_top_box: bool = False) -> Optional[int]:
Tim Hall79d07d22020-04-27 18:20:16 +0100756 address_offset = 0
757 coord = orig_coord
758
759 coord = coord[-len(self.storage_shape) :]
760
761 if self.sub_purpose == TensorSubPurpose.Standard:
762 for idx, c in enumerate(coord):
763 if is_top_box:
764 assert c > 0 and c <= self.shape[idx]
765 else:
766 assert c >= 0 and c < self.shape[idx]
767
768 if self.format == TensorFormat.WeightsCompressed:
769 if len(self.weight_compressed_offsets) == 0:
770 return 0
771
Louis Verhaard3c07c972020-05-07 08:12:58 +0200772 if self.needs_dma() and self.sub_purpose == TensorSubPurpose.DoubleBuffer:
Tim Hall79d07d22020-04-27 18:20:16 +0100773 depth = orig_coord[-1]
774 brick_depth = self.brick_size[-1]
775 # Clamp position at final element index
776 if depth > self.shape[-1]:
777 depth = self.shape[-1]
778
779 # Always round up to next boundary
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100780 index = numeric_util.round_up_divide(depth, brick_depth)
Tim Hall79d07d22020-04-27 18:20:16 +0100781 index = index % 2
Louis Verhaard93719a92020-12-08 10:02:31 +0100782 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100783
784 if len(self.compressed_values) <= 2:
785 if is_top_box and index == 0:
786 for cv in self.compressed_values:
787 address_offset += len(cv)
788 else:
789 address_offset = index * len(self.compressed_values[0])
790 else:
791 if is_top_box and index == 0:
792 address_offset = self.storage_shape[-1]
793 else:
794 address_offset = index * (self.storage_shape[-1] // 2)
795 else:
796 index = self.compressed_stream_index_from_coord(orig_coord)
797 assert index < len(self.weight_compressed_offsets)
798 address_offset = self.weight_compressed_offsets[index]
799 else:
800 if is_top_box:
801 coord = [c - 1 for c in coord]
802
803 # handle wraparound for partial buffers. make sure to do this after subtracting top box:
804 coord = [c % self.storage_shape[idx] for idx, c in enumerate(coord)]
805
806 strides, augmented_coord = self.get_strides_and_coord(coord)
807 if strides is None:
808 return None
809
810 if is_top_box:
811 address_offset += 1 * strides[-1] # one element
812
813 address_offset += np.dot(augmented_coord, strides)
814
815 assert address_offset >= 0
816 assert address_offset <= self.storage_size()
817 return address_offset
818
Louis Verhaard93719a92020-12-08 10:02:31 +0100819 def is_allocated_in_tensor_arena(self, scratch_tensor_mem_area: MemArea) -> bool:
Michael McGeaghf3e3ad72020-12-02 12:39:03 +0000820 return (self.mem_area == scratch_tensor_mem_area) and (self.mem_type in (MemType.Scratch, MemType.Scratch_fast))
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200821
Louis Verhaard93719a92020-12-08 10:02:31 +0100822 def equivalent(self, tens: "Tensor") -> bool:
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200823 return self.equivalence_id == tens.equivalence_id
824
Louis Verhaard93719a92020-12-08 10:02:31 +0100825 def set_all_shapes(self, shape: Shape):
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100826 self.shape = shape
827 self.storage_shape = shape
828 self.bandwidth_shape = shape
829
Louis Verhaard93719a92020-12-08 10:02:31 +0100830 def get_full_shape(self) -> Shape:
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100831 d = len(self.shape)
832 if d in (1, 3):
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100833 return numeric_util.full_shape(4, self.shape, 1)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100834 elif d == 2:
835 return [self.shape[0], 1, 1, self.shape[1]]
836 else:
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200837 return self.shape.copy()
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100838
Louis Verhaard93719a92020-12-08 10:02:31 +0100839 def is_quantized(self) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100840 # a tensor is quantized if it has an integral type and it contains valid quantization params
841
Tim Hall89567612020-10-27 11:57:57 +0000842 if not isinstance(self.quantization, QuantizationParameters):
Tim Hall93582962020-09-09 21:58:15 +0100843 return False
844
Tim Hall89567612020-10-27 11:57:57 +0000845 return (self.dtype.type & BaseType.Int) != 0 and self.quantization.is_valid()
Tim Hall93582962020-09-09 21:58:15 +0100846
Louis Verhaard6c74c3b2020-12-17 13:54:09 +0100847 def __lt__(self, other: "Tensor") -> bool:
848 return self.equivalence_id < other.equivalence_id
849
Tim Hall79d07d22020-04-27 18:20:16 +0100850 def __str__(self):
851 return "<nng.Tensor '%s' shape=%s dtype=%s>" % (self.name, self.shape, self.dtype)
852
853 __repr__ = __str__
Tim Hall93582962020-09-09 21:58:15 +0100854
Michael McGeagh528a56d2020-12-16 11:33:21 +0000855 def error(self, msg):
856 """
857 Raises a VelaError exception for errors encountered when parsing a Tensor
858
859 :param self: Tensor object that resulted in the error
860 :param msg: str object that contains a description of the specific error encountered
861 """
862
863 def _print_operators(ops):
864 lines = []
865 for idx, op in enumerate(ops):
866 op_type = getattr(op, "type", "Not an Operation")
867 op_id = getattr(op, "op_index", "-")
868 lines.append(f" {idx} = {op_type} ({op_id})")
869 return lines
870
871 lines = [f"Invalid {self.name} tensor. {msg}"]
872
873 lines += [" Driving operators:"]
874 lines += _print_operators(self.ops)
875
876 lines += [" Consuming operators:"]
877 lines += _print_operators(self.consumer_list)
878
879 raise VelaError("\n".join(lines))
880
Tim Hall93582962020-09-09 21:58:15 +0100881
Louis Verhaard93719a92020-12-08 10:02:31 +0100882def check_quantized_tens_scaling_equal(tens_a: Tensor, tens_b: Tensor) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100883 # checks that the scaling of two quantized tensors are equal
884
Tim Hall89567612020-10-27 11:57:57 +0000885 return tens_a.is_quantized() and tens_b.is_quantized() and tens_a.quantization.is_scaling_equal(tens_b.quantization)