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