|
| 1 | +# Luka Macan <luka.macan@unibo.it> |
| 2 | +# Arpan Suravi Prasad <prasadar@iis.ee.ethz.ch> |
| 3 | +# |
| 4 | +# Copyright 2023 ETH Zurich and University of Bologna |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# SPDX-License-Identifier: Apache-2.0 |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import numpy.typing as npt |
| 22 | + |
| 23 | + |
| 24 | +class NeurekaMemoryLayoutSiracusa: |
| 25 | + _WEIGHT_BANDWIDTH = 256 |
| 26 | + _CIN_SUBTILE_1x1 = 32 |
| 27 | + _CIN_SUBTILE_3x3 = 28 |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def weightEncode( |
| 31 | + weight: npt.NDArray[np.uint8], bits: int, depthwise: bool = False |
| 32 | + ) -> npt.NDArray[np.uint8]: |
| 33 | + """Unroll weight into expected memory format |
| 34 | +
|
| 35 | + Expected weight shape is (cout, cin, H, W). |
| 36 | + The produced memory layout depends on the weight kernel shape: |
| 37 | + - 3x3: (cout, cinMajor, Bits, H x W x cinMinor_3x3 packed into Weight Bandwidth bits), |
| 38 | + - 1x1: (cout, cinMajor, Bits x H x W x cinMinor_1x1 packed into Weight Bandwidth bits), |
| 39 | + where cinMajor is the ceil(cin / cin subtile <mode>) and cinMinor has to be padded with 0 to cin subtile <mode>. |
| 40 | + """ |
| 41 | + if depthwise: |
| 42 | + weight = weight.transpose(1, 0, 2, 3) # Swap cout and cin |
| 43 | + |
| 44 | + cout, cin, height, width = weight.shape |
| 45 | + cinSubtile = ( |
| 46 | + NeurekaMemoryLayoutSiracusa._CIN_SUBTILE_3x3 |
| 47 | + if height == 3 |
| 48 | + else NeurekaMemoryLayoutSiracusa._CIN_SUBTILE_1x1 |
| 49 | + ) |
| 50 | + |
| 51 | + # Pad cin to be divisible with CIN_SUBTILE |
| 52 | + if cin % cinSubtile != 0: |
| 53 | + cinPad = cinSubtile - cin % cinSubtile |
| 54 | + weight = np.pad( |
| 55 | + weight, |
| 56 | + ((0, 0), (0, cinPad), (0, 0), (0, 0)), |
| 57 | + "constant", |
| 58 | + constant_values=0, |
| 59 | + ) |
| 60 | + |
| 61 | + # Reshape into (cout, cinMajor, cinMinor, Flattened spatial, 1) |
| 62 | + # The 1 at the end is required by the unpacking |
| 63 | + cinMajor = int(np.ceil(cin / cinSubtile)) |
| 64 | + weight = weight.reshape(cout, cinMajor, cinSubtile, height * width, 1) |
| 65 | + |
| 66 | + # Unpack 'bits' bits in little order, e.g. bits=4: 3 => [1, 1, 0, 0] |
| 67 | + # (cout, cinMajor, cinSubtile, Flattened spatial, Bits) |
| 68 | + weight = np.unpackbits(weight, axis=-1, count=bits, bitorder="little") |
| 69 | + |
| 70 | + # Shuffle bits so that the final shape is: |
| 71 | + # (cout, cinMajor, Bits, Flattened spatial, cinSubtile) |
| 72 | + weight = weight.transpose(0, 1, 4, 3, 2) |
| 73 | + |
| 74 | + # Pack dimensions to fit into weight bandwidth |
| 75 | + if height == 3 and width == 3: |
| 76 | + # (cout * cinMajor * Bits, H * W * cinSubtile) |
| 77 | + weight = weight.reshape(-1, height * width * cinSubtile) |
| 78 | + # Pad only the last dimension to weight bandwidth size |
| 79 | + # (-1, Weight Bandwidth) |
| 80 | + weight = np.pad( |
| 81 | + weight, |
| 82 | + ((0, 0), (0, NeurekaMemoryLayoutSiracusa._WEIGHT_BANDWIDTH - weight.shape[-1])), |
| 83 | + "constant", |
| 84 | + constant_values=0, |
| 85 | + ) |
| 86 | + elif height == 1 and width == 1: |
| 87 | + # Tile cinSubtile into tiles of size 4 |
| 88 | + # (cout, cinMajor, Bits, Flattened spatial, cinSubtileMajor, cinSubtileTile) |
| 89 | + weight = weight.reshape( |
| 90 | + cout, cinMajor, bits, height * width, cinSubtile // 4, 4 |
| 91 | + ) # cout, cinMajor, bits, 1, 8, 4 |
| 92 | + # Pad bits to 8 |
| 93 | + if bits < 8: |
| 94 | + # (cout, cinMajor, PaddedBits, Flattened spatial, cinSubtileMajor, cinSubtileTile) |
| 95 | + weight = np.pad( |
| 96 | + weight, |
| 97 | + ((0, 0), (0, 0), (0, 8 - bits), (0, 0), (0, 0), (0, 0)), |
| 98 | + mode="constant", |
| 99 | + constant_values=0, |
| 100 | + ) |
| 101 | + # (cout, cinMajor, Flattened spatial, cinSubtileMajor, PaddedBits, cinSubtileTile) |
| 102 | + weight = weight.transpose(0, 1, 3, 4, 2, 5) |
| 103 | + # (-1, Weight Bandwidth) |
| 104 | + weight = weight.reshape( |
| 105 | + cout * cinMajor, NeurekaMemoryLayoutSiracusa._WEIGHT_BANDWIDTH |
| 106 | + ) # cout*cinMajor, 256b |
| 107 | + |
| 108 | + # Prepare for packing |
| 109 | + # (-1, Weight Bandwidth Bytes, 8) |
| 110 | + weightBandwidthBytes = int(np.ceil(NeurekaMemoryLayoutSiracusa._WEIGHT_BANDWIDTH / 8)) |
| 111 | + weight = np.stack(np.split(weight, weightBandwidthBytes, axis=-1), axis=-2) |
| 112 | + |
| 113 | + # Pack bits |
| 114 | + # (-1, Weight Bandwidth Bytes) |
| 115 | + weight = np.packbits(weight, axis=-1, bitorder="little") |
| 116 | + |
| 117 | + return weight.flatten() |
| 118 | + |
| 119 | + @staticmethod |
| 120 | + def weightDecode( |
| 121 | + weight: npt.NDArray[np.uint8], |
| 122 | + bits: int, |
| 123 | + cout: int, |
| 124 | + cin: int, |
| 125 | + height: int, |
| 126 | + width: int, |
| 127 | + ) -> npt.NDArray[np.uint8]: |
| 128 | + """Reverse of weightEncode""" |
| 129 | + cinSubtile = ( |
| 130 | + NeurekaMemoryLayoutSiracusa._CIN_SUBTILE_3x3 |
| 131 | + if height == 3 |
| 132 | + else NeurekaMemoryLayoutSiracusa._CIN_SUBTILE_1x1 |
| 133 | + ) |
| 134 | + cinMajor = int(np.ceil(cin / cinSubtile)) |
| 135 | + cinMinor = cinSubtile |
| 136 | + weightBandwidthBytes = int(np.ceil(NeurekaMemoryLayoutSiracusa._WEIGHT_BANDWIDTH / 8)) |
| 137 | + |
| 138 | + weight = weight.reshape(-1, weightBandwidthBytes, 1) |
| 139 | + weight = np.unpackbits(weight, axis=-1, count=8, bitorder="little") |
| 140 | + weight = weight.reshape(-1, NeurekaMemoryLayoutSiracusa._WEIGHT_BANDWIDTH) |
| 141 | + |
| 142 | + if height == 3 and width == 3: |
| 143 | + weight = weight[:, : height * width * cinMinor] |
| 144 | + weight = weight.reshape( |
| 145 | + cout, cinMajor, bits, height * width, cinMinor |
| 146 | + ).transpose(0, 1, 4, 3, 2) |
| 147 | + elif height == 1 and width == 1: |
| 148 | + weight = weight[:, : height * width * cinMinor * 8] |
| 149 | + weight = weight.reshape(cout, cinMajor, cinMinor // 4, 8, 4).transpose( |
| 150 | + 0, 1, 2, 4, 3 |
| 151 | + ) |
| 152 | + weight = np.packbits(weight, axis=-1, bitorder="little") |
| 153 | + weight = weight.reshape(cout, cinMajor * cinMinor, height, width) |
| 154 | + weight = weight[:, :cin, :, :] |
| 155 | + |
| 156 | + return weight |
0 commit comments