Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2021 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 "src/core/cpu/kernels/CpuFloorKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "src/core/CPP/Validate.h" |
| 31 | #include "src/core/helpers/AutoConfiguration.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
| 33 | |
| 34 | #include "src/core/common/Registrars.h" |
Georgios Pinitas | f8f0442 | 2021-01-08 17:25:55 +0000 | [diff] [blame] | 35 | #include "src/core/cpu/kernels/floor/list.h" |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace cpu |
| 40 | { |
| 41 | namespace kernels |
| 42 | { |
| 43 | namespace |
| 44 | { |
| 45 | struct FloorSelectorData |
| 46 | { |
| 47 | DataType dt; |
| 48 | }; |
| 49 | |
| 50 | using FloorSelectorPtr = std::add_pointer<bool(const FloorSelectorData &data)>::type; |
| 51 | using FloorUKernelPtr = std::add_pointer<void(const void *, void *, int)>::type; |
| 52 | |
| 53 | struct FloorUKernel |
| 54 | { |
| 55 | const char *name; |
| 56 | const FloorSelectorPtr is_selected; |
| 57 | FloorUKernelPtr func; |
| 58 | }; |
| 59 | |
| 60 | static const FloorUKernel available_kernels[] = |
| 61 | { |
| 62 | { |
| 63 | "fp16_neon_floor", |
| 64 | [](const FloorSelectorData & data) { return data.dt == DataType::F16; }, |
| 65 | REGISTER_FP16_NEON(arm_compute::cpu::fp16_neon_floor) |
| 66 | }, |
| 67 | { |
| 68 | "f32_neon_floor", |
| 69 | [](const FloorSelectorData & data) { return data.dt == DataType::F32; }, |
| 70 | REGISTER_FP32_NEON(arm_compute::cpu::fp32_neon_floor) |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | /** Micro-kernel selector |
| 75 | * |
| 76 | * @param[in] data Selection data passed to help pick the appropriate micro-kernel |
| 77 | * |
| 78 | * @return A matching micro-kernel else nullptr |
| 79 | */ |
| 80 | const FloorUKernel *get_implementation(const FloorSelectorData &data) |
| 81 | { |
| 82 | for(const auto &uk : available_kernels) |
| 83 | { |
| 84 | if(uk.is_selected(data)) |
| 85 | { |
| 86 | return &uk; |
| 87 | } |
| 88 | } |
| 89 | return nullptr; |
| 90 | } |
| 91 | |
| 92 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst) |
| 93 | { |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 95 | |
| 96 | const auto *uk = get_implementation(FloorSelectorData{ src->data_type() }); |
| 97 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->func == nullptr); |
| 98 | |
| 99 | // Validate in case of configured output |
| 100 | if(dst->total_size() > 0) |
| 101 | { |
| 102 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst); |
| 104 | } |
| 105 | |
| 106 | return Status{}; |
| 107 | } |
| 108 | } // namespace |
| 109 | |
| 110 | void CpuFloorKernel::configure(const ITensorInfo *src, ITensorInfo *dst) |
| 111 | { |
| 112 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 113 | |
| 114 | // Auto initialize output |
| 115 | auto_init_if_empty(*dst, src->tensor_shape(), 1, src->data_type()); |
| 116 | |
| 117 | // Validate |
| 118 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst)); |
| 119 | |
| 120 | // Configure kernel window |
| 121 | const Window win = calculate_max_window(*src, Steps()); |
| 122 | |
| 123 | Coordinates coord; |
| 124 | coord.set_num_dimensions(dst->num_dimensions()); |
| 125 | dst->set_valid_region(ValidRegion(coord, dst->tensor_shape())); |
| 126 | |
| 127 | ICPPKernel::configure(win); |
| 128 | } |
| 129 | |
| 130 | Window CpuFloorKernel::infer_window(const ITensorInfo *src, const ITensorInfo *dst) |
| 131 | { |
| 132 | ARM_COMPUTE_UNUSED(dst); |
| 133 | ARM_COMPUTE_ERROR_ON(!bool(validate_arguments(src, dst))); |
| 134 | |
| 135 | Window win; |
| 136 | win.use_tensor_dimensions(src->tensor_shape()); |
| 137 | return win; |
| 138 | } |
| 139 | |
| 140 | Status CpuFloorKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
| 141 | { |
| 142 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
| 143 | return Status{}; |
| 144 | } |
| 145 | |
| 146 | void CpuFloorKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 147 | { |
| 148 | ARM_COMPUTE_UNUSED(info); |
| 149 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 150 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 151 | |
| 152 | ARM_COMPUTE_ERROR_ON(tensors.empty()); |
| 153 | |
| 154 | const ITensor *src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 155 | ITensor *dst = tensors.get_tensor(TensorType::ACL_DST); |
| 156 | |
| 157 | const auto len = static_cast<int>(window.x().end()) - static_cast<int>(window.x().start()); |
| 158 | const auto *ukernel = get_implementation(FloorSelectorData{ src->info()->data_type() }); |
| 159 | |
| 160 | Window win{ window }; |
| 161 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 162 | |
| 163 | Iterator src_it(src, win); |
| 164 | Iterator dst_it(dst, win); |
| 165 | |
| 166 | execute_window_loop(win, [&](const Coordinates &) |
| 167 | { |
| 168 | ukernel->func(src_it.ptr(), dst_it.ptr(), len); |
| 169 | }, |
| 170 | src_it, dst_it); |
| 171 | } |
| 172 | |
| 173 | const char *CpuFloorKernel::name() const |
| 174 | { |
| 175 | return "CpuFloorKernel"; |
| 176 | } |
| 177 | } // namespace kernels |
| 178 | } // namespace cpu |
| 179 | } // namespace arm_compute |