Files
neural_network_runtime/frameworks/native/ops/slice_builder.cpp
T
yangyongjie 7f4a0afc68 !1 Add Neural Network Runtime code
* add neural network runtime
2022-10-28 02:32:29 +00:00

90 lines
2.8 KiB
C++

/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "slice_builder.h"
#include "mindir.h"
namespace OHOS {
namespace NeuralNetworkRuntime {
namespace Ops {
static const int INPUT_NUM = 3;
static const int OUTPUT_NUM = 1;
static const std::string OP_NAME = "Slice";
SliceBuilder::SliceBuilder() {}
SliceBuilder::~SliceBuilder() {}
/**
* Build method.
* 1.set attr of ops.
* 2.set inputIndex of ops.
* 3.set outputIndex of ops.
*/
OH_NN_ReturnCode SliceBuilder::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)
{
if (m_isBuild) {
LOGE("[SliceBuilder] Slice operation has been build, cannot build again.");
return OH_NN_OPERATION_FORBIDDEN;
}
OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
if (returnCode != OH_NN_SUCCESS) {
LOGE("[SliceBuilder] Passed invalid input or output index.");
return returnCode;
}
if (!paramsIndex.empty()) {
LOGE("[SliceBuilder] slice expects no parameters, but receive %zu", paramsIndex.size());
return OH_NN_INVALID_PARAMETER;
}
m_inputsIndex = inputsIndex;
m_outputsIndex = outputsIndex;
// The quantization type of the first output determinies that of the operator.
SetQuantType(outputsIndex, allTensors);
m_name = OP_NAME;
m_isBuild = true;
return OH_NN_SUCCESS;
}
LiteGraphPrimitvePtr SliceBuilder::GetPrimitive()
{
if (!m_isBuild) {
LOGE("[SliceBuilder] Cannot get primitive before call build.");
return {nullptr, DestroyLiteGraphPrimitive};
}
auto primitive = mindspore::lite::MindIR_SliceFusion_CreatePrimitive(m_axes);
if (primitive == nullptr) {
LOGE("[SliceBuilder] MindIR_SliceFusion_CreatePrimitive failed.");
return {nullptr, DestroyLiteGraphPrimitive};
}
LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
return graphPrimitivePtr;
}
REGISTER_OPS(SliceBuilder, OH_NN_OPS_SLICE);
} // namespace ops
} // namespace NeuralNetworkRuntime
} // namespace OHOS