blob: 6d2105f3b121ff8eeece12cea89b7dc411db40ae [file] [log] [blame]
Michalis Spyroue9362622018-11-23 17:41:37 +00001/*
2 * Copyright (c) 2018 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 "arm_compute/core/CL/kernels/CLElementWiseUnaryLayerKernel.h"
25
26#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLValidate.h"
28#include "arm_compute/core/CL/ICLTensor.h"
29
30using namespace arm_compute;
31
32namespace
33{
34Status validate_arguments(const ITensorInfo &input, const ITensorInfo &output)
35{
36 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&input);
37 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32);
38
39 // Validate in case of configured output
40 if(output.total_size() > 0)
41 {
42 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(&output);
43 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::F16, DataType::F32);
44 }
45
46 return Status{};
47}
48} // namespace
49
50void CLElementWiseUnaryLayerKernel::configure(const ICLTensor *input, ICLTensor *output, const ElementWiseUnary &op)
51{
52 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
53 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input->info(), *output->info()));
54
55 // Configure kernel window
56 _input = input;
57 _output = output;
58
59 const std::string kernel_name = "elementwise_unary";
60 const int vec_size_x = 16 / output->info()->element_size();
61 const int output_width_x = output->info()->tensor_shape().x();
62 const bool multi_access_x = (output_width_x / vec_size_x > 0);
63
64 Window win = calculate_max_window(*output->info());
65 if(multi_access_x)
66 {
67 win.set(Window::DimX,
68 Window::Dimension(win.x().start(), ceil_to_multiple(win.x().end(), vec_size_x), vec_size_x));
69 }
70 ICLKernel::configure_internal(win);
71
72 // Set kernel build options
73 CLBuildOptions build_opts;
74 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
75 build_opts.add_option_if(multi_access_x, "-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x));
76 build_opts.add_option_if(multi_access_x, "-DLAST_ACCESSED_X=" + support::cpp11::to_string(std::max<int>(output_width_x - vec_size_x, 0)));
77 switch(op)
78 {
79 case ElementWiseUnary::RSQRT:
80 build_opts.add_option("-DOPERATION=inverse_sqrt");
81 break;
82 case ElementWiseUnary::EXP:
83 build_opts.add_option("-DOPERATION=exponential");
84 break;
85 default:
86 ARM_COMPUTE_ERROR("Not implemented");
87 }
88
89 // Create kernel
90 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
91}
92
93Status CLElementWiseUnaryLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ElementWiseUnary &op)
94{
95 ARM_COMPUTE_UNUSED(op);
96 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
97 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input, *output));
98
99 return Status{};
100}
101
102void CLElementWiseUnaryLayerKernel::run(const Window &window, cl::CommandQueue &queue)
103{
104 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
105 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window);
106
107 Window collapsed = window.collapse_if_possible(ICLKernel::window(), Window::DimX);
108
109 do
110 {
111 unsigned int idx = 0;
112 add_1D_tensor_argument(idx, _input, collapsed);
113 add_1D_tensor_argument(idx, _output, collapsed);
114 enqueue(queue, *this, collapsed);
115 }
116 while(window.slide_window_slice_1D(collapsed));
117}