1 /*
2 * Copyright (c) 2024 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_depth_builder.h"
17
18 #include "transform.h"
19 #include "validation.h"
20 #include "mindir.h"
21 #include "ops_registry.h"
22
23 namespace OHOS {
24 namespace NeuralNetworkRuntime {
25 namespace Ops {
26 static const int INPUT_NUM = 1;
27 static const int OUTPUT_NUM = 1;
28 static const int PARAM_MAX_NUM = 1;
29 static const int SCALAR_LENGTH = 1;
30 static const std::string OP_NAME = "SpaceToDepth";
31
SpaceToDepthBuilder()32 SpaceToDepthBuilder::SpaceToDepthBuilder() {}
33
~SpaceToDepthBuilder()34 SpaceToDepthBuilder::~SpaceToDepthBuilder() {}
35
SetBlockSize(const std::shared_ptr<NNTensor> & tensor)36 OH_NN_ReturnCode SpaceToDepthBuilder::SetBlockSize(const std::shared_ptr<NNTensor>& tensor)
37 {
38 if (tensor->GetDataType() != OH_NN_INT64) {
39 LOGE("[SpaceToDepth] The blockSize should be type OH_NN_INT64.");
40 return OH_NN_INVALID_PARAMETER;
41 }
42
43 if (tensor->GetElementCount() != SCALAR_LENGTH) {
44 LOGE("[SpaceToDepth] The blockSize should be scalar.");
45 return OH_NN_INVALID_PARAMETER;
46 }
47
48 void* buffer = tensor->GetBuffer();
49 if (buffer == nullptr) {
50 LOGE("[SpaceToDepth] Tensor buffer is nullptr.");
51 return OH_NN_INVALID_PARAMETER;
52 }
53 m_blockSize = *(static_cast<const int64_t*>(buffer));
54
55 return OH_NN_SUCCESS;
56 }
57
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)58 OH_NN_ReturnCode SpaceToDepthBuilder::Build(const std::vector<uint32_t>& paramsIndex,
59 const std::vector<uint32_t>& inputsIndex,
60 const std::vector<uint32_t>& outputsIndex,
61 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
62 {
63 if (m_isBuild) {
64 LOGE("[SpaceToDepth] Build failed, the spaceToDepth operation has been build. cannot build again.");
65 return OH_NN_OPERATION_FORBIDDEN;
66 }
67
68 auto ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
69 if (ret != OH_NN_SUCCESS) {
70 LOGE("[SpaceToDepth] Build failed, passed invalid input or output index.");
71 return ret;
72 }
73
74 m_inputsIndex = inputsIndex;
75 m_outputsIndex = outputsIndex;
76
77 ret = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
78 if (ret != OH_NN_SUCCESS) {
79 LOGE("[SpaceToDepth] Build failed, passed invalid param index.");
80 return ret;
81 }
82
83 for (int i : paramsIndex) {
84 std::shared_ptr<NNTensor> tensor = allTensors[i];
85 tensor->IdentifyOpParameter();
86 if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
87 ret = (this->*(m_paramMap[tensor->GetType()]))(tensor);
88 } else {
89 LOGE("[SpaceToDepth] Build failed, param invalid, type=%d", tensor->GetType());
90 return OH_NN_INVALID_PARAMETER;
91 }
92
93 if (ret != OH_NN_SUCCESS) {
94 LOGE("[SpaceToDepth] Build failed, passed invalid param.");
95 return ret;
96 }
97 }
98
99 m_name = OP_NAME;
100 m_isBuild = true;
101 return OH_NN_SUCCESS;
102 }
103
GetPrimitive()104 LiteGraphPrimitvePtr SpaceToDepthBuilder::GetPrimitive()
105 {
106 if (!m_isBuild) {
107 LOGE("[SpaceToDepth] GetPrimitive failed, cannot get primitive before call build.");
108 return {nullptr, DestroyLiteGraphPrimitive};
109 }
110
111 mindspore::lite::Format format {mindspore::lite::FORMAT_NCHW};
112
113 void* primitive = mindspore::lite::MindIR_SpaceToDepth_CreatePrimitive(m_blockSize, format);
114 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive) ;
115 return graphPrimitivePtr;
116 }
117
118 REGISTER_OPS(SpaceToDepthBuilder, OH_NN_OPS_SPACE_TO_DEPTH);
119 } // namespace Ops
120 } // namespace NeuralNetworkRuntime
121 } // namespace OHOS