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