1 /*
2 * Copyright (c) 2017-2020 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/core/CL/kernels/CLReshapeLayerKernel.h"
25
26 #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"
34 #include "src/core/AccessWindowStatic.h"
35 #include "src/core/CL/CLValidate.h"
36 #include "src/core/helpers/WindowHelpers.h"
37 #include "support/Cast.h"
38
39 #include <string>
40
41 /** [CLReshapeLayerKernel Kernel] **/
42 namespace arm_compute
43 {
44 namespace
45 {
validate_arguments(const ITensorInfo * input,const ITensorInfo * output)46 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
47 {
48 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
50 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
51
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 }
58 } // namespace
59
configure(const CLCompileContext & compile_context,const ITensorInfo * input,ITensorInfo * output)60 void CLReshapeLayerKernel::configure(const CLCompileContext &compile_context, const ITensorInfo *input, ITensorInfo *output)
61 {
62 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
63 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, output));
64
65 auto padding_info = get_padding_info({ input, output });
66
67 // Create kernel
68 std::set<std::string> build_opts = { "-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->element_size()) };
69 _kernel = create_kernel(compile_context, "reshape_layer", build_opts);
70
71 // Add static arguments
72 const cl_int2 input_shape =
73 {
74 {
75 static_cast<cl_int>(input->tensor_shape()[0]),
76 static_cast<cl_int>(input->tensor_shape()[1])
77 }
78 };
79 const cl_int2 output_shape =
80 {
81 {
82 static_cast<cl_int>(output->tensor_shape()[0]),
83 static_cast<cl_int>(output->tensor_shape()[1])
84 }
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
91 Window win = calculate_max_window(*input);
92
93 // Set the output valid region
94 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
95 ICLKernel::configure_internal(win);
96
97 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
98 }
99
validate(const ITensorInfo * input,const ITensorInfo * output)100 Status 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
run_op(ITensorPack & tensors,const Window & window,cl::CommandQueue & queue)107 void CLReshapeLayerKernel::run_op(ITensorPack &tensors, const Window &window, cl::CommandQueue &queue)
108 {
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
115 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));
117
118 // Set inputs
119 unsigned int idx = 0;
120 add_3D_tensor_argument(idx, src, window_collapsed);
121 add_3D_tensor_argument(idx, dst, window_collapsed);
122 enqueue(queue, *this, slice, lws_hint());
123 }
124 } // namespace arm_compute
125 /** [CLReshapeLayerKernel Kernel] **/
126