• 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 "cast_builder.h"
17 
18 #include "frameworks/native/transform.h"
19 #include "frameworks/native/validation.h"
20 
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Ops {
24 static const int INPUT_NUM = 2;
25 static const int OUTPUT_NUM = 1;
26 static const int INPUT_TYPE = 1;
27 static const std::string OP_NAME = "Cast";
28 
CastBuilder()29 CastBuilder::CastBuilder() {}
30 
~CastBuilder()31 CastBuilder::~CastBuilder() {}
32 
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)33 OH_NN_ReturnCode CastBuilder::Build(const std::vector<uint32_t>& paramsIndex,
34                                     const std::vector<uint32_t>& inputsIndex,
35                                     const std::vector<uint32_t>& outputsIndex,
36                                     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
37 {
38     if (m_isBuild) {
39         LOGE("[Cast] Build failed, operation has been build, cannot build again.");
40         return OH_NN_OPERATION_FORBIDDEN;
41     }
42     auto ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
43     if (ret != OH_NN_SUCCESS) {
44         LOGE("[Cast] Build failed, the input or output index of Cast operation is invalid.");
45         return ret;
46     }
47     m_inputsIndex = inputsIndex;
48     m_outputsIndex = outputsIndex;
49 
50     auto castType = allTensors[inputsIndex[INPUT_TYPE]]->GetBuffer();
51     if (castType == nullptr) {
52         LOGE("[Cast] Build castType GetBuffer return nullptr.");
53         return OH_NN_INVALID_PARAMETER;
54     }
55     OH_NN_DataType* castTypeInt = reinterpret_cast<OH_NN_DataType *>(castType);
56     if (!Validation::ValidateTensorDataType(*castTypeInt)) {
57         LOGE("[Cast] Type of cast operator is not validation.");
58         return OH_NN_INVALID_PARAMETER;
59     }
60     *castTypeInt = (OH_NN_DataType)NNToHDI::TransDataType(*castTypeInt);
61 
62     if (!paramsIndex.empty()) {
63         LOGE("[Cast] Cast expects no parameters");
64         return OH_NN_INVALID_PARAMETER;
65     }
66 
67     // The quantization type of the first output determinies that of the operator.
68     SetQuantType(outputsIndex, allTensors);
69     m_isBuild = true;
70     m_name = OP_NAME;
71     return OH_NN_SUCCESS;
72 }
73 
GetPrimitive()74 LiteGraphPrimitvePtr CastBuilder::GetPrimitive()
75 {
76     if (!m_isBuild) {
77         LOGE("[Cast] Cannot get primitive before call build.");
78         return {nullptr, DestroyLiteGraphPrimitive};
79     }
80 
81     void* primitive = mindspore::lite::MindIR_Cast_CreatePrimitive();
82     LiteGraphPrimitvePtr  graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
83     return graphPrimitivePtr;
84 }
85 
86 REGISTER_OPS(CastBuilder, OH_NN_OPS_CAST);
87 } // namespace Ops
88 } // namespace NeuralNetworkRuntime
89 } // namespace OHOS
90