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 int PARAM_MAX_NUM = 1;
26 static const std::string OP_NAME = "Tile";
27 
TileBuilder()28 TileBuilder::TileBuilder() {}
29 
~TileBuilder()30 TileBuilder::~TileBuilder() {}
31 
SetDims(const std::shared_ptr<NNTensor> & tensor)32 OH_NN_ReturnCode TileBuilder::SetDims(const std::shared_ptr<NNTensor>& tensor)
33 {
34     if (tensor->GetDataType() != OH_NN_INT64) {
35         LOGE("[TileBuilder] The dims should be type OH_NN_INT64.");
36         return OH_NN_INVALID_PARAMETER;
37     }
38 
39     m_dims.clear();
40 
41     void* buffer = tensor->GetBuffer();
42     if (buffer == nullptr) {
43         LOGE("[TileBuilder] Tensor buffer is nullptr.");
44         return OH_NN_INVALID_PARAMETER;
45     }
46 
47     int64_t* pDims = static_cast<int64_t*>(buffer);
48 
49     uint32_t elementCount = tensor->GetElementCount();
50     for (uint32_t i = 0; i < elementCount; ++i) {
51         m_dims.emplace_back(*pDims);
52         ++pDims;
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 TileBuilder::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("[TileBuilder] Tile 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("[TileBuilder] 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("[TileBuilder] 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("[TileBuilder] Build failed, param invalid, type=%d", tensor->GetType());
92             return OH_NN_INVALID_PARAMETER;
93         }
94 
95         if (returnCode != OH_NN_SUCCESS) {
96             LOGE("[TileBuilder] Build failed, passed invalid param.");
97             return returnCode;
98         }
99     }
100 
101     m_inputsIndex = inputsIndex;
102     m_outputsIndex = outputsIndex;
103 
104     m_isBuild = true;
105     m_name = OP_NAME;
106     return OH_NN_SUCCESS;
107 }
108 
GetPrimitive()109 LiteGraphPrimitvePtr TileBuilder::GetPrimitive()
110 {
111     if (!m_isBuild) {
112         LOGE("[TileBuilder] Cannot get primitive before call build.");
113         return {nullptr, DestroyLiteGraphPrimitive};
114     }
115 
116     auto primitive = mindspore::lite::MindIR_TileFusion_CreatePrimitive(m_dims);
117     if (primitive == nullptr) {
118         LOGE("[TileBuilder] MindIR_TileFusion_CreatePrimitive failed.");
119         return {nullptr, DestroyLiteGraphPrimitive};
120     }
121 
122     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
123     return graphPrimitivePtr;
124 }
125 
126 REGISTER_OPS(TileBuilder, OH_NN_OPS_TILE);
127 } // namespace Ops
128 } // namespace NeuralNetworkRuntime
129 } // namespace OHOS
130