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 "scale_builder.h"
17 
18 #include "ops_registry.h"
19 #include "validation.h"
20 #include "transform.h"
21 
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24 namespace Ops {
25 static const int INPUT_NUM = 3;
26 static const int OUTPUT_NUM = 1;
27 static const int PARAM_MAX_NUM = 2;
28 static const int SCALE_LENGTH = 1;
29 static const std::string OP_NAME = "Scale";
30 
ScaleBuilder()31 ScaleBuilder::ScaleBuilder() {}
32 
~ScaleBuilder()33 ScaleBuilder::~ScaleBuilder() {}
34 
SetAxis(const std::shared_ptr<NNTensor> & tensor)35 OH_NN_ReturnCode ScaleBuilder::SetAxis(const std::shared_ptr<NNTensor>& tensor)
36 {
37     tensor->IdentifyOpParameter();
38     if (tensor->GetDataType() != OH_NN_INT64) {
39         LOGE("[ScaleBuilder] SetAxis failed, the axis should be type OH_NN_INT64.");
40         return OH_NN_INVALID_PARAMETER;
41     }
42 
43     if (tensor->GetElementCount() != SCALE_LENGTH) {
44         LOGE("[ScaleBuilder] SetAxis failed, the axis dimensions should be scaler.");
45         return OH_NN_INVALID_PARAMETER;
46     }
47 
48     void* buffer = tensor->GetBuffer();
49     if (buffer == nullptr) {
50         LOGE("[ScaleBuilder] SetAxis failed, the axis passed buffer is empty.");
51         return OH_NN_INVALID_PARAMETER;
52     }
53 
54     m_axis = static_cast<uint64_t*>(buffer);
55     return OH_NN_SUCCESS;
56 }
57 
SetActivationType(const std::shared_ptr<NNTensor> & tensor)58 OH_NN_ReturnCode ScaleBuilder::SetActivationType(const std::shared_ptr<NNTensor>& tensor)
59 {
60     tensor->IdentifyOpParameter();
61     if (tensor->GetDataType() != OH_NN_INT8) {
62         LOGE("[ScaleBuilder] SetActivationType failed, the activation should be type OH_NN_INT32.");
63         return OH_NN_INVALID_PARAMETER;
64     }
65 
66     if (tensor->GetElementCount() != SCALE_LENGTH) {
67         LOGE("[ScaleBuilder] SetActivationType failed, the activation dimensions should be scaler.");
68         return OH_NN_INVALID_PARAMETER;
69     }
70 
71     void* buffer = tensor->GetBuffer();
72     if (buffer == nullptr) {
73         LOGE("[ScaleBuilder] SetActivationType failed, the activation passed buffer is empty.");
74         return OH_NN_INVALID_PARAMETER;
75     }
76 
77     const int8_t* fuseData = static_cast<const int8_t*>(buffer);
78     if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*fuseData))) {
79         LOGE("[ScaleBuilder] SetActivationType failed, the activation input is invalid.");
80         return OH_NN_INVALID_PARAMETER;
81     }
82 
83     auto fuseType = (OH_NN_FuseType)(*fuseData);
84     m_activationType = NNToMS::TransfromFusionType(fuseType);
85     return OH_NN_SUCCESS;
86 }
87 
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)88 OH_NN_ReturnCode ScaleBuilder::Build(const std::vector<uint32_t>& paramsIndex,
89                                      const std::vector<uint32_t>& inputsIndex,
90                                      const std::vector<uint32_t>& outputsIndex,
91                                      const std::vector<std::shared_ptr<NNTensor>>& allTensors)
92 {
93     if (m_isBuild) {
94         LOGE("[ScaleBuilder] Build failed, the scale operation has been build, cannot build again.");
95         return OH_NN_OPERATION_FORBIDDEN;
96     }
97 
98     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
99     if (returnCode != OH_NN_SUCCESS) {
100         LOGE("[ScaleBuilder] Build failed, passed invalid input or output index.");
101         return returnCode;
102     }
103 
104     m_inputsIndex = inputsIndex;
105     m_outputsIndex = outputsIndex;
106 
107     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
108     if (returnCode != OH_NN_SUCCESS) {
109         LOGE("[ScaleBuilder] Build failed, passed invaid param index.");
110         return returnCode;
111     }
112 
113     for (uint32_t i : paramsIndex) {
114         std::shared_ptr<NNTensor> tensor = allTensors[i];
115         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
116             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
117         } else {
118             LOGE("[ScaleBuilder] Build failed, param invalid, type=%d", tensor->GetType());
119             return OH_NN_INVALID_PARAMETER;
120         }
121 
122         if (returnCode != OH_NN_SUCCESS) {
123             LOGE("[ScaleBuilder] Build failed, passed invalid param.");
124             return returnCode;
125         }
126     }
127 
128     // The quantization type of the first output determinies that of the operator.
129     SetQuantType(outputsIndex, allTensors);
130 
131     m_isBuild = true;
132     m_name = OP_NAME;
133     return OH_NN_SUCCESS;
134 }
135 
GetPrimitive()136 LiteGraphPrimitvePtr ScaleBuilder::GetPrimitive()
137 {
138     if (!m_isBuild) {
139         LOGE("[ScaleBuilder] GetPrimitive failed, cannot get primitive before call build.");
140         return {nullptr, DestroyLiteGraphPrimitive};
141     }
142 
143     void* primitive = mindspore::lite::MindIR_ScaleFusion_CreatePrimitive(*m_axis, m_activationType);
144     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
145     return graphPrimitivePtr;
146 }
147 
148 REGISTER_OPS(ScaleBuilder, OH_NN_OPS_SCALE);
149 } // namespace Ops
150 } // namespace NeuralNetworkRuntime
151 } // namespace OHOS