• 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 "div_builder.h"
17 
18 #include "frameworks/native/transform.h"
19 #include "frameworks/native/validation.h"
20 
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Ops {
24 static const int INPUT_NUM = 2;
25 static const int OUTPUT_NUM = 1;
26 static constexpr int SCALAR_LENGTH = 1;
27 static const std::string OP_NAME = "Div";
28 
DivBuilder()29 DivBuilder::DivBuilder() {}
30 
~DivBuilder()31 DivBuilder::~DivBuilder() {}
32 
SetActicationType(std::shared_ptr<NNTensor> tensor)33 OH_NN_ReturnCode DivBuilder::SetActicationType(std::shared_ptr<NNTensor> tensor)
34 {
35     tensor->IdentifyOpParameter();
36 
37     if (tensor->GetElementCount() != SCALAR_LENGTH) {
38         LOGE("[Div] SetActicationType failed, the Activation shoule be a scalar");
39         return OH_NN_INVALID_PARAMETER;
40     }
41 
42     if (tensor->GetDataType() != OH_NN_INT8) {
43         LOGE("[Div] SetActicationType failed, the activation should be type OH_NN_INT8.");
44         return OH_NN_INVALID_PARAMETER;
45     }
46 
47     void* buffer = tensor->GetBuffer();
48     if (buffer == nullptr) {
49         LOGE("[Div] SetActivation GetBuffer return nullptr");
50         return OH_NN_INVALID_PARAMETER;
51     }
52     const int8_t* fuseData = static_cast<int8_t*>(buffer);
53     if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*fuseData))) {
54         LOGE("[Div] SetActicationType failed, fuse activation type is invalid");
55         return OH_NN_INVALID_PARAMETER;
56     }
57     auto fuseType = (OH_NN_FuseType)(*fuseData);
58     m_activationType = NNToMS::TransfromFusionType(fuseType);
59 
60     return OH_NN_SUCCESS;
61 }
62 
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)63 OH_NN_ReturnCode DivBuilder::Build(const std::vector<uint32_t>& paramsIndex,
64                                    const std::vector<uint32_t>& inputsIndex,
65                                    const std::vector<uint32_t>& outputsIndex,
66                                    const std::vector<std::shared_ptr<NNTensor>>& allTensors)
67 {
68     if (m_isBuild) {
69         LOGE("[Div] Build failed, operation has been build, cannot build again.");
70         return OH_NN_OPERATION_FORBIDDEN;
71     }
72 
73     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
74     if (returnCode != OH_NN_SUCCESS) {
75         LOGE("[Div] Build failed, passed invalid input or output index.");
76         return returnCode;
77     }
78     m_inputsIndex = inputsIndex;
79     m_outputsIndex = outputsIndex;
80 
81     for (int i : paramsIndex) {
82         std::shared_ptr<NNTensor> tensor = allTensors[i];
83         switch (tensor->GetType()) {
84             case OH_NN_DIV_ACTIVATIONTYPE:
85                 returnCode = SetActicationType(tensor);
86                 break;
87             default:
88                 LOGE("[Div] Build failed, param invalid, type = %d.", tensor->GetType());
89                 return OH_NN_INVALID_PARAMETER;
90         }
91         if (returnCode != OH_NN_SUCCESS) {
92             LOGE("[Div] Build failed, passed invalid param.");
93             return returnCode;
94         }
95     }
96 
97     SetQuantType(outputsIndex, allTensors);
98 
99     m_isBuild = true;
100     m_name = OP_NAME;
101     return OH_NN_SUCCESS;
102 }
103 
GetPrimitive()104 LiteGraphPrimitvePtr DivBuilder::GetPrimitive()
105 {
106     if (!m_isBuild) {
107         LOGE("[Div] GetPrimitive failed, cannot get primitive before call build.");
108         return {nullptr, DestroyLiteGraphPrimitive};
109     }
110 
111     auto primitive = mindspore::lite::MindIR_DivFusion_CreatePrimitive(m_activationType);
112     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
113     return graphPrimitivePtr;
114 }
115 
116 REGISTER_OPS(DivBuilder, OH_NN_OPS_DIV);
117 } // namespace Ops
118 } // namespace NeuralNetworkRuntime
119 } // namespace OHOS
120