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 "range_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_MAX_NUM = 3;
24 static const int SCALAR_LENGTH = 1;
25 static const std::string OP_NAME = "Range";
26 
RangeBuilder()27 RangeBuilder::RangeBuilder() {}
28 
~RangeBuilder()29 RangeBuilder::~RangeBuilder() {}
30 
SetStart(const std::shared_ptr<NNTensor> & tensor)31 OH_NN_ReturnCode RangeBuilder::SetStart(const std::shared_ptr<NNTensor>& tensor)
32 {
33     if (tensor->GetDataType() != OH_NN_INT64) {
34         LOGE("[Range] The start should be type OH_NN_INT64.");
35         return OH_NN_INVALID_PARAMETER;
36     }
37 
38     if (tensor->GetElementCount() != SCALAR_LENGTH) {
39         LOGE("[Range] The start should be scalar.");
40         return OH_NN_INVALID_PARAMETER;
41     }
42 
43     void* buffer = tensor->GetBuffer();
44     if (buffer == nullptr) {
45         LOGE("[Range] Tensor buffer is nullptr.");
46         return OH_NN_INVALID_PARAMETER;
47     }
48     m_start = *(static_cast<const int64_t*>(buffer));
49 
50     return OH_NN_SUCCESS;
51 }
52 
SetLimit(const std::shared_ptr<NNTensor> & tensor)53 OH_NN_ReturnCode RangeBuilder::SetLimit(const std::shared_ptr<NNTensor>& tensor)
54 {
55     if (tensor->GetDataType() != OH_NN_INT64) {
56         LOGE("[Range] The limit should be type OH_NN_INT64.");
57         return OH_NN_INVALID_PARAMETER;
58     }
59 
60     if (tensor->GetElementCount() != SCALAR_LENGTH) {
61         LOGE("[Range] The limit should be scalar.");
62         return OH_NN_INVALID_PARAMETER;
63     }
64 
65     void* buffer = tensor->GetBuffer();
66     if (buffer == nullptr) {
67         LOGE("[Range] Tensor buffer is nullptr.");
68         return OH_NN_INVALID_PARAMETER;
69     }
70     m_limit = *(static_cast<const int64_t*>(buffer));
71 
72     return OH_NN_SUCCESS;
73 }
74 
SetDelta(const std::shared_ptr<NNTensor> & tensor)75 OH_NN_ReturnCode RangeBuilder::SetDelta(const std::shared_ptr<NNTensor>& tensor)
76 {
77     if (tensor->GetDataType() != OH_NN_INT64) {
78         LOGE("[Range] The delta should be type OH_NN_INT64.");
79         return OH_NN_INVALID_PARAMETER;
80     }
81 
82     if (tensor->GetElementCount() != SCALAR_LENGTH) {
83         LOGE("[Range] The delta should be scalar.");
84         return OH_NN_INVALID_PARAMETER;
85     }
86 
87     void* buffer = tensor->GetBuffer();
88     if (buffer == nullptr) {
89         LOGE("[Range] Tensor buffer is nullptr.");
90         return OH_NN_INVALID_PARAMETER;
91     }
92     m_delta = *(static_cast<const int64_t*>(buffer));
93 
94     return OH_NN_SUCCESS;
95 }
96 
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)97 OH_NN_ReturnCode RangeBuilder::Build(const std::vector<uint32_t>& paramsIndex,
98                                      const std::vector<uint32_t>& inputsIndex,
99                                      const std::vector<uint32_t>& outputsIndex,
100                                      const std::vector<std::shared_ptr<NNTensor>>& allTensors)
101 {
102     if (m_isBuild) {
103         LOGE("[Range] Build failed, the Range operation has been build. cannot build again.");
104         return OH_NN_OPERATION_FORBIDDEN;
105     }
106 
107     auto ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
108     if (ret != OH_NN_SUCCESS) {
109         LOGE("[Range] Build failed, passed invalid input or output index.");
110         return ret;
111     }
112 
113     m_inputsIndex = inputsIndex;
114     m_outputsIndex = outputsIndex;
115 
116     ret = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
117     if (ret != OH_NN_SUCCESS) {
118         LOGE("[Range] Build failed, passed invalid param index.");
119         return ret;
120     }
121 
122     for (int i : paramsIndex) {
123         std::shared_ptr<NNTensor> tensor = allTensors[i];
124         tensor->IdentifyOpParameter();
125         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
126             ret = (this->*(m_paramMap[tensor->GetType()]))(tensor);
127         } else {
128             LOGE("[Range] Build failed, param invalid, type=%d", tensor->GetType());
129             return OH_NN_INVALID_PARAMETER;
130         }
131 
132         if (ret != OH_NN_SUCCESS) {
133             LOGE("[Range] Build failed, passed invalid param.");
134             return ret;
135         }
136     }
137 
138     m_name = OP_NAME;
139     m_isBuild = true;
140     return OH_NN_SUCCESS;
141 }
142 
GetPrimitive()143 LiteGraphPrimitvePtr RangeBuilder::GetPrimitive()
144 {
145     if (!m_isBuild) {
146         LOGE("[Range] GetPrimitive failed, cannot get primitive before call build.");
147         return {nullptr, DestroyLiteGraphPrimitive};
148     }
149 
150     int64_t dType {0.0f};
151     void* primitive = mindspore::lite::MindIR_Range_CreatePrimitive(dType, m_start, m_limit, m_delta);
152     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive) ;
153     return graphPrimitivePtr;
154 }
155 
156 REGISTER_OPS(RangeBuilder, OH_NN_OPS_RANGE);
157 } // namespace Ops
158 } // namespace NeuralNetworkRuntime
159 } // namespace OHOS
160