blob: 84af8edbbb8a19f22612101fd5279125851fed34 [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.
Tim Hall79d07d22020-04-27 18:20:16 +010018import enum
Tim Hall79d07d22020-04-27 18:20:16 +010019import uuid
Jacob Bohlin1a666972020-09-11 10:04:15 +020020from collections import defaultdict
Louis Verhaard9db529a2020-09-23 10:27:11 +020021from functools import lru_cache
Diego Russoea6111a2020-04-14 18:41:58 +010022
23import numpy as np
24
25from . import numeric_util
Tim Hall93582962020-09-09 21:58:15 +010026from .data_type import BaseType
Michael McGeagh5778ffd2020-08-06 17:31:02 +010027from .data_type import DataType
Dwight Lidmana9390f72020-05-13 12:00:08 +020028from .ethos_u55_regs.ethos_u55_regs import resampling_mode
Louis Verhaardaee5d752020-09-30 09:01:52 +020029from .operation import Op
Michael McGeagh5778ffd2020-08-06 17:31:02 +010030from .operation import Operation
Diego Russoe8a10452020-04-21 17:39:10 +010031from .range_set import MemoryRangeSet
Tim Hall79d07d22020-04-27 18:20:16 +010032
33
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020034class MemType(enum.IntFlag):
35 Unknown = 0
36 Permanent_NPU = 1
37 Permanent_CPU = 2
38 Scratch = 3
39 Scratch_fast = 4
40 Size = Scratch_fast + 1
41
42 def display_name(self):
43 return ("Unknown", "Permanent_NPU", "Permanent_CPU", "Scratch", "Scratch_fast", "Size")[self.value]
44
45 def identifier_name(self):
46 return ("unknown", "permanent_npu", "permanent_cpu", "scratch", "scratch_fast", "size")[self.value]
47
48 def all():
49 return (MemType.Permanent_NPU, MemType.Permanent_CPU, MemType.Scratch, MemType.Scratch_fast)
50
51 def __str__(self):
52 return self.name
53
54
Tim Hall79d07d22020-04-27 18:20:16 +010055class MemArea(enum.IntFlag):
56 Unknown = 0
57 Sram = 1
58 Dram = 2
59 OnChipFlash = 3
60 OffChipFlash = 4
Louis Verhaard0b8268a2020-08-05 16:11:29 +020061 Shram = 5 # for LUT
62 Size = Shram + 1
Tim Hall79d07d22020-04-27 18:20:16 +010063
64 def display_name(self):
Louis Verhaard0b8268a2020-08-05 16:11:29 +020065 return ("Unknown", "SRAM", "DRAM", "On-chip Flash", "Off-chip Flash", "SHRAM", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010066
67 def identifier_name(self):
Louis Verhaard0b8268a2020-08-05 16:11:29 +020068 return ("unknown", "sram", "dram", "on_chip_flash", "off_chip_flash", "shram", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010069
70 def all():
Louis Verhaard0b8268a2020-08-05 16:11:29 +020071 return (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash, MemArea.Shram)
Tim Hall79d07d22020-04-27 18:20:16 +010072
73 def __str__(self):
74 return self.name
75
76
77class TensorPurpose(enum.IntFlag):
78 Unknown = 0
79 Weights = 1
80 FeatureMap = 2
81 Scratch = 3
Fredrik Svedberga0c36242020-06-03 15:43:31 +020082 LUT = 4
83 Size = 5
Tim Hall79d07d22020-04-27 18:20:16 +010084
85 def display_name(self):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020086 return ("Unknown", "Weights", "FeatureMap", "Scratch", "LUT", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010087
88 def identifier_name(self):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020089 return ("unknown", "weights", "feature_map", "scratch", "lut", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010090
91 def all():
92 return (TensorPurpose.Weights, TensorPurpose.FeatureMap)
93
94
95class TensorSubPurpose(enum.Enum):
96 Standard = 0
97 DoubleBuffer = 1
98 RollingBufferX = 2
99 RollingBufferY = 3
100 RollingBufferXY = 4
101
102 def display_name(self):
103 return ("Standard", "Double Buffer", "Rolling Buffer X", "Rolling Buffer Y", "Rolling Buffer XY")[self.value]
104
105 def identifier_name(self):
106 return ("standard", "double_buffer", "rolling_buffer_x", "rolling_buffer_y", "rolling_buffer_xy")[self.value]
107
108 def all():
109 return (
110 TensorSubPurpose.Standard,
111 TensorSubPurpose.DoubleBuffer,
112 TensorSubPurpose.RollingBufferX,
113 TensorSubPurpose.RollingBufferY,
114 TensorSubPurpose.RollingBufferXY,
115 )
116
117
118class TensorFormat(enum.Flag):
119 Unknown = 0
120 WeightsCompressed = 1
121 NHWC = 2
122 NHCWB16 = 3
123
124 def __str__(self):
125 return self.name
126
127
128class TensorBlockTraversal(enum.Enum):
129 Default = 0
130 DepthWise = 1
131 DepthFirst = 2
132 PartKernelFirst = 3
133
134
135def shape_num_elements(shp):
136 elems = 1
137 if shp is None:
138 return None
139 for d in shp:
140 if d is None:
141 return None
142 elems *= d
143 return elems
144
145
146def shape_fully_defined(shp):
147 if shp is None:
148 return False
149 for d in shp:
150 if d is None:
151 return False
152 return True
153
154
155def shape_round_to_quantum(shp, quantum):
156 new_shp = list(shp)
157
158 # Traverse backwards using length of shape since there may be more rounding quantums than shape elements
159 for i in range(-1, -len(shp) - 1, -1):
160 if new_shp[i] is not None:
161 new_shp[i] = numeric_util.round_up(new_shp[i], quantum[i])
162 return new_shp
163
164
Louis Verhaard9db529a2020-09-23 10:27:11 +0200165@lru_cache(maxsize=None)
166def create_equivalence_id(key):
167 # Generates equivalence_id based on the given key.
168 return uuid.uuid4()
169
170
Tim Hall79d07d22020-04-27 18:20:16 +0100171class QuantizationParameters:
172 __slots__ = "min", "max", "num_bits", "narrow_range", "scale_f32", "zero_point", "quant_min", "quant_max"
173
174 def __init__(self, min=None, max=None, num_bits=None, narrow_range=None):
175 self.min = min
176 self.max = max
177
178 self.num_bits = num_bits
179 self.narrow_range = narrow_range
180
181 self.scale_f32 = None
182 self.zero_point = None
183 self.quant_min = None
184 self.quant_max = None
185
186 def __str__(self):
187 return "<nng.QuantizationParameters min=%s max=%s, num_bits=%s, scale=%s, zero_point=%s>" % (
188 self.min,
189 self.max,
190 self.num_bits,
191 self.scale_f32,
192 self.zero_point,
193 )
194
195 __repr__ = __str__
196
197 def clone(self):
198 res = QuantizationParameters()
199 res.min = self.min
200 res.max = self.max
201
202 res.num_bits = self.num_bits
203 res.narrow_range = self.narrow_range
204
205 res.scale_f32 = self.scale_f32
206 res.zero_point = self.zero_point
207 res.quant_min = self.quant_min
208 res.quant_max = self.quant_max
209 return res
210
211 def dequantize(self, values):
212 if self.zero_point.size == 1 and self.scale_f32.size == 1:
213 # same scale is used for all values
214 res = (values.astype(np.float64) - self.zero_point) * self.scale_f32
215 else:
216 # a different scale is used for different sets of values
217 values_as_float = values.astype(np.float64)
218
219 # this is not compatible with the format of depthwise weights,
220 # where input is at index 3 (Output, Kh, Kw, Input)
221 # return the quantized values
222 return np.ndarray((values_as_float.shape))
223
224 shape = values_as_float.shape[0]
225 assert self.zero_point.size == self.scale_f32.size == shape
226 res = np.ndarray(values_as_float.shape)
227 for i in range(shape):
228 res[i] = (values_as_float[i] - self.zero_point[i]) * self.scale_f32[i]
229
230 return res
231
Tim Halle3786ac2020-07-28 17:40:50 +0100232 def is_scaling_equal(self, other):
Tim Hall93582962020-09-09 21:58:15 +0100233 # quantisation parameter scaling is not equal if 'other' is None because
234 # it implies that the tensor it belongs to is not quantised. otherwise,
235 # it depends upon whether the scale and zero point are equal
236
237 if other is None:
Tim Halle3786ac2020-07-28 17:40:50 +0100238 return False
239
Tim Hall93582962020-09-09 21:58:15 +0100240 assert isinstance(other, QuantizationParameters)
241
Tim Halle3786ac2020-07-28 17:40:50 +0100242 return self.scale_f32 == other.scale_f32 and self.zero_point == other.zero_point
243
Tim Hall93582962020-09-09 21:58:15 +0100244 def is_valid(self):
245 # quantisation parameters are consider valid if they have a scale and zero point
246
247 return None not in (self.scale_f32, self.zero_point)
248
Tim Hall79d07d22020-04-27 18:20:16 +0100249
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100250def create_const_tensor(name, shape, dtype, values, value_dtype=None, purpose=TensorPurpose.Unknown, quantization=None):
251 # Tensor
252 const_tensor = Tensor(shape, dtype, name + "_0")
253 const_tensor.purpose = purpose
254 const_tensor.quantization = quantization
255 const_tensor.values = np.array(values, dtype=value_dtype)
Jacob Bohlina41cd4d2020-08-26 18:21:28 +0200256 const_tensor.quant_values = np.frombuffer(const_tensor.values.tobytes(), dtype=np.uint8)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100257 # Operator
Louis Verhaardaee5d752020-09-30 09:01:52 +0200258 const_op = Operation(Op.Const, name)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100259 const_op.set_output_tensor(const_tensor)
260 return const_tensor
261
262
263def create_reshape_tensor(tens, shape, ifm_reshape=True):
264 if shape == tens.shape:
265 return tens
266 # Tensors
267 name = tens.name + "_reshape"
268 reshape_ifm = tens
269 reshape_ofm = tens.clone("_reshaped")
270 reshape_ofm.set_all_shapes(shape)
271 if not ifm_reshape:
272 reshape_ifm, reshape_ofm = reshape_ofm, reshape_ifm
273 # Operator
Louis Verhaardaee5d752020-09-30 09:01:52 +0200274 reshape_op = Operation(Op.Reshape, name)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100275 reshape_op.attrs["new_shape"] = shape
276 reshape_op.add_input_tensor(reshape_ifm)
277 reshape_op.add_input_tensor(create_const_tensor(name + "_shape", [1], DataType.int32, shape))
278 reshape_op.set_output_tensor(reshape_ofm)
279 return reshape_ofm if ifm_reshape else reshape_ifm
280
281
Jacob Bohlin1a666972020-09-11 10:04:15 +0200282# class that keeps track of all tensor addresses in the different memory types
283class TensorAddressMap:
284 address_map = defaultdict(dict) # dict (tens.equivalence_id -> dict (mem_type -> address))
285
286 @classmethod
287 def get_address_for_tens(cls, tens_id, mem_type):
288 return cls.address_map[tens_id].get(mem_type)
289
290 @classmethod
291 def set_address_for_tens(cls, tens_id, mem_type, address):
292 # Check previous address if there is one
293 previous_address = cls.address_map[tens_id].get(mem_type)
Louis Verhaard0b9c9a32020-09-15 14:05:38 +0200294 if address is not None and previous_address is not None:
Jacob Bohlin1a666972020-09-11 10:04:15 +0200295 assert previous_address == address, "Two different addresses cannot be assigned to the same tensor."
296
297 # Set tensor's address for memory type
298 cls.address_map[tens_id][mem_type] = address
299
300
Tim Hall79d07d22020-04-27 18:20:16 +0100301class Tensor:
302 __slots__ = (
303 "shape",
304 "storage_shape",
305 "bandwidth_shape",
306 "dtype",
307 "name",
308 "ops",
309 "consumer_list",
310 "values",
311 "quant_values",
312 "compressed_values",
Tim Hallf7e810a2020-06-25 15:04:31 +0100313 "compressed_values_substream_offsets",
Tim Hall79d07d22020-04-27 18:20:16 +0100314 "mem_area",
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200315 "mem_type",
Tim Hall79d07d22020-04-27 18:20:16 +0100316 "format",
317 "purpose",
318 "sub_purpose",
319 "alignment",
320 "weight_transpose_depthwise",
321 "storage_compression_scale",
322 "bandwidth_compression_scale",
323 "compression_scale_for_worst_weight_stream",
324 "weight_compression_scales",
325 "weight_compression_config",
Louis Verhaard9db529a2020-09-23 10:27:11 +0200326 "value_id",
Tim Hall79d07d22020-04-27 18:20:16 +0100327 "storage_rounding_quantum",
328 "brick_size",
Tim Hall79d07d22020-04-27 18:20:16 +0100329 "quantization",
330 "weight_compressed_offsets",
331 "element_size_bytes",
Tim Hall79d07d22020-04-27 18:20:16 +0100332 "block_traversal",
Tim Hall79d07d22020-04-27 18:20:16 +0100333 "equivalence_id",
Dwight Lidmana9390f72020-05-13 12:00:08 +0200334 "resampling_mode",
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200335 "avoid_NHCWB16",
Tim Hall79d07d22020-04-27 18:20:16 +0100336 )
337 AllocationQuantum = 16
338
339 def __init__(self, shape, dtype, name):
340 self.shape = shape
341 self.storage_shape = shape
342 self.bandwidth_shape = shape
343 self.dtype = dtype
344 self.name = name
345 self.equivalence_id = uuid.uuid4()
346
347 self.ops = []
348 self.consumer_list = []
Tim Hall79d07d22020-04-27 18:20:16 +0100349
350 self.values = None
351 self.quant_values = None
352 self.compressed_values = None
Tim Hallf7e810a2020-06-25 15:04:31 +0100353 self.compressed_values_substream_offsets = None
Tim Hall79d07d22020-04-27 18:20:16 +0100354 self.mem_area = MemArea.Unknown
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200355 self.mem_type = MemType.Unknown
Tim Hall79d07d22020-04-27 18:20:16 +0100356 self.format = TensorFormat.Unknown
357 self.purpose = TensorPurpose.Unknown
358 self.sub_purpose = TensorSubPurpose.Standard
359 self.alignment = Tensor.AllocationQuantum
360 self.weight_transpose_depthwise = False
361
362 self.storage_compression_scale = 1.0
363 self.bandwidth_compression_scale = 1.0
364 self.compression_scale_for_worst_weight_stream = 1.0
365 self.weight_compression_scales = None
Louis Verhaard9db529a2020-09-23 10:27:11 +0200366 # if two tensors have the same weight_compression_config, then they have the same compressed values
Tim Hall79d07d22020-04-27 18:20:16 +0100367 self.weight_compression_config = None
Louis Verhaard9db529a2020-09-23 10:27:11 +0200368 # if two tensors have the same value_id, then they have the same values
369 self.value_id = uuid.uuid4()
Tim Hall79d07d22020-04-27 18:20:16 +0100370 self.weight_compressed_offsets = []
371 self.storage_rounding_quantum = (1, 1, 1, 1)
372 self.brick_size = (1, 1, 1, 1)
Tim Hall79d07d22020-04-27 18:20:16 +0100373 self.element_size_bytes = 0
374
375 # quantization parameters
376 self.quantization = None
Tim Hall79d07d22020-04-27 18:20:16 +0100377 self.block_traversal = TensorBlockTraversal.Default
Dwight Lidmana9390f72020-05-13 12:00:08 +0200378 self.resampling_mode = resampling_mode.NONE
Tim Hall79d07d22020-04-27 18:20:16 +0100379
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200380 self.avoid_NHCWB16 = False
381
Jacob Bohlin1a666972020-09-11 10:04:15 +0200382 @property
383 def address(self):
384 return TensorAddressMap.get_address_for_tens(self.equivalence_id, self.mem_type)
385
386 @address.setter
387 def address(self, address):
388 TensorAddressMap.set_address_for_tens(self.equivalence_id, self.mem_type, address)
389
Tim Hall79d07d22020-04-27 18:20:16 +0100390 def element_size(self):
391 if self.element_size_bytes == 0:
392 return self.dtype.size_in_bits() / 8
393 return self.element_size_bytes
394
395 def clone(self, suffix="_clone"):
396 res = Tensor(self.shape, self.dtype, self.name + suffix)
397 res.storage_shape = list(self.storage_shape)
398 res.bandwidth_shape = list(self.bandwidth_shape)
399
400 res.ops = []
401 res.consumer_list = []
Tim Hall79d07d22020-04-27 18:20:16 +0100402
403 res.values = self.values
404 res.quant_values = self.quant_values
Tim Hall79d07d22020-04-27 18:20:16 +0100405 res.mem_area = self.mem_area
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200406 res.mem_type = self.mem_type
Tim Hall79d07d22020-04-27 18:20:16 +0100407 res.format = self.format
408 res.purpose = self.purpose
409 res.sub_purpose = self.sub_purpose
410 res.alignment = self.alignment
Tim Hall79d07d22020-04-27 18:20:16 +0100411 res.bandwidth_compression_scale = self.bandwidth_compression_scale
Tim Hall79d07d22020-04-27 18:20:16 +0100412 res.storage_rounding_quantum = self.storage_rounding_quantum
Tim Hall79d07d22020-04-27 18:20:16 +0100413
414 if self.quantization is not None:
415 res.quantization = self.quantization.clone()
416 else:
417 res.quantization = None
418
Dwight Lidmana9390f72020-05-13 12:00:08 +0200419 res.resampling_mode = self.resampling_mode
420
Louis Verhaard3c07c972020-05-07 08:12:58 +0200421 res.copy_compressed_weight_info(self)
Patrik Gustavsson458a2082020-08-13 13:41:05 +0200422 res.avoid_NHCWB16 = self.avoid_NHCWB16
Tim Hall79d07d22020-04-27 18:20:16 +0100423 return res
424
425 def clone_into_fast_storage(self, arch):
426 res = self.clone(suffix="_fast_storage")
427 res.mem_area = arch.fast_storage_mem_area
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200428 res.mem_type = MemType.Scratch_fast
Tim Hall79d07d22020-04-27 18:20:16 +0100429 return res
430
Louis Verhaard3c07c972020-05-07 08:12:58 +0200431 def copy_compressed_weight_info(self, src_tens):
432 # Copies compressed values + all related weight compression info from the given tensor
Louis Verhaard9db529a2020-09-23 10:27:11 +0200433 self.equivalence_id = src_tens.equivalence_id
Louis Verhaard3c07c972020-05-07 08:12:58 +0200434 self.compressed_values = src_tens.compressed_values
Tim Hallf7e810a2020-06-25 15:04:31 +0100435 self.compressed_values_substream_offsets = src_tens.compressed_values_substream_offsets
Louis Verhaard3c07c972020-05-07 08:12:58 +0200436 self.storage_shape = src_tens.storage_shape
437 self.brick_size = src_tens.brick_size
438 self.weight_compression_scales = src_tens.weight_compression_scales
439 self.weight_compressed_offsets = src_tens.weight_compressed_offsets
440 self.weight_transpose_depthwise = src_tens.weight_transpose_depthwise
441 self.compression_scale_for_worst_weight_stream = src_tens.compression_scale_for_worst_weight_stream
442 self.storage_compression_scale = src_tens.storage_compression_scale
443 self.block_traversal = src_tens.block_traversal
444 self.weight_compression_config = src_tens.weight_compression_config
Louis Verhaard9db529a2020-09-23 10:27:11 +0200445 self.value_id = src_tens.value_id
Louis Verhaard3c07c972020-05-07 08:12:58 +0200446
Tim Hall79d07d22020-04-27 18:20:16 +0100447 def set_format(self, fmt, arch):
448 self.format = fmt
449 shape_len = 0
450 try:
451 shape_len = len(self.shape)
452 except TypeError:
453 pass
454
455 self.storage_rounding_quantum = arch.storage_rounding_quantums[self.format]
456 self.storage_rounding_quantum = self.storage_rounding_quantum[-shape_len:]
Tim Hall79d07d22020-04-27 18:20:16 +0100457 self.brick_size = arch.brick_sizes[self.format]
458 self.brick_size = self.brick_size[-shape_len:]
459 if self.shape is None:
460 return
461
462 self.bandwidth_shape = shape_round_to_quantum(self.shape, self.brick_size)
463 self.storage_shape = shape_round_to_quantum(self.shape, self.storage_rounding_quantum)
464
465 if fmt == TensorFormat.WeightsCompressed:
466 compression_ratio = 5 / 8
467 self.storage_compression_scale = compression_ratio
468 self.bandwidth_compression_scale = compression_ratio
469 self.compression_scale_for_worst_weight_stream = compression_ratio
470
471 def storage_elements(self):
472 elems = shape_num_elements(self.storage_shape)
473 if elems is None:
474 return 0
475 return elems
476
477 def elements(self):
478 elems = shape_num_elements(self.shape)
479 if elems is None:
480 return 0
481 return elems
482
483 def has_fully_defined_shape(self):
484 return shape_fully_defined(self.shape)
485
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200486 def storage_size(self, scale=1.0):
487 raw_size = self.storage_elements() * self.element_size() * scale
Tim Hall79d07d22020-04-27 18:20:16 +0100488 if raw_size == 0:
489 raw_size = 1 # force it to take up space
490 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
491 return rounded_size
492
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200493 def storage_size_for_sub_purpose(self, arch, sub_purpose, param_a=None, param_b=None):
Tim Hall79d07d22020-04-27 18:20:16 +0100494 alt_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
495 elems = shape_num_elements(alt_shape)
496 if elems is None:
497 return 0
498 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Patrik Gustavsson90831bc2020-08-24 16:26:11 +0200499 raw_size = (
500 elems
501 * self.element_size()
502 * self.compression_scale_for_worst_weight_stream
503 * arch.weight_estimation_scaling
504 )
Tim Hall79d07d22020-04-27 18:20:16 +0100505 else:
Patrik Gustavsson9baa4c32020-08-20 13:59:01 +0200506 # Rolling buffers are used for intermediate data in ifm streaming
507 # These will all use the NHCWB16 format, and need to be aligned to 16 in the C-dimension
508 if alt_shape[-1] % 16 != 0:
509 nhcwb16_shape = alt_shape[0:-1] + [numeric_util.round_up(alt_shape[-1], 16)]
510 elems = shape_num_elements(nhcwb16_shape)
511
Tim Hall79d07d22020-04-27 18:20:16 +0100512 raw_size = elems * self.element_size() * self.storage_compression_scale
513 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
514 return rounded_size
515
516 def storage_shape_for_sub_purpose(self, sub_purpose, param_a, param_b):
Tim Hall79d07d22020-04-27 18:20:16 +0100517 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Jacob Bohline843d332020-06-23 12:12:56 +0200518 shp = list(self.shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100519 assert len(shp) >= 2
520 shp[-1] = min(shp[-1], param_a * 2)
Tim Hall79d07d22020-04-27 18:20:16 +0100521 else:
Jacob Bohline843d332020-06-23 12:12:56 +0200522 shp = list(self.storage_shape)
523 if sub_purpose == TensorSubPurpose.RollingBufferX:
524 assert len(shp) == 4
525 shp[0] = 1
526 shp[2] = min(shp[2], param_a)
527 elif sub_purpose == TensorSubPurpose.RollingBufferY:
528 assert len(shp) == 4
529 shp[0] = 1
530 shp[1] = min(shp[1], param_a)
531 elif sub_purpose == TensorSubPurpose.RollingBufferXY:
532 assert len(shp) == 4
533 shp[0] = 1
534 shp[2] = min(shp[2], param_a)
535 shp[1] = min(shp[1], param_b)
536 elif sub_purpose == TensorSubPurpose.Standard:
537 pass
538 else:
539 assert 0, "did not expect new sub purpose %s" % (sub_purpose,)
540
Tim Hall79d07d22020-04-27 18:20:16 +0100541 return shp
542
543 def set_new_sub_purpose(self, sub_purpose, param_a=None, param_b=None):
544 self.storage_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
545 self.sub_purpose = sub_purpose
546 if sub_purpose == TensorSubPurpose.DoubleBuffer:
547 self.storage_compression_scale = self.compression_scale_for_worst_weight_stream
548
549 def bandwidth(self):
550 elems = shape_num_elements(self.bandwidth_shape)
551 if elems is None:
552 return 0
553 return elems * self.element_size() * self.bandwidth_compression_scale
554
555 def consumers(self):
556 return self.consumer_list
557
558 def get_address_ranges_for_coordinates(self, start_coord, end_coord):
559 if self.sub_purpose in set(
560 (TensorSubPurpose.RollingBufferX, TensorSubPurpose.RollingBufferY, TensorSubPurpose.RollingBufferXY)
561 ):
562 # build dummy coordinates that cover the entire buffer
563 start_coord = [0] * len(start_coord)
564 end_coord = [min(self.storage_shape[i], self.shape[i]) for i in range(len(end_coord))]
565
566 start = self.address_for_coordinate(start_coord, is_top_box=False)
567 end = self.address_for_coordinate(end_coord, is_top_box=True)
568 return MemoryRangeSet(self.mem_area, start, end)
569
570 def addresses_for_rolling_buffer(self, start_coord, end_coord):
571 # returns ( box_height0, box_height1, box_width, [address_tl, address_tr, address_bl, address_br] )
572
573 if len(start_coord) < 4:
574 box_height0 = 1
575 box_width = 1
576
577 if len(start_coord) >= 2:
578 box_width = end_coord[-2] - start_coord[-2]
579
580 return box_height0, box_height0, box_width, [self.address_for_coordinate(start_coord), None, None, None]
581
582 crossing_y = numeric_util.round_up(start_coord[1] + 1, self.storage_shape[1])
583 crossing_x = numeric_util.round_up(start_coord[2] + 1, self.storage_shape[2])
584
585 crossing_y = min(crossing_y, end_coord[1])
586 crossing_x = min(crossing_x, end_coord[2])
587
588 box_height0 = crossing_y - start_coord[1]
589 box_width = crossing_x - start_coord[2]
590
591 addresses = [None] * 4
592 addresses[0] = self.address_for_coordinate(start_coord)
593
594 if end_coord[2] > crossing_x:
595 addresses[1] = self.address_for_coordinate([start_coord[0], start_coord[1], crossing_x, start_coord[3]])
596 raise Exception("Striping in vertical direction is not supported")
597 if end_coord[1] > crossing_y:
598 addresses[2] = self.address_for_coordinate([start_coord[0], crossing_y, start_coord[2], start_coord[3]])
599 if end_coord[1] > crossing_y and end_coord[2] > crossing_x:
600 addresses[3] = self.address_for_coordinate([start_coord[0], crossing_y, crossing_x, start_coord[3]])
601
602 return box_height0, box_height0, box_width, addresses
603
604 def address_for_coordinate(self, coord, is_top_box=False):
605 return self.address + self.address_offset_for_coordinate(coord, is_top_box)
606
607 def get_strides_and_coord(self, coord=None):
608 if coord is None:
609 coord = [0] * len(self.storage_shape)
610
611 augmented_coord = coord
612 augmented_shape = self.storage_shape
613 while len(augmented_shape) < 4:
614 augmented_shape = [1] + augmented_shape
615
616 while len(augmented_coord) < 4:
617 augmented_coord = [0] + augmented_coord
618
619 assert len(augmented_coord) == len(augmented_shape)
620
621 if self.format == TensorFormat.NHWC:
622 augmented_shape = [augmented_shape[0], augmented_shape[3]] + augmented_shape[1:3] + [1]
623 augmented_coord = [augmented_coord[0], augmented_coord[3]] + augmented_coord[1:3] + [0]
624 stride_order = [4, 1, 3, 2, 0]
625
626 elif self.format == TensorFormat.NHCWB16:
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200627 channel_divisor = 16
Tim Hall79d07d22020-04-27 18:20:16 +0100628 augmented_shape = augmented_shape[0:4] + [1]
629 augmented_coord = (
630 [augmented_coord[0], augmented_coord[3] // channel_divisor]
631 + augmented_coord[1:3]
632 + [augmented_coord[3] % channel_divisor]
633 )
634
635 if augmented_shape[1] == 0:
636 augmented_shape[1] = 1
637
638 else:
639 assert self.format in set((TensorFormat.Unknown, TensorFormat.WeightsCompressed))
640 return None, None
641
642 strides = [0] * len(augmented_shape)
643 stride = self.element_size() * self.storage_compression_scale
644
645 if self.format != TensorFormat.NHCWB16:
646 for i in stride_order:
647 strides[i] = stride
648 stride *= augmented_shape[i]
649 else:
650 assert len(strides) == 5
Tim Hall79d07d22020-04-27 18:20:16 +0100651 strides[4] = stride
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200652 strides[3] = 16 * stride # STRIDE_X
Tim Hall79d07d22020-04-27 18:20:16 +0100653 strides[1] = strides[3] * augmented_shape[2] # STRIDE_C
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200654 strides[2] = augmented_shape[2] * augmented_shape[3] * stride # STRIDE_Y
Tim Hall79d07d22020-04-27 18:20:16 +0100655 strides[0] = strides[2] * augmented_shape[1] # STRIDE_N
656
657 return strides, augmented_coord
658
659 def get_strides(self):
660 strides, _ = self.get_strides_and_coord()
661
662 return strides
663
Louis Verhaard3c07c972020-05-07 08:12:58 +0200664 def needs_dma(self):
Louis Verhaardaee5d752020-09-30 09:01:52 +0200665 return len(self.ops) == 1 and self.ops[0].type == Op.DMA
Louis Verhaard3c07c972020-05-07 08:12:58 +0200666
667 def get_dma_src_tensor(self):
668 # For weight tensors that need DMA: returns the source tensor in Flash, else None
669 # Note: for DMA ops, Pass.weight_tensor is referring to the SRAM weight tensor
670 return self.ops[0].inputs[0] if self.needs_dma() else None
671
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200672 def find_npu_op(self):
673 # Returns the NPU operator that uses this tensor, excluding DMA operators.
674 for op in self.consumers():
Louis Verhaardaee5d752020-09-30 09:01:52 +0200675 if op.type == Op.DMA:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200676 return op.outputs[0].find_npu_op()
Dwight Lidman940fdee2020-08-13 13:11:48 +0200677 if op.run_on_npu:
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200678 return op
679 return None
680
Tim Hall79d07d22020-04-27 18:20:16 +0100681 def compressed_stream_index_from_coord(self, coord):
682 assert self.format == TensorFormat.WeightsCompressed
683 assert len(self.compressed_values) > 0
684 assert len(self.compressed_values) + 1 == len(self.weight_compressed_offsets)
685
686 depth = coord[-1]
687 brick_depth = self.brick_size[-1]
688 # Clamp position at final element index
689 if depth > self.shape[-1]:
690 depth = self.shape[-1]
691
692 # Always round up to next boundary
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100693 index = numeric_util.round_up_divide(depth, brick_depth)
Tim Hall79d07d22020-04-27 18:20:16 +0100694
695 # Check boundaries on all but last weight set (which may be shorter
696 # than the brick we divided it up into)
697 if index < len(self.weight_compressed_offsets) - 1:
698 # There are no half-way points in the weights
699 if (depth % brick_depth) != 0:
700 raise Exception("Offset into weights must be aligned to a brick")
701
702 return index
703
704 def size_of_compressed_stream(self, index):
705 assert 0 <= index < len(self.compressed_values)
706 return len(self.compressed_values[index])
707
708 def is_last_index_in_compressed_stream(self, index):
709 assert 0 <= index < len(self.compressed_values)
710 return index == len(self.compressed_values) - 1
711
712 def address_offset_for_coordinate(self, orig_coord, is_top_box=False):
713 address_offset = 0
714 coord = orig_coord
715
716 coord = coord[-len(self.storage_shape) :]
717
718 if self.sub_purpose == TensorSubPurpose.Standard:
719 for idx, c in enumerate(coord):
720 if is_top_box:
721 assert c > 0 and c <= self.shape[idx]
722 else:
723 assert c >= 0 and c < self.shape[idx]
724
725 if self.format == TensorFormat.WeightsCompressed:
726 if len(self.weight_compressed_offsets) == 0:
727 return 0
728
Louis Verhaard3c07c972020-05-07 08:12:58 +0200729 if self.needs_dma() and self.sub_purpose == TensorSubPurpose.DoubleBuffer:
Tim Hall79d07d22020-04-27 18:20:16 +0100730 depth = orig_coord[-1]
731 brick_depth = self.brick_size[-1]
732 # Clamp position at final element index
733 if depth > self.shape[-1]:
734 depth = self.shape[-1]
735
736 # Always round up to next boundary
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100737 index = numeric_util.round_up_divide(depth, brick_depth)
Tim Hall79d07d22020-04-27 18:20:16 +0100738 index = index % 2
739
740 if len(self.compressed_values) <= 2:
741 if is_top_box and index == 0:
742 for cv in self.compressed_values:
743 address_offset += len(cv)
744 else:
745 address_offset = index * len(self.compressed_values[0])
746 else:
747 if is_top_box and index == 0:
748 address_offset = self.storage_shape[-1]
749 else:
750 address_offset = index * (self.storage_shape[-1] // 2)
751 else:
752 index = self.compressed_stream_index_from_coord(orig_coord)
753 assert index < len(self.weight_compressed_offsets)
754 address_offset = self.weight_compressed_offsets[index]
755 else:
756 if is_top_box:
757 coord = [c - 1 for c in coord]
758
759 # handle wraparound for partial buffers. make sure to do this after subtracting top box:
760 coord = [c % self.storage_shape[idx] for idx, c in enumerate(coord)]
761
762 strides, augmented_coord = self.get_strides_and_coord(coord)
763 if strides is None:
764 return None
765
766 if is_top_box:
767 address_offset += 1 * strides[-1] # one element
768
769 address_offset += np.dot(augmented_coord, strides)
770
771 assert address_offset >= 0
772 assert address_offset <= self.storage_size()
773 return address_offset
774
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200775 def is_allocated_in_tensor_arena(self, scratch_tensor_mem_area):
776 if self.mem_area == scratch_tensor_mem_area and (self.mem_type in set((MemType.Scratch, MemType.Scratch_fast))):
777 return True
778 return False
779
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200780 def equivalent(self, tens):
781 return self.equivalence_id == tens.equivalence_id
782
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100783 def set_all_shapes(self, shape):
784 self.shape = shape
785 self.storage_shape = shape
786 self.bandwidth_shape = shape
787
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100788 def get_full_shape(self):
789 d = len(self.shape)
790 if d in (1, 3):
Michael McGeagh8d3216f2020-08-10 11:35:57 +0100791 return numeric_util.full_shape(4, self.shape, 1)
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100792 elif d == 2:
793 return [self.shape[0], 1, 1, self.shape[1]]
794 else:
Fredrik Svedberg835d8e12020-09-04 09:46:17 +0200795 return self.shape.copy()
Michael McGeagh5778ffd2020-08-06 17:31:02 +0100796
Tim Hall93582962020-09-09 21:58:15 +0100797 def is_quantized(self):
798 # a tensor is quantized if it has an integral type and it contains valid quantization params
799
800 if (self.dtype.type & BaseType.Int) == 0 or self.quantization is None:
801 return False
802
803 assert isinstance(self.quantisation, QuantizationParameters)
804 assert self.quantization.is_valid()
805
806 return True
807
Tim Hall79d07d22020-04-27 18:20:16 +0100808 def __str__(self):
809 return "<nng.Tensor '%s' shape=%s dtype=%s>" % (self.name, self.shape, self.dtype)
810
811 __repr__ = __str__
Tim Hall93582962020-09-09 21:58:15 +0100812
813
814def check_tens_quantized(tens):
815 # checks that a tensor is quantized
816
817 return isinstance(tens, Tensor) and tens.is_quantized()
818
819
820def check_quantized_tens_scaling_equal(tens_a, tens_b):
821 # checks that the scaling of two quantized tensors are equal
822
823 assert check_tens_quantized(tens_a)
824 assert check_tens_quantized(tens_b)
825
826 return tens_a.quantization.is_scaling_equal(tens_b.quantization)