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 "expandims_builder.h"
17
18 namespace OHOS {
19 namespace NeuralNetworkRuntime {
20 namespace Ops {
21 static const int INPUT_NUM = 2;
22 static const int OUTPUT_NUM = 1;
23 static const std::string OP_NAME = "ExpandDims";
24
ExpandDimsBuilder()25 ExpandDimsBuilder::ExpandDimsBuilder() {}
26
~ExpandDimsBuilder()27 ExpandDimsBuilder::~ExpandDimsBuilder() {}
28
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)29 OH_NN_ReturnCode ExpandDimsBuilder::Build(const std::vector<uint32_t>& paramsIndex,
30 const std::vector<uint32_t>& inputsIndex,
31 const std::vector<uint32_t>& outputsIndex,
32 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
33 {
34 if (m_isBuild) {
35 LOGE("[ExpandDims] Build failed, operation has been build, cannot build again.");
36 return OH_NN_OPERATION_FORBIDDEN;
37 }
38
39 auto ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
40 if (ret != OH_NN_SUCCESS) {
41 LOGE("[ExpandDims] Build failed, the input or output index of ExpandDims operation is invalid.");
42 return ret;
43 }
44
45 m_inputsIndex = inputsIndex;
46 m_outputsIndex = outputsIndex;
47
48 if (!paramsIndex.empty()) {
49 LOGE("[ExpandDims] Build failed, expandDims expects no parameters");
50 return OH_NN_INVALID_PARAMETER;
51 }
52
53 m_isBuild = true;
54 m_name = OP_NAME;
55 return OH_NN_SUCCESS;
56 }
57
GetPrimitive()58 LiteGraphPrimitvePtr ExpandDimsBuilder::GetPrimitive()
59 {
60 if (!m_isBuild) {
61 LOGE("[ExpandDims] GetPrimitive failed, cannot get primitive before call build.");
62 return {nullptr, DestroyLiteGraphPrimitive};
63 }
64
65 void* primitive = mindspore::lite::MindIR_ExpandDims_CreatePrimitive();
66 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
67 return graphPrimitivePtr;
68 }
69
70 REGISTER_OPS(ExpandDimsBuilder, OH_NN_OPS_EXPAND_DIMS);
71 } // namespace Ops
72 } // namespace NeuralNetworkRuntime
73 } // namespace OHOS
74