blob: 19258b52dbf94982c062cf2f28598f86bf62490e [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
Tim Hall79d07d22020-04-27 18:20:16 +010024from .numeric_util import round_up_divide
Diego Russoe8a10452020-04-21 17:39:10 +010025from .range_set import MemoryRangeSet
Tim Hall79d07d22020-04-27 18:20:16 +010026
27
28class MemArea(enum.IntFlag):
29 Unknown = 0
30 Sram = 1
31 Dram = 2
32 OnChipFlash = 3
33 OffChipFlash = 4
34 Size = OffChipFlash + 1
35
36 def display_name(self):
37 return ("Unknown", "SRAM", "DRAM", "On-chip Flash", "Off-chip Flash", "Size")[self.value]
38
39 def identifier_name(self):
40 return ("unknown", "sram", "dram", "on_chip_flash", "off_chip_flash", "size")[self.value]
41
42 def all():
43 return (MemArea.Sram, MemArea.Dram, MemArea.OnChipFlash, MemArea.OffChipFlash)
44
45 def __str__(self):
46 return self.name
47
48
49class TensorPurpose(enum.IntFlag):
50 Unknown = 0
51 Weights = 1
52 FeatureMap = 2
53 Scratch = 3
54 Size = 4
55
56 def display_name(self):
57 return ("Unknown", "Weights", "FeatureMap", "Scratch", "Size")[self.value]
58
59 def identifier_name(self):
60 return ("unknown", "weights", "feature_map", "scratch", "size")[self.value]
61
62 def all():
63 return (TensorPurpose.Weights, TensorPurpose.FeatureMap)
64
65
66class TensorSubPurpose(enum.Enum):
67 Standard = 0
68 DoubleBuffer = 1
69 RollingBufferX = 2
70 RollingBufferY = 3
71 RollingBufferXY = 4
72
73 def display_name(self):
74 return ("Standard", "Double Buffer", "Rolling Buffer X", "Rolling Buffer Y", "Rolling Buffer XY")[self.value]
75
76 def identifier_name(self):
77 return ("standard", "double_buffer", "rolling_buffer_x", "rolling_buffer_y", "rolling_buffer_xy")[self.value]
78
79 def all():
80 return (
81 TensorSubPurpose.Standard,
82 TensorSubPurpose.DoubleBuffer,
83 TensorSubPurpose.RollingBufferX,
84 TensorSubPurpose.RollingBufferY,
85 TensorSubPurpose.RollingBufferXY,
86 )
87
88
89class TensorFormat(enum.Flag):
90 Unknown = 0
91 WeightsCompressed = 1
92 NHWC = 2
93 NHCWB16 = 3
94
95 def __str__(self):
96 return self.name
97
98
99class TensorBlockTraversal(enum.Enum):
100 Default = 0
101 DepthWise = 1
102 DepthFirst = 2
103 PartKernelFirst = 3
104
105
106def shape_num_elements(shp):
107 elems = 1
108 if shp is None:
109 return None
110 for d in shp:
111 if d is None:
112 return None
113 elems *= d
114 return elems
115
116
117def shape_fully_defined(shp):
118 if shp is None:
119 return False
120 for d in shp:
121 if d is None:
122 return False
123 return True
124
125
126def shape_round_to_quantum(shp, quantum):
127 new_shp = list(shp)
128
129 # Traverse backwards using length of shape since there may be more rounding quantums than shape elements
130 for i in range(-1, -len(shp) - 1, -1):
131 if new_shp[i] is not None:
132 new_shp[i] = numeric_util.round_up(new_shp[i], quantum[i])
133 return new_shp
134
135
136class QuantizationParameters:
137 __slots__ = "min", "max", "num_bits", "narrow_range", "scale_f32", "zero_point", "quant_min", "quant_max"
138
139 def __init__(self, min=None, max=None, num_bits=None, narrow_range=None):
140 self.min = min
141 self.max = max
142
143 self.num_bits = num_bits
144 self.narrow_range = narrow_range
145
146 self.scale_f32 = None
147 self.zero_point = None
148 self.quant_min = None
149 self.quant_max = None
150
151 def __str__(self):
152 return "<nng.QuantizationParameters min=%s max=%s, num_bits=%s, scale=%s, zero_point=%s>" % (
153 self.min,
154 self.max,
155 self.num_bits,
156 self.scale_f32,
157 self.zero_point,
158 )
159
160 __repr__ = __str__
161
162 def clone(self):
163 res = QuantizationParameters()
164 res.min = self.min
165 res.max = self.max
166
167 res.num_bits = self.num_bits
168 res.narrow_range = self.narrow_range
169
170 res.scale_f32 = self.scale_f32
171 res.zero_point = self.zero_point
172 res.quant_min = self.quant_min
173 res.quant_max = self.quant_max
174 return res
175
176 def dequantize(self, values):
177 if self.zero_point.size == 1 and self.scale_f32.size == 1:
178 # same scale is used for all values
179 res = (values.astype(np.float64) - self.zero_point) * self.scale_f32
180 else:
181 # a different scale is used for different sets of values
182 values_as_float = values.astype(np.float64)
183
184 # this is not compatible with the format of depthwise weights,
185 # where input is at index 3 (Output, Kh, Kw, Input)
186 # return the quantized values
187 return np.ndarray((values_as_float.shape))
188
189 shape = values_as_float.shape[0]
190 assert self.zero_point.size == self.scale_f32.size == shape
191 res = np.ndarray(values_as_float.shape)
192 for i in range(shape):
193 res[i] = (values_as_float[i] - self.zero_point[i]) * self.scale_f32[i]
194
195 return res
196
197
198class Tensor:
199 __slots__ = (
200 "shape",
201 "storage_shape",
202 "bandwidth_shape",
203 "dtype",
204 "name",
205 "ops",
206 "consumer_list",
207 "values",
208 "quant_values",
209 "compressed_values",
210 "mem_area",
211 "format",
212 "purpose",
213 "sub_purpose",
214 "alignment",
215 "weight_transpose_depthwise",
216 "storage_compression_scale",
217 "bandwidth_compression_scale",
218 "compression_scale_for_worst_weight_stream",
219 "weight_compression_scales",
220 "weight_compression_config",
221 "storage_rounding_quantum",
222 "brick_size",
223 "address",
224 "quantization",
225 "weight_compressed_offsets",
226 "element_size_bytes",
227 "reshaped",
228 "block_traversal",
229 "offset",
230 "cpu_tensor",
231 "npu_tensor",
232 "equivalence_id",
233 )
234 AllocationQuantum = 16
235
236 def __init__(self, shape, dtype, name):
237 self.shape = shape
238 self.storage_shape = shape
239 self.bandwidth_shape = shape
240 self.dtype = dtype
241 self.name = name
242 self.equivalence_id = uuid.uuid4()
243
244 self.ops = []
245 self.consumer_list = []
246 # Below attributes are only set if a tensor has been cloned,
247 # either from Cpu -> Npu or vice versa. Needed for offline allocation
248 self.cpu_tensor = None # reference to the corresponding Cpu tensor
249 self.npu_tensor = None # reference to the corresponding Npu tensor
250
251 self.values = None
252 self.quant_values = None
253 self.compressed_values = None
254 self.mem_area = MemArea.Unknown
255 self.format = TensorFormat.Unknown
256 self.purpose = TensorPurpose.Unknown
257 self.sub_purpose = TensorSubPurpose.Standard
258 self.alignment = Tensor.AllocationQuantum
259 self.weight_transpose_depthwise = False
260
261 self.storage_compression_scale = 1.0
262 self.bandwidth_compression_scale = 1.0
263 self.compression_scale_for_worst_weight_stream = 1.0
264 self.weight_compression_scales = None
265 self.weight_compression_config = None
266 self.weight_compressed_offsets = []
267 self.storage_rounding_quantum = (1, 1, 1, 1)
268 self.brick_size = (1, 1, 1, 1)
269 self.address = 0 # start address of tensor. will be filled in by tensor allocator
270 self.element_size_bytes = 0
271
272 # quantization parameters
273 self.quantization = None
274
275 self.reshaped = False
276 self.block_traversal = TensorBlockTraversal.Default
277
278 def element_size(self):
279 if self.element_size_bytes == 0:
280 return self.dtype.size_in_bits() / 8
281 return self.element_size_bytes
282
283 def clone(self, suffix="_clone"):
284 res = Tensor(self.shape, self.dtype, self.name + suffix)
285 res.storage_shape = list(self.storage_shape)
286 res.bandwidth_shape = list(self.bandwidth_shape)
287
288 res.ops = []
289 res.consumer_list = []
290 res.equivalence_id = self.equivalence_id
291
292 res.values = self.values
293 res.quant_values = self.quant_values
294 res.compressed_values = self.compressed_values
295 res.mem_area = self.mem_area
296 res.format = self.format
297 res.purpose = self.purpose
298 res.sub_purpose = self.sub_purpose
299 res.alignment = self.alignment
300 res.weight_transpose_depthwise = self.weight_transpose_depthwise
301
302 res.storage_compression_scale = self.storage_compression_scale
303 res.bandwidth_compression_scale = self.bandwidth_compression_scale
304 res.compression_scale_for_worst_weight_stream = self.compression_scale_for_worst_weight_stream
305 res.weight_compression_scales = self.weight_compression_scales
306 res.storage_rounding_quantum = self.storage_rounding_quantum
307 res.brick_size = self.brick_size
308 res.address = 0
309
310 if self.quantization is not None:
311 res.quantization = self.quantization.clone()
312 else:
313 res.quantization = None
314
315 return res
316
317 def clone_into_fast_storage(self, arch):
318 res = self.clone(suffix="_fast_storage")
319 res.mem_area = arch.fast_storage_mem_area
320 return res
321
322 def set_format(self, fmt, arch):
323 self.format = fmt
324 shape_len = 0
325 try:
326 shape_len = len(self.shape)
327 except TypeError:
328 pass
329
330 self.storage_rounding_quantum = arch.storage_rounding_quantums[self.format]
331 self.storage_rounding_quantum = self.storage_rounding_quantum[-shape_len:]
Tim Hall79d07d22020-04-27 18:20:16 +0100332 self.brick_size = arch.brick_sizes[self.format]
333 self.brick_size = self.brick_size[-shape_len:]
334 if self.shape is None:
335 return
336
337 self.bandwidth_shape = shape_round_to_quantum(self.shape, self.brick_size)
338 self.storage_shape = shape_round_to_quantum(self.shape, self.storage_rounding_quantum)
339
340 if fmt == TensorFormat.WeightsCompressed:
341 compression_ratio = 5 / 8
342 self.storage_compression_scale = compression_ratio
343 self.bandwidth_compression_scale = compression_ratio
344 self.compression_scale_for_worst_weight_stream = compression_ratio
345
346 def storage_elements(self):
347 elems = shape_num_elements(self.storage_shape)
348 if elems is None:
349 return 0
350 return elems
351
352 def elements(self):
353 elems = shape_num_elements(self.shape)
354 if elems is None:
355 return 0
356 return elems
357
358 def has_fully_defined_shape(self):
359 return shape_fully_defined(self.shape)
360
361 def storage_size(self):
362 raw_size = self.storage_elements() * self.element_size()
363 if raw_size == 0:
364 raw_size = 1 # force it to take up space
365 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
366 return rounded_size
367
368 def storage_size_for_sub_purpose(self, sub_purpose, param_a=None, param_b=None):
369 alt_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
370 elems = shape_num_elements(alt_shape)
371 if elems is None:
372 return 0
373 if sub_purpose == TensorSubPurpose.DoubleBuffer:
374 raw_size = elems * self.element_size() * self.compression_scale_for_worst_weight_stream
375 else:
376 raw_size = elems * self.element_size() * self.storage_compression_scale
377 rounded_size = numeric_util.round_up(numeric_util.round_up_to_int(raw_size), self.alignment)
378 return rounded_size
379
380 def storage_shape_for_sub_purpose(self, sub_purpose, param_a, param_b):
381 shp = list(self.storage_shape)
382 if sub_purpose == TensorSubPurpose.DoubleBuffer:
383 assert len(shp) >= 2
384 shp[-1] = min(shp[-1], param_a * 2)
385 elif sub_purpose == TensorSubPurpose.RollingBufferX:
386 assert len(shp) == 4
387 shp[0] = 1
388 shp[2] = min(shp[2], param_a)
389 elif sub_purpose == TensorSubPurpose.RollingBufferY:
390 assert len(shp) == 4
391 shp[0] = 1
392 shp[1] = min(shp[1], param_a)
393 elif sub_purpose == TensorSubPurpose.RollingBufferXY:
394 assert len(shp) == 4
395 shp[0] = 1
396 shp[2] = min(shp[2], param_a)
397 shp[1] = min(shp[1], param_b)
398 elif sub_purpose == TensorSubPurpose.Standard:
399 pass
400 else:
401 assert 0, "did not expect new sub purpose %s" % (sub_purpose,)
402 return shp
403
404 def set_new_sub_purpose(self, sub_purpose, param_a=None, param_b=None):
405 self.storage_shape = self.storage_shape_for_sub_purpose(sub_purpose, param_a, param_b)
406 self.sub_purpose = sub_purpose
407 if sub_purpose == TensorSubPurpose.DoubleBuffer:
408 self.storage_compression_scale = self.compression_scale_for_worst_weight_stream
409
410 def bandwidth(self):
411 elems = shape_num_elements(self.bandwidth_shape)
412 if elems is None:
413 return 0
414 return elems * self.element_size() * self.bandwidth_compression_scale
415
416 def consumers(self):
417 return self.consumer_list
418
419 def get_address_ranges_for_coordinates(self, start_coord, end_coord):
420 if self.sub_purpose in set(
421 (TensorSubPurpose.RollingBufferX, TensorSubPurpose.RollingBufferY, TensorSubPurpose.RollingBufferXY)
422 ):
423 # build dummy coordinates that cover the entire buffer
424 start_coord = [0] * len(start_coord)
425 end_coord = [min(self.storage_shape[i], self.shape[i]) for i in range(len(end_coord))]
426
427 start = self.address_for_coordinate(start_coord, is_top_box=False)
428 end = self.address_for_coordinate(end_coord, is_top_box=True)
429 return MemoryRangeSet(self.mem_area, start, end)
430
431 def addresses_for_rolling_buffer(self, start_coord, end_coord):
432 # returns ( box_height0, box_height1, box_width, [address_tl, address_tr, address_bl, address_br] )
433
434 if len(start_coord) < 4:
435 box_height0 = 1
436 box_width = 1
437
438 if len(start_coord) >= 2:
439 box_width = end_coord[-2] - start_coord[-2]
440
441 return box_height0, box_height0, box_width, [self.address_for_coordinate(start_coord), None, None, None]
442
443 crossing_y = numeric_util.round_up(start_coord[1] + 1, self.storage_shape[1])
444 crossing_x = numeric_util.round_up(start_coord[2] + 1, self.storage_shape[2])
445
446 crossing_y = min(crossing_y, end_coord[1])
447 crossing_x = min(crossing_x, end_coord[2])
448
449 box_height0 = crossing_y - start_coord[1]
450 box_width = crossing_x - start_coord[2]
451
452 addresses = [None] * 4
453 addresses[0] = self.address_for_coordinate(start_coord)
454
455 if end_coord[2] > crossing_x:
456 addresses[1] = self.address_for_coordinate([start_coord[0], start_coord[1], crossing_x, start_coord[3]])
457 raise Exception("Striping in vertical direction is not supported")
458 if end_coord[1] > crossing_y:
459 addresses[2] = self.address_for_coordinate([start_coord[0], crossing_y, start_coord[2], start_coord[3]])
460 if end_coord[1] > crossing_y and end_coord[2] > crossing_x:
461 addresses[3] = self.address_for_coordinate([start_coord[0], crossing_y, crossing_x, start_coord[3]])
462
463 return box_height0, box_height0, box_width, addresses
464
465 def address_for_coordinate(self, coord, is_top_box=False):
466 return self.address + self.address_offset_for_coordinate(coord, is_top_box)
467
468 def get_strides_and_coord(self, coord=None):
469 if coord is None:
470 coord = [0] * len(self.storage_shape)
471
472 augmented_coord = coord
473 augmented_shape = self.storage_shape
474 while len(augmented_shape) < 4:
475 augmented_shape = [1] + augmented_shape
476
477 while len(augmented_coord) < 4:
478 augmented_coord = [0] + augmented_coord
479
480 assert len(augmented_coord) == len(augmented_shape)
481
482 if self.format == TensorFormat.NHWC:
483 augmented_shape = [augmented_shape[0], augmented_shape[3]] + augmented_shape[1:3] + [1]
484 augmented_coord = [augmented_coord[0], augmented_coord[3]] + augmented_coord[1:3] + [0]
485 stride_order = [4, 1, 3, 2, 0]
486
487 elif self.format == TensorFormat.NHCWB16:
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200488 channel_divisor = 16
Tim Hall79d07d22020-04-27 18:20:16 +0100489 augmented_shape = augmented_shape[0:4] + [1]
490 augmented_coord = (
491 [augmented_coord[0], augmented_coord[3] // channel_divisor]
492 + augmented_coord[1:3]
493 + [augmented_coord[3] % channel_divisor]
494 )
495
496 if augmented_shape[1] == 0:
497 augmented_shape[1] = 1
498
499 else:
500 assert self.format in set((TensorFormat.Unknown, TensorFormat.WeightsCompressed))
501 return None, None
502
503 strides = [0] * len(augmented_shape)
504 stride = self.element_size() * self.storage_compression_scale
505
506 if self.format != TensorFormat.NHCWB16:
507 for i in stride_order:
508 strides[i] = stride
509 stride *= augmented_shape[i]
510 else:
511 assert len(strides) == 5
Tim Hall79d07d22020-04-27 18:20:16 +0100512 strides[4] = stride
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200513 strides[3] = 16 * stride # STRIDE_X
Tim Hall79d07d22020-04-27 18:20:16 +0100514 strides[1] = strides[3] * augmented_shape[2] # STRIDE_C
Patrik Gustavsson2213e902020-05-05 17:49:35 +0200515 strides[2] = augmented_shape[2] * augmented_shape[3] * stride # STRIDE_Y
Tim Hall79d07d22020-04-27 18:20:16 +0100516 strides[0] = strides[2] * augmented_shape[1] # STRIDE_N
517
518 return strides, augmented_coord
519
520 def get_strides(self):
521 strides, _ = self.get_strides_and_coord()
522
523 return strides
524
525 def compressed_stream_index_from_coord(self, coord):
526 assert self.format == TensorFormat.WeightsCompressed
527 assert len(self.compressed_values) > 0
528 assert len(self.compressed_values) + 1 == len(self.weight_compressed_offsets)
529
530 depth = coord[-1]
531 brick_depth = self.brick_size[-1]
532 # Clamp position at final element index
533 if depth > self.shape[-1]:
534 depth = self.shape[-1]
535
536 # Always round up to next boundary
537 index = round_up_divide(depth, brick_depth)
538
539 # Check boundaries on all but last weight set (which may be shorter
540 # than the brick we divided it up into)
541 if index < len(self.weight_compressed_offsets) - 1:
542 # There are no half-way points in the weights
543 if (depth % brick_depth) != 0:
544 raise Exception("Offset into weights must be aligned to a brick")
545
546 return index
547
548 def size_of_compressed_stream(self, index):
549 assert 0 <= index < len(self.compressed_values)
550 return len(self.compressed_values[index])
551
552 def is_last_index_in_compressed_stream(self, index):
553 assert 0 <= index < len(self.compressed_values)
554 return index == len(self.compressed_values) - 1
555
556 def address_offset_for_coordinate(self, orig_coord, is_top_box=False):
557 address_offset = 0
558 coord = orig_coord
559
560 coord = coord[-len(self.storage_shape) :]
561
562 if self.sub_purpose == TensorSubPurpose.Standard:
563 for idx, c in enumerate(coord):
564 if is_top_box:
565 assert c > 0 and c <= self.shape[idx]
566 else:
567 assert c >= 0 and c < self.shape[idx]
568
569 if self.format == TensorFormat.WeightsCompressed:
570 if len(self.weight_compressed_offsets) == 0:
571 return 0
572
573 if len(self.ops) == 1 and self.ops[0].type == "DMA" and self.sub_purpose == TensorSubPurpose.DoubleBuffer:
574 depth = orig_coord[-1]
575 brick_depth = self.brick_size[-1]
576 # Clamp position at final element index
577 if depth > self.shape[-1]:
578 depth = self.shape[-1]
579
580 # Always round up to next boundary
581 index = round_up_divide(depth, brick_depth)
582 index = index % 2
583
584 if len(self.compressed_values) <= 2:
585 if is_top_box and index == 0:
586 for cv in self.compressed_values:
587 address_offset += len(cv)
588 else:
589 address_offset = index * len(self.compressed_values[0])
590 else:
591 if is_top_box and index == 0:
592 address_offset = self.storage_shape[-1]
593 else:
594 address_offset = index * (self.storage_shape[-1] // 2)
595 else:
596 index = self.compressed_stream_index_from_coord(orig_coord)
597 assert index < len(self.weight_compressed_offsets)
598 address_offset = self.weight_compressed_offsets[index]
599 else:
600 if is_top_box:
601 coord = [c - 1 for c in coord]
602
603 # handle wraparound for partial buffers. make sure to do this after subtracting top box:
604 coord = [c % self.storage_shape[idx] for idx, c in enumerate(coord)]
605
606 strides, augmented_coord = self.get_strides_and_coord(coord)
607 if strides is None:
608 return None
609
610 if is_top_box:
611 address_offset += 1 * strides[-1] # one element
612
613 address_offset += np.dot(augmented_coord, strides)
614
615 assert address_offset >= 0
616 assert address_offset <= self.storage_size()
617 return address_offset
618
619 def __str__(self):
620 return "<nng.Tensor '%s' shape=%s dtype=%s>" % (self.name, self.shape, self.dtype)
621
622 __repr__ = __str__