David Svantesson | 3b162e5 | 2023-03-28 14:13:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 Arm Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "Reorder.h" |
| 25 | #include "src/core/NEON/kernels/arm_gemm/utils.hpp" |
| 26 | |
| 27 | namespace arm_compute |
| 28 | { |
| 29 | namespace test |
| 30 | { |
| 31 | namespace validation |
| 32 | { |
| 33 | namespace reference |
| 34 | { |
| 35 | |
| 36 | /* |
| 37 | * Generic transform. |
| 38 | * |
| 39 | * Assuming the untransposed case, this works by first reading <BlockBy> |
| 40 | * consecutive values from the first input row. This same number of values |
| 41 | * are then read from the next <IntBy-1> rows. Now return to the first |
| 42 | * input row and repeat. |
| 43 | * |
| 44 | * Need to cope with the work requested in either dimension not actually |
| 45 | * being a multiple of the block sizes. |
| 46 | */ |
| 47 | template <unsigned int tIntBy, unsigned int BlockBy, bool Transposed, size_t TOutSize, size_t TInSize, typename d_type, arm_gemm::VLType vlt> |
| 48 | struct Transform_ref |
| 49 | { |
| 50 | template <typename TOut, typename TIn> |
| 51 | static void Transform(TOut &out, const TIn in, const int stride, |
| 52 | const int y0, const int ymax, const int x0, const int xmax) |
| 53 | { |
| 54 | // NOTE: This code is disabled to avoid the call to get_vector_length(), so templated transforms will not be |
| 55 | // correct for SVE. This is not an issue as we have specializations for all SVE cases. |
| 56 | // For SVE cases we multiply the interleave factor by the vector length. |
| 57 | // const unsigned int IntBy = tIntBy * (vlt == VLType::SVE ? get_vector_length<TOut>() / BlockBy : 1); |
| 58 | const unsigned int IntBy = tIntBy; |
| 59 | int out_index = 0; |
| 60 | |
| 61 | const int n_whole_y_blocks = (ymax - y0) / IntBy; |
| 62 | const int y_remainders = (ymax - y0) % IntBy; |
| 63 | const int n_y_blocks = n_whole_y_blocks + (y_remainders ? 1 : 0); |
| 64 | |
| 65 | const int n_whole_x_blocks = (xmax - x0) / BlockBy; |
| 66 | const int x_remainders = (xmax - x0) % BlockBy; |
| 67 | const int n_x_blocks = n_whole_x_blocks + (x_remainders ? 1 : 0); |
| 68 | |
| 69 | // "Y" loop: advance down the rows of the source IntBy rows at a time. |
| 70 | // Set up fill_rows to show the number rows to copy from, and blank_rows |
| 71 | // for the number of blank rows to add. |
| 72 | for(int y_block = 0; y_block < n_y_blocks; y_block++) |
| 73 | { |
| 74 | const int fill_rows = (y_block < n_whole_y_blocks) ? IntBy : y_remainders; |
| 75 | const int blank_rows = IntBy - fill_rows; |
| 76 | |
| 77 | const int y_base = y0 + (y_block * IntBy); |
| 78 | |
| 79 | // So now advance along this block of rows, BlockBy columns at a time. |
| 80 | for(int x_block = 0; x_block < n_x_blocks; x_block++) |
| 81 | { |
| 82 | const int fill_cols = (x_block < n_whole_x_blocks) ? BlockBy : x_remainders; |
| 83 | const int blank_cols = BlockBy - fill_cols; |
| 84 | |
| 85 | const int x_base = x0 + (x_block * BlockBy); |
| 86 | |
| 87 | for(int row = 0; row < fill_rows; row++) |
| 88 | { |
| 89 | for(int col = 0; col < fill_cols; col++) |
| 90 | { |
| 91 | // In-range copy. If it's transposed, we reverse the sense of rows and columns here. |
| 92 | if(Transposed) |
| 93 | { |
| 94 | out[out_index] = in[(x_base + col) * stride + y_base + row]; |
| 95 | out_index++; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | out[out_index] = in[(y_base + row) * stride + x_base + col]; |
| 100 | out_index++; |
| 101 | } |
| 102 | } |
| 103 | // "col" tail - row is in range but column is out of range. |
| 104 | for(int col = 0; col < blank_cols; col++) |
| 105 | { |
| 106 | out[out_index] = 0; |
| 107 | out_index++; |
| 108 | } |
| 109 | } |
| 110 | // "row" tail - row is out of range so fill with zeros always. |
| 111 | const d_type zeroval = 0; |
| 112 | const int pads = blank_rows * (fill_cols + blank_cols); |
| 113 | |
| 114 | for(int i = 0; i < pads; i++) |
| 115 | { |
| 116 | out[out_index] = zeroval; |
| 117 | } |
| 118 | |
| 119 | out_index += pads; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | template <typename T> |
| 126 | SimpleTensor<T> reorder_layer(const SimpleTensor<T> &src, const TensorShape &output_shape, WeightFormat output_wf) |
| 127 | { |
| 128 | SimpleTensor<T> dst{ output_shape, src.data_type() }; |
| 129 | const int cols = src.shape()[0]; |
| 130 | const int rows = src.shape()[1]; |
| 131 | |
| 132 | switch(output_wf) |
| 133 | { |
| 134 | case WeightFormat::OHWIo4: |
| 135 | { |
| 136 | Transform_ref<4, 1, true, sizeof(float), sizeof(float), float, arm_gemm::VLType::None>::Transform<SimpleTensor<T> &, SimpleTensor<T>>(dst, src, rows, 0, rows, 0, cols); |
| 137 | break; |
| 138 | } |
| 139 | case WeightFormat::OHWIo8: |
| 140 | { |
| 141 | Transform_ref<8, 1, true, sizeof(float), sizeof(float), float, arm_gemm::VLType::None>::Transform<SimpleTensor<T> &, SimpleTensor<T>>(dst, src, rows, 0, rows, 0, cols); |
| 142 | break; |
| 143 | } |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | return dst; |
| 149 | } |
| 150 | |
| 151 | template SimpleTensor<float> reorder_layer(const SimpleTensor<float> &src, const TensorShape &output_shape, WeightFormat output_wf); |
| 152 | |
| 153 | } // namespace reference |
| 154 | } // namespace validation |
| 155 | } // namespace test |
| 156 | } // namespace arm_compute |