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_SpaceToBatch;
19
20 namespace mindspore {
21 namespace lite {
PopulateSpaceToBatchParameter(const void * prim)22 OpParameter *PopulateSpaceToBatchParameter(const void *prim) {
23 auto *primitive = static_cast<const schema::Primitive *>(prim);
24 MS_ASSERT(primitive != nullptr);
25 auto value = primitive->value_as_SpaceToBatch();
26 if (value == nullptr) {
27 MS_LOG(ERROR) << "param 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_size = value->block_size();
40 if (block_size == nullptr) {
41 MS_LOG(ERROR) << "block_size is nullptr";
42 free(param);
43 return nullptr;
44 }
45 auto block_sizes = std::vector<int64_t>(block_size->begin(), block_size->end());
46 if (block_sizes.size() > COMM_SHAPE_SIZE) {
47 MS_LOG(ERROR) << "The value of block_sizes.size() is too big";
48 free(param);
49 return nullptr;
50 }
51 param->m_ = static_cast<int>(block_sizes.size());
52
53 auto param_paddings = value->paddings();
54 if (param_paddings == nullptr) {
55 MS_LOG(ERROR) << "param_paddings is nullptr";
56 free(param);
57 return nullptr;
58 }
59 auto fb_paddings = param_paddings->data();
60 if (fb_paddings == nullptr) {
61 MS_LOG(ERROR) << "fb_paddings is nullptr";
62 free(param);
63 return nullptr;
64 }
65 if (fb_paddings->size() == 0 || *(fb_paddings->begin()) == nullptr || (*(fb_paddings->begin()))->data() == nullptr) {
66 MS_LOG(ERROR) << "exit attr is nullptr.";
67 free(param);
68 return nullptr;
69 }
70 if (fb_paddings->size() > static_cast<size_t>(INT_MAX) ||
71 (*(fb_paddings->begin()))->data()->size() > static_cast<size_t>(INT_MAX)) {
72 MS_LOG(ERROR) << "padding's size is too big.";
73 free(param);
74 return nullptr;
75 }
76 if (INT_MUL_OVERFLOW(static_cast<int>(fb_paddings->size()),
77 static_cast<int>((*(fb_paddings->begin()))->data()->size()))) {
78 MS_LOG(ERROR) << "padding's data length is too big.";
79 free(param);
80 return nullptr;
81 }
82 std::vector<int64_t> paddings;
83 for (auto fb_padding : *fb_paddings) {
84 auto paddings_data = fb_padding->data();
85 if (paddings_data == nullptr) {
86 MS_LOG(ERROR) << "paddings_data is nullptr";
87 free(param);
88 return nullptr;
89 }
90 auto paddings_vec = std::vector<int64_t>(paddings_data->begin(), paddings_data->end());
91 paddings.insert(paddings.end(), paddings_vec.begin(), paddings_vec.end());
92 }
93 if (paddings.size() > COMM_SHAPE_SIZE) {
94 MS_LOG(ERROR) << "Invalid paddings size " << paddings.size();
95 free(param);
96 return nullptr;
97 }
98 for (size_t i = 0; i < block_sizes.size(); ++i) {
99 param->block_sizes_[i] = static_cast<int>(block_sizes[i]);
100 }
101
102 for (size_t i = 0; i < paddings.size(); ++i) {
103 param->paddings_[i] = static_cast<int>(paddings[i]);
104 }
105 return reinterpret_cast<OpParameter *>(param);
106 }
107
108 REG_POPULATE(PrimitiveType_SpaceToBatch, PopulateSpaceToBatchParameter, SCHEMA_CUR)
109 } // namespace lite
110 } // namespace mindspore
111