Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 "GEMMReshapeRHSMatrix.h" |
| 25 | |
| 26 | #include "arm_compute/core/Types.h" |
| 27 | |
| 28 | #include "tests/validation/Helpers.h" |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <cmath> |
| 32 | #include <cstring> |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace test |
| 37 | { |
| 38 | namespace validation |
| 39 | { |
| 40 | namespace reference |
| 41 | { |
| 42 | template <typename T> |
| 43 | SimpleTensor<T> gemm_reshape_rhs_matrix(const SimpleTensor<T> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info) |
| 44 | { |
| 45 | ARM_COMPUTE_ERROR_ON(in.shape().num_dimensions() > 3); |
| 46 | |
| 47 | SimpleTensor<T> out{ output_shape, in.data_type() }; |
| 48 | |
| 49 | // Initialize the output tensor with zero |
| 50 | std::memset(&out[0], 0, out.num_elements() * sizeof(T)); |
| 51 | |
| 52 | const unsigned int N = in.shape()[0]; |
| 53 | const unsigned int K = in.shape()[1]; |
| 54 | const unsigned int B = in.shape()[2]; |
| 55 | |
| 56 | const unsigned int num_tiles_x = std::ceil(N / static_cast<float>(rhs_info.n0)); |
| 57 | const unsigned int num_tiles_y = std::ceil(K / static_cast<float>(rhs_info.k0)); |
| 58 | |
| 59 | const TensorShape tile_dims(rhs_info.n0, rhs_info.k0); |
| 60 | const TensorShape tile_dims_transposed(rhs_info.k0, rhs_info.n0); |
| 61 | |
| 62 | // Simple tensor for the input tile |
| 63 | SimpleTensor<T> src_tile{ tile_dims, in.data_type() }; |
| 64 | |
| 65 | // Simple tensor for the input tile |
| 66 | SimpleTensor<T> src_tile_transposed{ tile_dims_transposed, in.data_type() }; |
| 67 | |
| 68 | // Simple tensor to use when storing the values |
| 69 | SimpleTensor<T> *tile_to_use = rhs_info.transpose ? &src_tile_transposed : &src_tile; |
| 70 | |
| 71 | const unsigned int offset_output_x = rhs_info.interleave ? tile_to_use->shape()[0] : tile_to_use->shape()[0] * tile_to_use->shape()[1]; |
| 72 | const unsigned int step_output_x = rhs_info.interleave ? tile_to_use->shape()[0] * rhs_info.h0 : tile_to_use->shape()[0]; |
| 73 | |
| 74 | for(unsigned int z = 0; z < B; ++z) |
| 75 | { |
| 76 | for(unsigned int y = 0; y < num_tiles_y; ++y) |
| 77 | { |
| 78 | for(unsigned int x = 0; x < num_tiles_x; ++x) |
| 79 | { |
| 80 | // Get the tile from the input tensor |
| 81 | get_tile<T>(in, src_tile, Coordinates(x * rhs_info.n0, y * rhs_info.k0, z, 0)); |
| 82 | |
| 83 | if(rhs_info.transpose) |
| 84 | { |
| 85 | // Transpose matrix |
| 86 | transpose_matrix<T>(src_tile, src_tile_transposed); |
| 87 | } |
| 88 | |
| 89 | // Store |
| 90 | const unsigned int offset_output = (y * rhs_info.k0 * rhs_info.n0 * rhs_info.h0) + ((x % rhs_info.h0) * offset_output_x) + ((x / rhs_info.h0) * out.shape()[0]) + (z * out.shape()[0] * out.shape()[1]); |
| 91 | |
| 92 | for(unsigned int i = 0; i < tile_to_use->shape()[1]; ++i) |
| 93 | { |
| 94 | const unsigned int offset_tile = i * tile_to_use->shape()[0]; |
| 95 | |
| 96 | // Copy per row |
| 97 | std::copy(&(*tile_to_use)[offset_tile], &(*tile_to_use)[offset_tile + tile_to_use->shape()[0]], &out[offset_output + i * step_output_x]); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return out; |
| 104 | } |
| 105 | template SimpleTensor<int> gemm_reshape_rhs_matrix(const SimpleTensor<int> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info); |
| 106 | template SimpleTensor<short> gemm_reshape_rhs_matrix(const SimpleTensor<short> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info); |
| 107 | template SimpleTensor<char> gemm_reshape_rhs_matrix(const SimpleTensor<char> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info); |
| 108 | } // namespace reference |
| 109 | } // namespace validation |
| 110 | } // namespace test |
| 111 | } // namespace arm_compute |