1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "space_to_batch_nd_builder.h"
17
18 #include "mindir.h"
19
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
23 static const int INPUT_NUM = 1;
24 static const int OUTPUT_NUM = 1;
25 static const std::string OP_NAME = "SpaceToBatchND";
26 static const int PADDINGS_DATA_SIZE = 2;
27 static const int VECT_DATA_SIZE = 2;
28 static const int BLOCKSHAPE_RANK = 1;
29 static const int PADDINGS_RANK = 2;
30 static const int BLOCK_SIZE = 2;
31 static const int PADDINGS_SIZE = 4;
32
SpaceToBatchNDBuilder()33 SpaceToBatchNDBuilder::SpaceToBatchNDBuilder() {}
34
~SpaceToBatchNDBuilder()35 SpaceToBatchNDBuilder::~SpaceToBatchNDBuilder() {}
36
SetBlockShape(std::shared_ptr<NNTensor> tensor)37 OH_NN_ReturnCode SpaceToBatchNDBuilder::SetBlockShape(std::shared_ptr<NNTensor> tensor)
38 {
39 if (tensor->GetDataType() != OH_NN_INT64) {
40 LOGE("[SpaceToBatchNDBuilder] The 2nd input blockShape should be type OH_NN_INT64.");
41 return OH_NN_INVALID_PARAMETER;
42 }
43
44 auto blockshape_shape = tensor->GetDimensions();
45 if (blockshape_shape.size() != BLOCKSHAPE_RANK) {
46 LOGE("[SpaceToBatchNDBuilder] Invalid rank of shape of 2nd input blockShape, should be 1 dimensions.");
47 return OH_NN_INVALID_PARAMETER;
48 }
49
50 if (tensor->GetElementCount() != BLOCK_SIZE) {
51 LOGE("[SpaceToBatchNDBuilder] The 2nd input blockShape size should be 2.");
52 return OH_NN_INVALID_PARAMETER;
53 }
54
55 void* buffer = tensor->GetBuffer();
56 if (buffer == nullptr) {
57 LOGE("[SpaceToBatchNDBuilder] Tensor buffer is nullptr.");
58 return OH_NN_INVALID_PARAMETER;
59 }
60
61 const int64_t* blockShapeData = reinterpret_cast<const int64_t*>(buffer);
62 const uint32_t elementSize = tensor->GetElementCount();
63 for (uint32_t i = 0; i < elementSize; ++i) {
64 block_shape.push_back(blockShapeData[i]);
65 }
66
67 return OH_NN_SUCCESS;
68 }
69
SetPaddings(std::shared_ptr<NNTensor> tensor)70 OH_NN_ReturnCode SpaceToBatchNDBuilder::SetPaddings(std::shared_ptr<NNTensor> tensor)
71 {
72 if (tensor->GetDataType() != OH_NN_INT64) {
73 LOGE("[SpaceToBatchNDBuilder] The 3rd input paddings should be type OH_NN_INT64.");
74 return OH_NN_INVALID_PARAMETER;
75 }
76
77 auto paddings_shape = tensor->GetDimensions();
78 if (paddings_shape.size() != PADDINGS_RANK) {
79 LOGE("[SpaceToBatchNDBuilder] Invalid rank of shape of 3rd input paddings, should be 2 dimensions.");
80 return OH_NN_INVALID_PARAMETER;
81 }
82
83 if (tensor->GetElementCount() != PADDINGS_SIZE) {
84 LOGE("[SpaceToBatchNDBuilder] The 3rd input paddings size should be 4.");
85 return OH_NN_INVALID_PARAMETER;
86 }
87
88 OH_NN_ReturnCode returnCode = SetPadData(tensor);
89 if (returnCode != OH_NN_SUCCESS) {
90 LOGE("[SpaceToBatchNDBuilder] SetPadData failed.");
91 return returnCode;
92 }
93
94 return OH_NN_SUCCESS;
95 }
96 /**
97 * Build method.
98 * 1.set attr of ops.
99 * 2.set inputIndex of ops.
100 * 3.set outputIndex of ops.
101 */
Build(const std::vector<uint32_t> & paramsIndex,const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)102 OH_NN_ReturnCode SpaceToBatchNDBuilder::Build(const std::vector<uint32_t>& paramsIndex,
103 const std::vector<uint32_t>& inputsIndex,
104 const std::vector<uint32_t>& outputsIndex,
105 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
106 {
107 if (m_isBuild) {
108 LOGE("[SpaceToBatchNDBuilder] SpaceToBatchND operation has been build, cannot build again.");
109 return OH_NN_OPERATION_FORBIDDEN;
110 }
111
112 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
113 if (returnCode != OH_NN_SUCCESS) {
114 LOGE("[SpaceToBatchNDBuilder] Passed invalid input or output index.");
115 return returnCode;
116 }
117
118 m_inputsIndex = inputsIndex;
119 m_outputsIndex = outputsIndex;
120
121 for (int i : paramsIndex) {
122 std::shared_ptr<NNTensor> tensor = allTensors[i];
123 tensor->IdentifyOpParameter();
124 switch (tensor->GetType()) {
125 case OH_NN_SPACE_TO_BATCH_ND_BLOCK_SHAPE:
126 returnCode = SetBlockShape(tensor);
127 break;
128 case OH_NN_SPACE_TO_BATCH_ND_PADDINGS:
129 returnCode = SetPaddings(tensor);
130 break;
131 default:
132 LOGE("[SpaceToBatchNDBuilder] Parameter Type is invalid. type=%d", tensor->GetType());
133 return OH_NN_INVALID_PARAMETER;
134 }
135
136 if (returnCode != OH_NN_SUCCESS) {
137 LOGE("[SpaceToBatchNDBuilder] Passed invalid param.");
138 return returnCode;
139 }
140 }
141
142 // The quantization type of the first output determinies that of the operator.
143 SetQuantType(outputsIndex, allTensors);
144
145 m_isBuild = true;
146 m_name = OP_NAME;
147 return OH_NN_SUCCESS;
148 }
149
SetPadData(std::shared_ptr<NNTensor> tensor)150 OH_NN_ReturnCode SpaceToBatchNDBuilder::SetPadData(std::shared_ptr<NNTensor> tensor)
151 {
152 paddings.clear();
153
154 void* buffer = tensor->GetBuffer();
155 if (buffer == nullptr) {
156 LOGE("[SpaceToBatchNDBuilder] Tensor buffer is nullptr.");
157 return OH_NN_INVALID_PARAMETER;
158 }
159
160 const int64_t* paddingsData = reinterpret_cast<const int64_t*>(buffer);
161 for (int i = 0; i < PADDINGS_DATA_SIZE; i++) {
162 std::vector<int64_t> vect_data;
163 vect_data.reserve(VECT_DATA_SIZE);
164 for (int i = 0; i < VECT_DATA_SIZE; ++i) {
165 vect_data.push_back(paddingsData[i]);
166 }
167 paddings.push_back(vect_data);
168 }
169 return OH_NN_SUCCESS;
170 }
171
GetPrimitive()172 LiteGraphTensorPtr SpaceToBatchNDBuilder::GetPrimitive()
173 {
174 if (!m_isBuild) {
175 LOGE("[SpaceToBatchNDBuilder] Cannot get primitive before call build.");
176 return {nullptr, DestroyLiteGraphPrimitive};
177 }
178
179 auto primitive = mindspore::lite::MindIR_SpaceToBatchND_CreatePrimitive(block_shape, paddings);
180 if (primitive == nullptr) {
181 LOGE("[SpaceToBatchNDBuilder] MindIR_SpaceToBatchND_CreatePrimitive failed.");
182 return {nullptr, DestroyLiteGraphPrimitive};
183 }
184
185 LiteGraphTensorPtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
186 return graphPrimitivePtr;
187 }
188
189 REGISTER_OPS(SpaceToBatchNDBuilder, OH_NN_OPS_SPACE_TO_BATCH_ND);
190 } // namespace Ops
191 } // namespace NeuralNetworkRuntime
192 } // namespace OHOS
193