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 "ir/dtype/type_id.h"
17 #include "schema/model_v0_generated.h"
18 #include "src/ops/populate/populate_register.h"
19 #include "nnacl/constant_of_shape_parameter.h"
20
21 namespace mindspore::lite {
22 namespace {
PopulateConstantOfShapeParameter(const void * prim)23 OpParameter *PopulateConstantOfShapeParameter(const void *prim) {
24 MS_CHECK_TRUE_RET(prim != nullptr, nullptr);
25 auto *primitive = static_cast<const schema::v0::Primitive *>(prim);
26 auto attr = primitive->value_as_ConstantOfShape();
27 MS_CHECK_TRUE_RET(attr != nullptr, nullptr);
28 auto value = attr->value();
29 MS_CHECK_TRUE_RET(value != nullptr, nullptr);
30 if (value->size() == 0 || value->size() > 1) {
31 MS_LOG(ERROR) << "The value of constant of shape is empty or more than 1.";
32 return nullptr;
33 }
34 auto *param = reinterpret_cast<ConstantOfShapeParameter *>(malloc(sizeof(ConstantOfShapeParameter)));
35 if (param == nullptr) {
36 MS_LOG(ERROR) << "malloc ConstantOfShapeParameter failed.";
37 return nullptr;
38 }
39 memset(param, 0, sizeof(ConstantOfShapeParameter));
40 param->op_parameter_.type_ = schema::PrimitiveType_ConstantOfShape;
41 param->data_type_ = attr->dataType();
42 switch (param->data_type_) {
43 case kNumberTypeFloat32:
44 param->value_.f32_value_ = value->data()[0];
45 break;
46 case kNumberTypeInt32:
47 param->value_.int32_value_ = static_cast<int32_t>(value->data()[0]);
48 break;
49 default:
50 MS_LOG(ERROR) << "The value of constant of shape is invalid";
51 free(param);
52 return nullptr;
53 }
54 return reinterpret_cast<OpParameter *>(param);
55 }
56 } // namespace
57
58 Registry g_constantOfShapeV0ParameterRegistry(schema::v0::PrimitiveType_ConstantOfShape,
59 PopulateConstantOfShapeParameter, SCHEMA_V0);
60 } // namespace mindspore::lite
61