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 "reducemean_builder.h"
17 
18 #include "mindir.h"
19 #include "ops_registry.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 const int PARAM_MAX_NUM = 3;
27 static const int SCALE_LENGTH = 1;
28 static const std::string OP_NAME = "ReduceMean";
29 
ReduceMeanBuilder()30 ReduceMeanBuilder::ReduceMeanBuilder() {}
31 
~ReduceMeanBuilder()32 ReduceMeanBuilder:: ~ReduceMeanBuilder() {}
33 
SetCoeff(const std::shared_ptr<NNTensor> & tensor)34 OH_NN_ReturnCode ReduceMeanBuilder::SetCoeff(const std::shared_ptr<NNTensor>& tensor)
35 {
36     if (tensor->GetDataType() != OH_NN_FLOAT32) {
37         LOGE("[ReduceAll] The coeff should be type OH_NN_FLOAT32.");
38         return OH_NN_INVALID_PARAMETER;
39     }
40 
41     if (tensor->GetElementCount() != SCALE_LENGTH) {
42         LOGE("[ReduceAll] The coeff should be scalar.");
43         return OH_NN_INVALID_PARAMETER;
44     }
45 
46     void* buffer = tensor->GetBuffer();
47     if (buffer == nullptr) {
48         LOGE("[ReduceAll] Tensor buffer is nullptr.");
49         return OH_NN_INVALID_PARAMETER;
50     }
51     m_coeff = *(static_cast<const float*>(buffer));
52 
53     return OH_NN_SUCCESS;
54 }
55 
SetReduceToEnd(const std::shared_ptr<NNTensor> & tensor)56 OH_NN_ReturnCode ReduceMeanBuilder::SetReduceToEnd(const std::shared_ptr<NNTensor>& tensor)
57 {
58     if (tensor->GetDataType() != OH_NN_BOOL) {
59         LOGE("[ReduceAll] SetReduceToEnd failed, the reduceToEnd should be type OH_NN_BOOL.");
60         return OH_NN_INVALID_PARAMETER;
61     }
62 
63     tensor->IdentifyOpParameter();
64     if (tensor->GetElementCount() != SCALE_LENGTH) {
65         LOGE("[ReduceAll] SetReduceToEnd failed, the reduceToEnd dimensions should be scalar.");
66         return OH_NN_INVALID_PARAMETER;
67     }
68 
69     void* buffer = tensor->GetBuffer();
70     if (buffer == nullptr) {
71         LOGE("[ReduceAll] SetReduceToEnd failed, the reduceToEnd passed buffer is empty.");
72         return OH_NN_INVALID_PARAMETER;
73     }
74 
75     m_reduceToEnd = *(static_cast<bool*>(buffer));
76     return OH_NN_SUCCESS;
77 }
78 
SetKeepDims(const std::shared_ptr<NNTensor> & tensor)79 OH_NN_ReturnCode ReduceMeanBuilder::SetKeepDims(const std::shared_ptr<NNTensor>& tensor)
80 {
81     tensor->IdentifyOpParameter();
82     if (tensor->GetElementCount() != SCALE_LENGTH) {
83         LOGE("[ReduceMean] SetKeepDims failed, the keep_dims dimensions should be scalar.");
84         return OH_NN_INVALID_PARAMETER;
85     }
86 
87     if (tensor->GetDataType() != OH_NN_BOOL) {
88         LOGE("[ReduceMean] SetKeepDims failed, the keep_dims should be type OH_NN_BOOL.");
89         return OH_NN_INVALID_PARAMETER;
90     }
91 
92     void* buffer = tensor->GetBuffer();
93     if (buffer == nullptr) {
94         LOGE("[ReduceMean] SetKeepDims failed, the keep_dims passed buffer is empty.");
95         return OH_NN_INVALID_PARAMETER;
96     }
97 
98     m_keepDims = *(static_cast<bool*>(buffer));
99     return OH_NN_SUCCESS;
100 }
101 
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)102 OH_NN_ReturnCode ReduceMeanBuilder::Build(const std::vector<uint32_t>& paramsIndex,
103                                           const std::vector<uint32_t>& inputsIndex,
104                                           const std::vector<uint32_t>& outputsIndex,
105                                           const std::vector<std::shared_ptr<NNTensor>>& allTensors)
106 {
107     if (m_isBuild) {
108         LOGE("[ReduceMean] Build failed, the ReduceMean operation has been build, cannot build again.");
109         return OH_NN_OPERATION_FORBIDDEN;
110     }
111 
112     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
113     if (returnCode != OH_NN_SUCCESS) {
114         LOGE("[ReduceMean] Build failed, passed invalid input or output index of ReduceMean operation index.");
115         return returnCode;
116     }
117 
118     m_inputsIndex = inputsIndex;
119     m_outputsIndex = outputsIndex;
120 
121     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
122     if (returnCode != OH_NN_SUCCESS) {
123         LOGE("[ReduceMean] Build failed, passed invalid param index of ReduceMean operation index.");
124         return returnCode;
125     }
126 
127     for (uint32_t i : paramsIndex) {
128         std::shared_ptr<NNTensor> tensor = allTensors[i];
129         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
130             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
131         } else {
132             LOGE("[ReduceMean] Build failed, param invalid, type=%d", tensor->GetType());
133             return OH_NN_INVALID_PARAMETER;
134         }
135 
136         if (returnCode != OH_NN_SUCCESS) {
137             LOGE("[ReduceMean] Build failed, passed invalid param.");
138             return returnCode;
139         }
140     }
141 
142     SetQuantType(outputsIndex, allTensors);
143 
144     m_name = OP_NAME;
145     m_isBuild = true;
146     return OH_NN_SUCCESS;
147 }
148 
GetPrimitive()149 LiteGraphPrimitvePtr ReduceMeanBuilder::GetPrimitive()
150 {
151     if (!m_isBuild) {
152         LOGE("[ReduceMean] GetPrimitive failed, cannot get primitive before call build.");
153         return {nullptr, DestroyLiteGraphPrimitive};
154     }
155 
156     mindspore::lite::ReduceMode mode {mindspore::lite::REDUCE_MODE_MEAN};
157 
158     void* primitive = mindspore::lite::MindIR_ReduceFusion_CreatePrimitive(m_keepDims, mode, m_reduceToEnd, m_coeff);
159     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
160     return graphPrimitivePtr;
161 }
162 
163 REGISTER_OPS(ReduceMeanBuilder, OH_NN_OPS_REDUCE_MEAN);
164 } // namespace Ops
165 } // namespace NeuralNetworkRuntime
166 } // namespace OHOS