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 "slice_builder.h"
17 
18 #include "mindir.h"
19 
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
23 static const int INPUT_NUM = 3;
24 static const int OUTPUT_NUM = 1;
25 static const int PARAM_MAX_NUM = 1;
26 static const std::string OP_NAME = "Slice";
27 
SliceBuilder()28 SliceBuilder::SliceBuilder() {}
29 
~SliceBuilder()30 SliceBuilder::~SliceBuilder() {}
31 
SetAxes(const std::shared_ptr<NNTensor> & tensor)32 OH_NN_ReturnCode SliceBuilder::SetAxes(const std::shared_ptr<NNTensor>& tensor)
33 {
34     if (tensor->GetDataType() != OH_NN_INT64) {
35         LOGE("[SliceBuilder] The axes should be type OH_NN_INT64.");
36         return OH_NN_INVALID_PARAMETER;
37     }
38 
39     m_axes.clear();
40 
41     void* buffer = tensor->GetBuffer();
42     if (buffer == nullptr) {
43         LOGE("[SliceBuilder] Tensor buffer is nullptr.");
44         return OH_NN_INVALID_PARAMETER;
45     }
46 
47     int64_t* pAxes = static_cast<int64_t*>(buffer);
48 
49     uint32_t elementCount = tensor->GetElementCount();
50     for (uint32_t i = 0; i < elementCount; ++i) {
51         m_axes.emplace_back(*pAxes);
52         ++pAxes;
53     }
54     return OH_NN_SUCCESS;
55 }
56 
57 /**
58  * Build method.
59  * 1.set attr of ops.
60  * 2.set inputIndex of ops.
61  * 3.set outputIndex of ops.
62  */
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)63 OH_NN_ReturnCode SliceBuilder::Build(const std::vector<uint32_t>& paramsIndex,
64                                      const std::vector<uint32_t>& inputsIndex,
65                                      const std::vector<uint32_t>& outputsIndex,
66                                      const std::vector<std::shared_ptr<NNTensor>>& allTensors)
67 {
68     if (m_isBuild) {
69         LOGE("[SliceBuilder] Slice operation has been build, cannot build again.");
70         return OH_NN_OPERATION_FORBIDDEN;
71     }
72 
73     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
74     if (returnCode != OH_NN_SUCCESS) {
75         LOGE("[SliceBuilder] Passed invalid input or output index.");
76         return returnCode;
77     }
78 
79     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
80     if (returnCode != OH_NN_SUCCESS) {
81         LOGE("[SliceBuilder] Passed invalid param index.");
82         return returnCode;
83     }
84 
85     for (int i : paramsIndex) {
86         std::shared_ptr<NNTensor> tensor = allTensors[i];
87         tensor->IdentifyOpParameter();
88         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
89             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
90         } else {
91             LOGE("[SliceBuilder] Build failed, param invalid, type=%d", tensor->GetType());
92             return OH_NN_INVALID_PARAMETER;
93         }
94 
95         if (returnCode != OH_NN_SUCCESS) {
96             LOGE("[SliceBuilder] Build failed, passed invalid param.");
97             return returnCode;
98         }
99     }
100 
101     m_inputsIndex = inputsIndex;
102     m_outputsIndex = outputsIndex;
103 
104     // The quantization type of the first output determinies that of the operator.
105     SetQuantType(outputsIndex, allTensors);
106 
107     m_name = OP_NAME;
108     m_isBuild = true;
109     return OH_NN_SUCCESS;
110 }
111 
GetPrimitive()112 LiteGraphPrimitvePtr SliceBuilder::GetPrimitive()
113 {
114     if (!m_isBuild) {
115         LOGE("[SliceBuilder] Cannot get primitive before call build.");
116         return {nullptr, DestroyLiteGraphPrimitive};
117     }
118 
119     auto primitive = mindspore::lite::MindIR_SliceFusion_CreatePrimitive(m_axes);
120     if (primitive == nullptr) {
121         LOGE("[SliceBuilder] MindIR_SliceFusion_CreatePrimitive failed.");
122         return {nullptr, DestroyLiteGraphPrimitive};
123     }
124 
125     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
126     return graphPrimitivePtr;
127 }
128 
129 REGISTER_OPS(SliceBuilder, OH_NN_OPS_SLICE);
130 } // namespace ops
131 } // namespace NeuralNetworkRuntime
132 } // namespace OHOS
133