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