• 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 "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 std::string OP_NAME = "Slice";
26 
SliceBuilder()27 SliceBuilder::SliceBuilder() {}
28 
~SliceBuilder()29 SliceBuilder::~SliceBuilder() {}
30 
31 /**
32  * Build method.
33  * 1.set attr of ops.
34  * 2.set inputIndex of ops.
35  * 3.set outputIndex of ops.
36  */
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)37 OH_NN_ReturnCode SliceBuilder::Build(const std::vector<uint32_t>& paramsIndex,
38                                      const std::vector<uint32_t>& inputsIndex,
39                                      const std::vector<uint32_t>& outputsIndex,
40                                      const std::vector<std::shared_ptr<NNTensor>>& allTensors)
41 {
42     if (m_isBuild) {
43         LOGE("[SliceBuilder] Slice operation has been build, cannot build again.");
44         return OH_NN_OPERATION_FORBIDDEN;
45     }
46 
47     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
48     if (returnCode != OH_NN_SUCCESS) {
49         LOGE("[SliceBuilder] Passed invalid input or output index.");
50         return returnCode;
51     }
52 
53     if (!paramsIndex.empty()) {
54         LOGE("[SliceBuilder] slice expects no parameters, but receive %zu", paramsIndex.size());
55         return OH_NN_INVALID_PARAMETER;
56     }
57 
58     m_inputsIndex = inputsIndex;
59     m_outputsIndex = outputsIndex;
60 
61     // The quantization type of the first output determinies that of the operator.
62     SetQuantType(outputsIndex, allTensors);
63 
64     m_name = OP_NAME;
65     m_isBuild = true;
66     return OH_NN_SUCCESS;
67 }
68 
GetPrimitive()69 LiteGraphPrimitvePtr SliceBuilder::GetPrimitive()
70 {
71     if (!m_isBuild) {
72         LOGE("[SliceBuilder] Cannot get primitive before call build.");
73         return {nullptr, DestroyLiteGraphPrimitive};
74     }
75 
76     auto primitive = mindspore::lite::MindIR_SliceFusion_CreatePrimitive(m_axes);
77     if (primitive == nullptr) {
78         LOGE("[SliceBuilder] MindIR_SliceFusion_CreatePrimitive failed.");
79         return {nullptr, DestroyLiteGraphPrimitive};
80     }
81 
82     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
83     return graphPrimitivePtr;
84 }
85 
86 REGISTER_OPS(SliceBuilder, OH_NN_OPS_SLICE);
87 } // namespace ops
88 } // namespace NeuralNetworkRuntime
89 } // namespace OHOS
90