blob: 83908a1469b0f70bba76163324a4aba38849cea7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/CL/kernels/CLDepthConvertLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
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/Error.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34
35#include <cstddef>
36#include <set>
37#include <string>
38
39using namespace arm_compute;
40
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000041void CLDepthConvertLayerKernel::configure(const ICLTensor *input, ICLTensor *output, ConvertPolicy policy, uint32_t shift)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042{
steniu01da37e2f2017-06-29 10:14:58 +010043 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::U8, DataType::S16, DataType::QS16,
44 DataType::U16, DataType::U32, DataType::S32, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::U8, DataType::S16, DataType::QS16,
46 DataType::U16, DataType::U32, DataType::S32, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 ARM_COMPUTE_ERROR_ON(input == output);
48 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == output->info()->data_type(), "Input and output data types must be different");
49 ARM_COMPUTE_ERROR_ON(shift >= 8);
50
51 // Check if convertion is supported
steniu01da37e2f2017-06-29 10:14:58 +010052 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::QS8 && output->info()->data_type() != DataType::F32,
53 "Only data types supported [in] QS8 -> [out] F32");
54 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::QS16 && (output->info()->data_type() != DataType::F32),
55 "Only data types supported [in] QS16 -> [out] F32");
56 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::F32 && ((output->info()->data_type() != DataType::QS8) && output->info()->data_type() != DataType::QS16),
57 "Only data types supported [in] F32 -> [out] QS8, QS16");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010058 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U8 && (output->info()->data_type() != DataType::U16 && output->info()->data_type() != DataType::S16
59 && output->info()->data_type() != DataType::U32 && output->info()->data_type() != DataType::S32),
60 "Only data types supported [in] U8 -> [out] U16, S16, U32, S32");
61
62 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
63 && output->info()->data_type() != DataType::S32),
64 "Only data types supported [in] U16 -> [out] U8, U32, S32");
65
66 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
67 && output->info()->data_type() != DataType::S32),
68 "Only data types supported [in] S16 -> [out] U8, U32, S32");
69
70 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
71 && output->info()->data_type() != DataType::S16),
72 "Only data types supported [in] U32 -> [out] U8, U16, S16");
73
74 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
75 && output->info()->data_type() != DataType::S16),
76 "Only data types supported [in] S32 -> [out] U8, U16, S16");
77
steniu01da37e2f2017-06-29 10:14:58 +010078 // Auto initialize output shape if not initialized (We can only auto-configure the shape, datatype must be given)
79 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
80
81 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
82
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083 // Get data sizes
84 const size_t input_size = data_size_from_type(input->info()->data_type());
85 const size_t output_size = data_size_from_type(output->info()->data_type());
86
87 // Construct kernel name and build options
88 std::string kernel_name = "convert_depth";
89 std::set<std::string> build_opts;
90 if(input_size > output_size)
91 {
92 kernel_name += "_down";
Georgios Pinitas43fc5cd2017-10-12 11:05:18 +010093 // Down conversions from float always SATURATE as out-of-bounds conversion from float->integer is implementation defined
94 build_opts.insert(((policy == ConvertPolicy::WRAP) && !is_data_type_float(input->info()->data_type())) ? "-DWRAP" : "-DSATURATE");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095 }
96 else
97 {
98 kernel_name += "_up";
99 }
steniu01da37e2f2017-06-29 10:14:58 +0100100 build_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
101 build_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
102 if(is_data_type_fixed_point(input->info()->data_type()) || is_data_type_fixed_point(output->info()->data_type()))
103 {
104 build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position()));
105 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106
107 // Create kernel
108 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
109
110 // Set shift arg
111 unsigned int idx = 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
112 _kernel.setArg(idx++, shift);
113
114 // Configure kernel
115 constexpr unsigned int num_elems_processed_per_iteration = 16;
116 ICLSimple2DKernel::configure(input, output, num_elems_processed_per_iteration);
117}