blob: b14013bc34c3b5ff334bb8d4f443792efcbe0a05 [file] [log] [blame]
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +01003 *
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 "arm_compute/core/CL/kernels/CLReshapeLayerKernel.h"
25
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/IAccessWindow.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/WindowHelpers.h"
37#include "support/Cast.h"
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010038
39#include <string>
40
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000041/** [CLReshapeLayerKernel Kernel] **/
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010042namespace arm_compute
43{
Giuseppe Rossini84797632018-08-22 12:07:48 +010044namespace
45{
46Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
47{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini84797632018-08-22 12:07:48 +010049 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000050 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giuseppe Rossini84797632018-08-22 12:07:48 +010051
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
54 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
55
56 return Status{};
57}
Giuseppe Rossini84797632018-08-22 12:07:48 +010058} // namespace
59
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010060void CLReshapeLayerKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010061{
Giuseppe Rossini84797632018-08-22 12:07:48 +010062 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010063 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010064
Sheri Zhang11d73272020-10-28 14:01:55 +000065 auto padding_info = get_padding_info({ input, output });
66
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010067 // Create kernel
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010068 std::set<std::string> build_opts = { "-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->element_size()) };
Manuel Bottini4c6bd512020-04-08 10:15:51 +010069 _kernel = create_kernel(compile_context, "reshape_layer", build_opts);
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010070
71 // Add static arguments
72 const cl_int2 input_shape =
73 {
74 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010075 static_cast<cl_int>(input->tensor_shape()[0]),
76 static_cast<cl_int>(input->tensor_shape()[1])
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010077 }
78 };
79 const cl_int2 output_shape =
80 {
81 {
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010082 static_cast<cl_int>(output->tensor_shape()[0]),
83 static_cast<cl_int>(output->tensor_shape()[1])
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010084 }
85 };
86 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
87 _kernel.setArg<cl_int2>(idx++, input_shape);
88 _kernel.setArg<cl_int2>(idx++, output_shape);
89
90 // Configure kernel window
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010091 Window win = calculate_max_window(*input);
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010092
Giuseppe Rossini84797632018-08-22 12:07:48 +010093 // Set the output valid region
Michalis Spyrou2aad21a2020-07-02 12:43:53 +010094 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
Anthony Barbierb6eb3532018-08-08 13:20:04 +010095 ICLKernel::configure_internal(win);
Sheri Zhang11d73272020-10-28 14:01:55 +000096
97 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010098}
99
Giuseppe Rossini84797632018-08-22 12:07:48 +0100100Status CLReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
101{
102 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
103
104 return Status{};
105}
106
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100107void CLReshapeLayerKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100108{
109 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
110 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
111
112 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
113 Window slice = window_collapsed.first_slice_window_3D();
114
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100115 const auto src = utils::cast::polymorphic_downcast<const ICLTensor *>(tensors.get_const_tensor(TensorType::ACL_SRC));
116 auto dst = utils::cast::polymorphic_downcast<ICLTensor *>(tensors.get_tensor(TensorType::ACL_DST));
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100117
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100118 // Set inputs
119 unsigned int idx = 0;
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100120 add_3D_tensor_argument(idx, src, window_collapsed);
121 add_3D_tensor_argument(idx, dst, window_collapsed);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100122 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100123}
Michalis Spyrou2aad21a2020-07-02 12:43:53 +0100124} // namespace arm_compute
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000125/** [CLReshapeLayerKernel Kernel] **/