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 "unstack_builder.h"
17 
18 namespace OHOS {
19 namespace NeuralNetworkRuntime {
20 namespace Ops {
21 static const int INPUT_NUM = 1;
22 static const int OUTPUT_MIN_NUM = 1;
23 static const int PARAM_MAX_NUM = 1;
24 static const int SCALAR_LENGTH = 1;
25 static const std::string OP_NAME = "Unstack";
26 
UnstackBuilder()27 UnstackBuilder::UnstackBuilder() {}
28 
~UnstackBuilder()29 UnstackBuilder::~UnstackBuilder() {}
30 
SetAxis(const std::shared_ptr<NNTensor> & tensor)31 OH_NN_ReturnCode UnstackBuilder::SetAxis(const std::shared_ptr<NNTensor>& tensor)
32 {
33     if (tensor->GetDataType() != OH_NN_INT64) {
34         LOGE("[Unstack] The axis should be type OH_NN_INT64.");
35         return OH_NN_INVALID_PARAMETER;
36     }
37 
38     if (tensor->GetElementCount() != SCALAR_LENGTH) {
39         LOGE("[Unstack] The axis should be scalar.");
40         return OH_NN_INVALID_PARAMETER;
41     }
42 
43     void* buffer = tensor->GetBuffer();
44     if (buffer == nullptr) {
45         LOGE("[Unstack] Tensor buffer is nullptr.");
46         return OH_NN_INVALID_PARAMETER;
47     }
48     m_axis = *(static_cast<const int64_t*>(buffer));
49 
50     return OH_NN_SUCCESS;
51 }
52 
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)53 OH_NN_ReturnCode UnstackBuilder::Build(const std::vector<uint32_t>& paramsIndex,
54                                        const std::vector<uint32_t>& inputsIndex,
55                                        const std::vector<uint32_t>& outputsIndex,
56                                        const std::vector<std::shared_ptr<NNTensor>>& allTensors)
57 {
58     if (m_isBuild) {
59         LOGE("[Unstack] Build failed, the Unstack operation has been build. cannot build again.");
60         return OH_NN_OPERATION_FORBIDDEN;
61     }
62 
63     if (inputsIndex.size() != INPUT_NUM) {
64         LOGE("[Unstack] The number of index of inputs don't equal to %d.", INPUT_NUM);
65         return OH_NN_INVALID_PARAMETER;
66     }
67 
68     if (outputsIndex.size() < OUTPUT_MIN_NUM) {
69         LOGE("[Unstack] The number of index of outputs don't larger than %d.", OUTPUT_MIN_NUM);
70         return OH_NN_INVALID_PARAMETER;
71     }
72 
73     size_t allTensorsSize = allTensors.size();
74     bool isOverTensorSize = std::any_of(inputsIndex.begin(), inputsIndex.end(), [allTensorsSize](uint32_t index) {
75         return index >= allTensorsSize;
76     });
77     if (isOverTensorSize) {
78         LOGE("[Unstack] The index of inputs is out of range.");
79         return OH_NN_INVALID_PARAMETER;
80     }
81 
82     isOverTensorSize = std::any_of(outputsIndex.begin(), outputsIndex.end(), [allTensorsSize](uint32_t index) {
83         return index >= allTensorsSize;
84     });
85     if (isOverTensorSize) {
86         LOGE("[Unstack] The index of outputs is out of range.");
87         return OH_NN_INVALID_PARAMETER;
88     }
89 
90     m_inputsIndex = inputsIndex;
91     m_outputsIndex = outputsIndex;
92 
93     auto returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
94     if (returnCode != OH_NN_SUCCESS) {
95         LOGE("[Unstack] Passed invalid param index.");
96         return returnCode;
97     }
98 
99     for (int i : paramsIndex) {
100         std::shared_ptr<NNTensor> tensor = allTensors[i];
101         tensor->IdentifyOpParameter();
102         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
103             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
104         } else {
105             LOGE("[Unstack] Build failed, param invalid, type=%d", tensor->GetType());
106             return OH_NN_INVALID_PARAMETER;
107         }
108 
109         if (returnCode != OH_NN_SUCCESS) {
110             LOGE("[Unstack] Build failed, passed invalid param.");
111             return returnCode;
112         }
113     }
114 
115     m_name = OP_NAME;
116     m_isBuild = true;
117     return OH_NN_SUCCESS;
118 }
119 
GetPrimitive()120 LiteGraphPrimitvePtr UnstackBuilder::GetPrimitive()
121 {
122     if (!m_isBuild) {
123         LOGE("[Unstack] GetPrimitive failed, cannot get primitive before call build.");
124         return {nullptr, DestroyLiteGraphPrimitive};
125     }
126 
127     void* primitive = mindspore::lite::MindIR_Unstack_CreatePrimitive(m_axis);
128     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive) ;
129     return graphPrimitivePtr;
130 }
131 
132 REGISTER_OPS(UnstackBuilder, OH_NN_OPS_UNSTACK);
133 } // namespace Ops
134 } // namespace NeuralNetworkRuntime
135 } // namespace OHOS
136