6e0bcde7f29ff9a060f0c4ac9a50d79709e54138
[platform/core/ml/nnfw.git] / compute / ARMComputeEx / src / core / CL / kernels / CLCastBoolKernel.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Copyright (c) 2018-2020 ARM Limited.
19  *
20  * SPDX-License-Identifier: MIT
21  *
22  * Permission is hereby granted, free of charge, to any person obtaining a copy
23  * of this software and associated documentation files (the "Software"), to
24  * deal in the Software without restriction, including without limitation the
25  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
26  * sell copies of the Software, and to permit persons to whom the Software is
27  * furnished to do so, subject to the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be included in all
30  * copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38  * SOFTWARE.
39  */
40 #include "arm_compute/core/CL/kernels/CLCastBoolKernel.h"
41
42 #include "arm_compute/core/CL/CLHelpers.h"
43 #include "arm_compute/core/CL/CLKernelLibraryEx.h"
44 #include "arm_compute/core/CL/CLValidate.h"
45 #include "arm_compute/core/CL/ICLTensor.h"
46 #include "arm_compute/core/CL/OpenCL.h"
47 #include "arm_compute/core/Error.h"
48 #include "arm_compute/core/TensorInfo.h"
49 #include "arm_compute/core/Utils.h"
50 #include "arm_compute/core/Validate.h"
51 #include "support/StringSupport.h"
52
53 #include <cstddef>
54 #include <set>
55 #include <string>
56
57 namespace arm_compute
58 {
59 namespace
60 {
61 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
62 {
63   ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(output);
64   ARM_COMPUTE_RETURN_ERROR_ON(input == output);
65   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
66   ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S8,
67                                                        DataType::S16, DataType::U16, DataType::U32,
68                                                        DataType::S32, DataType::F16, DataType::F32);
69   ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == output->data_type(),
70                                   "Input and output data types must be different");
71
72   // Validate in case of configured output
73   if (output->total_size() > 0)
74   {
75     ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
76   }
77
78   return Status{};
79 }
80 } // namespace
81
82 void CLCastBoolKernel::configure(const ICLTensor *input, ICLTensor *output)
83 {
84   ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
85
86   // Auto initialize output shape if not initialized (We can only auto-configure the shape, datatype
87   // must be given)
88   set_shape_if_empty(*output->info(), input->info()->tensor_shape());
89
90   ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
91
92   // Get number of elements to process per iterations
93   constexpr unsigned int num_elems_processed_per_iteration = 16;
94
95   // Set build options
96   CLBuildOptions build_opts;
97   build_opts.add_option("-DVEC_SIZE=" +
98                         support::cpp11::to_string(num_elems_processed_per_iteration));
99   build_opts.add_option("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
100   build_opts.add_option("-DDATA_TYPE_OUT=" +
101                         get_cl_type_from_data_type(output->info()->data_type()));
102
103   // Create kernel
104   const std::string kernel_name = "cast_bool";
105   _kernel = static_cast<cl::Kernel>(
106       CLKernelLibraryEx::get().create_kernel(kernel_name, build_opts.options()));
107
108   // Configure kernel
109   ICLSimple2DKernel::configure(input, output, num_elems_processed_per_iteration);
110
111   // Collapse window
112   const Window &full_window = window();
113   Window collapsed_window = full_window.collapse_if_possible(full_window, Window::DimZ);
114   ICLKernel::configure_internal(collapsed_window);
115
116   // Set config_id for enabling LWS tuning
117   _config_id = kernel_name;
118   _config_id += "_";
119   _config_id += lower_string(string_from_data_type(output->info()->data_type()));
120   _config_id += "_";
121   _config_id += support::cpp11::to_string(output->info()->dimension(0));
122   _config_id += "_";
123   _config_id += support::cpp11::to_string(output->info()->dimension(1));
124 }
125
126 Status CLCastBoolKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
127 {
128   ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
129
130   return Status{};
131 }
132 } // namespace arm_compute