blob: d691338ef24e8230b36eeca14d6c7c62cf7b55b3 [file] [log] [blame]
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00003 *
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
34namespace arm_compute
35{
36namespace test
37{
38namespace validation
39{
40namespace reference
41{
42template <typename T>
43SimpleTensor<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];
Michalis Spyroud1d77222020-04-08 14:10:15 +010073#ifdef ARM_COMPUTE_OPENMP
74 #pragma omp parallel for schedule(dynamic, 1) collapse(3)
75#endif /* _OPENMP */
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +000076 for(unsigned int z = 0; z < B; ++z)
77 {
78 for(unsigned int y = 0; y < num_tiles_y; ++y)
79 {
80 for(unsigned int x = 0; x < num_tiles_x; ++x)
81 {
82 // Get the tile from the input tensor
83 get_tile<T>(in, src_tile, Coordinates(x * rhs_info.n0, y * rhs_info.k0, z, 0));
84
85 if(rhs_info.transpose)
86 {
87 // Transpose matrix
88 transpose_matrix<T>(src_tile, src_tile_transposed);
89 }
90
91 // Store
92 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]);
93
94 for(unsigned int i = 0; i < tile_to_use->shape()[1]; ++i)
95 {
96 const unsigned int offset_tile = i * tile_to_use->shape()[0];
97
98 // Copy per row
99 std::copy(&(*tile_to_use)[offset_tile], &(*tile_to_use)[offset_tile + tile_to_use->shape()[0]], &out[offset_output + i * step_output_x]);
100 }
101 }
102 }
103 }
104
105 return out;
106}
107template SimpleTensor<int> gemm_reshape_rhs_matrix(const SimpleTensor<int> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info);
108template SimpleTensor<short> gemm_reshape_rhs_matrix(const SimpleTensor<short> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info);
109template SimpleTensor<char> gemm_reshape_rhs_matrix(const SimpleTensor<char> &in, const TensorShape &output_shape, const GEMMRHSMatrixInfo &rhs_info);
110} // namespace reference
111} // namespace validation
112} // namespace test
113} // namespace arm_compute