• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved.
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 "Ceil.hpp"
14 #include "Exp.hpp"
15 #include "Log.hpp"
16 #include "Rsqrt.hpp"
17 #include "Sin.hpp"
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 
RefElementwiseUnaryWorkload(const ElementwiseUnaryQueueDescriptor & desc,const WorkloadInfo & info)29 RefElementwiseUnaryWorkload::RefElementwiseUnaryWorkload(const ElementwiseUnaryQueueDescriptor& desc,
30                                                          const WorkloadInfo& info)
31     : RefBaseWorkload<ElementwiseUnaryQueueDescriptor>(desc, info)
32 {}
33 
Execute() const34 void RefElementwiseUnaryWorkload::Execute() const
35 {
36     Execute(m_Data.m_Inputs, m_Data.m_Outputs);
37 }
38 
ExecuteAsync(ExecutionData & executionData)39 void RefElementwiseUnaryWorkload::ExecuteAsync(ExecutionData& executionData)
40 {
41 
42     WorkingMemDescriptor* workingMemDescriptor = static_cast<WorkingMemDescriptor*>(executionData.m_Data);
43     Execute(workingMemDescriptor->m_Inputs, workingMemDescriptor->m_Outputs);
44 }
45 
Execute(std::vector<ITensorHandle * > inputs,std::vector<ITensorHandle * > outputs) const46 void RefElementwiseUnaryWorkload::Execute(std::vector<ITensorHandle*> inputs, std::vector<ITensorHandle*> outputs) const
47 {
48     ARMNN_SCOPED_PROFILING_EVENT(Compute::CpuRef, "RefElementwiseUnaryWorkload_Execute");
49 
50     const TensorInfo& inputInfo = GetTensorInfo(inputs[0]);
51     const TensorInfo& outputInfo = GetTensorInfo(outputs[0]);
52 
53     const TensorShape& inShape = inputInfo.GetShape();
54     const TensorShape& outShape = outputInfo.GetShape();
55 
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());
58 
59     using AbsFunction   = ElementwiseUnaryFunction<abs<InType>>;
60     using CeilFunction  = ElementwiseUnaryFunction<ceil<InType>>;
61     using ExpFunction   = ElementwiseUnaryFunction<exp<InType>>;
62     using LogFunction   = ElementwiseUnaryFunction<log<InType>>;
63     using NegFunction   = ElementwiseUnaryFunction<std::negate<InType>>;
64     using RsqrtFunction = ElementwiseUnaryFunction<rsqrt<InType>>;
65     using SinFunction   = ElementwiseUnaryFunction<sin<InType>>;
66     using SqrtFunction  = ElementwiseUnaryFunction<sqrt<InType>>;
67 
68     switch (m_Data.m_Parameters.m_Operation)
69     {
70         case UnaryOperation::Abs:
71         {
72             AbsFunction(inShape, outShape, *input, *output);
73             break;
74         }
75         case UnaryOperation::Ceil:
76         {
77             CeilFunction(inShape, outShape, *input, *output);
78             break;
79         }
80         case UnaryOperation::Exp:
81         {
82             ExpFunction(inShape, outShape, *input, *output);
83             break;
84         }
85         case UnaryOperation::Log:
86         {
87             LogFunction(inShape, outShape, *input, *output);
88             break;
89         }
90         case UnaryOperation::Neg:
91         {
92             NegFunction(inShape, outShape, *input, *output);
93             break;
94         }
95         case UnaryOperation::Rsqrt:
96         {
97             RsqrtFunction(inShape, outShape, *input, *output);
98             break;
99         }
100         case UnaryOperation::Sin:
101         {
102             SinFunction(inShape, outShape, *input, *output);
103             break;
104         }
105         case UnaryOperation::Sqrt:
106         {
107             SqrtFunction(inShape, outShape, *input, *output);
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
119