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 static const int PARAM_MAX_NUM = 2;
27 static const int SCALAR_LENGTH = 1;
28 
TopKBuilder()29 TopKBuilder::TopKBuilder() {}
30 
~TopKBuilder()31 TopKBuilder::~TopKBuilder() {}
32 
SetSorted(const std::shared_ptr<NNTensor> & tensor)33 OH_NN_ReturnCode TopKBuilder::SetSorted(const std::shared_ptr<NNTensor>& tensor)
34 {
35     if (tensor->GetDataType() != OH_NN_BOOL) {
36         LOGE("[TopK] The sorted should be type OH_NN_BOOL.");
37         return OH_NN_INVALID_PARAMETER;
38     }
39 
40     void* buffer = tensor->GetBuffer();
41     if (buffer == nullptr) {
42         LOGE("[TopK] Tensor buffer is nullptr.");
43         return OH_NN_INVALID_PARAMETER;
44     }
45     m_sorted = *(static_cast<const bool *>(buffer));
46 
47     return OH_NN_SUCCESS;
48 }
49 
SetAxis(const std::shared_ptr<NNTensor> & tensor)50 OH_NN_ReturnCode TopKBuilder::SetAxis(const std::shared_ptr<NNTensor>& tensor)
51 {
52     if (tensor->GetDataType() != OH_NN_INT64) {
53         LOGE("[TopK] The axis should be type OH_NN_INT64.");
54         return OH_NN_INVALID_PARAMETER;
55     }
56 
57     if (tensor->GetElementCount() != SCALAR_LENGTH) {
58         LOGE("[TopK] The axis should be scalar.");
59         return OH_NN_INVALID_PARAMETER;
60     }
61 
62     void* buffer = tensor->GetBuffer();
63     if (buffer == nullptr) {
64         LOGE("[TopK] Tensor buffer is nullptr.");
65         return OH_NN_INVALID_PARAMETER;
66     }
67     m_axis = *(static_cast<const int64_t*>(buffer));
68 
69     return OH_NN_SUCCESS;
70 }
71 
72 /**
73  * Build method.
74  * 1.build primitive of ops.
75  * 2.build inputIndex of ops.
76  * 3.build outputIndex of ops.
77  */
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)78 OH_NN_ReturnCode TopKBuilder::Build(const std::vector<uint32_t>& paramsIndex,
79                                     const std::vector<uint32_t>& inputsIndex,
80                                     const std::vector<uint32_t>& outputsIndex,
81                                     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
82 {
83     if (m_isBuild) {
84         LOGE("[TopK] Build operation has been completed, cannot build again.");
85         return OH_NN_OPERATION_FORBIDDEN;
86     }
87 
88     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
89     if (returnCode != OH_NN_SUCCESS) {
90         LOGE("[TopK] Passed invalid input or output index.");
91         return returnCode;
92     }
93 
94     m_inputsIndex = inputsIndex;
95     m_outputsIndex = outputsIndex;
96 
97     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
98     if (returnCode != OH_NN_SUCCESS) {
99         LOGE("[TopK] Passed invalid param index.");
100         return returnCode;
101     }
102 
103     for (int i : paramsIndex) {
104         std::shared_ptr<NNTensor> tensor = allTensors[i];
105         tensor->IdentifyOpParameter();
106         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
107             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
108         } else {
109             LOGE("[TopK] Build failed, param invalid, type=%d", tensor->GetType());
110             return OH_NN_INVALID_PARAMETER;
111         }
112 
113         if (returnCode != OH_NN_SUCCESS) {
114             LOGE("[TopK] Passed invalid param.");
115             return returnCode;
116         }
117     }
118 
119     m_name = OP_NAME;
120     m_isBuild = true;
121     return OH_NN_SUCCESS;
122 }
123 
GetPrimitive()124 LiteGraphPrimitvePtr TopKBuilder::GetPrimitive()
125 {
126     if (!m_isBuild) {
127         LOGE("[TopK] Cannot get primitive before call build.");
128         return {nullptr, DestroyLiteGraphPrimitive};
129     }
130 
131     auto primitive = mindspore::lite::MindIR_TopKFusion_CreatePrimitive(m_sorted, m_axis);
132     if (primitive == nullptr) {
133         LOGE("[TopK] MindIR_TopKFusion_CreatePrimitive failed.");
134         return {nullptr, DestroyLiteGraphPrimitive};
135     }
136 
137     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
138     return graphPrimitivePtr;
139 }
140 
141 REGISTER_OPS(TopKBuilder, OH_NN_OPS_TOP_K);
142 } // namespace Ops
143 } // namespace NeuralNetworkRuntime
144 } // namespace OHOS
145