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