• 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 "concat_builder.h"
17 
18 namespace OHOS {
19 namespace NeuralNetworkRuntime {
20 namespace Ops {
21 static constexpr int MINIMUM_INTPUT = 2;
22 static constexpr int OUTPUT_NUM = 1;
23 static constexpr int AXIS_LENGTH = 1;
24 static const std::string OP_NAME = "Concat";
25 
ConcatBuilder()26 ConcatBuilder::ConcatBuilder() {}
27 
~ConcatBuilder()28 ConcatBuilder::~ConcatBuilder() {}
29 
SetAxis(std::shared_ptr<NNTensor> tensor)30 OH_NN_ReturnCode ConcatBuilder::SetAxis(std::shared_ptr<NNTensor> tensor)
31 {
32     tensor->IdentifyOpParameter();
33 
34     if (tensor->GetElementCount() != AXIS_LENGTH) {
35         LOGE("[Concat] SetAxis failed, the Activation shoule be a scalar");
36         return OH_NN_INVALID_PARAMETER;
37     }
38 
39     if (tensor->GetDataType() != OH_NN_INT64) {
40         LOGE("[Concat] SetAxis failed, the axis should be type OH_NN_INT64.");
41         return OH_NN_INVALID_PARAMETER;
42     }
43 
44     void* buffer = tensor->GetBuffer();
45     if (buffer == nullptr) {
46         LOGE("[Concat] SetAxis GetBuffer return nullptr.");
47         return OH_NN_INVALID_PARAMETER;
48     }
49     m_axis = *(static_cast<int64_t*>(buffer));
50 
51     return OH_NN_SUCCESS;
52 }
53 
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)54 OH_NN_ReturnCode ConcatBuilder::Build(const std::vector<uint32_t>& paramsIndex,
55     const std::vector<uint32_t>& inputsIndex, const std::vector<uint32_t>& outputsIndex,
56     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
57 {
58     if (m_isBuild) {
59         LOGE("[Concat] Build failed, operation has been build, cannot build again.");
60         return OH_NN_OPERATION_FORBIDDEN;
61     }
62 
63     if (inputsIndex.size() < MINIMUM_INTPUT) {
64         LOGE("[Concat] Build failed, Concat need more than one inputs.");
65         return OH_NN_INVALID_PARAMETER;
66     }
67 
68     if (outputsIndex.size() != OUTPUT_NUM) {
69         LOGE("[Concat] Build failed, The number of index of outputs not equal to 1.");
70         return OH_NN_INVALID_PARAMETER;
71     }
72 
73     OH_NN_ReturnCode returnCode = SetInputsAndOutputs(inputsIndex, outputsIndex, allTensors);
74     if (returnCode != OH_NN_SUCCESS) {
75         LOGE("[Concat] Build failed, set inputs or outputs failed.");
76         return returnCode;
77     }
78 
79     for (int i : paramsIndex) {
80         std::shared_ptr<NNTensor> tensor = allTensors[i];
81         switch (tensor->GetType()) {
82             case OH_NN_CONCAT_AXIS:
83                 returnCode = SetAxis(tensor);
84                 break;
85             default:
86                 LOGE("[Concat] Build failed, param invalid, type = %d.", tensor->GetType());
87                 return OH_NN_INVALID_PARAMETER;
88         }
89         if (returnCode != OH_NN_SUCCESS) {
90             LOGE("[Concat] Build failed, passed invalid param.");
91             return returnCode;
92         }
93     }
94 
95     // The quantization type of the first output determinies that of the operator.
96     SetQuantType(outputsIndex, allTensors);
97 
98     m_isBuild = true;
99     m_name = OP_NAME;
100     return OH_NN_SUCCESS;
101 }
102 
SetInputsAndOutputs(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)103 OH_NN_ReturnCode ConcatBuilder::SetInputsAndOutputs(const std::vector<uint32_t>& inputsIndex,
104                                                     const std::vector<uint32_t>& outputsIndex,
105                                                     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
106 {
107     size_t allTensorsSize = allTensors.size();
108     for (auto index : inputsIndex) {
109         if (index >= allTensorsSize) {
110             LOGE("[Concat] Invalid input index, it is out of range %zu.", allTensorsSize);
111             return OH_NN_INVALID_PARAMETER;
112         }
113     }
114 
115     for (auto index : outputsIndex) {
116         if (index >= allTensorsSize) {
117             LOGE("[Concat] Invalid output index, it is out of range %zu.", allTensorsSize);
118             return OH_NN_INVALID_PARAMETER;
119         }
120     }
121 
122     m_inputsIndex.clear();
123     m_inputsIndex = inputsIndex;
124 
125     m_outputsIndex.clear();
126     m_outputsIndex = outputsIndex;
127 
128     return OH_NN_SUCCESS;
129 }
130 
GetPrimitive()131 LiteGraphPrimitvePtr ConcatBuilder::GetPrimitive()
132 {
133     if (!m_isBuild) {
134         LOGE("[Concat] GetPrimitive failed, cannot get primitive before call build.");
135         return {nullptr, DestroyLiteGraphPrimitive};
136     }
137 
138     void* primitive = mindspore::lite::MindIR_Concat_CreatePrimitive(m_axis);
139     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
140     return graphPrimitivePtr;
141 }
142 
143 REGISTER_OPS(ConcatBuilder, OH_NN_OPS_CONCAT);
144 } // namespace Ops
145 } // namespace NeuralNetworkRuntime
146 } // namespace OHOS