mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-07 01:22:59 +08:00

* 10-29/14:05 * 新增cmake * 新增rknpu2 backend * 10-29/14:43 * Runtime fd_type新增RKNPU代码 * 10-29/15:02 * 新增ppseg RKNPU2推理代码 * 10-29/15:46 * 新增ppseg RKNPU2 cpp example代码 * 10-29/15:51 * 新增README文档 * 10-29/15:51 * 按照要求修改部分注释以及变量名称 * 10-29/15:51 * 修复重命名之后,cc文件中的部分代码还用旧函数名的bug * 10-29/22:32 * str(Device::NPU)将输出NPU而不是UNKOWN * 修改runtime文件中的注释格式 * 新增Building Summary ENABLE_RKNPU2_BACKEND输出 * pybind新增支持rknpu2 * 新增python编译选项 * 新增PPSeg Python代码 * 新增以及更新各种文档 * 10-30/14:11 * 尝试修复编译cuda时产生的错误 * 10-30/19:27 * 修改CpuName和CoreMask层级 * 修改ppseg rknn推理层级 * 图片将移动到网络进行下载 * 10-30/19:39 * 更新文档 * 10-30/19:39 * 更新文档 * 更新ppseg rknpu2 example中的函数命名方式 * 更新ppseg rknpu2 example为一个cc文件 * 修复disable_normalize_and_permute部分的逻辑错误 * 移除rknpu2初始化时的无用参数 * 10-30/19:39 * 尝试重置python代码 * 10-30/10:16 * rknpu2_config.h文件不再包含rknn_api头文件防止出现导入错误的问题 * 10-31/14:31 * 修改pybind,支持最新的rknpu2 backends * 再次支持ppseg python推理 * 移动cpuname 和 coremask的层级 * 10-31/15:35 * 尝试修复rknpu2导入错误 * 10-31/19:00 * 新增RKNPU2模型导出代码以及其对应的文档 * 更新大量文档错误 * 10-31/19:00 * 现在编译完fastdeploy仓库后无需重新设置RKNN2_TARGET_SOC * 10-31/19:26 * 修改部分错误文档 * 10-31/19:26 * 修复错误删除的部分 * 修复各种错误文档 * 修复FastDeploy.cmake在设置RKNN2_TARGET_SOC错误时,提示错误的信息 * 修复rknpu2_backend.cc中存在的中文注释 * 10-31/20:45 * 删除无用的注释 * 10-31/20:45 * 按照要求修改Device::NPU为Device::RKNPU,硬件将共用valid_hardware_backends * 删除无用注释以及debug代码 * 11-01/09:45 * 更新变量命名方式 * 11-01/10:16 * 修改部分文档,修改函数命名方式 Co-authored-by: Jason <jiangjiajun@baidu.com>
135 lines
4.2 KiB
C++
135 lines
4.2 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 <pybind11/numpy.h>
|
|
#include <pybind11/pybind11.h>
|
|
#include <pybind11/stl.h>
|
|
|
|
#include <type_traits>
|
|
|
|
#include "fastdeploy/runtime.h"
|
|
|
|
#ifdef ENABLE_VISION
|
|
#include "fastdeploy/vision.h"
|
|
#include "fastdeploy/pipeline.h"
|
|
#endif
|
|
|
|
#ifdef ENABLE_TEXT
|
|
#include "fastdeploy/text.h"
|
|
#endif
|
|
|
|
#include "fastdeploy/core/float16.h"
|
|
|
|
namespace fastdeploy {
|
|
|
|
void BindBackend(pybind11::module&);
|
|
void BindVision(pybind11::module&);
|
|
void BindText(pybind11::module& m);
|
|
void BindPipeline(pybind11::module& m);
|
|
void BindRKNPU2Config(pybind11::module&);
|
|
|
|
pybind11::dtype FDDataTypeToNumpyDataType(const FDDataType& fd_dtype);
|
|
|
|
FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype);
|
|
|
|
void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor,
|
|
bool share_buffer = false);
|
|
void PyArrayToTensorList(std::vector<pybind11::array>& pyarray,
|
|
std::vector<FDTensor>* tensor,
|
|
bool share_buffer = false);
|
|
pybind11::array TensorToPyArray(const FDTensor& tensor);
|
|
|
|
#ifdef ENABLE_VISION
|
|
cv::Mat PyArrayToCvMat(pybind11::array& pyarray);
|
|
#endif
|
|
|
|
template <typename T>
|
|
FDDataType CTypeToFDDataType() {
|
|
if (std::is_same<T, int32_t>::value) {
|
|
return FDDataType::INT32;
|
|
} else if (std::is_same<T, int64_t>::value) {
|
|
return FDDataType::INT64;
|
|
} else if (std::is_same<T, float>::value) {
|
|
return FDDataType::FP32;
|
|
} else if (std::is_same<T, double>::value) {
|
|
return FDDataType::FP64;
|
|
}
|
|
FDASSERT(false,
|
|
"CTypeToFDDataType only support int32/int64/float32/float64 now.");
|
|
return FDDataType::FP32;
|
|
}
|
|
|
|
template <typename T>
|
|
std::vector<pybind11::array> PyBackendInfer(
|
|
T& self, const std::vector<std::string>& names,
|
|
std::vector<pybind11::array>& data) {
|
|
std::vector<FDTensor> inputs(data.size());
|
|
for (size_t i = 0; i < data.size(); ++i) {
|
|
// TODO(jiangjiajun) here is considered to use user memory directly
|
|
auto dtype = NumpyDataTypeToFDDataType(data[i].dtype());
|
|
std::vector<int64_t> data_shape;
|
|
data_shape.insert(data_shape.begin(), data[i].shape(),
|
|
data[i].shape() + data[i].ndim());
|
|
inputs[i].Resize(data_shape, dtype);
|
|
memcpy(inputs[i].MutableData(), data[i].mutable_data(), data[i].nbytes());
|
|
inputs[i].name = names[i];
|
|
}
|
|
|
|
std::vector<FDTensor> outputs(self.NumOutputs());
|
|
self.Infer(inputs, &outputs);
|
|
|
|
std::vector<pybind11::array> results;
|
|
results.reserve(outputs.size());
|
|
for (size_t i = 0; i < outputs.size(); ++i) {
|
|
auto numpy_dtype = FDDataTypeToNumpyDataType(outputs[i].dtype);
|
|
results.emplace_back(pybind11::array(numpy_dtype, outputs[i].shape));
|
|
memcpy(results[i].mutable_data(), outputs[i].Data(),
|
|
outputs[i].Numel() * FDDataTypeSize(outputs[i].dtype));
|
|
}
|
|
return results;
|
|
}
|
|
|
|
} // namespace fastdeploy
|
|
|
|
namespace pybind11 {
|
|
namespace detail {
|
|
|
|
// Note: use same enum number of float16 in numpy.
|
|
// import numpy as np
|
|
// print np.dtype(np.float16).num # 23
|
|
constexpr int NPY_FLOAT16_ = 23;
|
|
|
|
// Note: Since float16 is not a builtin type in C++, we register
|
|
// fastdeploy::float16 as numpy.float16.
|
|
// Ref: https://github.com/pybind/pybind11/issues/1776
|
|
template <>
|
|
struct npy_format_descriptor<fastdeploy::float16> {
|
|
static pybind11::dtype dtype() {
|
|
handle ptr = npy_api::get().PyArray_DescrFromType_(NPY_FLOAT16_);
|
|
return reinterpret_borrow<pybind11::dtype>(ptr);
|
|
}
|
|
static std::string format() {
|
|
// Note: "e" represents float16.
|
|
// Details at:
|
|
// https://docs.python.org/3/library/struct.html#format-characters.
|
|
return "e";
|
|
}
|
|
static constexpr auto name = _("float16");
|
|
};
|
|
|
|
} // namespace detail
|
|
} // namespace pybind11
|