1 /**
2 * Copyright 2019-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 #include "src/common/ops/populate/populate_register.h"
17 #include "nnacl/fp32/activation_fp32.h"
18 using mindspore::schema::PrimitiveType_Activation;
19
20 namespace mindspore {
21 namespace lite {
PopulateRelu6Parameter(const void * prim)22 OpParameter *PopulateRelu6Parameter(const void *prim) {
23 if (prim == nullptr) {
24 MS_LOG(ERROR) << "prim is nullptr.";
25 return nullptr;
26 }
27 auto primitive = static_cast<const schema::Primitive *>(prim);
28 auto value = primitive->value_as_Activation();
29 if (value == nullptr) {
30 MS_LOG(ERROR) << "value is nullptr";
31 return nullptr;
32 }
33
34 auto *param = reinterpret_cast<ActivationParameter *>(malloc(sizeof(ActivationParameter)));
35 if (param == nullptr) {
36 MS_LOG(ERROR) << "malloc ActivationParameter failed.";
37 return nullptr;
38 }
39 (void)memset(param, 0, sizeof(ActivationParameter));
40
41 param->op_parameter_.type_ = primitive->value_type();
42 param->type_ = static_cast<int>(value->activation_type());
43 param->alpha_ = value->alpha();
44 param->min_val_ = value->min_val();
45 param->max_val_ = value->max_val();
46 param->approximate_ = value->approximate();
47
48 std::set<int> activation_types = {
49 schema::ActivationType_RELU, schema::ActivationType_RELU6, schema::ActivationType_LEAKY_RELU,
50 schema::ActivationType_SIGMOID, schema::ActivationType_TANH, schema::ActivationType_SWISH,
51 schema::ActivationType_HSWISH, schema::ActivationType_HSIGMOID, schema::ActivationType_HARD_TANH,
52 schema::ActivationType_GELU, schema::ActivationType_SOFTPLUS, schema::ActivationType_ELU};
53 if (activation_types.find(param->type_) == activation_types.end()) {
54 MS_LOG(ERROR) << "invalid activation type: " << param->type_;
55 free(param);
56 return nullptr;
57 }
58 return reinterpret_cast<OpParameter *>(param);
59 }
60
61 REG_POPULATE(PrimitiveType_Activation, PopulateRelu6Parameter, SCHEMA_CUR)
62 } // namespace lite
63 } // namespace mindspore
64