blob: 2f5b2466b1fb18a421758637a3c97766d21faf66 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
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{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010043 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16,
44 DataType::U16, DataType::U32, DataType::S32);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16,
steniu01da37e2f2017-06-29 10:14:58 +010046 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
52 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U8 && (output->info()->data_type() != DataType::U16 && output->info()->data_type() != DataType::S16
53 && output->info()->data_type() != DataType::U32 && output->info()->data_type() != DataType::S32),
54 "Only data types supported [in] U8 -> [out] U16, S16, U32, S32");
55
56 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
57 && output->info()->data_type() != DataType::S32),
58 "Only data types supported [in] U16 -> [out] U8, U32, S32");
59
60 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
61 && output->info()->data_type() != DataType::S32),
62 "Only data types supported [in] S16 -> [out] U8, U32, S32");
63
64 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
65 && output->info()->data_type() != DataType::S16),
66 "Only data types supported [in] U32 -> [out] U8, U16, S16");
67
68 ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
69 && output->info()->data_type() != DataType::S16),
70 "Only data types supported [in] S32 -> [out] U8, U16, S16");
71
steniu01da37e2f2017-06-29 10:14:58 +010072 // Auto initialize output shape if not initialized (We can only auto-configure the shape, datatype must be given)
73 set_shape_if_empty(*output->info(), input->info()->tensor_shape());
74
75 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
76
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 // Get data sizes
78 const size_t input_size = data_size_from_type(input->info()->data_type());
79 const size_t output_size = data_size_from_type(output->info()->data_type());
80
81 // Construct kernel name and build options
82 std::string kernel_name = "convert_depth";
83 std::set<std::string> build_opts;
84 if(input_size > output_size)
85 {
86 kernel_name += "_down";
Georgios Pinitas43fc5cd2017-10-12 11:05:18 +010087 // Down conversions from float always SATURATE as out-of-bounds conversion from float->integer is implementation defined
88 build_opts.insert(((policy == ConvertPolicy::WRAP) && !is_data_type_float(input->info()->data_type())) ? "-DWRAP" : "-DSATURATE");
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 }
90 else
91 {
92 kernel_name += "_up";
93 }
steniu01da37e2f2017-06-29 10:14:58 +010094 build_opts.emplace("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
95 build_opts.emplace("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096
97 // Create kernel
98 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
99
100 // Set shift arg
101 unsigned int idx = 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
102 _kernel.setArg(idx++, shift);
103
104 // Configure kernel
105 constexpr unsigned int num_elems_processed_per_iteration = 16;
106 ICLSimple2DKernel::configure(input, output, num_elems_processed_per_iteration);
107}