Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 Arm Limited. |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 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/CLFlattenLayerKernel.h" |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 25 | #include "arm_compute/core/CL/ICLTensor.h" |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 29 | #include "support/StringSupport.h" |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 30 | |
| 31 | using namespace arm_compute::misc::shape_calculator; |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace |
| 36 | { |
| 37 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
| 38 | { |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 39 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 40 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 41 | |
| 42 | // Checks performed when output is configured |
| 43 | if(output->total_size() != 0) |
| 44 | { |
| 45 | const TensorInfo tensor_info_output = input->clone()->set_tensor_shape(compute_flatten_shape(input)); |
| 46 | |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return Status{}; |
| 53 | } |
| 54 | |
| 55 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 56 | { |
| 57 | // Output tensor auto initialization if not yet initialized |
| 58 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_flatten_shape(input))); |
| 59 | |
| 60 | Window win = calculate_max_window(*input, Steps()); // Flatten does not need paddings |
| 61 | |
| 62 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 63 | |
| 64 | return std::make_pair(Status{}, win); |
| 65 | } |
| 66 | } // namespace |
| 67 | |
| 68 | CLFlattenLayerKernel::CLFlattenLayerKernel() |
| 69 | : _input(nullptr), _output(nullptr) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void CLFlattenLayerKernel::configure(const ICLTensor *input, ICLTensor *output) |
| 74 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 75 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 76 | } |
| 77 | |
Manuel Bottini | 256c0b9 | 2020-04-21 13:29:30 +0100 | [diff] [blame] | 78 | void CLFlattenLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 79 | { |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 80 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 81 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
| 82 | |
| 83 | _input = input; |
| 84 | _output = output; |
| 85 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 86 | // Configure kernel window |
| 87 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 88 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 89 | ICLKernel::configure_internal(win_config.second); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 90 | |
Gian Marco Iodice | a7b54f4 | 2018-11-12 15:42:17 +0000 | [diff] [blame] | 91 | CLBuildOptions build_opts; |
Sheri Zhang | 0de45d0 | 2020-04-17 14:59:13 +0100 | [diff] [blame] | 92 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(data_size_from_type(input->info()->data_type()))); |
Gian Marco Iodice | a7b54f4 | 2018-11-12 15:42:17 +0000 | [diff] [blame] | 93 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 94 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 95 | build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 96 | build_opts.add_option_if(output->info()->num_dimensions() > 2, "-DDST_DIM1=" + support::cpp11::to_string(output->info()->dimension(1))); |
| 97 | |
| 98 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 99 | _kernel = create_kernel(compile_context, "flatten", build_opts.options()); |
Gian Marco Iodice | a7b54f4 | 2018-11-12 15:42:17 +0000 | [diff] [blame] | 100 | |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 101 | // Set config_id for enabling LWS tuning |
| 102 | _config_id = "flatten"; |
| 103 | _config_id += "_"; |
| 104 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 105 | _config_id += "_"; |
| 106 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 107 | _config_id += "_"; |
| 108 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 109 | _config_id += "_"; |
| 110 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 111 | _config_id += "_"; |
| 112 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 113 | _config_id += "_"; |
| 114 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 115 | } |
| 116 | |
| 117 | Status CLFlattenLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 118 | { |
| 119 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 120 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
| 121 | return Status{}; |
| 122 | } |
| 123 | |
| 124 | void CLFlattenLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 125 | { |
| 126 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 127 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(ICLKernel::window(), window); |
| 128 | |
Gian Marco Iodice | a7b54f4 | 2018-11-12 15:42:17 +0000 | [diff] [blame] | 129 | Window collapsed_window = window.collapse(ICLKernel::window(), Window::DimZ); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 130 | |
Gian Marco Iodice | a7b54f4 | 2018-11-12 15:42:17 +0000 | [diff] [blame] | 131 | Window output_window; |
| 132 | output_window.use_tensor_dimensions(_output->info()->tensor_shape()); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 133 | |
| 134 | // Run kernel |
Gian Marco Iodice | a7b54f4 | 2018-11-12 15:42:17 +0000 | [diff] [blame] | 135 | unsigned int idx = 0; |
| 136 | add_4D_tensor_argument(idx, _input, collapsed_window); |
| 137 | add_3D_tensor_argument(idx, _output, output_window); |
| 138 | enqueue(queue, *this, collapsed_window, lws_hint()); |
Gian Marco Iodice | 215b4ea | 2018-06-28 16:29:29 +0100 | [diff] [blame] | 139 | } |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 140 | } // namespace arm_compute |