George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [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 | */ |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 24 | #include "src/core/cpu/kernels/CpuElementwiseUnaryKernel.h" |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 25 | |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/CPP/Validate.h" |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 31 | #include "src/core/common/Registrars.h" |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 32 | #include "src/core/cpu/kernels/elementwise/neon/elementwise_unary_list.h" |
| 33 | #include "src/core/cpu/kernels/elementwise/sve/elementwise_unary_list.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 36 | #include "support/ToolchainSupport.h" |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 37 | |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 40 | namespace cpu |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 44 | namespace |
| 45 | { |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 46 | using ElementwiseUnarySelector = std::add_pointer<bool(DataType)>::type; |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 47 | |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 48 | struct ElementwiseUnaryKernel |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 49 | { |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 50 | const char *name; |
| 51 | const ElementwiseUnarySelector is_selected; |
| 52 | CpuElementwiseUnaryKernel::ElementwiseUnaryUkernelPtr ukernel; |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static const ElementwiseUnaryKernel available_kernels[] = |
| 56 | { |
| 57 | #if defined(__ARM_FEATURE_SVE) |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 58 | { |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 59 | "fp32_sve_elementwise_unary", |
| 60 | [](DataType dt) { return dt == DataType::F32; }, |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 61 | REGISTER_FP32_SVE(arm_compute::cpu::elementwise_sve_op<float>), |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 62 | }, |
| 63 | { |
| 64 | "fp16_sve_elementwise_unary", |
| 65 | [](DataType dt) { return dt == DataType::F16; }, |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 66 | REGISTER_FP16_SVE(arm_compute::cpu::elementwise_sve_op<__fp16>), |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 67 | }, |
| 68 | { |
| 69 | "s32_sve_elementwise_unary", |
| 70 | [](DataType dt) { return dt == DataType::S32; }, |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 71 | REGISTER_INTEGER_SVE(arm_compute::cpu::elementwise_sve_op<int32_t>), |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 72 | }, |
| 73 | #endif // defined(__ARM_FEATURE_SVE) |
| 74 | { |
| 75 | "fp32_neon_elementwise_unary", |
| 76 | [](DataType dt) { return dt == DataType::F32; }, |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 77 | REGISTER_FP32_NEON(arm_compute::cpu::elementwise_op<float>), |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 78 | }, |
| 79 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 80 | { |
| 81 | "fp16_neon_elementwise_unary", |
| 82 | [](DataType dt) { return dt == DataType::F16; }, |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 83 | REGISTER_FP32_NEON(arm_compute::cpu::elementwise_op<__fp16>), |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 84 | }, |
| 85 | #endif // defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 86 | { |
| 87 | "s32_neon_elementwise_unary", |
| 88 | [](DataType dt) { return dt == DataType::S32; }, |
Georgios Pinitas | 70eb53b | 2021-01-06 19:42:21 +0000 | [diff] [blame] | 89 | REGISTER_INTEGER_NEON(arm_compute::cpu::elementwise_op<int32_t>), |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 90 | }, |
| 91 | }; |
| 92 | |
| 93 | const ElementwiseUnaryKernel *get_implementation(DataType dt) |
| 94 | { |
| 95 | for(const auto &uk : available_kernels) |
| 96 | { |
| 97 | if(uk.is_selected(dt)) |
| 98 | { |
| 99 | return &uk; |
| 100 | } |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 101 | } |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 102 | return nullptr; |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 103 | } |
Michalis Spyrou | 18e20ff | 2020-05-06 17:03:59 +0100 | [diff] [blame] | 104 | } // namespace |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 105 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 106 | CpuElementwiseUnaryKernel::CpuElementwiseUnaryKernel() |
| 107 | : _op() |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 108 | { |
| 109 | } |
| 110 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 111 | void CpuElementwiseUnaryKernel::configure(ElementWiseUnary op, const ITensorInfo &input, ITensorInfo &output) |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 112 | { |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 113 | ARM_COMPUTE_ERROR_THROW_ON(validate(op, input, output)); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 114 | |
| 115 | // Configure kernel window |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 116 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 117 | const TensorShape &out_shape = broadcast_pair.first; |
| 118 | const ValidRegion &valid_region = broadcast_pair.second; |
| 119 | |
| 120 | // Auto initialize output if not initialized |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 121 | auto_init_if_empty(output, out_shape, 1, input.data_type()); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 122 | |
| 123 | Window win = calculate_max_window(valid_region); |
| 124 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 125 | _op = op; |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 126 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 127 | ICpuKernel::configure(win); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 130 | Status CpuElementwiseUnaryKernel::validate(ElementWiseUnary op, const ITensorInfo &input, const ITensorInfo &output) |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 131 | { |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 132 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input); |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 133 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 134 | const auto *uk = get_implementation(input.data_type()); |
Sang-Hoon Park | af1870b | 2020-12-08 18:50:56 +0000 | [diff] [blame] | 135 | ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr); |
| 136 | |
Michalis Spyrou | 18e20ff | 2020-05-06 17:03:59 +0100 | [diff] [blame] | 137 | switch(op) |
| 138 | { |
| 139 | case ElementWiseUnary::EXP: |
| 140 | case ElementWiseUnary::RSQRT: |
| 141 | case ElementWiseUnary::LOG: |
| 142 | case ElementWiseUnary::ROUND: |
| 143 | case ElementWiseUnary::SIN: |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 144 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32); |
Michalis Spyrou | 18e20ff | 2020-05-06 17:03:59 +0100 | [diff] [blame] | 145 | break; |
| 146 | case ElementWiseUnary::NEG: |
| 147 | case ElementWiseUnary::ABS: |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 148 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input, 1, DataType::F16, DataType::F32, DataType::S32); |
Michalis Spyrou | 18e20ff | 2020-05-06 17:03:59 +0100 | [diff] [blame] | 149 | break; |
| 150 | default: |
| 151 | ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported"); |
| 152 | } |
| 153 | // Validate in case of configured output |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 154 | if(output.total_size() > 0) |
Michalis Spyrou | 18e20ff | 2020-05-06 17:03:59 +0100 | [diff] [blame] | 155 | { |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 156 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input, &output); |
Michalis Spyrou | 18e20ff | 2020-05-06 17:03:59 +0100 | [diff] [blame] | 157 | } |
| 158 | |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 159 | return Status{}; |
| 160 | } |
| 161 | |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 162 | void CpuElementwiseUnaryKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 163 | { |
| 164 | ARM_COMPUTE_UNUSED(info); |
| 165 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 166 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
| 167 | |
| 168 | auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 169 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 170 | auto func = get_implementation(src->info()->data_type())->ukernel; |
| 171 | ARM_COMPUTE_ERROR_ON(func == nullptr); |
| 172 | func(src, dst, window, _op); |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 173 | } |
Sang-Hoon Park | 7249f15 | 2021-01-22 11:55:03 +0000 | [diff] [blame] | 174 | } // namespace kernels |
| 175 | } // namespace cpu |
George Wort | 5a97b28 | 2018-12-21 16:21:04 +0000 | [diff] [blame] | 176 | } // namespace arm_compute |