• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "nnacl/conv_parameter.h"
18 #include "src/ops/populate/populate_register.h"
19 using mindspore::schema::PrimitiveType_Conv2DFusion;
20 
21 namespace mindspore {
22 namespace lite {
PopulateConvParameter(const void * prim)23 OpParameter *PopulateConvParameter(const void *prim) {
24   auto primitive = static_cast<const schema::Primitive *>(prim);
25   MS_ASSERT(primitive != nullptr);
26   auto value = primitive->value_as_Conv2DFusion();
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) {
45     if (kernel_size->size() < kMinShapeSizeTwo) {
46       MS_LOG(ERROR) << "kernel size is invalid.";
47       free(param);
48       return nullptr;
49     }
50     param->kernel_h_ = static_cast<int>(*(kernel_size->begin()));
51     param->kernel_w_ = static_cast<int>(*(kernel_size->begin() + 1));
52   } else {
53     param->kernel_h_ = -1;
54     param->kernel_w_ = -1;
55   }
56   if (stride == nullptr || dilation == nullptr) {
57     MS_LOG(ERROR) << "kernel_size/stride/dilation is nullptr";
58     free(param);
59     return nullptr;
60   }
61   if (stride->size() < kMinShapeSizeTwo || dilation->size() < kMinShapeSizeTwo) {
62     MS_LOG(ERROR) << "stride size: " << stride->size() << ", dilation size: " << dilation->size();
63     free(param);
64     return nullptr;
65   }
66 
67   param->group_ = static_cast<int>(value->group());
68   param->stride_h_ = static_cast<int>(*(stride->begin()));
69   param->stride_w_ = static_cast<int>(*(stride->begin() + 1));
70   switch (value->pad_mode()) {
71     case schema::PadMode_SAME:
72       param->pad_mode_ = Pad_same;
73       break;
74     case schema::PadMode_VALID:
75       param->pad_mode_ = Pad_valid;
76       break;
77     default:
78       param->pad_mode_ = Pad_pad;
79   }
80   if (pad_list == nullptr || pad_list->size() < kMinShapeSizeFour) {
81     param->pad_u_ = 0;
82     param->pad_d_ = 0;
83     param->pad_l_ = 0;
84     param->pad_r_ = 0;
85   } else {
86     param->pad_u_ = static_cast<int>(*(pad_list->begin()));
87     param->pad_d_ = static_cast<int>(*(pad_list->begin() + 1));
88     param->pad_l_ = static_cast<int>(*(pad_list->begin() + kOffsetTwo));
89     param->pad_r_ = static_cast<int>(*(pad_list->begin() + kOffsetThree));
90   }
91   param->dilation_h_ = static_cast<int>(*(dilation->begin()));
92   param->dilation_w_ = static_cast<int>(*(dilation->begin() + 1));
93   param->input_channel_ = static_cast<int>(value->in_channel());
94   param->output_channel_ = static_cast<int>(value->out_channel());
95   auto act_type = value->activation_type();
96   switch (act_type) {
97     case schema::ActivationType_RELU:
98       param->act_type_ = ActType_Relu;
99       break;
100     case schema::ActivationType_RELU6:
101       param->act_type_ = ActType_Relu6;
102       break;
103     default:
104       param->act_type_ = ActType_No;
105   }
106   return reinterpret_cast<OpParameter *>(param);
107 }
108 
109 REG_POPULATE(PrimitiveType_Conv2DFusion, PopulateConvParameter, SCHEMA_CUR)
110 }  // namespace lite
111 }  // namespace mindspore
112