blob: f4775e0c196d0b0f03a11fbc83c25d98ebc4272c [file] [log] [blame]
josh minor4a3c6102020-01-06 16:40:46 -06001//
Teresa Charlin93f0ad02023-03-23 15:28:02 +00002// Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved.
josh minor4a3c6102020-01-06 16:40:46 -06003// 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 Charlin93f0ad02023-03-23 15:28:02 +000013#include "Ceil.hpp"
josh minor4a3c6102020-01-06 16:40:46 -060014#include "Exp.hpp"
Teresa Charlin50de4fa2021-05-31 18:47:33 +010015#include "Log.hpp"
josh minor4a3c6102020-01-06 16:40:46 -060016#include "Rsqrt.hpp"
Teresa Charlin50de4fa2021-05-31 18:47:33 +010017#include "Sin.hpp"
josh minor4a3c6102020-01-06 16:40:46 -060018#include "Sqrt.hpp"
19
20#include <Profiling.hpp>
21
22#include <armnn/TypesUtils.hpp>
23
24#include <functional>
25
26namespace armnn
27{
28
29RefElementwiseUnaryWorkload::RefElementwiseUnaryWorkload(const ElementwiseUnaryQueueDescriptor& desc,
30 const WorkloadInfo& info)
Finn Williams73c547d2022-02-15 20:47:34 +000031 : RefBaseWorkload<ElementwiseUnaryQueueDescriptor>(desc, info)
josh minor4a3c6102020-01-06 16:40:46 -060032{}
33
Finn Williamsb8181f72021-04-07 10:23:21 +010034void RefElementwiseUnaryWorkload::Execute() const
josh minor4a3c6102020-01-06 16:40:46 -060035{
Finn Williamsb8181f72021-04-07 10:23:21 +010036 Execute(m_Data.m_Inputs, m_Data.m_Outputs);
josh minor4a3c6102020-01-06 16:40:46 -060037}
38
Matthew Sloyan2d213a72022-06-30 17:13:04 +010039void RefElementwiseUnaryWorkload::ExecuteAsync(ExecutionData& executionData)
Finn Williamsb8181f72021-04-07 10:23:21 +010040{
41
Matthew Sloyan2d213a72022-06-30 17:13:04 +010042 WorkingMemDescriptor* workingMemDescriptor = static_cast<WorkingMemDescriptor*>(executionData.m_Data);
43 Execute(workingMemDescriptor->m_Inputs, workingMemDescriptor->m_Outputs);
Finn Williamsb8181f72021-04-07 10:23:21 +010044}
45
46void RefElementwiseUnaryWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const
josh minor4a3c6102020-01-06 16:40:46 -060047{
48 ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefElementwiseUnaryWorkload_Execute");
49
Finn Williamsb8181f72021-04-07 10:23:21 +010050 const TensorInfo& inputInfo = GetTensorInfo(inputs[0]);
51 const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
josh minor4a3c6102020-01-06 16:40:46 -060052
53 const TensorShape& inShape = inputInfo.GetShape();
54 const TensorShape& outShape = outputInfo.GetShape();
55
Finn Williamsb8181f72021-04-07 10:23:21 +010056 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 minor4a3c6102020-01-06 16:40:46 -060058
59 using AbsFunction = ElementwiseUnaryFunction<abs<InType>>;
Teresa Charlin93f0ad02023-03-23 15:28:02 +000060 using CeilFunction = ElementwiseUnaryFunction<ceil<InType>>;
josh minor4a3c6102020-01-06 16:40:46 -060061 using ExpFunction = ElementwiseUnaryFunction<exp<InType>>;
Teresa Charlin50de4fa2021-05-31 18:47:33 +010062 using LogFunction = ElementwiseUnaryFunction<log<InType>>;
josh minor4a3c6102020-01-06 16:40:46 -060063 using NegFunction = ElementwiseUnaryFunction<std::negate<InType>>;
64 using RsqrtFunction = ElementwiseUnaryFunction<rsqrt<InType>>;
Teresa Charlin50de4fa2021-05-31 18:47:33 +010065 using SinFunction = ElementwiseUnaryFunction<sin<InType>>;
josh minor4a3c6102020-01-06 16:40:46 -060066 using SqrtFunction = ElementwiseUnaryFunction<sqrt<InType>>;
67
68 switch (m_Data.m_Parameters.m_Operation)
69 {
70 case UnaryOperation::Abs:
71 {
Finn Williamsb8181f72021-04-07 10:23:21 +010072 AbsFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060073 break;
74 }
Teresa Charlin93f0ad02023-03-23 15:28:02 +000075 case UnaryOperation::Ceil:
76 {
77 CeilFunction(inShape, outShape, *input, *output);
78 break;
79 }
josh minor4a3c6102020-01-06 16:40:46 -060080 case UnaryOperation::Exp:
81 {
Finn Williamsb8181f72021-04-07 10:23:21 +010082 ExpFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060083 break;
84 }
Teresa Charlin50de4fa2021-05-31 18:47:33 +010085 case UnaryOperation::Log:
86 {
87 LogFunction(inShape, outShape, *input, *output);
88 break;
89 }
josh minor4a3c6102020-01-06 16:40:46 -060090 case UnaryOperation::Neg:
91 {
Finn Williamsb8181f72021-04-07 10:23:21 +010092 NegFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060093 break;
94 }
95 case UnaryOperation::Rsqrt:
96 {
Finn Williamsb8181f72021-04-07 10:23:21 +010097 RsqrtFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060098 break;
99 }
Teresa Charlin50de4fa2021-05-31 18:47:33 +0100100 case UnaryOperation::Sin:
101 {
102 SinFunction(inShape, outShape, *input, *output);
103 break;
104 }
josh minor4a3c6102020-01-06 16:40:46 -0600105 case UnaryOperation::Sqrt:
106 {
Finn Williamsb8181f72021-04-07 10:23:21 +0100107 SqrtFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -0600108 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