blob: c2d6b6e5df477e84772b63e588f2d2fa82f2ae40 [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
Diego Russoea6111a2020-04-14 18:41:58 +010020
21import numpy as np
22
23from . import numeric_util
Dwight Lidmana9390f72020-05-13 12:00:08 +020024from .ethos_u55_regs.ethos_u55_regs import resampling_mode
Tim Hall79d07d22020-04-27 18:20:16 +010025from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010026from .range_set import MemoryRangeSet
Tim Hall79d07d22020-04-27 18:20:16 +010027
28
Patrik Gustavssoneca2e952020-05-27 09:15:11 +020029class MemType(enum.IntFlag):
30 Unknown = 0
31 Permanent_NPU = 1
32 Permanent_CPU = 2
33 Scratch = 3
34 Scratch_fast = 4
35 Size = Scratch_fast + 1
36
37 def display_name(self):
38 return ("Unknown", "Permanent_NPU", "Permanent_CPU", "Scratch", "Scratch_fast", "Size")[self.value]
39
40 def identifier_name(self):
41 return ("unknown", "permanent_npu", "permanent_cpu", "scratch", "scratch_fast", "size")[self.value]
42
43 def all():
44 return (MemType.Permanent_NPU, MemType.Permanent_CPU, MemType.Scratch, MemType.Scratch_fast)
45
46 def __str__(self):
47 return self.name
48
49
Tim Hall79d07d22020-04-27 18:20:16 +010050class MemArea(enum.IntFlag):
51 Unknown = 0
52 Sram = 1
53 Dram = 2
54 OnChipFlash = 3
55 OffChipFlash = 4
56 Size = OffChipFlash + 1
57
58 def display_name(self):
59 return ("Unknown", "SRAM", "DRAM", "On-chip Flash", "Off-chip Flash", "Size")[self.value]
60
61 def identifier_name(self):
62 return ("unknown", "sram", "dram", "on_chip_flash", "off_chip_flash", "size")[self.value]
63
64 def all():
65 return (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash)
66
67 def __str__(self):
68 return self.name
69
70
71class TensorPurpose(enum.IntFlag):
72 Unknown = 0
73 Weights = 1
74 FeatureMap = 2
75 Scratch = 3
Fredrik Svedberga0c36242020-06-03 15:43:31 +020076 LUT = 4
77 Size = 5
Tim Hall79d07d22020-04-27 18:20:16 +010078
79 def display_name(self):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020080 return ("Unknown", "Weights", "FeatureMap", "Scratch", "LUT", "Size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010081
82 def identifier_name(self):
Fredrik Svedberga0c36242020-06-03 15:43:31 +020083 return ("unknown", "weights", "feature_map", "scratch", "lut", "size")[self.value]
Tim Hall79d07d22020-04-27 18:20:16 +010084
85 def all():
86 return (TensorPurpose.Weights, TensorPurpose.FeatureMap)
87
88
89class TensorSubPurpose(enum.Enum):
90 Standard = 0
91 DoubleBuffer = 1
92 RollingBufferX = 2
93 RollingBufferY = 3
94 RollingBufferXY = 4
95
96 def display_name(self):
97 return ("Standard", "Double Buffer", "Rolling Buffer X", "Rolling Buffer Y", "Rolling Buffer XY")[self.value]
98
99 def identifier_name(self):
100 return ("standard", "double_buffer", "rolling_buffer_x", "rolling_buffer_y", "rolling_buffer_xy")[self.value]
101
102 def all():
103 return (
104 TensorSubPurpose.Standard,
105 TensorSubPurpose.DoubleBuffer,
106 TensorSubPurpose.RollingBufferX,
107 TensorSubPurpose.RollingBufferY,
108 TensorSubPurpose.RollingBufferXY,
109 )
110
111
112class TensorFormat(enum.Flag):
113 Unknown = 0
114 WeightsCompressed = 1
115 NHWC = 2
116 NHCWB16 = 3
117
118 def __str__(self):
119 return self.name
120
121
122class TensorBlockTraversal(enum.Enum):
123 Default = 0
124 DepthWise = 1
125 DepthFirst = 2
126 PartKernelFirst = 3
127
128
129def shape_num_elements(shp):
130 elems = 1
131 if shp is None:
132 return None
133 for d in shp:
134 if d is None:
135 return None
136 elems *= d
137 return elems
138
139
140def shape_fully_defined(shp):
141 if shp is None:
142 return False
143 for d in shp:
144 if d is None:
145 return False
146 return True
147
148
149def shape_round_to_quantum(shp, quantum):
150 new_shp = list(shp)
151
152 # Traverse backwards using length of shape since there may be more rounding quantums than shape elements
153 for i in range(-1, -len(shp) - 1, -1):
154 if new_shp[i] is not None:
155 new_shp[i] = numeric_util.round_up(new_shp[i], quantum[i])
156 return new_shp
157
158
159class QuantizationParameters:
160 __slots__ = "min", "max", "num_bits", "narrow_range", "scale_f32", "zero_point", "quant_min", "quant_max"
161
162 def __init__(self, min=None, max=None, num_bits=None, narrow_range=None):
163 self.min = min
164 self.max = max
165
166 self.num_bits = num_bits
167 self.narrow_range = narrow_range
168
169 self.scale_f32 = None
170 self.zero_point = None
171 self.quant_min = None
172 self.quant_max = None
173
174 def __str__(self):
175 return "<nng.QuantizationParameters min=%s max=%s, num_bits=%s, scale=%s, zero_point=%s>" % (
176 self.min,
177 self.max,
178 self.num_bits,
179 self.scale_f32,
180 self.zero_point,
181 )
182
183 __repr__ = __str__
184
Dwight Lidmanebe26c72020-06-09 11:40:54 +0200185 def __eq__(self, other):
186 if other is None:
187 return False
188 if not isinstance(other, QuantizationParameters):
189 return False
190
191 pairs = ((getattr(self, s), getattr(other, s)) for s in QuantizationParameters.__slots__)
192
193 return all(np.array_equal(a, b) for a, b in pairs)
194
195 def __ne__(self, other):
196 return not self == other
197
Tim Hall79d07d22020-04-27 18:20:16 +0100198 def clone(self):
199 res = QuantizationParameters()
200 res.min = self.min
201 res.max = self.max
202
203 res.num_bits = self.num_bits
204 res.narrow_range = self.narrow_range
205
206 res.scale_f32 = self.scale_f32
207 res.zero_point = self.zero_point
208 res.quant_min = self.quant_min
209 res.quant_max = self.quant_max
210 return res
211
212 def dequantize(self, values):
213 if self.zero_point.size == 1 and self.scale_f32.size == 1:
214 # same scale is used for all values
215 res = (values.astype(np.float64) - self.zero_point) * self.scale_f32
216 else:
217 # a different scale is used for different sets of values
218 values_as_float = values.astype(np.float64)
219
220 # this is not compatible with the format of depthwise weights,
221 # where input is at index 3 (Output, Kh, Kw, Input)
222 # return the quantized values
223 return np.ndarray((values_as_float.shape))
224
225 shape = values_as_float.shape[0]
226 assert self.zero_point.size == self.scale_f32.size == shape
227 res = np.ndarray(values_as_float.shape)
228 for i in range(shape):
229 res[i] = (values_as_float[i] - self.zero_point[i]) * self.scale_f32[i]
230
231 return res
232
233
234class Tensor:
235 __slots__ = (
236 "shape",
237 "storage_shape",
238 "bandwidth_shape",
239 "dtype",
240 "name",
241 "ops",
242 "consumer_list",
243 "values",
244 "quant_values",
245 "compressed_values",
Tim Hallf7e810a2020-06-25 15:04:31 +0100246 "compressed_values_substream_offsets",
Tim Hall79d07d22020-04-27 18:20:16 +0100247 "mem_area",
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200248 "mem_type",
Tim Hall79d07d22020-04-27 18:20:16 +0100249 "format",
250 "purpose",
251 "sub_purpose",
252 "alignment",
253 "weight_transpose_depthwise",
254 "storage_compression_scale",
255 "bandwidth_compression_scale",
256 "compression_scale_for_worst_weight_stream",
257 "weight_compression_scales",
258 "weight_compression_config",
259 "storage_rounding_quantum",
260 "brick_size",
261 "address",
262 "quantization",
263 "weight_compressed_offsets",
264 "element_size_bytes",
Tim Hall79d07d22020-04-27 18:20:16 +0100265 "block_traversal",
Tim Hall79d07d22020-04-27 18:20:16 +0100266 "cpu_tensor",
267 "npu_tensor",
268 "equivalence_id",
Dwight Lidmana9390f72020-05-13 12:00:08 +0200269 "resampling_mode",
Tim Hall79d07d22020-04-27 18:20:16 +0100270 )
271 AllocationQuantum = 16
272
273 def __init__(self, shape, dtype, name):
274 self.shape = shape
275 self.storage_shape = shape
276 self.bandwidth_shape = shape
277 self.dtype = dtype
278 self.name = name
279 self.equivalence_id = uuid.uuid4()
280
281 self.ops = []
282 self.consumer_list = []
283 # Below attributes are only set if a tensor has been cloned,
284 # either from Cpu -> Npu or vice versa. Needed for offline allocation
285 self.cpu_tensor = None # reference to the corresponding Cpu tensor
286 self.npu_tensor = None # reference to the corresponding Npu tensor
287
288 self.values = None
289 self.quant_values = None
290 self.compressed_values = None
Tim Hallf7e810a2020-06-25 15:04:31 +0100291 self.compressed_values_substream_offsets = None
Tim Hall79d07d22020-04-27 18:20:16 +0100292 self.mem_area = MemArea.Unknown
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200293 self.mem_type = MemType.Unknown
Tim Hall79d07d22020-04-27 18:20:16 +0100294 self.format = TensorFormat.Unknown
295 self.purpose = TensorPurpose.Unknown
296 self.sub_purpose = TensorSubPurpose.Standard
297 self.alignment = Tensor.AllocationQuantum
298 self.weight_transpose_depthwise = False
299
300 self.storage_compression_scale = 1.0
301 self.bandwidth_compression_scale = 1.0
302 self.compression_scale_for_worst_weight_stream = 1.0
303 self.weight_compression_scales = None
304 self.weight_compression_config = None
305 self.weight_compressed_offsets = []
306 self.storage_rounding_quantum = (1, 1, 1, 1)
307 self.brick_size = (1, 1, 1, 1)
Charles Xu04ce34c2020-06-23 12:42:28 +0200308 self.address = None # start address of tensor. will be filled in by tensor allocator
Tim Hall79d07d22020-04-27 18:20:16 +0100309 self.element_size_bytes = 0
310
311 # quantization parameters
312 self.quantization = None
Tim Hall79d07d22020-04-27 18:20:16 +0100313 self.block_traversal = TensorBlockTraversal.Default
Dwight Lidmana9390f72020-05-13 12:00:08 +0200314 self.resampling_mode = resampling_mode.NONE
Tim Hall79d07d22020-04-27 18:20:16 +0100315
316 def element_size(self):
317 if self.element_size_bytes == 0:
318 return self.dtype.size_in_bits() / 8
319 return self.element_size_bytes
320
321 def clone(self, suffix="_clone"):
322 res = Tensor(self.shape, self.dtype, self.name + suffix)
323 res.storage_shape = list(self.storage_shape)
324 res.bandwidth_shape = list(self.bandwidth_shape)
325
326 res.ops = []
327 res.consumer_list = []
328 res.equivalence_id = self.equivalence_id
329
330 res.values = self.values
331 res.quant_values = self.quant_values
Tim Hall79d07d22020-04-27 18:20:16 +0100332 res.mem_area = self.mem_area
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200333 res.mem_type = self.mem_type
Tim Hall79d07d22020-04-27 18:20:16 +0100334 res.format = self.format
335 res.purpose = self.purpose
336 res.sub_purpose = self.sub_purpose
337 res.alignment = self.alignment
Tim Hall79d07d22020-04-27 18:20:16 +0100338 res.bandwidth_compression_scale = self.bandwidth_compression_scale
Tim Hall79d07d22020-04-27 18:20:16 +0100339 res.storage_rounding_quantum = self.storage_rounding_quantum
Charles Xu04ce34c2020-06-23 12:42:28 +0200340 res.address = None
Tim Hall79d07d22020-04-27 18:20:16 +0100341
342 if self.quantization is not None:
343 res.quantization = self.quantization.clone()
344 else:
345 res.quantization = None
346
Dwight Lidmana9390f72020-05-13 12:00:08 +0200347 res.resampling_mode = self.resampling_mode
348
Louis Verhaard3c07c972020-05-07 08:12:58 +0200349 res.copy_compressed_weight_info(self)
Tim Hall79d07d22020-04-27 18:20:16 +0100350 return res
351
352 def clone_into_fast_storage(self, arch):
353 res = self.clone(suffix="_fast_storage")
354 res.mem_area = arch.fast_storage_mem_area
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200355 res.mem_type = MemType.Scratch_fast
Tim Hall79d07d22020-04-27 18:20:16 +0100356 return res
357
Louis Verhaard3c07c972020-05-07 08:12:58 +0200358 def copy_compressed_weight_info(self, src_tens):
359 # Copies compressed values + all related weight compression info from the given tensor
360 self.compressed_values = src_tens.compressed_values
Tim Hallf7e810a2020-06-25 15:04:31 +0100361 self.compressed_values_substream_offsets = src_tens.compressed_values_substream_offsets
Louis Verhaard3c07c972020-05-07 08:12:58 +0200362 self.storage_shape = src_tens.storage_shape
363 self.brick_size = src_tens.brick_size
364 self.weight_compression_scales = src_tens.weight_compression_scales
365 self.weight_compressed_offsets = src_tens.weight_compressed_offsets
366 self.weight_transpose_depthwise = src_tens.weight_transpose_depthwise
367 self.compression_scale_for_worst_weight_stream = src_tens.compression_scale_for_worst_weight_stream
368 self.storage_compression_scale = src_tens.storage_compression_scale
369 self.block_traversal = src_tens.block_traversal
370 self.weight_compression_config = src_tens.weight_compression_config
371
Tim Hall79d07d22020-04-27 18:20:16 +0100372 def set_format(self, fmt, arch):
373 self.format = fmt
374 shape_len = 0
375 try:
376 shape_len = len(self.shape)
377 except TypeError:
378 pass
379
380 self.storage_rounding_quantum = arch.storage_rounding_quantums[self.format]
381 self.storage_rounding_quantum = self.storage_rounding_quantum[-shape_len:]
Tim Hall79d07d22020-04-27 18:20:16 +0100382 self.brick_size = arch.brick_sizes[self.format]
383 self.brick_size = self.brick_size[-shape_len:]
384 if self.shape is None:
385 return
386
387 self.bandwidth_shape = shape_round_to_quantum(self.shape, self.brick_size)
388 self.storage_shape = shape_round_to_quantum(self.shape, self.storage_rounding_quantum)
389
390 if fmt == TensorFormat.WeightsCompressed:
391 compression_ratio = 5 / 8
392 self.storage_compression_scale = compression_ratio
393 self.bandwidth_compression_scale = compression_ratio
394 self.compression_scale_for_worst_weight_stream = compression_ratio
395
396 def storage_elements(self):
397 elems = shape_num_elements(self.storage_shape)
398 if elems is None:
399 return 0
400 return elems
401
402 def elements(self):
403 elems = shape_num_elements(self.shape)
404 if elems is None:
405 return 0
406 return elems
407
408 def has_fully_defined_shape(self):
409 return shape_fully_defined(self.shape)
410
411 def storage_size(self):
412 raw_size = self.storage_elements() * self.element_size()
413 if raw_size == 0:
414 raw_size = 1 # force it to take up space
415 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
416 return rounded_size
417
418 def storage_size_for_sub_purpose(self, sub_purpose, param_a=None, param_b=None):
419 alt_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
420 elems = shape_num_elements(alt_shape)
421 if elems is None:
422 return 0
423 if sub_purpose == TensorSubPurpose.DoubleBuffer:
424 raw_size = elems * self.element_size() * self.compression_scale_for_worst_weight_stream
425 else:
426 raw_size = elems * self.element_size() * self.storage_compression_scale
427 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
428 return rounded_size
429
430 def storage_shape_for_sub_purpose(self, sub_purpose, param_a, param_b):
Tim Hall79d07d22020-04-27 18:20:16 +0100431 if sub_purpose == TensorSubPurpose.DoubleBuffer:
Jacob Bohline843d332020-06-23 12:12:56 +0200432 shp = list(self.shape)
Tim Hall79d07d22020-04-27 18:20:16 +0100433 assert len(shp) >= 2
434 shp[-1] = min(shp[-1], param_a * 2)
Tim Hall79d07d22020-04-27 18:20:16 +0100435 else:
Jacob Bohline843d332020-06-23 12:12:56 +0200436 shp = list(self.storage_shape)
437 if sub_purpose == TensorSubPurpose.RollingBufferX:
438 assert len(shp) == 4
439 shp[0] = 1
440 shp[2] = min(shp[2], param_a)
441 elif sub_purpose == TensorSubPurpose.RollingBufferY:
442 assert len(shp) == 4
443 shp[0] = 1
444 shp[1] = min(shp[1], param_a)
445 elif sub_purpose == TensorSubPurpose.RollingBufferXY:
446 assert len(shp) == 4
447 shp[0] = 1
448 shp[2] = min(shp[2], param_a)
449 shp[1] = min(shp[1], param_b)
450 elif sub_purpose == TensorSubPurpose.Standard:
451 pass
452 else:
453 assert 0, "did not expect new sub purpose %s" % (sub_purpose,)
454
Tim Hall79d07d22020-04-27 18:20:16 +0100455 return shp
456
457 def set_new_sub_purpose(self, sub_purpose, param_a=None, param_b=None):
458 self.storage_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
459 self.sub_purpose = sub_purpose
460 if sub_purpose == TensorSubPurpose.DoubleBuffer:
461 self.storage_compression_scale = self.compression_scale_for_worst_weight_stream
462
463 def bandwidth(self):
464 elems = shape_num_elements(self.bandwidth_shape)
465 if elems is None:
466 return 0
467 return elems * self.element_size() * self.bandwidth_compression_scale
468
469 def consumers(self):
470 return self.consumer_list
471
472 def get_address_ranges_for_coordinates(self, start_coord, end_coord):
473 if self.sub_purpose in set(
474 (TensorSubPurpose.RollingBufferX, TensorSubPurpose.RollingBufferY, TensorSubPurpose.RollingBufferXY)
475 ):
476 # build dummy coordinates that cover the entire buffer
477 start_coord = [0] * len(start_coord)
478 end_coord = [min(self.storage_shape[i], self.shape[i]) for i in range(len(end_coord))]
479
480 start = self.address_for_coordinate(start_coord, is_top_box=False)
481 end = self.address_for_coordinate(end_coord, is_top_box=True)
482 return MemoryRangeSet(self.mem_area, start, end)
483
484 def addresses_for_rolling_buffer(self, start_coord, end_coord):
485 # returns ( box_height0, box_height1, box_width, [address_tl, address_tr, address_bl, address_br] )
486
487 if len(start_coord) < 4:
488 box_height0 = 1
489 box_width = 1
490
491 if len(start_coord) >= 2:
492 box_width = end_coord[-2] - start_coord[-2]
493
494 return box_height0, box_height0, box_width, [self.address_for_coordinate(start_coord), None, None, None]
495
496 crossing_y = numeric_util.round_up(start_coord[1] + 1, self.storage_shape[1])
497 crossing_x = numeric_util.round_up(start_coord[2] + 1, self.storage_shape[2])
498
499 crossing_y = min(crossing_y, end_coord[1])
500 crossing_x = min(crossing_x, end_coord[2])
501
502 box_height0 = crossing_y - start_coord[1]
503 box_width = crossing_x - start_coord[2]
504
505 addresses = [None] * 4
506 addresses[0] = self.address_for_coordinate(start_coord)
507
508 if end_coord[2] > crossing_x:
509 addresses[1] = self.address_for_coordinate([start_coord[0], start_coord[1], crossing_x, start_coord[3]])
510 raise Exception("Striping in vertical direction is not supported")
511 if end_coord[1] > crossing_y:
512 addresses[2] = self.address_for_coordinate([start_coord[0], crossing_y, start_coord[2], start_coord[3]])
513 if end_coord[1] > crossing_y and end_coord[2] > crossing_x:
514 addresses[3] = self.address_for_coordinate([start_coord[0], crossing_y, crossing_x, start_coord[3]])
515
516 return box_height0, box_height0, box_width, addresses
517
518 def address_for_coordinate(self, coord, is_top_box=False):
519 return self.address + self.address_offset_for_coordinate(coord, is_top_box)
520
521 def get_strides_and_coord(self, coord=None):
522 if coord is None:
523 coord = [0] * len(self.storage_shape)
524
525 augmented_coord = coord
526 augmented_shape = self.storage_shape
527 while len(augmented_shape) < 4:
528 augmented_shape = [1] + augmented_shape
529
530 while len(augmented_coord) < 4:
531 augmented_coord = [0] + augmented_coord
532
533 assert len(augmented_coord) == len(augmented_shape)
534
535 if self.format == TensorFormat.NHWC:
536 augmented_shape = [augmented_shape[0], augmented_shape[3]] + augmented_shape[1:3] + [1]
537 augmented_coord = [augmented_coord[0], augmented_coord[3]] + augmented_coord[1:3] + [0]
538 stride_order = [4, 1, 3, 2, 0]
539
540 elif self.format == TensorFormat.NHCWB16:
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200541 channel_divisor = 16
Tim Hall79d07d22020-04-27 18:20:16 +0100542 augmented_shape = augmented_shape[0:4] + [1]
543 augmented_coord = (
544 [augmented_coord[0], augmented_coord[3] // channel_divisor]
545 + augmented_coord[1:3]
546 + [augmented_coord[3] % channel_divisor]
547 )
548
549 if augmented_shape[1] == 0:
550 augmented_shape[1] = 1
551
552 else:
553 assert self.format in set((TensorFormat.Unknown, TensorFormat.WeightsCompressed))
554 return None, None
555
556 strides = [0] * len(augmented_shape)
557 stride = self.element_size() * self.storage_compression_scale
558
559 if self.format != TensorFormat.NHCWB16:
560 for i in stride_order:
561 strides[i] = stride
562 stride *= augmented_shape[i]
563 else:
564 assert len(strides) == 5
Tim Hall79d07d22020-04-27 18:20:16 +0100565 strides[4] = stride
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200566 strides[3] = 16 * stride # STRIDE_X
Tim Hall79d07d22020-04-27 18:20:16 +0100567 strides[1] = strides[3] * augmented_shape[2] # STRIDE_C
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200568 strides[2] = augmented_shape[2] * augmented_shape[3] * stride # STRIDE_Y
Tim Hall79d07d22020-04-27 18:20:16 +0100569 strides[0] = strides[2] * augmented_shape[1] # STRIDE_N
570
571 return strides, augmented_coord
572
573 def get_strides(self):
574 strides, _ = self.get_strides_and_coord()
575
576 return strides
577
Louis Verhaard3c07c972020-05-07 08:12:58 +0200578 def needs_dma(self):
579 return len(self.ops) == 1 and self.ops[0].type == "DMA"
580
581 def get_dma_src_tensor(self):
582 # For weight tensors that need DMA: returns the source tensor in Flash, else None
583 # Note: for DMA ops, Pass.weight_tensor is referring to the SRAM weight tensor
584 return self.ops[0].inputs[0] if self.needs_dma() else None
585
Louis Verhaardb2fb2122020-06-04 15:51:24 +0200586 def find_npu_op(self):
587 # Returns the NPU operator that uses this tensor, excluding DMA operators.
588 for op in self.consumers():
589 if op.type == "DMA":
590 return op.outputs[0].find_npu_op()
591 if "npu_block_type" in op.attrs:
592 return op
593 return None
594
Tim Hall79d07d22020-04-27 18:20:16 +0100595 def compressed_stream_index_from_coord(self, coord):
596 assert self.format == TensorFormat.WeightsCompressed
597 assert len(self.compressed_values) > 0
598 assert len(self.compressed_values) + 1 == len(self.weight_compressed_offsets)
599
600 depth = coord[-1]
601 brick_depth = self.brick_size[-1]
602 # Clamp position at final element index
603 if depth > self.shape[-1]:
604 depth = self.shape[-1]
605
606 # Always round up to next boundary
607 index = round_up_divide(depth, brick_depth)
608
609 # Check boundaries on all but last weight set (which may be shorter
610 # than the brick we divided it up into)
611 if index < len(self.weight_compressed_offsets) - 1:
612 # There are no half-way points in the weights
613 if (depth % brick_depth) != 0:
614 raise Exception("Offset into weights must be aligned to a brick")
615
616 return index
617
618 def size_of_compressed_stream(self, index):
619 assert 0 <= index < len(self.compressed_values)
620 return len(self.compressed_values[index])
621
622 def is_last_index_in_compressed_stream(self, index):
623 assert 0 <= index < len(self.compressed_values)
624 return index == len(self.compressed_values) - 1
625
626 def address_offset_for_coordinate(self, orig_coord, is_top_box=False):
627 address_offset = 0
628 coord = orig_coord
629
630 coord = coord[-len(self.storage_shape) :]
631
632 if self.sub_purpose == TensorSubPurpose.Standard:
633 for idx, c in enumerate(coord):
634 if is_top_box:
635 assert c > 0 and c <= self.shape[idx]
636 else:
637 assert c >= 0 and c < self.shape[idx]
638
639 if self.format == TensorFormat.WeightsCompressed:
640 if len(self.weight_compressed_offsets) == 0:
641 return 0
642
Louis Verhaard3c07c972020-05-07 08:12:58 +0200643 if self.needs_dma() and self.sub_purpose == TensorSubPurpose.DoubleBuffer:
Tim Hall79d07d22020-04-27 18:20:16 +0100644 depth = orig_coord[-1]
645 brick_depth = self.brick_size[-1]
646 # Clamp position at final element index
647 if depth > self.shape[-1]:
648 depth = self.shape[-1]
649
650 # Always round up to next boundary
651 index = round_up_divide(depth, brick_depth)
652 index = index % 2
653
654 if len(self.compressed_values) <= 2:
655 if is_top_box and index == 0:
656 for cv in self.compressed_values:
657 address_offset += len(cv)
658 else:
659 address_offset = index * len(self.compressed_values[0])
660 else:
661 if is_top_box and index == 0:
662 address_offset = self.storage_shape[-1]
663 else:
664 address_offset = index * (self.storage_shape[-1] // 2)
665 else:
666 index = self.compressed_stream_index_from_coord(orig_coord)
667 assert index < len(self.weight_compressed_offsets)
668 address_offset = self.weight_compressed_offsets[index]
669 else:
670 if is_top_box:
671 coord = [c - 1 for c in coord]
672
673 # handle wraparound for partial buffers. make sure to do this after subtracting top box:
674 coord = [c % self.storage_shape[idx] for idx, c in enumerate(coord)]
675
676 strides, augmented_coord = self.get_strides_and_coord(coord)
677 if strides is None:
678 return None
679
680 if is_top_box:
681 address_offset += 1 * strides[-1] # one element
682
683 address_offset += np.dot(augmented_coord, strides)
684
685 assert address_offset >= 0
686 assert address_offset <= self.storage_size()
687 return address_offset
688
Patrik Gustavssoneca2e952020-05-27 09:15:11 +0200689 def is_allocated_in_tensor_arena(self, scratch_tensor_mem_area):
690 if self.mem_area == scratch_tensor_mem_area and (self.mem_type in set((MemType.Scratch, MemType.Scratch_fast))):
691 return True
692 return False
693
Michael McGeagh6a8d4242020-07-28 12:17:59 +0100694 def set_all_shapes(self, shape):
695 self.shape = shape
696 self.storage_shape = shape
697 self.bandwidth_shape = shape
698
Tim Hall79d07d22020-04-27 18:20:16 +0100699 def __str__(self):
700 return "<nng.Tensor '%s' shape=%s dtype=%s>" % (self.name, self.shape, self.dtype)
701
702 __repr__ = __str__