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 "top_k_builder.h"
17
18 #include "mindir.h"
19
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
23 static const std::string OP_NAME = "TopK";
24 static const int INPUT_NUM = 2;
25 static const int OUTPUT_NUM = 2;
26
TopKBuilder()27 TopKBuilder::TopKBuilder() {}
28
~TopKBuilder()29 TopKBuilder::~TopKBuilder() {}
30
SetSorted(std::shared_ptr<NNTensor> tensor)31 OH_NN_ReturnCode TopKBuilder::SetSorted(std::shared_ptr<NNTensor> tensor)
32 {
33 if (tensor->GetDataType() != OH_NN_BOOL) {
34 LOGE("[TopK] The sorted should be type OH_NN_BOOL.");
35 return OH_NN_INVALID_PARAMETER;
36 }
37
38 void* buffer = tensor->GetBuffer();
39 if (buffer == nullptr) {
40 LOGE("[TopK] Tensor buffer is nullptr.");
41 return OH_NN_INVALID_PARAMETER;
42 }
43 m_sorted = *(static_cast<const bool *>(buffer));
44
45 return OH_NN_SUCCESS;
46 }
47
48 /**
49 * Build method.
50 * 1.build primitive of ops.
51 * 2.build inputIndex of ops.
52 * 3.build outputIndex of ops.
53 */
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)54 OH_NN_ReturnCode TopKBuilder::Build(const std::vector<uint32_t>& paramsIndex,
55 const std::vector<uint32_t>& inputsIndex,
56 const std::vector<uint32_t>& outputsIndex,
57 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
58 {
59 if (m_isBuild) {
60 LOGE("[TopK] Build operation has been completed, cannot build again.");
61 return OH_NN_OPERATION_FORBIDDEN;
62 }
63
64 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
65 if (returnCode != OH_NN_SUCCESS) {
66 LOGE("[TopK] Passed invalid input or output index.");
67 return returnCode;
68 }
69
70 m_inputsIndex = inputsIndex;
71 m_outputsIndex = outputsIndex;
72
73 for (int i : paramsIndex) {
74 std::shared_ptr<NNTensor> tensor = allTensors[i];
75 tensor->IdentifyOpParameter();
76 switch (tensor->GetType()) {
77 case OH_NN_TOP_K_SORTED:
78 returnCode = SetSorted(tensor);
79 break;
80 default:
81 LOGE("[TopK] Parameter Type is invalid. type=%d", tensor->GetType());
82 return OH_NN_INVALID_PARAMETER;
83 }
84
85 if (returnCode != OH_NN_SUCCESS) {
86 LOGE("[TopK] Passed invalid param.");
87 return returnCode;
88 }
89 }
90
91 m_name = OP_NAME;
92 m_isBuild = true;
93 return OH_NN_SUCCESS;
94 }
95
GetPrimitive()96 LiteGraphPrimitvePtr TopKBuilder::GetPrimitive()
97 {
98 if (!m_isBuild) {
99 LOGE("[TopK] Cannot get primitive before call build.");
100 return {nullptr, DestroyLiteGraphPrimitive};
101 }
102
103 int64_t axis = 0;
104 auto primitive = mindspore::lite::MindIR_TopKFusion_CreatePrimitive(m_sorted, axis);
105 if (primitive == nullptr) {
106 LOGE("[TopK] MindIR_TopKFusion_CreatePrimitive failed.");
107 return {nullptr, DestroyLiteGraphPrimitive};
108 }
109
110 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
111 return graphPrimitivePtr;
112 }
113
114 REGISTER_OPS(TopKBuilder, OH_NN_OPS_TOP_K);
115 } // namespace Ops
116 } // namespace NeuralNetworkRuntime
117 } // namespace OHOS
118