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/log_adapter.h"
17 #include "nnacl/conv_parameter.h"
18 #include "src/common/ops/populate/populate_register.h"
19 using mindspore::schema::PrimitiveType_AdderFusion;
20
21 namespace mindspore {
22 namespace lite {
PopulateAdderParameter(const void * prim)23 OpParameter *PopulateAdderParameter(const void *prim) {
24 auto primitive = static_cast<const schema::Primitive *>(prim);
25 MS_ASSERT(primitive != nullptr);
26 auto value = primitive->value_as_AdderFusion();
27 if (value == nullptr) {
28 MS_LOG(ERROR) << "value is nullptr";
29 return nullptr;
30 }
31
32 auto *param = reinterpret_cast<ConvParameter *>(malloc(sizeof(ConvParameter)));
33 if (param == nullptr) {
34 MS_LOG(ERROR) << "malloc ConvParameter failed.";
35 return nullptr;
36 }
37 memset(param, 0, sizeof(ConvParameter));
38
39 param->op_parameter_.type_ = primitive->value_type();
40 auto kernel_size = value->kernel_size();
41 auto stride = value->stride();
42 auto pad_list = value->pad_list();
43 auto dilation = value->dilation();
44 if (kernel_size == nullptr || stride == nullptr || pad_list == nullptr || dilation == nullptr) {
45 MS_LOG(ERROR) << "exist attr is nullptr";
46 free(param);
47 return nullptr;
48 }
49 if (kernel_size->size() < kMinShapeSizeTwo || stride->size() < kMinShapeSizeTwo ||
50 pad_list->size() < kMinShapeSizeFour || dilation->size() < kMinShapeSizeTwo) {
51 MS_LOG(ERROR) << "exist attr size is invalid.";
52 free(param);
53 return nullptr;
54 }
55 param->kernel_h_ = static_cast<int>(*(kernel_size->begin()));
56 param->kernel_w_ = static_cast<int>(*(kernel_size->begin() + 1));
57 param->group_ = static_cast<int>(value->group());
58 param->stride_h_ = static_cast<int>(*(stride->begin()));
59 param->stride_w_ = static_cast<int>(*(stride->begin() + 1));
60 param->pad_u_ = static_cast<int>(*(pad_list->begin()));
61 param->pad_d_ = static_cast<int>(*(pad_list->begin() + 1));
62 param->pad_l_ = static_cast<int>(*(pad_list->begin() + kOffsetTwo));
63 param->pad_r_ = static_cast<int>(*(pad_list->begin() + kOffsetThree));
64 param->dilation_h_ = static_cast<int>(*(dilation->begin()));
65 param->dilation_w_ = static_cast<int>(*(dilation->begin() + 1));
66 param->input_channel_ = static_cast<int>(value->in_channel());
67 param->output_channel_ = static_cast<int>(value->out_channel());
68 auto act_type = value->activation_type();
69 switch (act_type) {
70 case schema::ActivationType_RELU:
71 param->act_type_ = ActType_Relu;
72 break;
73 case schema::ActivationType_RELU6:
74 param->act_type_ = ActType_Relu6;
75 break;
76 default:
77 if (act_type != schema::ActivationType_NO_ACTIVATION) {
78 MS_LOG(ERROR) << "activation type does not support, " << act_type;
79 free(param);
80 return nullptr;
81 }
82 param->act_type_ = ActType_No;
83 break;
84 }
85 return reinterpret_cast<OpParameter *>(param);
86 }
87
88 REG_POPULATE(PrimitiveType_AdderFusion, PopulateAdderParameter, SCHEMA_CUR)
89 } // namespace lite
90 } // namespace mindspore
91