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 "batch_to_space_nd_builder.h"
17 
18 namespace OHOS {
19 namespace NeuralNetworkRuntime {
20 namespace Ops {
21 static const int INPUT_NUM = 1;
22 static const int OUTPUT_NUM = 1;
23 static const int PARAM_MAX_NUM = 2;
24 static const int CROPS_ROWS = 2;
25 static const int CROPS_COLUMN = 2;
26 static const std::string OP_NAME = "BatchToSpaceND";
27 
BatchToSpaceNDBuilder()28 BatchToSpaceNDBuilder::BatchToSpaceNDBuilder() {}
29 
~BatchToSpaceNDBuilder()30 BatchToSpaceNDBuilder::~BatchToSpaceNDBuilder() {}
31 
SetInputBlock(const std::shared_ptr<NNTensor> & tensor)32 OH_NN_ReturnCode BatchToSpaceNDBuilder::SetInputBlock(const std::shared_ptr<NNTensor>& tensor)
33 {
34     tensor->IdentifyOpParameter();
35 
36     if (tensor->GetDataType() != OH_NN_INT64) {
37         LOGE("[BatchToSpaceND] SetInputBlock failed, the BlockSize should be type OH_NN_INT64.");
38         return OH_NN_INVALID_PARAMETER;
39     }
40 
41     void* buffer = tensor->GetBuffer();
42     if (buffer == nullptr) {
43         LOGE("[BatchToSpaceND] SetInputBlock GetBuffer return nullptr.");
44         return OH_NN_INVALID_PARAMETER;
45     }
46     int64_t* pBlockSize = static_cast<int64_t*>(buffer);
47 
48     uint32_t elementCount = tensor->GetElementCount();
49     for (uint32_t i = 0; i < elementCount; ++i) {
50         m_blockSize.emplace_back(*pBlockSize);
51         ++pBlockSize;
52     }
53     return OH_NN_SUCCESS;
54 }
55 
SetInputCrops(const std::shared_ptr<NNTensor> & tensor)56 OH_NN_ReturnCode BatchToSpaceNDBuilder::SetInputCrops(const std::shared_ptr<NNTensor>& tensor)
57 {
58     tensor->IdentifyOpParameter();
59 
60     if (tensor->GetDataType() != OH_NN_INT64) {
61         LOGE("[BatchToSpaceND] SetInputCrops failed, the Crops should be type OH_NN_INT64.");
62         return OH_NN_INVALID_PARAMETER;
63     }
64 
65     void* buffer = tensor->GetBuffer();
66     if (buffer == nullptr) {
67         LOGE("[BatchToSpaceND] SetInputCrops GetBuffer return nullptr.");
68         return OH_NN_INVALID_PARAMETER;
69     }
70     int64_t* pCropsData = static_cast<int64_t*>(buffer);
71 
72     std::vector<std::vector<int64_t>> cropsData;
73     for (int i = 0; i < CROPS_ROWS; i++) {
74         std::vector<int64_t> vect_data;
75         vect_data.reserve(CROPS_COLUMN);
76         for (int j = 0; j < CROPS_COLUMN; j++) {
77             vect_data.push_back(*pCropsData++);
78         }
79         cropsData.push_back(vect_data);
80     }
81     m_crops = cropsData;
82 
83     return OH_NN_SUCCESS;
84 }
85 
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)86 OH_NN_ReturnCode BatchToSpaceNDBuilder::Build(const std::vector<uint32_t>& paramsIndex,
87     const std::vector<uint32_t>& inputsIndex, const std::vector<uint32_t>& outputsIndex,
88     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
89 {
90     if (m_isBuild) {
91         LOGE("[BatchToSpaceND] Build failed, operation has been build, cannot build again.");
92         return OH_NN_OPERATION_FORBIDDEN;
93     }
94 
95     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
96     if (returnCode != OH_NN_SUCCESS) {
97         LOGE("[BatchToSpaceND] Build failed, passed invalid input or output index.");
98         return returnCode;
99     }
100 
101     m_inputsIndex = inputsIndex;
102     m_outputsIndex = outputsIndex;
103 
104     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
105     if (returnCode != OH_NN_SUCCESS) {
106         LOGE("[BatchToSpaceND] Build failed, passed invalid param index.");
107         return returnCode;
108     }
109 
110     for (int i : paramsIndex) {
111         std::shared_ptr<NNTensor> tensor = allTensors[i];
112         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
113             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
114         } else {
115             LOGE("[BatchToSpaceND] Build failed, param invalid, type=%d", tensor->GetType());
116             return OH_NN_INVALID_PARAMETER;
117         }
118 
119         if (returnCode != OH_NN_SUCCESS) {
120             LOGE("[BatchToSpaceND] Build failed, passed invalid param.");
121             return returnCode;
122         }
123     }
124 
125     // The quantization type of the first output determinies that of the operator.
126     SetQuantType(outputsIndex, allTensors);
127 
128     m_isBuild = true;
129     m_name = OP_NAME;
130     return OH_NN_SUCCESS;
131 }
132 
GetPrimitive()133 LiteGraphPrimitvePtr BatchToSpaceNDBuilder::GetPrimitive()
134 {
135     if (!m_isBuild) {
136         LOGE("[BatchToSpaceND] Cannot get primitive before call build.");
137         return {nullptr, DestroyLiteGraphPrimitive};
138     }
139 
140     void* primitive = mindspore::lite::MindIR_BatchToSpaceND_CreatePrimitive(m_blockSize, m_crops);
141     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
142     return graphPrimitivePtr;
143 }
144 
145 REGISTER_OPS(BatchToSpaceNDBuilder, OH_NN_OPS_BATCH_TO_SPACE_ND);
146 } // namespace Ops
147 } // namespace NeuralNetworkRuntime
148 } // namespace OHOS
149