• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018-2022 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "src/cpu/kernels/CpuElementwiseUnaryKernel.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/Validate.h"
30 #include "src/core/CPP/Validate.h"
31 #include "src/core/common/Registrars.h"
32 #include "src/core/helpers/AutoConfiguration.h"
33 #include "src/core/helpers/WindowHelpers.h"
34 #include "src/cpu/kernels/elementwise_unary/list.h"
35 #include "support/ToolchainSupport.h"
36 
37 namespace arm_compute
38 {
39 namespace cpu
40 {
41 namespace kernels
42 {
43 namespace
44 {
45 static const std::vector<CpuElementwiseUnaryKernel::ElementwiseUnaryKernel> available_kernels =
46 {
47     {
48         "sve_fp32_elementwise_unary",
49         [](const DataTypeISASelectorData & data)
__anon51624f530202() 50         {
51             return (data.dt == DataType::F32 && data.isa.sve);
52         },
53         REGISTER_FP32_SVE(sve_fp32_elementwise_unary)
54     },
55     {
56         "sve_fp16_elementwise_unary",
57         [](const DataTypeISASelectorData & data)
__anon51624f530302() 58         {
59             return (data.dt == DataType::F16 && data.isa.sve && data.isa.fp16);
60         },
61         REGISTER_FP16_SVE(sve_fp16_elementwise_unary),
62     },
63     {
64         "sve_s32_elementwise_unary",
65         [](const DataTypeISASelectorData & data)
__anon51624f530402() 66         {
67             return (data.dt == DataType::S32 && data.isa.sve);
68         },
69         REGISTER_INTEGER_SVE(sve_s32_elementwise_unary),
70     },
71     {
72         "neon_fp32_elementwise_unary",
73         [](const DataTypeISASelectorData & data)
__anon51624f530502() 74         {
75             return data.dt == DataType::F32;
76         },
77         REGISTER_FP32_NEON(neon_fp32_elementwise_unary),
78     },
79     {
80         "neon_fp16_elementwise_unary",
81         [](const DataTypeISASelectorData & data)
__anon51624f530602() 82         {
83             return data.dt == DataType::F16 && data.isa.fp16;
84         },
85         REGISTER_FP16_NEON(neon_fp16_elementwise_unary),
86     },
87     {
88         "neon_s32_elementwise_unary",
89         [](const DataTypeISASelectorData & data)
__anon51624f530702() 90         {
91             return data.dt == DataType::S32;
92         },
93         REGISTER_INTEGER_NEON(neon_s32_elementwise_unary),
94     },
95 };
96 
97 } // namespace
98 
configure(ElementWiseUnary op,const ITensorInfo & src,ITensorInfo & dst)99 void CpuElementwiseUnaryKernel::configure(ElementWiseUnary op, const ITensorInfo &src, ITensorInfo &dst)
100 {
101     ARM_COMPUTE_ERROR_THROW_ON(validate(op, src, dst));
102     const auto uk = CpuElementwiseUnaryKernel::get_implementation(DataTypeISASelectorData{ src.data_type(), CPUInfo::get().get_isa() });
103     ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
104 
105     _op         = op;
106     _run_method = uk->ukernel;
107     _name       = std::string("CpuElementwiseUnaryKernel").append("/").append(uk->name);
108 
109     // If input shape is dynamic, expect a configured window and dst at run-time.
110     if(src.is_dynamic())
111     {
112         return;
113     }
114 
115     auto shape_and_window = compute_output_shape_and_window(src.tensor_shape());
116     auto_init_if_empty(dst, shape_and_window.first, 1, src.data_type());
117     ICpuKernel::configure(shape_and_window.second);
118 }
119 
validate(ElementWiseUnary op,const ITensorInfo & src,const ITensorInfo & dst)120 Status CpuElementwiseUnaryKernel::validate(ElementWiseUnary op, const ITensorInfo &src, const ITensorInfo &dst)
121 {
122     ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&src);
123 
124     const auto *uk = CpuElementwiseUnaryKernel::get_implementation(DataTypeISASelectorData{ src.data_type(), CPUInfo::get().get_isa() });
125 
126     ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
127 
128     switch(op)
129     {
130         case ElementWiseUnary::EXP:
131         case ElementWiseUnary::RSQRT:
132         case ElementWiseUnary::LOG:
133         case ElementWiseUnary::ROUND:
134         case ElementWiseUnary::SIN:
135             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::F16, DataType::F32);
136             break;
137         case ElementWiseUnary::NEG:
138         case ElementWiseUnary::ABS:
139             ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&src, 1, DataType::F16, DataType::F32, DataType::S32);
140             break;
141         default:
142             ARM_COMPUTE_ERROR("ElementWiseUnary operation not supported");
143     }
144     // Validate in case of configured dst
145     if(dst.total_size() > 0)
146     {
147         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&src, &dst);
148     }
149 
150     return Status{};
151 }
152 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)153 void CpuElementwiseUnaryKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
154 {
155     ARM_COMPUTE_UNUSED(info);
156 
157     auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
158     auto dst = tensors.get_tensor(TensorType::ACL_DST);
159 
160     _run_method(src, dst, window, _op);
161 }
162 
name() const163 const char *CpuElementwiseUnaryKernel::name() const
164 {
165     return _name.c_str();
166 }
167 
get_available_kernels()168 const std::vector<CpuElementwiseUnaryKernel::ElementwiseUnaryKernel> &CpuElementwiseUnaryKernel::get_available_kernels()
169 {
170     return available_kernels;
171 }
172 
173 } // namespace kernels
174 } // namespace cpu
175 } // namespace arm_compute
176