blob: 257cb5ffc83d12c9aeedad4b5ea177e76a2c9b5c [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 Verhaard93719a92020-12-08 10:02:31 +010024from typing import Dict
25from typing import List
26from typing import Optional
27from typing import Tuple
28from typing import Union
29from uuid import UUID
Diego Russoea6111a2020-04-14 18:41:58 +010030
31import numpy as np
32
Michael McGeagh7a6f8432020-12-02 15:29:22 +000033from . import errors # Import this way due to cyclic imports
Diego Russoea6111a2020-04-14 18:41:58 +010034from . 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
Dwight Lidmana9390f72020-05-13 12:00:08 +020037from .ethos_u55_regs.ethos_u55_regs import resampling_mode
Louis Verhaardaee5d752020-09-30 09:01:52 +020038from .operation import Op
Michael McGeagh5778ffd2020-08-06 17:31:02 +010039from .operation import Operation
Louis Verhaard93719a92020-12-08 10:02:31 +010040
41Shape = List
Tim Hall79d07d22020-04-27 18:20:16 +010042
43
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020044class MemType(enum.IntFlag):
45 Unknown = 0
46 Permanent_NPU = 1
47 Permanent_CPU = 2
48 Scratch = 3
49 Scratch_fast = 4
50 Size = Scratch_fast + 1
51
Louis Verhaard93719a92020-12-08 10:02:31 +010052 def display_name(self) -> str:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020053 return ("Unknown", "Permanent_NPU", "Permanent_CPU", "Scratch", "Scratch_fast", "Size")[self.value]
54
Louis Verhaard93719a92020-12-08 10:02:31 +010055 def identifier_name(self) -> str:
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020056 return ("unknown", "permanent_npu", "permanent_cpu", "scratch", "scratch_fast", "size")[self.value]
57
Louis Verhaard93719a92020-12-08 10:02:31 +010058 @staticmethod
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020059 def all():
60 return (MemType.Permanent_NPU, MemType.Permanent_CPU, MemType.Scratch, MemType.Scratch_fast)
61
62 def __str__(self):
63 return self.name
64
65
Diqing Zhongf842b692020-12-11 13:07:37 +010066class BandwidthDirection(enum.IntEnum):
67 Read = 0
68 Write = auto()
69 Size = auto()
70
71 def display_name(self):
72 return self.name
73
74 def identifier_name(self):
75 return self.name.lower()
76
77 @staticmethod
78 def all():
79 return (BandwidthDirection.Read, BandwidthDirection.Write)
80
81
Tim Hall79d07d22020-04-27 18:20:16 +010082class MemArea(enum.IntFlag):
83 Unknown = 0
84 Sram = 1
85 Dram = 2
86 OnChipFlash = 3
87 OffChipFlash = 4
Louis Verhaard0b8268a2020-08-05 16:11:29 +020088 Shram = 5 # for LUT
89 Size = Shram + 1
Tim Hall79d07d22020-04-27 18:20:16 +010090
Louis Verhaard93719a92020-12-08 10:02:31 +010091 def display_name(self) -> str:
Louis Verhaard0b8268a2020-08-05 16:11:29 +020092 return ("Unknown", "SRAM", "DRAM", "On-chip Flash", "Off-chip Flash", "SHRAM", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010093
Louis Verhaard93719a92020-12-08 10:02:31 +010094 def identifier_name(self) -> str:
Louis Verhaard0b8268a2020-08-05 16:11:29 +020095 return ("unknown", "sram", "dram", "on_chip_flash", "off_chip_flash", "shram", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010096
Louis Verhaard93719a92020-12-08 10:02:31 +010097 @staticmethod
Tim Hall79d07d22020-04-27 18:20:16 +010098 def all():
Louis Verhaard0b8268a2020-08-05 16:11:29 +020099 return (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash, MemArea.Shram)
Tim Hall79d07d22020-04-27 18:20:16 +0100100
101 def __str__(self):
102 return self.name
103
104
105class TensorPurpose(enum.IntFlag):
106 Unknown = 0
107 Weights = 1
108 FeatureMap = 2
109 Scratch = 3
Fredrik Svedberga0c36242020-06-03 15:43:31 +0200110 LUT = 4
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100111 FSBias = 5
112 Size = 6
Tim Hall79d07d22020-04-27 18:20:16 +0100113
Louis Verhaard93719a92020-12-08 10:02:31 +0100114 def display_name(self) -> str:
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100115 return ("Unknown", "Weights", "FeatureMap", "Scratch", "LUT", "FastStorageBias", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +0100116
Louis Verhaard93719a92020-12-08 10:02:31 +0100117 def identifier_name(self) -> str:
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100118 return ("unknown", "weights", "feature_map", "scratch", "lut", "fast_storage_bias", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +0100119
Louis Verhaard93719a92020-12-08 10:02:31 +0100120 @staticmethod
Tim Hall79d07d22020-04-27 18:20:16 +0100121 def all():
Andreas Nevalainen897cc142020-10-28 15:42:08 +0100122 return (TensorPurpose.Weights, TensorPurpose.FeatureMap, TensorPurpose.FSBias)
Tim Hall79d07d22020-04-27 18:20:16 +0100123
124
125class TensorSubPurpose(enum.Enum):
126 Standard = 0
127 DoubleBuffer = 1
128 RollingBufferX = 2
129 RollingBufferY = 3
130 RollingBufferXY = 4
131
Louis Verhaard93719a92020-12-08 10:02:31 +0100132 def display_name(self) -> str:
Tim Hall79d07d22020-04-27 18:20:16 +0100133 return ("Standard", "Double Buffer", "Rolling Buffer X", "Rolling Buffer Y", "Rolling Buffer XY")[self.value]
134
Louis Verhaard93719a92020-12-08 10:02:31 +0100135 def identifier_name(self) -> str:
Tim Hall79d07d22020-04-27 18:20:16 +0100136 return ("standard", "double_buffer", "rolling_buffer_x", "rolling_buffer_y", "rolling_buffer_xy")[self.value]
137
Louis Verhaard93719a92020-12-08 10:02:31 +0100138 @staticmethod
Tim Hall79d07d22020-04-27 18:20:16 +0100139 def all():
140 return (
141 TensorSubPurpose.Standard,
142 TensorSubPurpose.DoubleBuffer,
143 TensorSubPurpose.RollingBufferX,
144 TensorSubPurpose.RollingBufferY,
145 TensorSubPurpose.RollingBufferXY,
146 )
147
148
149class TensorFormat(enum.Flag):
150 Unknown = 0
151 WeightsCompressed = 1
152 NHWC = 2
153 NHCWB16 = 3
154
155 def __str__(self):
156 return self.name
157
158
159class TensorBlockTraversal(enum.Enum):
160 Default = 0
161 DepthWise = 1
162 DepthFirst = 2
163 PartKernelFirst = 3
164
165
Louis Verhaard93719a92020-12-08 10:02:31 +0100166def shape_num_elements(shp: Shape) -> Optional[int]:
Tim Hall79d07d22020-04-27 18:20:16 +0100167 elems = 1
168 if shp is None:
169 return None
170 for d in shp:
171 if d is None:
172 return None
173 elems *= d
174 return elems
175
176
Louis Verhaard93719a92020-12-08 10:02:31 +0100177def shape_fully_defined(shp: Shape) -> bool:
Tim Hall79d07d22020-04-27 18:20:16 +0100178 if shp is None:
179 return False
180 for d in shp:
181 if d is None:
182 return False
183 return True
184
185
Louis Verhaard93719a92020-12-08 10:02:31 +0100186def shape_round_to_quantum(shp: Shape, quantum: Tuple) -> Shape:
Tim Hall79d07d22020-04-27 18:20:16 +0100187 new_shp = list(shp)
188
189 # Traverse backwards using length of shape since there may be more rounding quantums than shape elements
190 for i in range(-1, -len(shp) - 1, -1):
191 if new_shp[i] is not None:
192 new_shp[i] = numeric_util.round_up(new_shp[i], quantum[i])
193 return new_shp
194
195
Louis Verhaard9db529a2020-09-23 10:27:11 +0200196@lru_cache(maxsize=None)
Louis Verhaard93719a92020-12-08 10:02:31 +0100197def create_equivalence_id(key) -> UUID:
Louis Verhaard9db529a2020-09-23 10:27:11 +0200198 # Generates equivalence_id based on the given key.
199 return uuid.uuid4()
200
201
Tim Hall79d07d22020-04-27 18:20:16 +0100202class QuantizationParameters:
203 __slots__ = "min", "max", "num_bits", "narrow_range", "scale_f32", "zero_point", "quant_min", "quant_max"
204
Louis Verhaard93719a92020-12-08 10:02:31 +0100205 def __init__(
206 self,
207 min: Union[float, np.ndarray, None] = None,
208 max: Union[float, np.ndarray, None] = None,
209 num_bits=None,
210 narrow_range=None,
211 ):
Tim Hall79d07d22020-04-27 18:20:16 +0100212 self.min = min
213 self.max = max
214
215 self.num_bits = num_bits
216 self.narrow_range = narrow_range
217
Louis Verhaard93719a92020-12-08 10:02:31 +0100218 self.scale_f32: Union[float, np.ndarray, None] = None
219 self.zero_point: Union[int, np.ndarray, None] = None
220 self.quant_min: Optional[float] = None
221 self.quant_max: Optional[float] = None
Tim Hall79d07d22020-04-27 18:20:16 +0100222
223 def __str__(self):
224 return "<nng.QuantizationParameters min=%s max=%s, num_bits=%s, scale=%s, zero_point=%s>" % (
225 self.min,
226 self.max,
227 self.num_bits,
228 self.scale_f32,
229 self.zero_point,
230 )
231
232 __repr__ = __str__
233
Louis Verhaard93719a92020-12-08 10:02:31 +0100234 def clone(self) -> "QuantizationParameters":
Tim Hall79d07d22020-04-27 18:20:16 +0100235 res = QuantizationParameters()
236 res.min = self.min
237 res.max = self.max
238
239 res.num_bits = self.num_bits
240 res.narrow_range = self.narrow_range
241
242 res.scale_f32 = self.scale_f32
243 res.zero_point = self.zero_point
244 res.quant_min = self.quant_min
245 res.quant_max = self.quant_max
246 return res
247
248 def dequantize(self, values):
249 if self.zero_point.size == 1 and self.scale_f32.size == 1:
250 # same scale is used for all values
251 res = (values.astype(np.float64) - self.zero_point) * self.scale_f32
252 else:
253 # a different scale is used for different sets of values
254 values_as_float = values.astype(np.float64)
255
256 # this is not compatible with the format of depthwise weights,
257 # where input is at index 3 (Output, Kh, Kw, Input)
258 # return the quantized values
259 return np.ndarray((values_as_float.shape))
260
Tim Hall79d07d22020-04-27 18:20:16 +0100261 return res
262
Louis Verhaard93719a92020-12-08 10:02:31 +0100263 def is_scaling_equal(self, other: Optional["QuantizationParameters"]) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100264 # quantisation parameter scaling is not equal if 'other' is None because
265 # it implies that the tensor it belongs to is not quantised. otherwise,
266 # it depends upon whether the scale and zero point are equal
267
Tim Hall89567612020-10-27 11:57:57 +0000268 if not isinstance(other, QuantizationParameters):
Tim Halle3786ac2020-07-28 17:40:50 +0100269 return False
270
271 return self.scale_f32 == other.scale_f32 and self.zero_point == other.zero_point
272
Louis Verhaard93719a92020-12-08 10:02:31 +0100273 def is_valid(self) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100274 # quantisation parameters are consider valid if they have a scale and zero point
275
276 return None not in (self.scale_f32, self.zero_point)
277
Louis Verhaard93719a92020-12-08 10:02:31 +0100278 def is_per_axis(self) -> bool:
Dwight Lidmanc7187432020-11-16 17:40:46 +0100279 """Returns True if either the scale, zero point, minimum or maximum values are arrays"""
280 for attr in ("scale_f32", "zero_point", "min", "max"):
281 if isinstance(getattr(self, attr), np.ndarray):
282 return True
283 return False
284
Tim Hall79d07d22020-04-27 18:20:16 +0100285
Louis Verhaard93719a92020-12-08 10:02:31 +0100286def create_const_tensor(
287 name: str,
288 shape: Shape,
289 dtype: DataType,
290 values: np.ndarray,
291 value_dtype: np.dtype = None,
292 purpose: TensorPurpose = TensorPurpose.Unknown,
293 quantization: QuantizationParameters = None,
294):
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100295 # Tensor
296 const_tensor = Tensor(shape, dtype, name + "_0")
297 const_tensor.purpose = purpose
298 const_tensor.quantization = quantization
299 const_tensor.values = np.array(values, dtype=value_dtype)
Jacob Bohlina41cd4d2020-08-26 18:21:28 +0200300 const_tensor.quant_values = np.frombuffer(const_tensor.values.tobytes(), dtype=np.uint8)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100301 # Operator
Louis Verhaardaee5d752020-09-30 09:01:52 +0200302 const_op = Operation(Op.Const, name)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100303 const_op.set_output_tensor(const_tensor)
304 return const_tensor
305
306
307def create_reshape_tensor(tens, shape, ifm_reshape=True):
308 if shape == tens.shape:
309 return tens
310 # Tensors
311 name = tens.name + "_reshape"
312 reshape_ifm = tens
313 reshape_ofm = tens.clone("_reshaped")
314 reshape_ofm.set_all_shapes(shape)
315 if not ifm_reshape:
316 reshape_ifm, reshape_ofm = reshape_ofm, reshape_ifm
317 # Operator
Louis Verhaardaee5d752020-09-30 09:01:52 +0200318 reshape_op = Operation(Op.Reshape, name)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100319 reshape_op.attrs["new_shape"] = shape
320 reshape_op.add_input_tensor(reshape_ifm)
321 reshape_op.add_input_tensor(create_const_tensor(name + "_shape", [1], DataType.int32, shape))
322 reshape_op.set_output_tensor(reshape_ofm)
323 return reshape_ofm if ifm_reshape else reshape_ifm
324
325
Jacob Bohlin1a666972020-09-11 10:04:15 +0200326# class that keeps track of all tensor addresses in the different memory types
327class TensorAddressMap:
Louis Verhaard93719a92020-12-08 10:02:31 +0100328 address_map: Dict = defaultdict(dict) # dict (tens.equivalence_id -> dict (mem_type -> address))
Jacob Bohlin1a666972020-09-11 10:04:15 +0200329
330 @classmethod
Louis Verhaard93719a92020-12-08 10:02:31 +0100331 def get_address_for_tens(cls, tens_id: UUID, mem_type: MemType) -> int:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200332 return cls.address_map[tens_id].get(mem_type)
333
334 @classmethod
Louis Verhaard93719a92020-12-08 10:02:31 +0100335 def set_address_for_tens(cls, tens_id: UUID, mem_type: MemType, address: int):
Jacob Bohlin1a666972020-09-11 10:04:15 +0200336 # Check previous address if there is one
337 previous_address = cls.address_map[tens_id].get(mem_type)
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200338 if address is not None and previous_address is not None:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200339 assert previous_address == address, "Two different addresses cannot be assigned to the same tensor."
340
341 # Set tensor's address for memory type
342 cls.address_map[tens_id][mem_type] = address
343
344
Tim Hall79d07d22020-04-27 18:20:16 +0100345class Tensor:
346 __slots__ = (
347 "shape",
348 "storage_shape",
349 "bandwidth_shape",
350 "dtype",
351 "name",
352 "ops",
353 "consumer_list",
354 "values",
355 "quant_values",
356 "compressed_values",
Tim Hallf7e810a2020-06-25 15:04:31 +0100357 "compressed_values_substream_offsets",
Tim Hall79d07d22020-04-27 18:20:16 +0100358 "mem_area",
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200359 "mem_type",
Tim Hall79d07d22020-04-27 18:20:16 +0100360 "format",
361 "purpose",
362 "sub_purpose",
363 "alignment",
364 "weight_transpose_depthwise",
365 "storage_compression_scale",
366 "bandwidth_compression_scale",
367 "compression_scale_for_worst_weight_stream",
368 "weight_compression_scales",
369 "weight_compression_config",
Louis Verhaard9db529a2020-09-23 10:27:11 +0200370 "value_id",
Tim Hall79d07d22020-04-27 18:20:16 +0100371 "storage_rounding_quantum",
372 "brick_size",
Tim Hall79d07d22020-04-27 18:20:16 +0100373 "quantization",
374 "weight_compressed_offsets",
375 "element_size_bytes",
Tim Hall79d07d22020-04-27 18:20:16 +0100376 "block_traversal",
Tim Hall79d07d22020-04-27 18:20:16 +0100377 "equivalence_id",
Dwight Lidmana9390f72020-05-13 12:00:08 +0200378 "resampling_mode",
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200379 "avoid_NHCWB16",
Tim Hall79d07d22020-04-27 18:20:16 +0100380 )
381 AllocationQuantum = 16
382
Louis Verhaard93719a92020-12-08 10:02:31 +0100383 def __init__(self, shape: Shape, dtype: DataType, name: str):
Tim Hall79d07d22020-04-27 18:20:16 +0100384 self.shape = shape
385 self.storage_shape = shape
386 self.bandwidth_shape = shape
387 self.dtype = dtype
388 self.name = name
Louis Verhaard93719a92020-12-08 10:02:31 +0100389 self.equivalence_id: UUID = uuid.uuid4()
Tim Hall79d07d22020-04-27 18:20:16 +0100390
Louis Verhaard93719a92020-12-08 10:02:31 +0100391 self.ops: List[Operation] = []
392 self.consumer_list: List[Operation] = []
Tim Hall79d07d22020-04-27 18:20:16 +0100393
Louis Verhaard93719a92020-12-08 10:02:31 +0100394 self.values: Optional[np.ndarray] = None
395 self.quant_values: Optional[np.ndarray] = None
396 self.compressed_values: Optional[np.ndarray] = None
397 self.compressed_values_substream_offsets: Optional[List] = None
398 self.mem_area: MemArea = MemArea.Unknown
399 self.mem_type: MemType = MemType.Unknown
400 self.format: TensorFormat = TensorFormat.Unknown
401 self.purpose: TensorPurpose = TensorPurpose.Unknown
402 self.sub_purpose: TensorSubPurpose = TensorSubPurpose.Standard
403 self.alignment: int = Tensor.AllocationQuantum
404 self.weight_transpose_depthwise: bool = False
Tim Hall79d07d22020-04-27 18:20:16 +0100405
Louis Verhaard93719a92020-12-08 10:02:31 +0100406 self.storage_compression_scale: float = 1.0
407 self.bandwidth_compression_scale: float = 1.0
408 self.compression_scale_for_worst_weight_stream: float = 1.0
409 self.weight_compression_scales: Optional[np.ndarray] = None
Louis Verhaard9db529a2020-09-23 10:27:11 +0200410 # if two tensors have the same weight_compression_config, then they have the same compressed values
Tim Hall79d07d22020-04-27 18:20:16 +0100411 self.weight_compression_config = None
Louis Verhaard9db529a2020-09-23 10:27:11 +0200412 # if two tensors have the same value_id, then they have the same values
Louis Verhaard93719a92020-12-08 10:02:31 +0100413 self.value_id: UUID = uuid.uuid4()
414 self.weight_compressed_offsets: List = []
415 self.storage_rounding_quantum: Tuple = (1, 1, 1, 1)
416 self.brick_size: Tuple = (1, 1, 1, 1)
417 self.element_size_bytes: int = 0
Tim Hall79d07d22020-04-27 18:20:16 +0100418
419 # quantization parameters
Louis Verhaard93719a92020-12-08 10:02:31 +0100420 self.quantization: Optional[QuantizationParameters] = None
421 self.block_traversal: TensorBlockTraversal = TensorBlockTraversal.Default
422 self.resampling_mode: resampling_mode = resampling_mode.NONE
Tim Hall79d07d22020-04-27 18:20:16 +0100423
Louis Verhaard93719a92020-12-08 10:02:31 +0100424 self.avoid_NHCWB16: bool = False
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200425
Jacob Bohlin1a666972020-09-11 10:04:15 +0200426 @property
Louis Verhaard93719a92020-12-08 10:02:31 +0100427 def address(self) -> int:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200428 return TensorAddressMap.get_address_for_tens(self.equivalence_id, self.mem_type)
429
430 @address.setter
Louis Verhaard93719a92020-12-08 10:02:31 +0100431 def address(self, address: int):
Jacob Bohlin1a666972020-09-11 10:04:15 +0200432 TensorAddressMap.set_address_for_tens(self.equivalence_id, self.mem_type, address)
433
Louis Verhaard93719a92020-12-08 10:02:31 +0100434 def element_size(self) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100435 if self.element_size_bytes == 0:
436 return self.dtype.size_in_bits() / 8
437 return self.element_size_bytes
438
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100439 # Returns a copy, renamed to self.name + suffix
440 # The references to Operators will be empty when returned
441 # Depending on set_unique, the copy is shallow, or deep
442 # For set_unique==True, a new equivalence_id will be set
Louis Verhaard93719a92020-12-08 10:02:31 +0100443 def clone(self, suffix="_clone", set_unique: bool = False) -> "Tensor":
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100444 if set_unique:
445 res = copy.deepcopy(self)
446 res.equivalence_id = uuid.uuid4()
447 else:
448 res = copy.copy(self)
449 res.storage_shape = list(self.storage_shape)
450 res.bandwidth_shape = list(self.bandwidth_shape)
451 if self.quantization is not None:
452 res.quantization = self.quantization.clone()
Tim Hall79d07d22020-04-27 18:20:16 +0100453
Patrik Gustavsson6ae0e422020-11-04 12:43:50 +0100454 res.name = res.name + suffix
Tim Hall79d07d22020-04-27 18:20:16 +0100455 res.ops = []
456 res.consumer_list = []
Tim Hall79d07d22020-04-27 18:20:16 +0100457
Tim Hall79d07d22020-04-27 18:20:16 +0100458 return res
459
Louis Verhaard93719a92020-12-08 10:02:31 +0100460 def clone_into_fast_storage(self, arch) -> "Tensor":
Tim Hall79d07d22020-04-27 18:20:16 +0100461 res = self.clone(suffix="_fast_storage")
462 res.mem_area = arch.fast_storage_mem_area
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200463 res.mem_type = MemType.Scratch_fast
Tim Hall79d07d22020-04-27 18:20:16 +0100464 return res
465
Louis Verhaard93719a92020-12-08 10:02:31 +0100466 def copy_compressed_weight_info(self, src_tens: "Tensor"):
Louis Verhaard3c07c972020-05-07 08:12:58 +0200467 # Copies compressed values + all related weight compression info from the given tensor
Louis Verhaard9db529a2020-09-23 10:27:11 +0200468 self.equivalence_id = src_tens.equivalence_id
Louis Verhaard3c07c972020-05-07 08:12:58 +0200469 self.compressed_values = src_tens.compressed_values
Tim Hallf7e810a2020-06-25 15:04:31 +0100470 self.compressed_values_substream_offsets = src_tens.compressed_values_substream_offsets
Louis Verhaard3c07c972020-05-07 08:12:58 +0200471 self.storage_shape = src_tens.storage_shape
472 self.brick_size = src_tens.brick_size
473 self.weight_compression_scales = src_tens.weight_compression_scales
474 self.weight_compressed_offsets = src_tens.weight_compressed_offsets
475 self.weight_transpose_depthwise = src_tens.weight_transpose_depthwise
476 self.compression_scale_for_worst_weight_stream = src_tens.compression_scale_for_worst_weight_stream
477 self.storage_compression_scale = src_tens.storage_compression_scale
Diqing Zhong7e1d1d12020-10-30 15:10:46 +0100478 self.bandwidth_compression_scale = src_tens.bandwidth_compression_scale
Louis Verhaard3c07c972020-05-07 08:12:58 +0200479 self.block_traversal = src_tens.block_traversal
480 self.weight_compression_config = src_tens.weight_compression_config
Louis Verhaard9db529a2020-09-23 10:27:11 +0200481 self.value_id = src_tens.value_id
Louis Verhaard3c07c972020-05-07 08:12:58 +0200482
Louis Verhaard93719a92020-12-08 10:02:31 +0100483 def set_format(self, fmt: TensorFormat, arch):
Tim Hall79d07d22020-04-27 18:20:16 +0100484 self.format = fmt
485 shape_len = 0
486 try:
487 shape_len = len(self.shape)
488 except TypeError:
489 pass
490
Louis Verhaard0411edb2020-11-16 16:37:11 +0100491 if shape_len > 4:
492 return
Tim Hall79d07d22020-04-27 18:20:16 +0100493 self.storage_rounding_quantum = arch.storage_rounding_quantums[self.format]
Louis Verhaard93719a92020-12-08 10:02:31 +0100494 self.storage_rounding_quantum = tuple(self.storage_rounding_quantum[-shape_len:])
Tim Hall79d07d22020-04-27 18:20:16 +0100495 self.brick_size = arch.brick_sizes[self.format]
Louis Verhaard93719a92020-12-08 10:02:31 +0100496 self.brick_size = tuple(self.brick_size[-shape_len:])
Tim Hall79d07d22020-04-27 18:20:16 +0100497 if self.shape is None:
498 return
499
500 self.bandwidth_shape = shape_round_to_quantum(self.shape, self.brick_size)
501 self.storage_shape = shape_round_to_quantum(self.shape, self.storage_rounding_quantum)
502
503 if fmt == TensorFormat.WeightsCompressed:
504 compression_ratio = 5 / 8
505 self.storage_compression_scale = compression_ratio
506 self.bandwidth_compression_scale = compression_ratio
507 self.compression_scale_for_worst_weight_stream = compression_ratio
508
Louis Verhaard93719a92020-12-08 10:02:31 +0100509 def storage_elements(self) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100510 elems = shape_num_elements(self.storage_shape)
511 if elems is None:
512 return 0
513 return elems
514
Louis Verhaard93719a92020-12-08 10:02:31 +0100515 def elements(self) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100516 elems = shape_num_elements(self.shape)
517 if elems is None:
518 return 0
519 return elems
520
Louis Verhaard93719a92020-12-08 10:02:31 +0100521 def has_fully_defined_shape(self) -> bool:
Tim Hall79d07d22020-04-27 18:20:16 +0100522 return shape_fully_defined(self.shape)
523
Louis Verhaard93719a92020-12-08 10:02:31 +0100524 def storage_size(self, scale: float = 1.0) -> int:
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200525 raw_size = self.storage_elements() * self.element_size() * scale
Tim Hall79d07d22020-04-27 18:20:16 +0100526 if raw_size == 0:
527 raw_size = 1 # force it to take up space
528 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
529 return rounded_size
530
Louis Verhaard93719a92020-12-08 10:02:31 +0100531 def storage_size_for_sub_purpose(
532 self, arch, sub_purpose: TensorSubPurpose, param_a: Optional[int] = None, param_b: Optional[int] = None
533 ) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100534 alt_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
535 elems = shape_num_elements(alt_shape)
536 if elems is None:
537 return 0
538 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200539 raw_size = (
540 elems
541 * self.element_size()
542 * self.compression_scale_for_worst_weight_stream
543 * arch.weight_estimation_scaling
544 )
Tim Hall79d07d22020-04-27 18:20:16 +0100545 else:
Patrik Gustavsson9baa4c32020-08-20 13:59:01 +0200546 # Rolling buffers are used for intermediate data in ifm streaming
547 # These will all use the NHCWB16 format, and need to be aligned to 16 in the C-dimension
548 if alt_shape[-1] % 16 != 0:
549 nhcwb16_shape = alt_shape[0:-1] + [numeric_util.round_up(alt_shape[-1], 16)]
550 elems = shape_num_elements(nhcwb16_shape)
551
Tim Hall79d07d22020-04-27 18:20:16 +0100552 raw_size = elems * self.element_size() * self.storage_compression_scale
553 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
554 return rounded_size
555
Louis Verhaard93719a92020-12-08 10:02:31 +0100556 def storage_shape_for_sub_purpose(
557 self, sub_purpose: TensorSubPurpose, param_a: Optional[int], param_b: Optional[int]
558 ) -> Shape:
Tim Hall79d07d22020-04-27 18:20:16 +0100559 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Jacob Bohline843d332020-06-23 12:12:56 +0200560 shp = list(self.shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100561 assert len(shp) >= 2
Louis Verhaard93719a92020-12-08 10:02:31 +0100562 assert param_a is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100563 shp[-1] = min(shp[-1], param_a * 2)
Tim Hall79d07d22020-04-27 18:20:16 +0100564 else:
Jacob Bohline843d332020-06-23 12:12:56 +0200565 shp = list(self.storage_shape)
566 if sub_purpose == TensorSubPurpose.RollingBufferX:
567 assert len(shp) == 4
Louis Verhaard93719a92020-12-08 10:02:31 +0100568 assert param_a is not None
Jacob Bohline843d332020-06-23 12:12:56 +0200569 shp[0] = 1
570 shp[2] = min(shp[2], param_a)
571 elif sub_purpose == TensorSubPurpose.RollingBufferY:
572 assert len(shp) == 4
Louis Verhaard93719a92020-12-08 10:02:31 +0100573 assert param_a is not None
Jacob Bohline843d332020-06-23 12:12:56 +0200574 shp[0] = 1
575 shp[1] = min(shp[1], param_a)
576 elif sub_purpose == TensorSubPurpose.RollingBufferXY:
577 assert len(shp) == 4
Louis Verhaard93719a92020-12-08 10:02:31 +0100578 assert param_a is not None
579 assert param_b is not None
Jacob Bohline843d332020-06-23 12:12:56 +0200580 shp[0] = 1
581 shp[2] = min(shp[2], param_a)
582 shp[1] = min(shp[1], param_b)
583 elif sub_purpose == TensorSubPurpose.Standard:
584 pass
585 else:
586 assert 0, "did not expect new sub purpose %s" % (sub_purpose,)
587
Tim Hall79d07d22020-04-27 18:20:16 +0100588 return shp
589
Louis Verhaard93719a92020-12-08 10:02:31 +0100590 def set_new_sub_purpose(self, sub_purpose: TensorSubPurpose, param_a=None, param_b=None):
Tim Hall79d07d22020-04-27 18:20:16 +0100591 self.storage_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
592 self.sub_purpose = sub_purpose
593 if sub_purpose == TensorSubPurpose.DoubleBuffer:
594 self.storage_compression_scale = self.compression_scale_for_worst_weight_stream
595
Louis Verhaard93719a92020-12-08 10:02:31 +0100596 def bandwidth(self) -> float:
Tim Hall79d07d22020-04-27 18:20:16 +0100597 elems = shape_num_elements(self.bandwidth_shape)
598 if elems is None:
599 return 0
600 return elems * self.element_size() * self.bandwidth_compression_scale
601
Louis Verhaard93719a92020-12-08 10:02:31 +0100602 def consumers(self) -> List[Operation]:
Tim Hall79d07d22020-04-27 18:20:16 +0100603 return self.consumer_list
604
Louis Verhaard93719a92020-12-08 10:02:31 +0100605 def addresses_for_rolling_buffer(self, start_coord: Shape, end_coord: Shape) -> Tuple:
Tim Hall79d07d22020-04-27 18:20:16 +0100606 # returns ( box_height0, box_height1, box_width, [address_tl, address_tr, address_bl, address_br] )
607
608 if len(start_coord) < 4:
609 box_height0 = 1
610 box_width = 1
611
612 if len(start_coord) >= 2:
613 box_width = end_coord[-2] - start_coord[-2]
614
615 return box_height0, box_height0, box_width, [self.address_for_coordinate(start_coord), None, None, None]
616
617 crossing_y = numeric_util.round_up(start_coord[1] + 1, self.storage_shape[1])
618 crossing_x = numeric_util.round_up(start_coord[2] + 1, self.storage_shape[2])
619
620 crossing_y = min(crossing_y, end_coord[1])
621 crossing_x = min(crossing_x, end_coord[2])
622
623 box_height0 = crossing_y - start_coord[1]
624 box_width = crossing_x - start_coord[2]
625
Louis Verhaard93719a92020-12-08 10:02:31 +0100626 addresses: List = [None] * 4
Tim Hall79d07d22020-04-27 18:20:16 +0100627 addresses[0] = self.address_for_coordinate(start_coord)
628
629 if end_coord[2] > crossing_x:
630 addresses[1] = self.address_for_coordinate([start_coord[0], start_coord[1], crossing_x, start_coord[3]])
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000631 raise errors.UnsupportedFeatureError("Striping in vertical direction is not supported")
Tim Hall79d07d22020-04-27 18:20:16 +0100632 if end_coord[1] > crossing_y:
633 addresses[2] = self.address_for_coordinate([start_coord[0], crossing_y, start_coord[2], start_coord[3]])
634 if end_coord[1] > crossing_y and end_coord[2] > crossing_x:
635 addresses[3] = self.address_for_coordinate([start_coord[0], crossing_y, crossing_x, start_coord[3]])
636
637 return box_height0, box_height0, box_width, addresses
638
Louis Verhaard93719a92020-12-08 10:02:31 +0100639 def address_for_coordinate(self, coord: Shape, is_top_box: bool = False) -> int:
640 offset = self.address_offset_for_coordinate(coord, is_top_box)
641 assert offset is not None
642 return self.address + offset
Tim Hall79d07d22020-04-27 18:20:16 +0100643
Louis Verhaard93719a92020-12-08 10:02:31 +0100644 def get_strides_and_coord(self, coord: Optional[Shape] = None) -> Tuple[Optional[Shape], Optional[Shape]]:
Tim Hall79d07d22020-04-27 18:20:16 +0100645 if coord is None:
646 coord = [0] * len(self.storage_shape)
647
648 augmented_coord = coord
649 augmented_shape = self.storage_shape
650 while len(augmented_shape) < 4:
651 augmented_shape = [1] + augmented_shape
652
653 while len(augmented_coord) < 4:
654 augmented_coord = [0] + augmented_coord
655
656 assert len(augmented_coord) == len(augmented_shape)
657
658 if self.format == TensorFormat.NHWC:
659 augmented_shape = [augmented_shape[0], augmented_shape[3]] + augmented_shape[1:3] + [1]
660 augmented_coord = [augmented_coord[0], augmented_coord[3]] + augmented_coord[1:3] + [0]
Tim Hall79d07d22020-04-27 18:20:16 +0100661
662 elif self.format == TensorFormat.NHCWB16:
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200663 channel_divisor = 16
Tim Hall79d07d22020-04-27 18:20:16 +0100664 augmented_shape = augmented_shape[0:4] + [1]
665 augmented_coord = (
666 [augmented_coord[0], augmented_coord[3] // channel_divisor]
667 + augmented_coord[1:3]
668 + [augmented_coord[3] % channel_divisor]
669 )
670
671 if augmented_shape[1] == 0:
672 augmented_shape[1] = 1
673
674 else:
Michael McGeaghf3e3ad72020-12-02 12:39:03 +0000675 assert self.format in (TensorFormat.Unknown, TensorFormat.WeightsCompressed)
Tim Hall79d07d22020-04-27 18:20:16 +0100676 return None, None
677
Louis Verhaard93719a92020-12-08 10:02:31 +0100678 strides: List = [0] * len(augmented_shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100679 stride = self.element_size() * self.storage_compression_scale
680
681 if self.format != TensorFormat.NHCWB16:
Louis Verhaard93719a92020-12-08 10:02:31 +0100682 stride_order = [4, 1, 3, 2, 0]
Tim Hall79d07d22020-04-27 18:20:16 +0100683 for i in stride_order:
684 strides[i] = stride
685 stride *= augmented_shape[i]
686 else:
687 assert len(strides) == 5
Tim Hall79d07d22020-04-27 18:20:16 +0100688 strides[4] = stride
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200689 strides[3] = 16 * stride # STRIDE_X
Tim Hall79d07d22020-04-27 18:20:16 +0100690 strides[1] = strides[3] * augmented_shape[2] # STRIDE_C
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200691 strides[2] = augmented_shape[2] * augmented_shape[3] * stride # STRIDE_Y
Tim Hall79d07d22020-04-27 18:20:16 +0100692 strides[0] = strides[2] * augmented_shape[1] # STRIDE_N
693
694 return strides, augmented_coord
695
Louis Verhaard93719a92020-12-08 10:02:31 +0100696 def get_strides(self) -> Shape:
Tim Hall79d07d22020-04-27 18:20:16 +0100697 strides, _ = self.get_strides_and_coord()
Louis Verhaard93719a92020-12-08 10:02:31 +0100698 assert strides is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100699 return strides
700
Louis Verhaard93719a92020-12-08 10:02:31 +0100701 def needs_dma(self) -> bool:
Louis Verhaardaee5d752020-09-30 09:01:52 +0200702 return len(self.ops) == 1 and self.ops[0].type == Op.DMA
Louis Verhaard3c07c972020-05-07 08:12:58 +0200703
Louis Verhaard93719a92020-12-08 10:02:31 +0100704 def get_dma_src_tensor(self) -> "Optional[Tensor]":
Louis Verhaard3c07c972020-05-07 08:12:58 +0200705 # For weight tensors that need DMA: returns the source tensor in Flash, else None
706 # Note: for DMA ops, Pass.weight_tensor is referring to the SRAM weight tensor
707 return self.ops[0].inputs[0] if self.needs_dma() else None
708
Louis Verhaard93719a92020-12-08 10:02:31 +0100709 def find_npu_op(self) -> Optional[Operation]:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200710 # Returns the NPU operator that uses this tensor, excluding DMA operators.
711 for op in self.consumers():
Louis Verhaardaee5d752020-09-30 09:01:52 +0200712 if op.type == Op.DMA:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200713 return op.outputs[0].find_npu_op()
Dwight Lidman940fdee2020-08-13 13:11:48 +0200714 if op.run_on_npu:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200715 return op
Louis Verhaard93719a92020-12-08 10:02:31 +0100716 return None
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200717
Louis Verhaard93719a92020-12-08 10:02:31 +0100718 def compressed_stream_index_from_coord(self, coord: Shape) -> int:
Tim Hall79d07d22020-04-27 18:20:16 +0100719 assert self.format == TensorFormat.WeightsCompressed
Louis Verhaard93719a92020-12-08 10:02:31 +0100720 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100721 assert len(self.compressed_values) > 0
722 assert len(self.compressed_values) + 1 == len(self.weight_compressed_offsets)
723
724 depth = coord[-1]
725 brick_depth = self.brick_size[-1]
726 # Clamp position at final element index
727 if depth > self.shape[-1]:
728 depth = self.shape[-1]
729
730 # Always round up to next boundary
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100731 index = numeric_util.round_up_divide(depth, brick_depth)
Tim Hall79d07d22020-04-27 18:20:16 +0100732
733 # Check boundaries on all but last weight set (which may be shorter
734 # than the brick we divided it up into)
735 if index < len(self.weight_compressed_offsets) - 1:
736 # There are no half-way points in the weights
737 if (depth % brick_depth) != 0:
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000738 raise errors.UnsupportedFeatureError("Offset into weights must be aligned to a brick")
Tim Hall79d07d22020-04-27 18:20:16 +0100739
740 return index
741
Louis Verhaard93719a92020-12-08 10:02:31 +0100742 def size_of_compressed_stream(self, index: int) -> int:
743 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100744 assert 0 <= index < len(self.compressed_values)
745 return len(self.compressed_values[index])
746
Louis Verhaard93719a92020-12-08 10:02:31 +0100747 def is_last_index_in_compressed_stream(self, index: int) -> bool:
748 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100749 assert 0 <= index < len(self.compressed_values)
750 return index == len(self.compressed_values) - 1
751
Louis Verhaard93719a92020-12-08 10:02:31 +0100752 def address_offset_for_coordinate(self, orig_coord: Shape, is_top_box: bool = False) -> Optional[int]:
Tim Hall79d07d22020-04-27 18:20:16 +0100753 address_offset = 0
754 coord = orig_coord
755
756 coord = coord[-len(self.storage_shape) :]
757
758 if self.sub_purpose == TensorSubPurpose.Standard:
759 for idx, c in enumerate(coord):
760 if is_top_box:
761 assert c > 0 and c <= self.shape[idx]
762 else:
763 assert c >= 0 and c < self.shape[idx]
764
765 if self.format == TensorFormat.WeightsCompressed:
766 if len(self.weight_compressed_offsets) == 0:
767 return 0
768
Louis Verhaard3c07c972020-05-07 08:12:58 +0200769 if self.needs_dma() and self.sub_purpose == TensorSubPurpose.DoubleBuffer:
Tim Hall79d07d22020-04-27 18:20:16 +0100770 depth = orig_coord[-1]
771 brick_depth = self.brick_size[-1]
772 # Clamp position at final element index
773 if depth > self.shape[-1]:
774 depth = self.shape[-1]
775
776 # Always round up to next boundary
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100777 index = numeric_util.round_up_divide(depth, brick_depth)
Tim Hall79d07d22020-04-27 18:20:16 +0100778 index = index % 2
Louis Verhaard93719a92020-12-08 10:02:31 +0100779 assert self.compressed_values is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100780
781 if len(self.compressed_values) <= 2:
782 if is_top_box and index == 0:
783 for cv in self.compressed_values:
784 address_offset += len(cv)
785 else:
786 address_offset = index * len(self.compressed_values[0])
787 else:
788 if is_top_box and index == 0:
789 address_offset = self.storage_shape[-1]
790 else:
791 address_offset = index * (self.storage_shape[-1] // 2)
792 else:
793 index = self.compressed_stream_index_from_coord(orig_coord)
794 assert index < len(self.weight_compressed_offsets)
795 address_offset = self.weight_compressed_offsets[index]
796 else:
797 if is_top_box:
798 coord = [c - 1 for c in coord]
799
800 # handle wraparound for partial buffers. make sure to do this after subtracting top box:
801 coord = [c % self.storage_shape[idx] for idx, c in enumerate(coord)]
802
803 strides, augmented_coord = self.get_strides_and_coord(coord)
804 if strides is None:
805 return None
806
807 if is_top_box:
808 address_offset += 1 * strides[-1] # one element
809
810 address_offset += np.dot(augmented_coord, strides)
811
812 assert address_offset >= 0
813 assert address_offset <= self.storage_size()
814 return address_offset
815
Louis Verhaard93719a92020-12-08 10:02:31 +0100816 def is_allocated_in_tensor_arena(self, scratch_tensor_mem_area: MemArea) -> bool:
Michael McGeaghf3e3ad72020-12-02 12:39:03 +0000817 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 +0200818
Louis Verhaard93719a92020-12-08 10:02:31 +0100819 def equivalent(self, tens: "Tensor") -> bool:
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200820 return self.equivalence_id == tens.equivalence_id
821
Louis Verhaard93719a92020-12-08 10:02:31 +0100822 def set_all_shapes(self, shape: Shape):
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100823 self.shape = shape
824 self.storage_shape = shape
825 self.bandwidth_shape = shape
826
Louis Verhaard93719a92020-12-08 10:02:31 +0100827 def get_full_shape(self) -> Shape:
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100828 d = len(self.shape)
829 if d in (1, 3):
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100830 return numeric_util.full_shape(4, self.shape, 1)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100831 elif d == 2:
832 return [self.shape[0], 1, 1, self.shape[1]]
833 else:
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200834 return self.shape.copy()
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100835
Louis Verhaard93719a92020-12-08 10:02:31 +0100836 def is_quantized(self) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100837 # a tensor is quantized if it has an integral type and it contains valid quantization params
838
Tim Hall89567612020-10-27 11:57:57 +0000839 if not isinstance(self.quantization, QuantizationParameters):
Tim Hall93582962020-09-09 21:58:15 +0100840 return False
841
Tim Hall89567612020-10-27 11:57:57 +0000842 return (self.dtype.type & BaseType.Int) != 0 and self.quantization.is_valid()
Tim Hall93582962020-09-09 21:58:15 +0100843
Tim Hall79d07d22020-04-27 18:20:16 +0100844 def __str__(self):
845 return "<nng.Tensor '%s' shape=%s dtype=%s>" % (self.name, self.shape, self.dtype)
846
847 __repr__ = __str__
Tim Hall93582962020-09-09 21:58:15 +0100848
849
Louis Verhaard93719a92020-12-08 10:02:31 +0100850def check_quantized_tens_scaling_equal(tens_a: Tensor, tens_b: Tensor) -> bool:
Tim Hall93582962020-09-09 21:58:15 +0100851 # checks that the scaling of two quantized tensors are equal
852
Tim Hall89567612020-10-27 11:57:57 +0000853 return tens_a.is_quantized() and tens_b.is_quantized() and tens_a.quantization.is_scaling_equal(tens_b.quantization)