blob: ce792489c5d3b548579351e7ea6f81a2ea5834bf [file] [log] [blame]
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +01001/*
Manuel Bottini4c6bd512020-04-08 10:15:51 +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
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010030#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/IAccessWindow.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010036#include "arm_compute/core/Window.h"
37
38#include <string>
39
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +000040/** [CLReshapeLayerKernel Kernel] **/
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010041using namespace arm_compute;
42
Giuseppe Rossini84797632018-08-22 12:07:48 +010043namespace
44{
45Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
46{
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Giuseppe Rossini84797632018-08-22 12:07:48 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000049 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Giuseppe Rossini84797632018-08-22 12:07:48 +010050
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
53 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().total_size() != output->tensor_shape().total_size());
54
55 return Status{};
56}
57
58} // namespace
59
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010060CLReshapeLayerKernel::CLReshapeLayerKernel()
61 : _input(nullptr), _output(nullptr)
62{
63}
64
65void CLReshapeLayerKernel::configure(const ICLTensor *input, ICLTensor *output)
66{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010067 configure(CLKernelLibrary::get().get_compile_context(), input, output);
68}
69
Manuel Bottini2803f702020-04-21 16:20:03 +010070void CLReshapeLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010071{
Giuseppe Rossini84797632018-08-22 12:07:48 +010072 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
73 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010074
75 _input = input;
76 _output = output;
77
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010078 // Create kernel
Sang-Hoon Park0779fec2019-11-13 17:08:12 +000079 std::set<std::string> build_opts = { "-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size()) };
Manuel Bottini4c6bd512020-04-08 10:15:51 +010080 _kernel = create_kernel(compile_context, "reshape_layer", build_opts);
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +010081
82 // Add static arguments
83 const cl_int2 input_shape =
84 {
85 {
86 static_cast<cl_int>(_input->info()->tensor_shape()[0]),
87 static_cast<cl_int>(_input->info()->tensor_shape()[1])
88 }
89 };
90 const cl_int2 output_shape =
91 {
92 {
93 static_cast<cl_int>(_output->info()->tensor_shape()[0]),
94 static_cast<cl_int>(_output->info()->tensor_shape()[1])
95 }
96 };
97 unsigned int idx = 2 * num_arguments_per_3D_tensor(); // Skip the input and output parameters
98 _kernel.setArg<cl_int2>(idx++, input_shape);
99 _kernel.setArg<cl_int2>(idx++, output_shape);
100
101 // Configure kernel window
Giuseppe Rossini84797632018-08-22 12:07:48 +0100102 Window win = calculate_max_window(*input->info());
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100103
Giuseppe Rossini84797632018-08-22 12:07:48 +0100104 // Set the output valid region
105 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100106 ICLKernel::configure_internal(win);
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100107}
108
Giuseppe Rossini84797632018-08-22 12:07:48 +0100109Status CLReshapeLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
110{
111 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
112
113 return Status{};
114}
115
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100116void CLReshapeLayerKernel::run(const Window &window, cl::CommandQueue &queue)
117{
118 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
119 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
120
121 Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimZ);
122 Window slice = window_collapsed.first_slice_window_3D();
123
124 // Set inputs
125 unsigned int idx = 0;
126 add_3D_tensor_argument(idx, _input, window_collapsed);
127 add_3D_tensor_argument(idx, _output, window_collapsed);
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100128 enqueue(queue, *this, slice, lws_hint());
Georgios Pinitas5ee66ea2017-09-07 17:29:16 +0100129}
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000130/** [CLReshapeLayerKernel Kernel] **/