Files
FastDeploy/fastdeploy/runtime/backends/rknpu2/rknpu2_backend.h
Jason 7a4b2f3177 [Other] Optimize rknpu2 backend (#1318)
* Optimize rknpu2 backend

* Fix error

* Fix comparision error
2023-02-15 11:11:37 +08:00

89 lines
2.6 KiB
C++

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#pragma once
#include "fastdeploy/runtime/backends/backend.h"
#include "fastdeploy/runtime/backends/rknpu2/option.h"
#include "fastdeploy/core/fd_tensor.h"
#include "rknn_api.h" // NOLINT
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
namespace fastdeploy {
class RKNPU2Backend : public BaseBackend {
public:
RKNPU2Backend() = default;
virtual ~RKNPU2Backend();
bool Init(const RuntimeOption& runtime_option);
int NumInputs() const override {
return static_cast<int>(inputs_desc_.size());
}
int NumOutputs() const override {
return static_cast<int>(outputs_desc_.size());
}
TensorInfo GetInputInfo(int index) override;
TensorInfo GetOutputInfo(int index) override;
std::vector<TensorInfo> GetInputInfos() override;
std::vector<TensorInfo> GetOutputInfos() override;
bool Infer(std::vector<FDTensor>& inputs, std::vector<FDTensor>* outputs,
bool copy_to_fd = true) override;
private:
// BaseBackend API
void BuildOption(const RKNPU2BackendOption& option);
// RKNN API
bool LoadModel(void* model);
bool GetSDKAndDeviceVersion();
bool SetCoreMask(const rknpu2::CoreMask& core_mask) const;
bool GetModelInputOutputInfos();
// The object of rknn context.
rknn_context ctx{};
// The structure rknn_sdk_version is used to indicate the version
// information of the RKNN SDK.
rknn_sdk_version sdk_ver{};
// The structure rknn_input_output_num represents the number of
// input and output Tensor
rknn_input_output_num io_num{};
std::vector<TensorInfo> inputs_desc_;
std::vector<TensorInfo> outputs_desc_;
rknn_tensor_attr* input_attrs_ = nullptr;
rknn_tensor_attr* output_attrs_ = nullptr;
rknn_tensor_mem** input_mems_;
rknn_tensor_mem** output_mems_;
bool infer_init = false;
RKNPU2BackendOption option_;
static void DumpTensorAttr(rknn_tensor_attr& attr);
static FDDataType RknnTensorTypeToFDDataType(rknn_tensor_type type);
static rknn_tensor_type FDDataTypeToRknnTensorType(FDDataType type);
};
} // namespace fastdeploy