blob: be153636f95ffb78389abac188e33502c2363902 [file] [log] [blame]
josh minor4a3c6102020-01-06 16:40:46 -06001//
Teresa Charlin50de4fa2021-05-31 18:47:33 +01002// Copyright © 2019 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"
13#include "Exp.hpp"
Teresa Charlin50de4fa2021-05-31 18:47:33 +010014#include "Log.hpp"
josh minor4a3c6102020-01-06 16:40:46 -060015#include "Rsqrt.hpp"
Teresa Charlin50de4fa2021-05-31 18:47:33 +010016#include "Sin.hpp"
josh minor4a3c6102020-01-06 16:40:46 -060017#include "Sqrt.hpp"
18
19#include <Profiling.hpp>
20
21#include <armnn/TypesUtils.hpp>
22
23#include <functional>
24
25namespace armnn
26{
27
28RefElementwiseUnaryWorkload::RefElementwiseUnaryWorkload(const ElementwiseUnaryQueueDescriptor& desc,
29 const WorkloadInfo& info)
30 : BaseWorkload<ElementwiseUnaryQueueDescriptor>(desc, info)
31{}
32
Finn Williamsb8181f72021-04-07 10:23:21 +010033void RefElementwiseUnaryWorkload::Execute() const
josh minor4a3c6102020-01-06 16:40:46 -060034{
Finn Williamsb8181f72021-04-07 10:23:21 +010035 Execute(m_Data.m_Inputs, m_Data.m_Outputs);
josh minor4a3c6102020-01-06 16:40:46 -060036}
37
Finn Williamsb8181f72021-04-07 10:23:21 +010038void RefElementwiseUnaryWorkload::ExecuteAsync(WorkingMemDescriptor &workingMemDescriptor)
39{
40
41 Execute(workingMemDescriptor.m_Inputs, workingMemDescriptor.m_Outputs);
42}
43
44void RefElementwiseUnaryWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const
josh minor4a3c6102020-01-06 16:40:46 -060045{
46 ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefElementwiseUnaryWorkload_Execute");
47
Finn Williamsb8181f72021-04-07 10:23:21 +010048 const TensorInfo& inputInfo = GetTensorInfo(inputs[0]);
49 const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
josh minor4a3c6102020-01-06 16:40:46 -060050
51 const TensorShape& inShape = inputInfo.GetShape();
52 const TensorShape& outShape = outputInfo.GetShape();
53
Finn Williamsb8181f72021-04-07 10:23:21 +010054 std::unique_ptr<Decoder<InType>> input = MakeDecoder<InType>(inputInfo, inputs[0]->Map());
55 std::unique_ptr<Encoder<OutType>> output= MakeEncoder<OutType>(outputInfo, outputs[0]->Map());
josh minor4a3c6102020-01-06 16:40:46 -060056
57 using AbsFunction = ElementwiseUnaryFunction<abs<InType>>;
58 using ExpFunction = ElementwiseUnaryFunction<exp<InType>>;
Teresa Charlin50de4fa2021-05-31 18:47:33 +010059 using LogFunction = ElementwiseUnaryFunction<log<InType>>;
josh minor4a3c6102020-01-06 16:40:46 -060060 using NegFunction = ElementwiseUnaryFunction<std::negate<InType>>;
61 using RsqrtFunction = ElementwiseUnaryFunction<rsqrt<InType>>;
Teresa Charlin50de4fa2021-05-31 18:47:33 +010062 using SinFunction = ElementwiseUnaryFunction<sin<InType>>;
josh minor4a3c6102020-01-06 16:40:46 -060063 using SqrtFunction = ElementwiseUnaryFunction<sqrt<InType>>;
64
65 switch (m_Data.m_Parameters.m_Operation)
66 {
67 case UnaryOperation::Abs:
68 {
Finn Williamsb8181f72021-04-07 10:23:21 +010069 AbsFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060070 break;
71 }
72 case UnaryOperation::Exp:
73 {
Finn Williamsb8181f72021-04-07 10:23:21 +010074 ExpFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060075 break;
76 }
Teresa Charlin50de4fa2021-05-31 18:47:33 +010077 case UnaryOperation::Log:
78 {
79 LogFunction(inShape, outShape, *input, *output);
80 break;
81 }
josh minor4a3c6102020-01-06 16:40:46 -060082 case UnaryOperation::Neg:
83 {
Finn Williamsb8181f72021-04-07 10:23:21 +010084 NegFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060085 break;
86 }
87 case UnaryOperation::Rsqrt:
88 {
Finn Williamsb8181f72021-04-07 10:23:21 +010089 RsqrtFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -060090 break;
91 }
Teresa Charlin50de4fa2021-05-31 18:47:33 +010092 case UnaryOperation::Sin:
93 {
94 SinFunction(inShape, outShape, *input, *output);
95 break;
96 }
josh minor4a3c6102020-01-06 16:40:46 -060097 case UnaryOperation::Sqrt:
98 {
Finn Williamsb8181f72021-04-07 10:23:21 +010099 SqrtFunction(inShape, outShape, *input, *output);
josh minor4a3c6102020-01-06 16:40:46 -0600100 break;
101 }
102 default:
103 {
104 throw InvalidArgumentException(std::string("Unsupported unary operation ") +
105 GetUnaryOperationAsCString(m_Data.m_Parameters.m_Operation), CHECK_LOCATION());
106 }
107 }
108}
109
110} // namespace armnn