1 /** 2 * Copyright 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 "schema/model_v0_generated.h" 18 #include "src/ops/populate/populate_register.h" 19 #include "nnacl/conv_parameter.h" 20 #include "nnacl/op_base.h" 21 22 namespace mindspore { 23 namespace lite { 24 namespace { PopulateConvParameter(const void * prim)25OpParameter *PopulateConvParameter(const void *prim) { 26 auto *primitive = static_cast<const schema::v0::Primitive *>(prim); 27 MS_ASSERT(primitive != nullptr); 28 auto conv2d_prim = primitive->value_as_Conv2D(); 29 if (conv2d_prim == nullptr) { 30 MS_LOG(ERROR) << "conv2d_prim is nullptr"; 31 return nullptr; 32 } 33 auto *conv_param = reinterpret_cast<ConvParameter *>(malloc(sizeof(ConvParameter))); 34 if (conv_param == nullptr) { 35 MS_LOG(ERROR) << "malloc ConvParameter failed."; 36 return nullptr; 37 } 38 memset(conv_param, 0, sizeof(ConvParameter)); 39 conv_param->op_parameter_.type_ = schema::PrimitiveType_Conv2DFusion; 40 41 conv_param->kernel_h_ = conv2d_prim->kernelH(); 42 conv_param->kernel_w_ = conv2d_prim->kernelW(); 43 conv_param->group_ = conv2d_prim->group(); 44 conv_param->stride_h_ = conv2d_prim->strideH(); 45 conv_param->stride_w_ = conv2d_prim->strideW(); 46 conv_param->pad_u_ = conv2d_prim->padUp(); 47 conv_param->pad_d_ = conv2d_prim->padDown(); 48 conv_param->pad_l_ = conv2d_prim->padLeft(); 49 conv_param->pad_r_ = conv2d_prim->padRight(); 50 conv_param->dilation_h_ = conv2d_prim->dilateH(); 51 conv_param->dilation_w_ = conv2d_prim->dilateW(); 52 conv_param->input_channel_ = conv2d_prim->channelIn(); 53 conv_param->output_channel_ = conv2d_prim->channelOut(); 54 conv_param->group_ = conv2d_prim->group(); 55 auto pad_mode = conv2d_prim->padMode(); 56 57 switch (pad_mode) { 58 case schema::v0::PadMode_SAME_UPPER: 59 conv_param->pad_mode_ = Pad_same; 60 break; 61 case schema::v0::PadMode_VALID: 62 conv_param->pad_mode_ = Pad_valid; 63 break; 64 default: 65 conv_param->pad_mode_ = Pad_pad; 66 break; 67 } 68 auto act_type = conv2d_prim->activationType(); 69 switch (act_type) { 70 case schema::v0::ActivationType_RELU: 71 conv_param->act_type_ = ActType_Relu; 72 break; 73 case schema::v0::ActivationType_RELU6: 74 conv_param->act_type_ = ActType_Relu6; 75 break; 76 default: 77 conv_param->act_type_ = ActType_No; 78 break; 79 } 80 return reinterpret_cast<OpParameter *>(conv_param); 81 } 82 } // namespace 83 84 Registry g_conv2DV0ParameterRegistry(schema::v0::PrimitiveType_Conv2D, PopulateConvParameter, SCHEMA_V0); 85 } // namespace lite 86 } // namespace mindspore 87