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/ops/populate/populate_register.h"
17 #include "nnacl/fp32/space_to_batch_fp32.h"
18 using mindspore::schema::PrimitiveType_SpaceToBatchND;
19
20 namespace mindspore {
21 namespace lite {
PopulateSpaceToBatchNDParameter(const void * prim)22 OpParameter *PopulateSpaceToBatchNDParameter(const void *prim) {
23 auto *primitive = static_cast<const schema::Primitive *>(prim);
24 MS_ASSERT(primitive != nullptr);
25 auto value = primitive->value_as_SpaceToBatchND();
26 if (value == nullptr) {
27 MS_LOG(ERROR) << "value is nullptr";
28 return nullptr;
29 }
30
31 auto *param = reinterpret_cast<SpaceToBatchParameter *>(malloc(sizeof(SpaceToBatchParameter)));
32 if (param == nullptr) {
33 MS_LOG(ERROR) << "malloc SpaceToBatchParameter failed.";
34 return nullptr;
35 }
36 memset(param, 0, sizeof(SpaceToBatchParameter));
37
38 param->op_parameter_.type_ = primitive->value_type();
39 auto block_shape = value->block_shape();
40 if (block_shape == nullptr) {
41 return reinterpret_cast<OpParameter *>(param);
42 }
43 auto block_shapes = std::vector<int64_t>(block_shape->begin(), block_shape->end());
44 if (block_shapes.size() > COMM_SHAPE_SIZE) {
45 MS_LOG(ERROR) << "The value of block_shapes.size() is too big";
46 free(param);
47 return nullptr;
48 }
49
50 auto param_paddings = value->paddings();
51 if (param_paddings == nullptr) {
52 MS_LOG(ERROR) << "param_paddings is nullptr";
53 free(param);
54 return nullptr;
55 }
56 auto fb_paddings = param_paddings->data();
57 if (fb_paddings == nullptr) {
58 MS_LOG(ERROR) << "fb_paddings is nullptr";
59 free(param);
60 return nullptr;
61 }
62 if (fb_paddings->size() == 0 || *(fb_paddings->begin()) == nullptr || (*(fb_paddings->begin()))->data() == nullptr) {
63 MS_LOG(ERROR) << "exit attr is nullptr.";
64 free(param);
65 return nullptr;
66 }
67 if (fb_paddings->size() > static_cast<size_t>(INT_MAX) ||
68 (*(fb_paddings->begin()))->data()->size() > static_cast<size_t>(INT_MAX)) {
69 MS_LOG(ERROR) << "padding's size is too big.";
70 free(param);
71 return nullptr;
72 }
73 if (INT_MUL_OVERFLOW(static_cast<int>(fb_paddings->size()),
74 static_cast<int>((*(fb_paddings->begin()))->data()->size()))) {
75 MS_LOG(ERROR) << "padding's data length is too big.";
76 free(param);
77 return nullptr;
78 }
79 std::vector<int64_t> paddings;
80 for (auto fb_padding : *fb_paddings) {
81 auto paddings_data = fb_padding->data();
82 if (paddings_data == nullptr) {
83 MS_LOG(ERROR) << "paddings_data is nullptr";
84 free(param);
85 return nullptr;
86 }
87 auto paddings_vec = std::vector<int64_t>(paddings_data->begin(), paddings_data->end());
88 paddings.insert(paddings.end(), paddings_vec.begin(), paddings_vec.end());
89 }
90 if (paddings.size() > COMM_SHAPE_SIZE) {
91 MS_LOG(ERROR) << "Invalid paddings size " << paddings.size();
92 free(param);
93 return nullptr;
94 }
95 for (size_t i = 0; i < block_shapes.size(); ++i) {
96 param->block_sizes_[i] = static_cast<int>(block_shapes[i]);
97 }
98 param->m_ = block_shapes.size();
99
100 for (size_t i = 0; i < paddings.size(); ++i) {
101 param->paddings_[i] = static_cast<int>(paddings[i]);
102 }
103 return reinterpret_cast<OpParameter *>(param);
104 }
105
106 REG_POPULATE(PrimitiveType_SpaceToBatchND, PopulateSpaceToBatchNDParameter, SCHEMA_CUR)
107 } // namespace lite
108 } // namespace mindspore
109