Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/runtime/cpu/operators/CpuElementwise.h" |
| 25 | #include "src/core/cpu/kernels/CpuElementwiseKernel.h" |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 26 | #include "src/core/helpers/WindowHelpers.h" |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace cpu |
| 31 | { |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 32 | void CpuElementwiseBase::run(ITensorPack &tensors) |
| 33 | { |
| 34 | // If the kernel has been configured, use the window from the kernel. |
| 35 | if(_kernel->is_window_configured()) |
| 36 | { |
| 37 | ICpuOperator::run(tensors); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | auto src0_info = tensors.get_const_tensor(TensorType::ACL_SRC_0)->info(); |
| 42 | auto src1_info = tensors.get_const_tensor(TensorType::ACL_SRC_1)->info(); |
Sang-Hoon Park | d0b7b4b | 2021-03-09 10:47:30 +0000 | [diff] [blame] | 43 | auto shape_and_window = compute_output_shape_and_window(src0_info->tensor_shape(), src1_info->tensor_shape()); |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 44 | ICpuOperator::run(tensors, shape_and_window.second); |
| 45 | } |
| 46 | |
Sang-Hoon Park | 6b0bf99 | 2021-02-17 13:12:53 +0000 | [diff] [blame] | 47 | template <ArithmeticOperation op> |
| 48 | void CpuElementwiseArithmetic<op>::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 49 | { |
| 50 | auto k = std::make_unique<kernels::CpuArithmeticKernel>(); |
Sang-Hoon Park | 6b0bf99 | 2021-02-17 13:12:53 +0000 | [diff] [blame] | 51 | k->configure(op, src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 52 | _kernel = std::move(k); |
| 53 | } |
| 54 | |
Sang-Hoon Park | 6b0bf99 | 2021-02-17 13:12:53 +0000 | [diff] [blame] | 55 | template <ArithmeticOperation op> |
| 56 | Status CpuElementwiseArithmetic<op>::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 57 | { |
Sang-Hoon Park | 6b0bf99 | 2021-02-17 13:12:53 +0000 | [diff] [blame] | 58 | return kernels::CpuArithmeticKernel::validate(op, src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Sang-Hoon Park | 6b0bf99 | 2021-02-17 13:12:53 +0000 | [diff] [blame] | 61 | template class CpuElementwiseArithmetic<ArithmeticOperation::MAX>; |
| 62 | template class CpuElementwiseArithmetic<ArithmeticOperation::MIN>; |
| 63 | template class CpuElementwiseArithmetic<ArithmeticOperation::SQUARED_DIFF>; |
| 64 | template class CpuElementwiseArithmetic<ArithmeticOperation::PRELU>; |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 65 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 66 | void CpuElementwiseDivision::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 67 | { |
| 68 | auto k = std::make_unique<kernels::CpuDivisionKernel>(); |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 69 | k->configure(src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 70 | _kernel = std::move(k); |
| 71 | } |
| 72 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 73 | Status CpuElementwiseDivision::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 74 | { |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 75 | return kernels::CpuDivisionKernel::validate(src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 78 | void CpuElementwisePower::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 79 | { |
| 80 | auto k = std::make_unique<kernels::CpuPowerKernel>(); |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 81 | k->configure(src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 82 | _kernel = std::move(k); |
| 83 | } |
| 84 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 85 | Status CpuElementwisePower::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 86 | { |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 87 | return kernels::CpuPowerKernel::validate(src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | template <ComparisonOperation COP> |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 91 | void CpuElementwiseComparisonStatic<COP>::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 92 | { |
| 93 | auto k = std::make_unique<kernels::CpuComparisonKernel>(); |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 94 | k->configure(COP, src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 95 | _kernel = std::move(k); |
| 96 | } |
| 97 | |
| 98 | template <ComparisonOperation COP> |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 99 | Status CpuElementwiseComparisonStatic<COP>::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 100 | { |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 101 | return kernels::CpuComparisonKernel::validate(COP, src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 104 | void CpuElementwiseComparison::configure(const ITensorInfo *src0, const ITensorInfo *src1, ITensorInfo *dst, ComparisonOperation op) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 105 | { |
| 106 | auto k = std::make_unique<kernels::CpuComparisonKernel>(); |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 107 | k->configure(op, src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 108 | _kernel = std::move(k); |
| 109 | } |
| 110 | |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 111 | Status CpuElementwiseComparison::validate(const ITensorInfo *src0, const ITensorInfo *src1, const ITensorInfo *dst, ComparisonOperation op) |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 112 | { |
Sang-Hoon Park | 668ccdc | 2021-02-03 10:32:59 +0000 | [diff] [blame] | 113 | return kernels::CpuComparisonKernel::validate(op, src0, src1, dst); |
Sang-Hoon Park | 63001ac | 2021-01-18 14:20:27 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Supported Specializations |
| 117 | template class CpuElementwiseComparisonStatic<ComparisonOperation::Equal>; |
| 118 | template class CpuElementwiseComparisonStatic<ComparisonOperation::NotEqual>; |
| 119 | template class CpuElementwiseComparisonStatic<ComparisonOperation::Greater>; |
| 120 | template class CpuElementwiseComparisonStatic<ComparisonOperation::GreaterEqual>; |
| 121 | template class CpuElementwiseComparisonStatic<ComparisonOperation::Less>; |
| 122 | template class CpuElementwiseComparisonStatic<ComparisonOperation::LessEqual>; |
| 123 | } // namespace cpu |
| 124 | } // namespace arm_compute |