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