• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020-2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ops/fusion/activation.h"
18 #include <vector>
19 #include "abstract/ops/op_infer.h"
20 #include "abstract/ops/primitive_infer_map.h"
21 #include "base/base.h"
22 #include "mindapi/base/shared_ptr.h"
23 #include "mindapi/ir/value.h"
24 #include "mindapi/src/helper.h"
25 #include "mindspore/core/ops/lite_ops.h"
26 #include "ops/op_name.h"
27 #include "ops/primitive_c.h"
28 #include "utils/check_convert_utils.h"
29 #include "utils/convert_utils_base.h"
30 #include "utils/log_adapter.h"
31 
32 namespace mindspore {
33 namespace ops {
34 MIND_API_OPERATOR_IMPL(Activation, BaseOperator);
set_alpha(const float alpha)35 void Activation::set_alpha(const float alpha) { (void)this->AddAttr(kAlpha, api::MakeValue(alpha)); }
36 
set_min_val(const float min_val)37 void Activation::set_min_val(const float min_val) { (void)this->AddAttr(kMinVal, api::MakeValue(min_val)); }
38 
set_max_val(const float max_val)39 void Activation::set_max_val(const float max_val) { (void)this->AddAttr(kMaxVal, api::MakeValue(max_val)); }
40 
set_activation_type(const ActivationType & activation_type)41 void Activation::set_activation_type(const ActivationType &activation_type) {
42   int64_t swi = activation_type;
43   (void)this->AddAttr(kActivationType, api::MakeValue(swi));
44 }
45 
get_alpha() const46 float Activation::get_alpha() const {
47   auto value_ptr = this->GetAttr(kAlpha);
48   return GetValue<float>(value_ptr);
49 }
50 
get_min_val() const51 float Activation::get_min_val() const {
52   auto value_ptr = this->GetAttr(kMinVal);
53   return GetValue<float>(value_ptr);
54 }
55 
get_max_val() const56 float Activation::get_max_val() const {
57   auto value_ptr = this->GetAttr(kMaxVal);
58   return GetValue<float>(value_ptr);
59 }
60 
get_activation_type() const61 ActivationType Activation::get_activation_type() const {
62   auto value_ptr = GetAttr(kActivationType);
63   return ActivationType(GetValue<int64_t>(value_ptr));
64 }
65 
set_approximate(bool approximate)66 void Activation::set_approximate(bool approximate) { (void)this->AddAttr(kApproximate, api::MakeValue(approximate)); }
67 
get_approximate() const68 bool Activation::get_approximate() const {
69   auto value_ptr = this->GetAttr(kApproximate);
70   return value_ptr != nullptr && GetValue<bool>(value_ptr);
71 }
72 
Init(const float alpha,const float min_val,const float max_val,const ActivationType & activation_type,bool approximate)73 void Activation::Init(const float alpha, const float min_val, const float max_val,
74                       const ActivationType &activation_type, bool approximate) {
75   this->set_alpha(alpha);
76   this->set_min_val(min_val);
77   this->set_max_val(max_val);
78   this->set_activation_type(activation_type);
79   this->set_approximate(approximate);
80 }
81 
82 class ActivationInfer : public abstract::OpInferBase {
83  public:
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const84   BaseShapePtr InferShape(const PrimitivePtr &primitive,
85                           const std::vector<AbstractBasePtr> &input_args) const override {
86     MS_EXCEPTION_IF_NULL(primitive);
87     (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1,
88                                              primitive->name());
89     MS_EXCEPTION_IF_NULL(input_args[0]);
90     return input_args[0]->GetShape();
91   }
92 
InferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args) const93   TypePtr InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) const override {
94     MS_EXCEPTION_IF_NULL(prim);
95     (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim->name());
96     MS_EXCEPTION_IF_NULL(input_args[0]);
97     return input_args[0]->GetType();
98   }
99 };
100 
101 REGISTER_PRIMITIVE_OP_INFER_IMPL(Activation, prim::kPrimActivation, ActivationInfer, false);
102 }  // namespace ops
103 }  // namespace mindspore
104