1 /*
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 */
24 #include "src/cpu/kernels/CpuConvertFullyConnectedWeightsKernel.h"
25
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/Types.h"
28 #include "src/core/helpers/AutoConfiguration.h"
29 #include "src/core/helpers/WindowHelpers.h"
30
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 namespace kernels
36 {
configure(const ITensorInfo * src,ITensorInfo * dst,const TensorShape & original_input_shape,DataLayout data_layout)37 void CpuConvertFullyConnectedWeightsKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const TensorShape &original_input_shape,
38 DataLayout data_layout)
39
40 {
41 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
42
43 // Output tensor auto initialisation if not yet initialized
44 auto_init_if_empty(*dst, *src->clone());
45
46 ARM_COMPUTE_ERROR_THROW_ON(CpuConvertFullyConnectedWeightsKernel::validate(src, dst, original_input_shape, data_layout));
47
48 const DataLayout input_data_layout = (data_layout == DataLayout::NCHW) ? DataLayout::NHWC : DataLayout::NCHW;
49
50 const int width_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::WIDTH);
51 const int height_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::HEIGHT);
52 const int channel_idx = get_data_layout_dimension_index(input_data_layout, DataLayoutDimension::CHANNEL);
53
54 const unsigned int num_elems_per_input_plane = original_input_shape[width_idx] * original_input_shape[height_idx];
55 const unsigned int num_channels = original_input_shape[channel_idx];
56
57 _factor1 = (data_layout == DataLayout::NCHW) ? num_elems_per_input_plane : num_channels;
58 _factor2 = (data_layout == DataLayout::NCHW) ? num_channels : num_elems_per_input_plane;
59
60 // Configure kernel window
61 Window win = calculate_max_window(*src, Steps());
62 ICpuKernel::configure(win);
63 }
64
validate(const ITensorInfo * src,const ITensorInfo * dst,const TensorShape & original_input_shape,DataLayout data_layout)65 Status CpuConvertFullyConnectedWeightsKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const TensorShape &original_input_shape,
66 DataLayout data_layout)
67 {
68 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src);
69 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
70 ARM_COMPUTE_RETURN_ERROR_ON(src->num_dimensions() != 2);
71 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(1) != original_input_shape.total_size_lower(3));
72 ARM_COMPUTE_RETURN_ERROR_ON(data_layout == DataLayout::UNKNOWN);
73
74 // Checks performed when dst is configured
75 if((dst != nullptr) && (dst->total_size() != 0))
76 {
77 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
78 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
79 }
80
81 return Status{};
82 }
83
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)84 void CpuConvertFullyConnectedWeightsKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
85 {
86 ARM_COMPUTE_UNUSED(info);
87 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
88 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
89
90 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
91 auto dst = tensors.get_tensor(TensorType::ACL_DST);
92
93 const unsigned int dst_stride_x = dst->info()->strides_in_bytes().x();
94 const unsigned int dst_stride_y = dst->info()->strides_in_bytes().y();
95 const unsigned int element_size = src->info()->element_size();
96
97 Iterator input(src, window);
98 Iterator output(dst, window);
99
100 execute_window_loop(window, [&](const Coordinates & id)
101 {
102 memcpy(output.ptr() + id.x() * dst_stride_x + (id.y() % _factor1 * _factor2 + id.y() / _factor1) * dst_stride_y, input.ptr(), element_size);
103 },
104 input);
105 }
106
name() const107 const char *CpuConvertFullyConnectedWeightsKernel::name() const
108 {
109 return "CpuConvertFullyConnectedWeightsKernel";
110 }
111 } // namespace kernels
112 } // namespace cpu
113 } // namespace arm_compute
114