• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int PARAM_MAX_NUM = 2;
26 static const std::string OP_NAME = "SpaceToBatchND";
27 static const int PADDINGS_DATA_SIZE = 2;
28 static const int VECT_DATA_SIZE = 2;
29 static const int BLOCKSHAPE_RANK = 1;
30 static const int PADDINGS_RANK = 2;
31 static const int BLOCK_SIZE = 2;
32 static const int PADDINGS_SIZE = 4;
33 
SpaceToBatchNDBuilder()34 SpaceToBatchNDBuilder::SpaceToBatchNDBuilder() {}
35 
~SpaceToBatchNDBuilder()36 SpaceToBatchNDBuilder::~SpaceToBatchNDBuilder() {}
37 
SetBlockShape(const std::shared_ptr<NNTensor> & tensor)38 OH_NN_ReturnCode SpaceToBatchNDBuilder::SetBlockShape(const std::shared_ptr<NNTensor>& tensor)
39 {
40     if (tensor->GetDataType() != OH_NN_INT64) {
41         LOGE("[SpaceToBatchNDBuilder] The 2nd input blockShape should be type OH_NN_INT64.");
42         return OH_NN_INVALID_PARAMETER;
43     }
44 
45     auto blockshape_shape = tensor->GetDimensions();
46     if (blockshape_shape.size() != BLOCKSHAPE_RANK) {
47         LOGE("[SpaceToBatchNDBuilder] Invalid rank of shape of 2nd input blockShape, should be 1 dimensions.");
48         return OH_NN_INVALID_PARAMETER;
49     }
50 
51     if (tensor->GetElementCount() != BLOCK_SIZE) {
52         LOGE("[SpaceToBatchNDBuilder] The 2nd input blockShape size should be 2.");
53         return OH_NN_INVALID_PARAMETER;
54     }
55 
56     void* buffer = tensor->GetBuffer();
57     if (buffer == nullptr) {
58         LOGE("[SpaceToBatchNDBuilder] Tensor buffer is nullptr.");
59         return OH_NN_INVALID_PARAMETER;
60     }
61 
62     const int64_t* blockShapeData = reinterpret_cast<const int64_t*>(buffer);
63     const uint32_t elementSize = tensor->GetElementCount();
64     for (uint32_t i = 0; i < elementSize; ++i) {
65         block_shape.push_back(blockShapeData[i]);
66     }
67 
68     return OH_NN_SUCCESS;
69 }
70 
SetPaddings(const std::shared_ptr<NNTensor> & tensor)71 OH_NN_ReturnCode SpaceToBatchNDBuilder::SetPaddings(const std::shared_ptr<NNTensor>& tensor)
72 {
73     if (tensor->GetDataType() != OH_NN_INT64) {
74         LOGE("[SpaceToBatchNDBuilder] The 3rd input paddings should be type OH_NN_INT64.");
75         return OH_NN_INVALID_PARAMETER;
76     }
77 
78     auto paddings_shape = tensor->GetDimensions();
79     if (paddings_shape.size() != PADDINGS_RANK) {
80         LOGE("[SpaceToBatchNDBuilder] Invalid rank of shape of 3rd input paddings, should be 2 dimensions.");
81         return OH_NN_INVALID_PARAMETER;
82     }
83 
84     if (tensor->GetElementCount() != PADDINGS_SIZE) {
85         LOGE("[SpaceToBatchNDBuilder] The 3rd input paddings size should be 4.");
86         return OH_NN_INVALID_PARAMETER;
87     }
88 
89     OH_NN_ReturnCode returnCode = SetPadData(tensor);
90     if (returnCode != OH_NN_SUCCESS) {
91         LOGE("[SpaceToBatchNDBuilder] SetPadData failed.");
92         return returnCode;
93     }
94 
95     return OH_NN_SUCCESS;
96 }
97 /**
98  * Build method.
99  * 1.set attr of ops.
100  * 2.set inputIndex of ops.
101  * 3.set outputIndex of ops.
102  */
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)103 OH_NN_ReturnCode SpaceToBatchNDBuilder::Build(const std::vector<uint32_t>& paramsIndex,
104                                               const std::vector<uint32_t>& inputsIndex,
105                                               const std::vector<uint32_t>& outputsIndex,
106                                               const std::vector<std::shared_ptr<NNTensor>>& allTensors)
107 {
108     if (m_isBuild) {
109         LOGE("[SpaceToBatchNDBuilder] SpaceToBatchND operation has been build, cannot build again.");
110         return OH_NN_OPERATION_FORBIDDEN;
111     }
112 
113     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
114     if (returnCode != OH_NN_SUCCESS) {
115         LOGE("[SpaceToBatchNDBuilder] Passed invalid input or output index.");
116         return returnCode;
117     }
118 
119     m_inputsIndex = inputsIndex;
120     m_outputsIndex = outputsIndex;
121 
122     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
123     if (returnCode != OH_NN_SUCCESS) {
124         LOGE("[SpaceToBatchNDBuilder] Passed invalid param index.");
125         return returnCode;
126     }
127 
128     for (int i : paramsIndex) {
129         std::shared_ptr<NNTensor> tensor = allTensors[i];
130         tensor->IdentifyOpParameter();
131         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
132             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
133         } else {
134             LOGE("[SpaceToBatchNDBuilder] Build failed, param invalid, type=%d", tensor->GetType());
135             return OH_NN_INVALID_PARAMETER;
136         }
137 
138         if (returnCode != OH_NN_SUCCESS) {
139             LOGE("[SpaceToBatchNDBuilder] Passed invalid param.");
140             return returnCode;
141         }
142     }
143 
144     // The quantization type of the first output determinies that of the operator.
145     SetQuantType(outputsIndex, allTensors);
146 
147     m_isBuild = true;
148     m_name = OP_NAME;
149     return OH_NN_SUCCESS;
150 }
151 
SetPadData(const std::shared_ptr<NNTensor> & tensor)152 OH_NN_ReturnCode SpaceToBatchNDBuilder::SetPadData(const std::shared_ptr<NNTensor>& tensor)
153 {
154     paddings.clear();
155 
156     void* buffer = tensor->GetBuffer();
157     if (buffer == nullptr) {
158         LOGE("[SpaceToBatchNDBuilder] Tensor buffer is nullptr.");
159         return OH_NN_INVALID_PARAMETER;
160     }
161 
162     const int64_t* paddingsData = reinterpret_cast<const int64_t*>(buffer);
163     for (int i = 0; i < PADDINGS_DATA_SIZE; i++) {
164         std::vector<int64_t> vect_data;
165         vect_data.reserve(VECT_DATA_SIZE);
166         for (int j = 0; j < VECT_DATA_SIZE; ++j) {
167             vect_data.push_back(paddingsData[j]);
168         }
169         paddings.push_back(vect_data);
170     }
171     return OH_NN_SUCCESS;
172 }
173 
GetPrimitive()174 LiteGraphTensorPtr SpaceToBatchNDBuilder::GetPrimitive()
175 {
176     if (!m_isBuild) {
177         LOGE("[SpaceToBatchNDBuilder] Cannot get primitive before call build.");
178         return {nullptr, DestroyLiteGraphPrimitive};
179     }
180 
181     auto primitive = mindspore::lite::MindIR_SpaceToBatchND_CreatePrimitive(block_shape, paddings);
182     if (primitive == nullptr) {
183         LOGE("[SpaceToBatchNDBuilder] MindIR_SpaceToBatchND_CreatePrimitive failed.");
184         return {nullptr, DestroyLiteGraphPrimitive};
185     }
186 
187     LiteGraphTensorPtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
188     return graphPrimitivePtr;
189 }
190 
191 REGISTER_OPS(SpaceToBatchNDBuilder, OH_NN_OPS_SPACE_TO_BATCH_ND);
192 } // namespace Ops
193 } // namespace NeuralNetworkRuntime
194 } // namespace OHOS
195