1 /*
2 * Copyright (c) 2021 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 "PostOps.h"
25
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/Types.h"
28 #include "arm_compute/core/experimental/PostOps.h"
29 #include "support/Cast.h"
30 #include "tests/validation/reference/ActivationLayer.h"
31 #include "tests/validation/reference/ElementwiseOperations.h"
32
33 namespace arm_compute
34 {
35 namespace test
36 {
37 namespace validation
38 {
39 namespace reference
40 {
41 template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
post_ops(const SimpleTensor<T> & a,experimental::PostOpList<SimpleTensor<T>> post_ops)42 SimpleTensor<T> post_ops(const SimpleTensor<T> &a, experimental::PostOpList<SimpleTensor<T>> post_ops)
43 {
44 // Create reference
45 SimpleTensor<T> dst{ a };
46
47 for(auto &post_op : post_ops.get_list())
48 {
49 switch(post_op->type())
50 {
51 case experimental::PostOpType::Activation:
52 {
53 const auto _post_op = utils::cast::polymorphic_downcast<const experimental::PostOpAct<SimpleTensor<T>> *>(post_op.get());
54 dst = reference::activation_layer(dst, _post_op->_act_info);
55 break;
56 }
57 case experimental::PostOpType::Eltwise_Add:
58 {
59 const auto _post_op = utils::cast::polymorphic_downcast<const experimental::PostOpEltwiseAdd<SimpleTensor<T>> *>(post_op.get());
60 dst = reference::arithmetic_operation(ArithmeticOperation::ADD, dst, _post_op->_addend, dst, _post_op->_policy);
61 break;
62 }
63 case experimental::PostOpType::Eltwise_PRelu:
64 {
65 const auto _post_op = utils::cast::polymorphic_downcast<const experimental::PostOpEltwisePRelu<SimpleTensor<T>> *>(post_op.get());
66
67 // If previous main operation output is the the first pRelu argument, then pass it as src1 parameter of the arithmetic operation
68 if(_post_op->_prev_dst_pos == 0)
69 {
70 dst = reference::arithmetic_operation(ArithmeticOperation::PRELU, dst, _post_op->_alpha_param, dst, _post_op->_policy);
71 }
72 // If previous main operation output is the the second pRelu argument, then pass it as src2 parameter of the arithmetic operation
73 else if(_post_op->_prev_dst_pos == 1)
74 {
75 dst = reference::arithmetic_operation(ArithmeticOperation::PRELU, _post_op->_alpha_param, dst, dst, _post_op->_policy);
76 }
77 break;
78 }
79 default:
80 {
81 ARM_COMPUTE_ERROR("Unsupported PostOpType");
82 }
83 }
84 }
85 return dst;
86 }
87
88 template SimpleTensor<float> post_ops(const SimpleTensor<float> &a, experimental::PostOpList<SimpleTensor<float>> post_ops);
89 template SimpleTensor<half> post_ops(const SimpleTensor<half> &a, experimental::PostOpList<SimpleTensor<half>> post_ops);
90 } // namespace reference
91 } // namespace validation
92 } // namespace test
93 } // namespace arm_compute