Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 1 | # 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 Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 16 | # Description: |
| 17 | # Compresses and pads the weigths. It also calculates the scales and packs with the biases. |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 18 | import math |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 19 | from collections import namedtuple |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 20 | from typing import Tuple |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 21 | |
| 22 | import numpy as np |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 23 | |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 24 | from .api import NpuBlockTraversal |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 25 | from .architecture_features import Accelerator |
| 26 | from .architecture_features import ArchitectureFeatures |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 27 | from .data_type import DataType |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 28 | from .errors import UnsupportedFeatureError |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 29 | from .nn_graph import SchedulingStrategy |
| 30 | from .numeric_util import round_up |
Patrik Gustavsson | d89c09e | 2020-07-08 11:27:12 +0200 | [diff] [blame] | 31 | from .numeric_util import round_up_divide |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 32 | from .operation import NpuBlockType |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 33 | from .operation import Op |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 34 | from .scaling import quantise_scale |
| 35 | from .scaling import reduced_quantise_scale |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 36 | from .tensor import create_equivalence_id |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 37 | from .tensor import TensorBlockTraversal |
| 38 | from .tensor import TensorFormat |
| 39 | from .tensor import TensorPurpose |
| 40 | from .tensor import TensorSubPurpose |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 41 | from ethosu import mlw_codec |
Diego Russo | e8a1045 | 2020-04-21 17:39:10 +0100 | [diff] [blame] | 42 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 43 | |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 44 | # Contains meta info for a weight compression. If two tensors have identical weight compression config, |
| 45 | # then they also will have identical compressed weights. |
| 46 | WeightCompressionConfig = namedtuple( |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 47 | "WeightCompressionConfig", ["npu_block_type", "ofm_block_depth", "ofm_depth_step", "dilation", "value_id"] |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 51 | def encode_weights( |
| 52 | accelerator: Accelerator, |
| 53 | weights_volume: np.ndarray, |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 54 | dilation_xy: Tuple[int, int], |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 55 | ifm_bitdepth: int, |
| 56 | ofm_block_depth: int, |
| 57 | is_depthwise: bool, |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 58 | block_traversal: NpuBlockTraversal, |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 59 | ): |
| 60 | """ |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 61 | Internal implementation of the public facing API to use weight encoding. |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 62 | |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 63 | :param accelerator: architecture_features.Accelerator enum to pick the correct Ethos-U accelerator |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 64 | :param weights_volume: numpy.ndarray in OHWI layout with a shape of four |
| 65 | :param dilation_xy: a two element tuple of dilation attributes in x,y dimension |
| 66 | :param ifm_bitdepth: the bitdepth of input feature map |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 67 | :param ofm_block_depth: the depth of blocks for Ethos-U processing |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 68 | :param is_depthwise: a boolean indicating these weights are used for a depthwise traversal |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 69 | :param block_traversal: indicates how these weights are traversed on sub-kernel basis |
| 70 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 71 | :return: a bytearray of compressed weights |
| 72 | """ |
Manupa Karunaratne | 8b24f2b | 2020-08-12 18:26:39 +0000 | [diff] [blame] | 73 | # Check arg types |
| 74 | assert isinstance(accelerator, Accelerator) |
| 75 | assert isinstance(weights_volume, np.ndarray) |
| 76 | assert isinstance(dilation_xy, tuple) |
| 77 | assert isinstance(ifm_bitdepth, int) |
| 78 | assert isinstance(ofm_block_depth, int) |
| 79 | assert isinstance(is_depthwise, bool) |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 80 | assert isinstance(block_traversal, NpuBlockTraversal) |
Manupa Karunaratne | 8b24f2b | 2020-08-12 18:26:39 +0000 | [diff] [blame] | 81 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 82 | # Checks for weight layout |
| 83 | assert len(weights_volume.shape) == 4, "weights ndarray should have a shape of 4" |
| 84 | |
| 85 | # It cannot be both partkernel and depthwise |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 86 | assert not ( |
| 87 | is_depthwise and block_traversal == NpuBlockTraversal.PART_KERNEL_FIRST |
| 88 | ), "encode_weights :: partkernel and depthwise are mutually exclusive" |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 89 | |
| 90 | # Check valid values for dilation |
| 91 | assert dilation_xy[0] in (1, 2), "encode_weights :: dilation x should be 1 or 2 not {}".format(dilation_xy[0]) |
| 92 | assert dilation_xy[1] in (1, 2), "encode_weights :: dilation y should be 1 or 2 not {}".format(dilation_xy[1]) |
| 93 | |
| 94 | ifm_ublock = ArchitectureFeatures.accelerator_configs[accelerator].ifm_ublock |
| 95 | ofm_ublock = ArchitectureFeatures.accelerator_configs[accelerator].ofm_ublock |
| 96 | raw_stream = generate_brick( |
| 97 | ifm_ublock=ifm_ublock, |
| 98 | ofm_ublock=ofm_ublock, |
| 99 | brick_weights=weights_volume, |
| 100 | ofm_block_depth=ofm_block_depth, |
| 101 | is_depthwise=is_depthwise, |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 102 | is_partkernel=block_traversal == NpuBlockTraversal.PART_KERNEL_FIRST, |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 103 | ifm_bitdepth=ifm_bitdepth, |
| 104 | dilation=dilation_xy, |
| 105 | ) |
| 106 | encoded_stream = encode(raw_stream) |
| 107 | return encoded_stream |
| 108 | |
| 109 | |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 110 | def encode_bias(bias: np.int64, scale: int, shift: int): |
| 111 | """ |
Louis Verhaard | aeae567 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 112 | Internal implementation of public facing API to pack bias and scale values as required by the Ethos-U |
Tim Hall | c8a7386 | 2020-10-27 12:43:14 +0000 | [diff] [blame] | 113 | |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 114 | :param bias: 64bit signed number that includes 40bit signed bias |
| 115 | :param scale: 32bit scale value |
| 116 | :param shift: 6bit shift value |
| 117 | :return: packed 80bit [0(2-bits),shift(6-bits),scale(32-bits),bias(40-bits)] |
| 118 | """ |
Manupa Karunaratne | 8b24f2b | 2020-08-12 18:26:39 +0000 | [diff] [blame] | 119 | # Check arg types |
| 120 | assert isinstance(bias, np.int64) |
| 121 | assert isinstance(scale, int) |
| 122 | assert isinstance(shift, int) |
| 123 | |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 124 | assert -(1 << (40 - 1)) <= bias < (1 << (40 - 1)) # signed 40-bit range |
| 125 | assert 0 <= scale < (1 << 32) # unsigned 32-bit range |
| 126 | assert 0 <= shift < (1 << 6) # unsigned 6-bit range |
| 127 | |
| 128 | data = bytearray(10) |
| 129 | data[0] = (bias >> (0 * 8)) & 0xFF |
| 130 | data[1] = (bias >> (1 * 8)) & 0xFF |
| 131 | data[2] = (bias >> (2 * 8)) & 0xFF |
| 132 | data[3] = (bias >> (3 * 8)) & 0xFF |
| 133 | data[4] = (bias >> (4 * 8)) & 0xFF |
| 134 | data[5] = (scale >> (0 * 8)) & 0xFF |
| 135 | data[6] = (scale >> (1 * 8)) & 0xFF |
| 136 | data[7] = (scale >> (2 * 8)) & 0xFF |
| 137 | data[8] = (scale >> (3 * 8)) & 0xFF |
| 138 | data[9] = shift & 0x3F |
| 139 | return data |
| 140 | |
| 141 | |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 142 | def create_weight_compression_config(tens, npu_block_type, ofm_block_depth, ofm_depth_step, dilation): |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 143 | # Note: for an ofm block only its depth is used in weight compression. |
| 144 | # And block depth > ofm depth gives same result as block depth == ofm depth |
| 145 | block_depth = min(ofm_block_depth, tens.quant_values.shape[-1]) |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 146 | return WeightCompressionConfig(npu_block_type, block_depth, ofm_depth_step, dilation, tens.value_id) |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 147 | |
| 148 | |
| 149 | def set_storage_shape(tens): |
| 150 | # Sets the storage shape depending on the tensor's sub purpose |
| 151 | if tens.sub_purpose == TensorSubPurpose.DoubleBuffer and len(tens.compressed_values) > 2: |
| 152 | offset = 2 * np.amax([len(x) for x in tens.compressed_values]) |
| 153 | assert offset % 16 == 0 |
| 154 | else: |
| 155 | offset = tens.weight_compressed_offsets[-1] |
| 156 | tens.storage_shape = [1, 1, 1, offset] |
| 157 | |
| 158 | |
| 159 | class CompressedWeightCache: |
| 160 | # Contains weight compressions for all weight tensors in a graph |
| 161 | def __init__(self): |
| 162 | self.cache = {} # maps from WeightCompressionConfig to a tensor clone containing compressed weights |
| 163 | |
| 164 | def get_tensor_with_same_compression(self, wcc): |
| 165 | return self.cache.get(wcc) |
| 166 | |
| 167 | def add(self, tens): |
| 168 | # Adds the compressed weights from the tensor to the cache |
| 169 | wcc = tens.weight_compression_config |
| 170 | # Clone the tensor to make sure that nothing related to the weight compression is modified |
| 171 | tens_clone = tens.clone("_weights{}_{}".format(wcc.ofm_block_depth, wcc.ofm_depth_step)) |
| 172 | self.cache[wcc] = tens_clone |
| 173 | |
| 174 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 175 | def encode(weight_stream): |
Patrik Gustavsson | 5ff9944 | 2020-07-10 10:12:17 +0200 | [diff] [blame] | 176 | if len(weight_stream) == 0: |
| 177 | return [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 178 | assert np.amin(weight_stream) >= -255 |
| 179 | assert np.amax(weight_stream) <= 255 |
| 180 | |
| 181 | # Encode flattened signed weight stream |
| 182 | compressed = mlw_codec.encode(weight_stream) |
| 183 | |
| 184 | # pad with 0xFF as needed so the length of the weight stream |
| 185 | # is a multiple of 16 |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 186 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 187 | while (len(compressed) % 16) != 0: |
| 188 | compressed.append(0xFF) |
| 189 | |
| 190 | return compressed |
| 191 | |
| 192 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 193 | def generate_brick( |
| 194 | ifm_ublock, ofm_ublock, brick_weights, ofm_block_depth, is_depthwise, is_partkernel, ifm_bitdepth, dilation |
| 195 | ): |
| 196 | |
| 197 | decomp_h = ArchitectureFeatures.SubKernelMax.height // dilation[0] |
| 198 | decomp_w = ArchitectureFeatures.SubKernelMax.width // dilation[1] |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 199 | # Expect weights formatted OHWI |
| 200 | ofm_depth = brick_weights.shape[-4] |
| 201 | ifm_depth = brick_weights.shape[-1] |
| 202 | kernel_width = brick_weights.shape[-2] |
| 203 | kernel_height = brick_weights.shape[-3] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 204 | # IFM block depth |
| 205 | if is_partkernel or (ifm_bitdepth == 16): |
| 206 | # IFM block depth is always 16 for part-kernel-first |
| 207 | ifm_block_depth = 16 |
| 208 | elif ifm_bitdepth == 8: |
| 209 | ifm_block_depth = 32 |
| 210 | else: |
| 211 | assert False |
| 212 | |
| 213 | stream = [] |
| 214 | |
| 215 | # Top level striping - OFM blocks in the entire brick's depth |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 216 | for ofm_block_z in range(0, ofm_depth, ofm_block_depth): |
| 217 | clipped_ofm_block_depth = min(ofm_block_depth, ofm_depth - ofm_block_z) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 218 | # IFM blocks required for the brick |
| 219 | for ifm_block_z in range(0, (1 if is_depthwise else ifm_depth), ifm_block_depth): |
| 220 | if is_depthwise: |
| 221 | clipped_ifm_block_depth = ifm_ublock.depth |
| 222 | else: |
| 223 | clipped_ifm_block_depth = ( |
| 224 | min(ifm_block_depth, ifm_depth - ifm_block_z) if is_partkernel else ifm_block_depth |
| 225 | ) |
| 226 | # Weight decomposition |
| 227 | # Subkernel Splitting (H) |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 228 | for subkernel_y in range(0, kernel_height, decomp_h): |
| 229 | sub_height = min(kernel_height - subkernel_y, decomp_h) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 230 | # Subkernel splitting (W) |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 231 | for subkernel_x in range(0, kernel_width, decomp_w): |
| 232 | sub_width = min(kernel_width - subkernel_x, decomp_w) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 233 | subkernel_elements = sub_width * sub_height |
| 234 | # Part kernel first works across the kernel H/W and needs padding |
| 235 | if is_partkernel: |
| 236 | if ifm_bitdepth == 16 and subkernel_elements % 2 != 0: |
| 237 | subkernel_elements = int(math.ceil(subkernel_elements / 2) * 2) |
| 238 | elif ifm_bitdepth == 8 and subkernel_elements % 4 != 0: |
| 239 | subkernel_elements = int(math.ceil(subkernel_elements / 4) * 4) |
| 240 | |
| 241 | # Depthwise Conv requires multiple of 4 kernel elements in its weight block |
| 242 | # this is different from normal conv which is considered "weights depth-first" |
| 243 | elif is_depthwise: |
| 244 | subkernel_elements = int(math.ceil(subkernel_elements / 4.0) * 4) |
| 245 | |
| 246 | ifm_block_depth_outer = clipped_ifm_block_depth if is_partkernel else 1 |
| 247 | ifm_block_depth_inner = 1 if is_partkernel else clipped_ifm_block_depth |
| 248 | # IFM Ublocks in IFM-block over depth for part-kernel-first mode |
| 249 | # For depth-first IFM Ublocks are traversed after subkernel elements so this loop is ignored. |
| 250 | for ifm_ublk_outer in range(0, ifm_block_depth_outer, ifm_ublock.depth): |
| 251 | # OFM Ublocks in OFM-block over depth |
| 252 | for ofm_ublk in range(0, clipped_ofm_block_depth, ofm_ublock.depth): |
| 253 | # HW Kernel element traversal - cannot be a H/W loop due to element |
| 254 | # padding requirement on depthwise/part-kernel configurations |
| 255 | for element in range(subkernel_elements): |
| 256 | kx = element % sub_width |
| 257 | ky = element // sub_width |
| 258 | # IFM Ublocks in IFM-block over depth (only 1 ublock if depthwise) |
| 259 | # In case of part-kernel-first IFM Ublock traversal have already been handled |
| 260 | # and this loop is ignored. |
| 261 | for ifm_ublk_inner in range(0, ifm_block_depth_inner, ifm_ublock.depth): |
| 262 | # Feed OFM ublock elements |
| 263 | for ofm_ublock_z in range(ofm_ublock.depth): |
| 264 | # Source IFM ublock elements (only 1 element deep if depthwise) |
| 265 | for ifm_ublock_z in range(1 if is_depthwise else ifm_ublock.depth): |
| 266 | # Source position within the current subkernel |
| 267 | wx = subkernel_x + kx |
| 268 | wy = subkernel_y + ky |
| 269 | # Source IFM/OFM slices |
| 270 | ifm_ublk = ifm_ublk_inner + ifm_ublk_outer |
| 271 | ifm_z = ifm_block_z + ifm_ublk + ifm_ublock_z |
| 272 | ofm_z = ofm_block_z + ofm_ublk + ofm_ublock_z |
| 273 | if (ifm_z >= ifm_depth) or (ofm_z >= ofm_depth) or (ky >= sub_height): |
| 274 | stream.append(0) |
| 275 | else: |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 276 | stream.append(brick_weights[ofm_z][wy][wx][ifm_z]) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 277 | return stream |
| 278 | |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 279 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 280 | def core_deinterleave(hwio, core, ncores): |
| 281 | # Put weights back into OHWI |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 282 | ohwi = np.transpose(hwio, (3, 0, 1, 2)) |
| 283 | return ohwi[core : ohwi.shape[0] : ncores] |
| 284 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 285 | |
| 286 | # Compress the weights |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 287 | def compress_weights(arch, nng, tens, npu_block_type, ofm_block_depth, ofm_depth_step, dilation): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 288 | assert tens.purpose == TensorPurpose.Weights |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 289 | |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 290 | # Check the weight cache |
| 291 | if nng.weight_cache is None: |
| 292 | nng.weight_cache = CompressedWeightCache() |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 293 | wcc = create_weight_compression_config(tens, npu_block_type, ofm_block_depth, ofm_depth_step, dilation) |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 294 | tens.weight_compression_config = wcc |
Louis Verhaard | 9db529a | 2020-09-23 10:27:11 +0200 | [diff] [blame] | 295 | # Reassign equivalence id such that tensors with same weight compression get identical equivalence ids, |
| 296 | # but tensors with the same values but different compression get different equivalence ids |
| 297 | tens.equivalence_id = create_equivalence_id(wcc) |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 298 | tens_cached = nng.weight_cache.get_tensor_with_same_compression(wcc) |
| 299 | if tens_cached is not None: |
| 300 | # Cache hit, copy weights from the cache |
| 301 | tens.copy_compressed_weight_info(tens_cached) |
| 302 | set_storage_shape(tens) |
| 303 | return |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 304 | # No cache hit, perform the compression |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 305 | assert tens.quantization is not None |
| 306 | assert tens.quantization.scale_f32 is not None |
| 307 | assert tens.quantization.zero_point is not None |
| 308 | |
| 309 | zero_point = tens.quantization.zero_point |
| 310 | quant_buf = tens.quant_values.astype(np.int64) |
| 311 | |
| 312 | # Early zero-point correction |
| 313 | weights = quant_buf - zero_point |
| 314 | |
| 315 | if len(weights.shape) == 2: |
| 316 | weights = np.expand_dims(np.expand_dims(weights, axis=0), axis=0) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 317 | |
| 318 | compression_scales = [] |
| 319 | compressed_offsets = [] |
| 320 | encoded_streams = [] |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 321 | encoded_streams_substream_offsets = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 322 | offset = 0 |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 323 | max_single_buffer_len = 0 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 324 | |
| 325 | ifm_bitdepth = tens.consumer_list[0].inputs[0].dtype.size_in_bits() |
| 326 | ifm_depth = weights.shape[-2] |
| 327 | if npu_block_type == NpuBlockType.ConvolutionDepthWise: |
| 328 | tens.block_traversal = TensorBlockTraversal.DepthWise |
| 329 | if npu_block_type == NpuBlockType.ConvolutionMxN: |
| 330 | # Determine which block traversal strategy has better DPU utilization |
Jacob Bohlin | de2a57f | 2020-08-10 15:21:42 +0200 | [diff] [blame] | 331 | kernel_size = weights.shape[0] * weights.shape[1] |
| 332 | depth_utilization = weights.shape[2] / round_up(weights.shape[2], 32 if ifm_bitdepth == 8 else 16) |
| 333 | part_kernel_utilization = (weights.shape[2] / round_up(weights.shape[2], 8)) * ( |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 334 | kernel_size / round_up(kernel_size, 4 if ifm_bitdepth == 8 else 2) |
| 335 | ) |
| 336 | if part_kernel_utilization >= depth_utilization or ifm_depth <= 8: |
| 337 | # Part-kernel first is always better for ifm depths <= 8 |
| 338 | tens.block_traversal = TensorBlockTraversal.PartKernelFirst |
| 339 | else: |
| 340 | tens.block_traversal = TensorBlockTraversal.DepthFirst |
| 341 | |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 342 | is_depthwise = tens.block_traversal == TensorBlockTraversal.DepthWise |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 343 | if tens.block_traversal == TensorBlockTraversal.PartKernelFirst: |
| 344 | block_traversal = NpuBlockTraversal.PART_KERNEL_FIRST |
| 345 | else: |
| 346 | block_traversal = NpuBlockTraversal.DEPTH_FIRST |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 347 | |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 348 | if tens.consumer_list[0].type == Op.Conv2DBackpropInputSwitchedBias: |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 349 | # Transpose Convoluion, reverse weights in H and W axes |
Tim Hall | c30f495 | 2020-06-15 20:47:35 +0100 | [diff] [blame] | 350 | weights = np.flip(weights, axis=(0, 1)) |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 351 | |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 352 | # Calculate brick size |
Jacob Bohlin | de2a57f | 2020-08-10 15:21:42 +0200 | [diff] [blame] | 353 | brick_size = (weights.shape[0], weights.shape[1], weights.shape[2], min(tens.shape[-1], ofm_depth_step)) |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 354 | elements_in_brick = np.prod(brick_size) |
| 355 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 356 | # Slice weight stream up depth-ways into bricks and compress |
| 357 | full_ofm_depth = quant_buf.shape[-1] |
| 358 | for idx in range(0, full_ofm_depth, ofm_depth_step): |
| 359 | # Get the weights necessary for this brick |
| 360 | count = min(full_ofm_depth - idx, ofm_depth_step) |
| 361 | brick_weights = weights[:, :, :, idx : idx + count] |
| 362 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 363 | substream_offsets = [0] |
| 364 | encoded_stream = [] |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 365 | |
| 366 | # For each core, deinterleave weights from the larger volume |
| 367 | # and generate separate compressed streams. |
| 368 | for core in range(0, min(arch.ncores, full_ofm_depth)): |
| 369 | core_weights = core_deinterleave(brick_weights, core, arch.ncores) |
Tim Hall | 6231676 | 2020-06-25 16:55:02 +0100 | [diff] [blame] | 370 | |
| 371 | block_depth = (ofm_block_depth + arch.ncores - 1 - core) // arch.ncores |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 372 | encoded_substream = [] |
Tim Hall | 6231676 | 2020-06-25 16:55:02 +0100 | [diff] [blame] | 373 | if block_depth != 0: |
Manupa Karunaratne | d83d2e1 | 2020-07-20 12:05:32 +0100 | [diff] [blame] | 374 | encoded_substream = encode_weights( |
| 375 | accelerator=arch.accelerator_config, |
| 376 | weights_volume=core_weights, |
| 377 | dilation_xy=dilation, |
| 378 | ifm_bitdepth=ifm_bitdepth, |
| 379 | ofm_block_depth=block_depth, |
| 380 | is_depthwise=is_depthwise, |
Louis Verhaard | e8a5a78 | 2020-11-02 18:04:27 +0100 | [diff] [blame] | 381 | block_traversal=block_traversal, |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 382 | ) |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 383 | encoded_stream.extend(encoded_substream) |
| 384 | substream_offsets.append(len(encoded_stream)) |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 385 | |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 386 | encoded_streams.append(encoded_stream) |
| 387 | encoded_streams_substream_offsets.append(substream_offsets) |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 388 | |
| 389 | # Remember maximum encoded length for DoubleBuffering |
| 390 | max_single_buffer_len = max(max_single_buffer_len, len(encoded_stream)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 391 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 392 | # Remember where we put it for linear addressing |
| 393 | compressed_offsets.append(offset) |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 394 | offset += len(encoded_stream) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 395 | assert offset % 16 == 0 |
| 396 | |
| 397 | # Compression scale tracking |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 398 | compression_scales.append(len(encoded_stream) / elements_in_brick) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 399 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 400 | # Track total length as last element of the offsets array |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 401 | compressed_offsets.append(offset) |
| 402 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 403 | tens.weight_compression_scales = compression_scales |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 404 | tens.weight_compressed_offsets = compressed_offsets |
| 405 | tens.compression_scale_for_worst_weight_stream = np.amax(compression_scales) |
| 406 | tens.storage_compression_scale = tens.bandwidth_compression_scale = np.average(compression_scales) |
| 407 | tens.compressed_values = encoded_streams |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 408 | tens.compressed_values_substream_offsets = encoded_streams_substream_offsets |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 409 | tens.brick_size = brick_size |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 410 | set_storage_shape(tens) |
| 411 | nng.weight_cache.add(tens) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 412 | |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 413 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 414 | def calc_scales_and_pack_biases(tens, arch, ofm_depth_step, rescale_for_faf=False): |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 415 | assert tens.purpose in [TensorPurpose.FeatureMap, TensorPurpose.FSBias] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 416 | assert tens.format == TensorFormat.NHWC |
| 417 | # the connected operator should expect a bias input unless it is a FullyConnected |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 418 | assert tens.consumer_list[0].type.needs_bias() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 419 | # the input bias tensor is the same as that connected to the operator |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 420 | bias_tens = tens.consumer_list[0].bias |
Jacob Bohlin | cf7da10 | 2020-05-20 09:03:40 +0200 | [diff] [blame] | 421 | assert tens is bias_tens |
| 422 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 423 | # the operator should only have a single output |
| 424 | assert len(tens.consumer_list[0].outputs) == 1 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 425 | biases = tens.quant_values |
| 426 | |
| 427 | first_consumer_op = tens.consumer_list[0] |
| 428 | ifm_dtype = first_consumer_op.inputs[0].dtype |
| 429 | ifm_scale = first_consumer_op.inputs[0].quantization.scale_f32 |
Louis Verhaard | 98a3499 | 2020-09-01 10:39:04 +0200 | [diff] [blame] | 430 | ofm_scale = first_consumer_op.get_output_quantization().scale_f32 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 431 | weight_scales = first_consumer_op.inputs[1].quantization.scale_f32 |
| 432 | |
| 433 | # biases can have multiple consumers for rnn cells. if so, then check that they are all the same |
| 434 | for op in tens.consumer_list[1:]: |
| 435 | assert ifm_scale == op.inputs[0].quantization.scale_f32 |
Louis Verhaard | 98a3499 | 2020-09-01 10:39:04 +0200 | [diff] [blame] | 436 | assert ofm_scale == op.get_output_quantization().scale_f32 |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 437 | assert weight_scales == op.inputs[1].quantization.scale_f32 |
| 438 | |
| 439 | if not hasattr(weight_scales, "__iter__"): |
| 440 | # If weight_scales is not already an iterable make it into a list |
| 441 | weight_scales = [weight_scales] |
| 442 | |
| 443 | # Convert scales to np.double (from np.float32) to conform to TensorFlow Lite which |
| 444 | # uses double during scaling calculations |
| 445 | # TensorFlow Lite casts the scales slightly differently for uint8 and int8 |
| 446 | if not rescale_for_faf: |
| 447 | if ifm_dtype == DataType.uint8: |
| 448 | scales = [np.double(ifm_scale * weight_scale) / np.double(ofm_scale) for weight_scale in weight_scales] |
Fredrik Svedberg | d67c0aa | 2020-03-30 13:15:28 +0200 | [diff] [blame] | 449 | elif ifm_dtype == DataType.int8 or ifm_dtype == DataType.int16: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 450 | scales = [ |
| 451 | (np.double(ifm_scale) * np.double(weight_scale)) / np.double(ofm_scale) |
| 452 | for weight_scale in weight_scales |
| 453 | ] |
| 454 | else: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 455 | raise UnsupportedFeatureError(f"Compression of {ifm_dtype} is not implemented; Tensor: '{tens.name}'") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 456 | else: |
| 457 | if ifm_dtype == DataType.uint8: |
| 458 | scales = [np.double(ifm_scale * weight_scale * 0x3000) for weight_scale in weight_scales] |
Fredrik Svedberg | d67c0aa | 2020-03-30 13:15:28 +0200 | [diff] [blame] | 459 | elif ifm_dtype == DataType.int8 or ifm_dtype == DataType.int16: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 460 | scales = [(np.double(ifm_scale * 0x3000) * np.double(weight_scale)) for weight_scale in weight_scales] |
| 461 | else: |
Michael McGeagh | 7a6f843 | 2020-12-02 15:29:22 +0000 | [diff] [blame] | 462 | raise UnsupportedFeatureError(f"Compression of {ifm_dtype} is not implemented; Tensor: '{tens.name}'") |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 463 | |
| 464 | # quantise all of the weight scales into (scale_factor, shift) |
Fredrik Svedberg | d67c0aa | 2020-03-30 13:15:28 +0200 | [diff] [blame] | 465 | if ifm_dtype == DataType.int16: |
| 466 | quantised_scales = [reduced_quantise_scale(scale) for scale in scales] |
| 467 | else: |
| 468 | quantised_scales = [quantise_scale(scale) for scale in scales] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 469 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 470 | # pack the biases and scales |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 471 | if len(quantised_scales) == 1: |
| 472 | # If only 1 quantised scale is used, repeat that value for the length of the biases |
| 473 | quantised_scales = [quantised_scales[0]] * len(biases) |
| 474 | |
| 475 | assert len(quantised_scales) == len(biases) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 476 | tens.element_size_bytes = 10 |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 477 | tens.compressed_values = [] |
| 478 | tens.compressed_values_substream_offsets = [] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 479 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 480 | total_elements = len(quantised_scales) |
Patrik Gustavsson | d89c09e | 2020-07-08 11:27:12 +0200 | [diff] [blame] | 481 | alignment_bytes = 0 |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 482 | for i in range(0, total_elements, ofm_depth_step): |
| 483 | # Extract streams from brick to generate substreams for each core |
| 484 | stream = bytearray() |
| 485 | substream_offsets = [0] |
| 486 | max_len = min(ofm_depth_step, total_elements - i) |
| 487 | for core in range(0, min(arch.ncores, max_len)): |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 488 | core_scales = quantised_scales[i + core : i + core + max_len : arch.ncores] |
| 489 | core_biases = biases[i + core : i + core + max_len : arch.ncores] |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 490 | for j, core_bias in enumerate(core_biases): |
Manupa Karunaratne | bef228b | 2020-07-29 18:06:28 +0100 | [diff] [blame] | 491 | stream.extend(encode_bias(np.int64(core_bias), *core_scales[j])) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 492 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 493 | # Align to 16 for start for next substream |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 494 | remainder = (len(stream)) % 16 |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 495 | if remainder > 0: |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 496 | stream.extend(bytearray(16 - remainder)) |
Patrik Gustavsson | d89c09e | 2020-07-08 11:27:12 +0200 | [diff] [blame] | 497 | alignment_bytes += 16 - remainder |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 498 | |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 499 | substream_offsets.append(len(stream)) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 500 | |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 501 | # Add to compressed values with their substream offset lists to the tensor |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 502 | tens.compressed_values.append(stream) |
| 503 | tens.compressed_values_substream_offsets.append(substream_offsets) |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 504 | |
Patrik Gustavsson | d89c09e | 2020-07-08 11:27:12 +0200 | [diff] [blame] | 505 | tens.storage_shape = [total_elements + round_up_divide(alignment_bytes, tens.element_size_bytes)] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 506 | |
Jacob Bohlin | e843d33 | 2020-06-23 12:12:56 +0200 | [diff] [blame] | 507 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 508 | def update_pass_weight_and_scale_tensors(nng, arch): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 509 | for sg in nng.subgraphs: |
| 510 | for ps in sg.passes: |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 511 | tens = ps.weight_tensor |
| 512 | if tens is not None: |
Louis Verhaard | b2fb212 | 2020-06-04 15:51:24 +0200 | [diff] [blame] | 513 | op = tens.find_npu_op() |
Dwight Lidman | 940fdee | 2020-08-13 13:11:48 +0200 | [diff] [blame] | 514 | if op is None: |
| 515 | continue |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 516 | needs_dma = tens.needs_dma() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 517 | if ps.cascade.strategy == SchedulingStrategy.WeightStream and needs_dma: |
| 518 | ofm_depth_step = ps.block_config[-1] |
| 519 | else: |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 520 | ofm_depth_step = tens.shape[-1] |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 521 | compress_weights( |
Louis Verhaard | aee5d75 | 2020-09-30 09:01:52 +0200 | [diff] [blame] | 522 | arch, nng, tens, op.type.npu_block_type, ps.block_config[-1], ofm_depth_step, op.get_dilation_h_w() |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 523 | ) |
Diqing Zhong | 66d7ec0 | 2021-02-01 19:07:04 +0100 | [diff] [blame] | 524 | nng.total_compressed_weights += tens.weight_compressed_offsets[-1] |
| 525 | nng.total_original_weights += tens.elements() * tens.element_size() |
| 526 | |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 527 | # Update source tensor |
Louis Verhaard | 3c07c97 | 2020-05-07 08:12:58 +0200 | [diff] [blame] | 528 | if needs_dma: |
| 529 | src_tens = tens.get_dma_src_tensor() |
| 530 | src_tens.shape = tens.shape |
| 531 | src_tens.quant_values = tens.quant_values |
| 532 | src_tens.copy_compressed_weight_info(tens) |
| 533 | set_storage_shape(src_tens) |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 534 | |
Diego Russo | ea6111a | 2020-04-14 18:41:58 +0100 | [diff] [blame] | 535 | if ps.scale_tensor is not None: |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 536 | rescale_for_faf = False |
Michael McGeagh | f3e3ad7 | 2020-12-02 12:39:03 +0000 | [diff] [blame] | 537 | if (ps.ops[-1].type in (Op.Sigmoid, Op.Tanh)) and (ps.npu_block_type != NpuBlockType.ElementWise): |
Tim Hall | 79d07d2 | 2020-04-27 18:20:16 +0100 | [diff] [blame] | 538 | rescale_for_faf = True |
Tim Hall | f7e810a | 2020-06-25 15:04:31 +0100 | [diff] [blame] | 539 | calc_scales_and_pack_biases(ps.scale_tensor, arch, ofm_depth_step, rescale_for_faf) |
Andreas Nevalainen | 897cc14 | 2020-10-28 15:42:08 +0100 | [diff] [blame] | 540 | if ps.scale_tensor.ops[0].type == Op.DMA: |
| 541 | src_tens = ps.scale_tensor.get_dma_src_tensor() |
| 542 | src_tens.shape = ps.scale_tensor.shape |
| 543 | src_tens.quant_values = ps.scale_tensor.quant_values |
| 544 | src_tens.element_size_bytes = ps.scale_tensor.element_size_bytes |
| 545 | src_tens.copy_compressed_weight_info(ps.scale_tensor) |