josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 1 | // |
Teresa Charlin | 93f0ad0 | 2023-03-23 15:28:02 +0000 | [diff] [blame] | 2 | // Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved. |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "RefElementwiseUnaryWorkload.hpp" |
| 7 | |
| 8 | #include "Decoders.hpp" |
| 9 | #include "ElementwiseFunction.hpp" |
| 10 | #include "Encoders.hpp" |
| 11 | #include "RefWorkloadUtils.hpp" |
| 12 | #include "Abs.hpp" |
Teresa Charlin | 93f0ad0 | 2023-03-23 15:28:02 +0000 | [diff] [blame] | 13 | #include "Ceil.hpp" |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 14 | #include "Exp.hpp" |
Teresa Charlin | 50de4fa | 2021-05-31 18:47:33 +0100 | [diff] [blame] | 15 | #include "Log.hpp" |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 16 | #include "Rsqrt.hpp" |
Teresa Charlin | 50de4fa | 2021-05-31 18:47:33 +0100 | [diff] [blame] | 17 | #include "Sin.hpp" |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 18 | #include "Sqrt.hpp" |
| 19 | |
| 20 | #include <Profiling.hpp> |
| 21 | |
| 22 | #include <armnn/TypesUtils.hpp> |
| 23 | |
| 24 | #include <functional> |
| 25 | |
| 26 | namespace armnn |
| 27 | { |
| 28 | |
| 29 | RefElementwiseUnaryWorkload::RefElementwiseUnaryWorkload(const ElementwiseUnaryQueueDescriptor& desc, |
| 30 | const WorkloadInfo& info) |
Finn Williams | 73c547d | 2022-02-15 20:47:34 +0000 | [diff] [blame] | 31 | : RefBaseWorkload<ElementwiseUnaryQueueDescriptor>(desc, info) |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 32 | {} |
| 33 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 34 | void RefElementwiseUnaryWorkload::Execute() const |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 35 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 36 | Execute(m_Data.m_Inputs, m_Data.m_Outputs); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 37 | } |
| 38 | |
Matthew Sloyan | 2d213a7 | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 39 | void RefElementwiseUnaryWorkload::ExecuteAsync(ExecutionData& executionData) |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 40 | { |
| 41 | |
Matthew Sloyan | 2d213a7 | 2022-06-30 17:13:04 +0100 | [diff] [blame] | 42 | WorkingMemDescriptor* workingMemDescriptor = static_cast<WorkingMemDescriptor*>(executionData.m_Data); |
| 43 | Execute(workingMemDescriptor->m_Inputs, workingMemDescriptor->m_Outputs); |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void RefElementwiseUnaryWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 47 | { |
| 48 | ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefElementwiseUnaryWorkload_Execute"); |
| 49 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 50 | const TensorInfo& inputInfo = GetTensorInfo(inputs[0]); |
| 51 | const TensorInfo& outputInfo = GetTensorInfo(outputs[0]); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 52 | |
| 53 | const TensorShape& inShape = inputInfo.GetShape(); |
| 54 | const TensorShape& outShape = outputInfo.GetShape(); |
| 55 | |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 56 | std::unique_ptr<Decoder<InType>> input = MakeDecoder<InType>(inputInfo, inputs[0]->Map()); |
| 57 | std::unique_ptr<Encoder<OutType>> output= MakeEncoder<OutType>(outputInfo, outputs[0]->Map()); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 58 | |
| 59 | using AbsFunction = ElementwiseUnaryFunction<abs<InType>>; |
Teresa Charlin | 93f0ad0 | 2023-03-23 15:28:02 +0000 | [diff] [blame] | 60 | using CeilFunction = ElementwiseUnaryFunction<ceil<InType>>; |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 61 | using ExpFunction = ElementwiseUnaryFunction<exp<InType>>; |
Teresa Charlin | 50de4fa | 2021-05-31 18:47:33 +0100 | [diff] [blame] | 62 | using LogFunction = ElementwiseUnaryFunction<log<InType>>; |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 63 | using NegFunction = ElementwiseUnaryFunction<std::negate<InType>>; |
| 64 | using RsqrtFunction = ElementwiseUnaryFunction<rsqrt<InType>>; |
Teresa Charlin | 50de4fa | 2021-05-31 18:47:33 +0100 | [diff] [blame] | 65 | using SinFunction = ElementwiseUnaryFunction<sin<InType>>; |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 66 | using SqrtFunction = ElementwiseUnaryFunction<sqrt<InType>>; |
| 67 | |
| 68 | switch (m_Data.m_Parameters.m_Operation) |
| 69 | { |
| 70 | case UnaryOperation::Abs: |
| 71 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 72 | AbsFunction(inShape, outShape, *input, *output); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 73 | break; |
| 74 | } |
Teresa Charlin | 93f0ad0 | 2023-03-23 15:28:02 +0000 | [diff] [blame] | 75 | case UnaryOperation::Ceil: |
| 76 | { |
| 77 | CeilFunction(inShape, outShape, *input, *output); |
| 78 | break; |
| 79 | } |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 80 | case UnaryOperation::Exp: |
| 81 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 82 | ExpFunction(inShape, outShape, *input, *output); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 83 | break; |
| 84 | } |
Teresa Charlin | 50de4fa | 2021-05-31 18:47:33 +0100 | [diff] [blame] | 85 | case UnaryOperation::Log: |
| 86 | { |
| 87 | LogFunction(inShape, outShape, *input, *output); |
| 88 | break; |
| 89 | } |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 90 | case UnaryOperation::Neg: |
| 91 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 92 | NegFunction(inShape, outShape, *input, *output); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 93 | break; |
| 94 | } |
| 95 | case UnaryOperation::Rsqrt: |
| 96 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 97 | RsqrtFunction(inShape, outShape, *input, *output); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 98 | break; |
| 99 | } |
Teresa Charlin | 50de4fa | 2021-05-31 18:47:33 +0100 | [diff] [blame] | 100 | case UnaryOperation::Sin: |
| 101 | { |
| 102 | SinFunction(inShape, outShape, *input, *output); |
| 103 | break; |
| 104 | } |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 105 | case UnaryOperation::Sqrt: |
| 106 | { |
Finn Williams | b8181f7 | 2021-04-07 10:23:21 +0100 | [diff] [blame] | 107 | SqrtFunction(inShape, outShape, *input, *output); |
josh minor | 4a3c610 | 2020-01-06 16:40:46 -0600 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | default: |
| 111 | { |
| 112 | throw InvalidArgumentException(std::string("Unsupported unary operation ") + |
| 113 | GetUnaryOperationAsCString(m_Data.m_Parameters.m_Operation), CHECK_LOCATION()); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | } // namespace armnn |