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