blob: 87703ec63176f6d747dbb60ed7227512f2a8532b [file] [log] [blame]
Teresa Charlin562bee52021-04-13 17:44:15 +01001/*
2 * Copyright (c) 2018-2021 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuConvertFullyConnectedWeightsKernel.h"
Teresa Charlin562bee52021-04-13 17:44:15 +010025
26#include "arm_compute/core/Helpers.h"
27#include "arm_compute/core/Types.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010028
Teresa Charlin562bee52021-04-13 17:44:15 +010029#include "src/core/helpers/AutoConfiguration.h"
30#include "src/core/helpers/WindowHelpers.h"
31
32namespace arm_compute
33{
34namespace cpu
35{
36namespace kernels
37{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010038void CpuConvertFullyConnectedWeightsKernel::configure(const ITensorInfo *src,
39 ITensorInfo *dst,
40 const TensorShape &original_input_shape,
41 DataLayout data_layout)
Teresa Charlin562bee52021-04-13 17:44:15 +010042
43{
44 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
45
46 // Output tensor auto initialisation if not yet initialized
47 auto_init_if_empty(*dst, *src->clone());
48
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049 ARM_COMPUTE_ERROR_THROW_ON(
50 CpuConvertFullyConnectedWeightsKernel::validate(src, dst, original_input_shape, data_layout));
Teresa Charlin562bee52021-04-13 17:44:15 +010051
52 const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
53
54 const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
55 const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
56 const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
57
58 const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
59 const unsigned int num_channels = original_input_shape[channel_idx];
60
61 _factor1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
62 _factor2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
63
64 // Configure kernel window
65 Window win = calculate_max_window(*src, Steps());
66 ICpuKernel::configure(win);
67}
68
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010069Status CpuConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *src,
70 const ITensorInfo *dst,
71 const TensorShape &original_input_shape,
72 DataLayout data_layout)
Teresa Charlin562bee52021-04-13 17:44:15 +010073{
74 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
75 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
76 ARM_COMPUTE_RETURN_ERROR_ON(src->num_dimensions() != 2);
77 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(1) != original_input_shape.total_size_lower(3));
78 ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
79
80 // Checks performed when dst is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010081 if ((dst != nullptr) && (dst->total_size() != 0))
Teresa Charlin562bee52021-04-13 17:44:15 +010082 {
83 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
84 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
85 }
86
87 return Status{};
88}
89
Teresa Charlin562bee52021-04-13 17:44:15 +010090void CpuConvertFullyConnectedWeightsKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
91{
92 ARM_COMPUTE_UNUSED(info);
93 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
94 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
95
96 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
97 auto dst = tensors.get_tensor(TensorType::ACL_DST);
98
Michele Di Giorgioa86433a2021-07-28 14:10:47 +010099 const unsigned int dst_stride_x = dst->info()->strides_in_bytes().x();
100 const unsigned int dst_stride_y = dst->info()->strides_in_bytes().y();
101 const unsigned int element_size = src->info()->element_size();
102
103 Iterator input(src, window);
104 Iterator output(dst, window);
105
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100106 execute_window_loop(
107 window,
108 [&](const Coordinates &id)
109 {
110 memcpy(output.ptr() + id.x() * dst_stride_x +
111 (id.y() % _factor1 * _factor2 + id.y() / _factor1) * dst_stride_y,
112 input.ptr(), element_size);
113 },
114 input);
Teresa Charlin562bee52021-04-13 17:44:15 +0100115}
116
117const char *CpuConvertFullyConnectedWeightsKernel::name() const
118{
119 return "CpuConvertFullyConnectedWeightsKernel";
120}
121} // namespace kernels
122} // namespace cpu
123} // namespace arm_compute