• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/ops/populate/populate_register.h"
17 #include "nnacl/op_base.h"
18 #include "nnacl/affine_parameter.h"
19 
20 using mindspore::schema::PrimitiveType_Affine;
21 
22 namespace mindspore {
23 namespace lite {
ReleaseParam(AffineParameter * affine,MatMulParameter * matmul)24 static void ReleaseParam(AffineParameter *affine, MatMulParameter *matmul) {
25   if (affine != nullptr) {
26     free(affine);
27   }
28   if (matmul != nullptr) {
29     free(matmul);
30   }
31 }
32 
PopulateAffineParameter(const void * prim)33 OpParameter *PopulateAffineParameter(const void *prim) {
34   MS_CHECK_TRUE_RET(prim != nullptr, nullptr);
35   auto primitive = static_cast<const schema::Primitive *>(prim);
36   auto value = primitive->value_as_Affine();
37   if (value == nullptr) {
38     MS_LOG(ERROR) << "cast affine_primitive to value failed";
39     return nullptr;
40   }
41   auto *affine_param = reinterpret_cast<AffineParameter *>(malloc(sizeof(AffineParameter)));
42   if (affine_param == nullptr) {
43     MS_LOG(ERROR) << "malloc Affine Parameter failed.";
44     return nullptr;
45   }
46   memset(affine_param, 0, sizeof(AffineParameter));
47   auto *matmul_param = reinterpret_cast<MatMulParameter *>(malloc(sizeof(MatMulParameter)));
48   if (matmul_param == nullptr) {
49     MS_LOG(ERROR) << "malloc MatMulParameter failed.";
50     ReleaseParam(affine_param, nullptr);
51     return nullptr;
52   }
53   memset(matmul_param, 0, sizeof(MatMulParameter));
54   matmul_param->op_parameter_.type_ = primitive->value_type();
55   matmul_param->b_transpose_ = value->transpose_b();
56   matmul_param->a_transpose_ = value->transpose_a();
57   matmul_param->has_bias_ = false;
58   matmul_param->act_type_ = ActType_No;
59 
60   affine_param->matmul_parameter_ = matmul_param;
61   affine_param->op_parameter_.type_ = primitive->value_type();
62   affine_param->activation_type_ = static_cast<int>(value->activation_type());
63   auto context_attr = value->context();
64   if (context_attr == nullptr) {
65     MS_LOG(ERROR) << "context is nullptr";
66     ReleaseParam(affine_param, matmul_param);
67     return nullptr;
68   }
69   std::vector<int> context(context_attr->begin(), context_attr->end());
70   affine_param->context_size_ = static_cast<int>(context.size());
71 
72   // malloc && memset for context
73   affine_param->context_ = reinterpret_cast<int *>(malloc(context.size() * sizeof(int)));
74   if (affine_param->context_ == nullptr) {
75     MS_LOG(ERROR) << "malloc param context_ for affine layer failed!";
76     ReleaseParam(affine_param, matmul_param);
77     return nullptr;
78   }
79   (void)memset(affine_param->context_, 0, context.size() * sizeof(int));
80   for (size_t i = 0; i < context.size(); ++i) {
81     affine_param->context_[i] = context.at(i);
82   }
83   affine_param->output_dim_ = value->output_dim();
84   return reinterpret_cast<OpParameter *>(affine_param);
85 }
86 
87 REG_POPULATE(PrimitiveType_Affine, PopulateAffineParameter, SCHEMA_CUR)
88 }  // namespace lite
89 }  // namespace mindspore
90