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