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 "tile_builder.h"
17
18 #include "mindir.h"
19
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
23 static const int INPUT_NUM = 2;
24 static const int OUTPUT_NUM = 1;
25 static const std::string OP_NAME = "Tile";
26
TileBuilder()27 TileBuilder::TileBuilder() {}
28
~TileBuilder()29 TileBuilder::~TileBuilder() {}
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 TileBuilder::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("[TileBuilder] Tile 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("[TileBuilder] Passed invalid input or output index.");
50 return returnCode;
51 }
52
53 if (!paramsIndex.empty()) {
54 LOGE("[TileBuilder] TransposeBuilder 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 m_isBuild = true;
62 m_name = OP_NAME;
63 return OH_NN_SUCCESS;
64 }
65
GetPrimitive()66 LiteGraphPrimitvePtr TileBuilder::GetPrimitive()
67 {
68 if (!m_isBuild) {
69 LOGE("[TileBuilder] Cannot get primitive before call build.");
70 return {nullptr, DestroyLiteGraphPrimitive};
71 }
72
73 auto primitive = mindspore::lite::MindIR_TileFusion_CreatePrimitive(m_dims);
74 if (primitive == nullptr) {
75 LOGE("[TileBuilder] MindIR_TileFusion_CreatePrimitive failed.");
76 return {nullptr, DestroyLiteGraphPrimitive};
77 }
78
79 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
80 return graphPrimitivePtr;
81 }
82
83 REGISTER_OPS(TileBuilder, OH_NN_OPS_TILE);
84 } // namespace Ops
85 } // namespace NeuralNetworkRuntime
86 } // namespace OHOS
87