Compute Library
 22.11
PostOps.cpp
Go to the documentation of this file.
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 
27 #include "arm_compute/core/Types.h"
29 #include "support/Cast.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>
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  {
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  }
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  }
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 
90 } // namespace reference
91 } // namespace validation
92 } // namespace test
93 } // namespace arm_compute
SimpleTensor< T > activation_layer(const SimpleTensor< T > &src, ActivationLayerInfo info, const QuantizationInfo &oq_info)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
SimpleTensor< T > post_ops(const SimpleTensor< T > &a, experimental::PostOpList< SimpleTensor< T >> post_ops)
(EXPERIMENTAL_POST_OPS)
Definition: PostOps.cpp:42
decltype(strategy::transforms) typedef type
SimpleTensor< T > arithmetic_operation(ArithmeticOperation op, const SimpleTensor< T > &src1, const SimpleTensor< T > &src2, SimpleTensor< T > &dst, ConvertPolicy convert_policy)
Copyright (c) 2017-2022 Arm Limited.
y*x if x < 0, x otherwise
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
A sequence of PostOps that can be appended to the end of other operators.
Definition: IPostOp.h:119