mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-16 21:51:31 +08:00
[Backend] Remove deprecated ort api (#2034)
* [Backend] Remove deprecated ort api `Ort::CustomOpApi` deprecated in ONNXRuntime 1.15 fix https://github.com/PaddlePaddle/FastDeploy/issues/2033 * Improve compatibly with ort older than 1.14 * Update fastdeploy/runtime/backends/ort/ops/multiclass_nms.cc Co-authored-by: Horror Proton <107091537+horror-proton@users.noreply.github.com> * Fix double free and wrong attrib type in ort/ops --------- Co-authored-by: Jason <928090362@qq.com>
This commit is contained in:
@@ -17,13 +17,6 @@
|
||||
#include "adaptive_pool2d.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
struct OrtTensorDimensions : std::vector<int64_t> {
|
||||
OrtTensorDimensions(Ort::CustomOpApi ort, const OrtValue* value) {
|
||||
OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value);
|
||||
std::vector<int64_t>::operator=(ort.GetTensorShape(info));
|
||||
ort.ReleaseTensorTypeAndShapeInfo(info);
|
||||
}
|
||||
};
|
||||
|
||||
void AdaptivePool2dKernel::CpuAdaptivePool(
|
||||
const std::vector<int64_t>& input_size,
|
||||
@@ -68,25 +61,38 @@ void AdaptivePool2dKernel::CpuAdaptivePool(
|
||||
}
|
||||
|
||||
void AdaptivePool2dKernel::Compute(OrtKernelContext* context) {
|
||||
const OrtValue* input = ort_.KernelContext_GetInput(context, 0);
|
||||
#if ORT_API_VERSION >= 14
|
||||
Ort::KernelContext ort_context{context};
|
||||
Ort::ConstValue input = ort_context.GetInput(0);
|
||||
#else
|
||||
Ort::CustomOpApi api{ort_};
|
||||
Ort::Unowned<const Ort::Value> input{
|
||||
const_cast<OrtValue*>(api.KernelContext_GetInput(context, 0))};
|
||||
#endif
|
||||
auto input_data = input.GetTensorData<float>();
|
||||
auto input_dim = input.GetTensorTypeAndShapeInfo().GetShape();
|
||||
|
||||
const float* input_data =
|
||||
reinterpret_cast<const float*>(ort_.GetTensorData<float>(input));
|
||||
|
||||
OrtTensorDimensions input_dim(ort_, input);
|
||||
output_size_[0] = input_dim[0];
|
||||
std::vector<int64_t> input_size;
|
||||
for (auto i : input_dim) {
|
||||
input_size.push_back(i);
|
||||
}
|
||||
|
||||
OrtValue* output = ort_.KernelContext_GetOutput(
|
||||
context, 0, output_size_.data(), output_size_.size());
|
||||
|
||||
float* output_data = ort_.GetTensorMutableData<float>(output);
|
||||
#if ORT_API_VERSION >= 14
|
||||
auto output = ort_context.GetOutput(0, output_size_);
|
||||
#else
|
||||
Ort::Unowned<Ort::Value> output{api.KernelContext_GetOutput(
|
||||
context, 0, output_size_.data(), output_size_.size())};
|
||||
#endif
|
||||
float* output_data = output.GetTensorMutableData<float>();
|
||||
if (!strcmp(this->provider_, "CUDAExecutionProvider")) {
|
||||
#ifdef WITH_GPU
|
||||
auto compute_stream = ort_.KernelContext_GetGPUComputeStream(context);
|
||||
auto compute_stream =
|
||||
#if ORT_API_VERSION >= 14
|
||||
ort_context.GetGPUComputeStream();
|
||||
#else
|
||||
api.KernelContext_GetGPUComputeStream(context);
|
||||
#endif
|
||||
CudaAdaptivePool(input_size, output_size_, output_data, input_data,
|
||||
compute_stream, pooling_type_);
|
||||
#else
|
||||
@@ -100,13 +106,19 @@ void AdaptivePool2dKernel::Compute(OrtKernelContext* context) {
|
||||
}
|
||||
|
||||
void AdaptivePool2dKernel::GetAttribute(const OrtKernelInfo* info) {
|
||||
pooling_type_ =
|
||||
ort_.KernelInfoGetAttribute<std::string>(info, "pooling_type");
|
||||
#if ORT_API_VERSION >= 14
|
||||
Ort::ConstKernelInfo ort_info{info};
|
||||
pooling_type_ = ort_info.GetAttribute<std::string>("pooling_type");
|
||||
output_size_ = ort_info.GetAttributes<int64_t>("output_size");
|
||||
#else
|
||||
Ort::CustomOpApi api{ort_};
|
||||
pooling_type_ = api.KernelInfoGetAttribute<std::string>(info, "pooling_type");
|
||||
output_size_ =
|
||||
ort_.KernelInfoGetAttribute<std::vector<int64_t>>(info, "output_size");
|
||||
FDASSERT(
|
||||
output_size_.size() == 4 && output_size_[2] > 0 && output_size_[3] > 0,
|
||||
"The output size of adaptive pool must be positive.");
|
||||
api.KernelInfoGetAttribute<std::vector<int64_t>>(info, "output_size");
|
||||
#endif
|
||||
FDASSERT(output_size_.size() == 4 && output_size_[2] > 0 &&
|
||||
output_size_[3] > 0,
|
||||
"The output size of adaptive pool must be positive.");
|
||||
}
|
||||
} // namespace fastdeploy
|
||||
|
||||
|
@@ -33,12 +33,12 @@ struct AdaptivePool2dKernel {
|
||||
protected:
|
||||
std::string pooling_type_ = "avg";
|
||||
std::vector<int64_t> output_size_ = {};
|
||||
Ort::CustomOpApi ort_;
|
||||
OrtApi ort_;
|
||||
void* compute_stream_;
|
||||
const char* provider_;
|
||||
|
||||
public:
|
||||
AdaptivePool2dKernel(Ort::CustomOpApi ort, const OrtKernelInfo* info,
|
||||
AdaptivePool2dKernel(OrtApi ort, const OrtKernelInfo* info,
|
||||
const char* provider)
|
||||
: ort_(ort) {
|
||||
GetAttribute(info);
|
||||
@@ -57,7 +57,7 @@ struct AdaptivePool2dKernel {
|
||||
struct AdaptivePool2dOp
|
||||
: Ort::CustomOpBase<AdaptivePool2dOp, AdaptivePool2dKernel> {
|
||||
explicit AdaptivePool2dOp(const char* provider) : provider_(provider) {}
|
||||
void* CreateKernel(Ort::CustomOpApi api, const OrtKernelInfo* info) const {
|
||||
void* CreateKernel(OrtApi api, const OrtKernelInfo* info) const {
|
||||
return new AdaptivePool2dKernel(api, info, provider_);
|
||||
}
|
||||
|
||||
|
@@ -23,14 +23,6 @@
|
||||
|
||||
namespace fastdeploy {
|
||||
|
||||
struct OrtTensorDimensions : std::vector<int64_t> {
|
||||
OrtTensorDimensions(Ort::CustomOpApi ort, const OrtValue* value) {
|
||||
OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value);
|
||||
std::vector<int64_t>::operator=(ort.GetTensorShape(info));
|
||||
ort.ReleaseTensorTypeAndShapeInfo(info);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
bool SortScorePairDescend(const std::pair<float, T>& pair1,
|
||||
const std::pair<float, T>& pair2) {
|
||||
@@ -165,14 +157,24 @@ int MultiClassNmsKernel::NMSForEachSample(
|
||||
}
|
||||
|
||||
void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
||||
const OrtValue* boxes = ort_.KernelContext_GetInput(context, 0);
|
||||
const OrtValue* scores = ort_.KernelContext_GetInput(context, 1);
|
||||
const float* boxes_data =
|
||||
reinterpret_cast<const float*>(ort_.GetTensorData<float>(boxes));
|
||||
const float* scores_data =
|
||||
reinterpret_cast<const float*>(ort_.GetTensorData<float>(scores));
|
||||
OrtTensorDimensions boxes_dim(ort_, boxes);
|
||||
OrtTensorDimensions scores_dim(ort_, scores);
|
||||
#if ORT_API_VERSION >= 14
|
||||
Ort::KernelContext ort_context{context};
|
||||
Ort::ConstValue boxes = ort_context.GetInput(0);
|
||||
Ort::ConstValue scores = ort_context.GetInput(1);
|
||||
#else
|
||||
Ort::CustomOpApi api{ort_};
|
||||
Ort::Unowned<const Ort::Value> boxes{
|
||||
const_cast<OrtValue*>(api.KernelContext_GetInput(context, 0))};
|
||||
Ort::Unowned<const Ort::Value> scores{
|
||||
const_cast<OrtValue*>(api.KernelContext_GetInput(context, 1))};
|
||||
#endif
|
||||
|
||||
auto boxes_data = boxes.GetTensorData<float>();
|
||||
auto scores_data = scores.GetTensorData<float>();
|
||||
|
||||
auto boxes_dim = boxes.GetTensorTypeAndShapeInfo().GetShape();
|
||||
auto scores_dim = scores.GetTensorTypeAndShapeInfo().GetShape();
|
||||
|
||||
int score_size = scores_dim.size();
|
||||
|
||||
int64_t batch_size = scores_dim[0];
|
||||
@@ -183,12 +185,16 @@ void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
||||
FDASSERT(score_size == 3,
|
||||
"Require rank of input scores be 3, but now it's %d.", score_size);
|
||||
FDASSERT(boxes_dim[2] == 4,
|
||||
"Require the 3-dimension of input boxes be 4, but now it's %lld.",
|
||||
"Require the 3-dimension of input boxes be 4, but now it's %ld.",
|
||||
box_dim);
|
||||
std::vector<int64_t> out_num_rois_dims = {batch_size};
|
||||
OrtValue* out_num_rois = ort_.KernelContext_GetOutput(
|
||||
context, 2, out_num_rois_dims.data(), out_num_rois_dims.size());
|
||||
int32_t* out_num_rois_data = ort_.GetTensorMutableData<int32_t>(out_num_rois);
|
||||
#if ORT_API_VERSION >= 14
|
||||
auto out_num_rois = ort_context.GetOutput(2, out_num_rois_dims);
|
||||
#else
|
||||
Ort::Unowned<Ort::Value> out_num_rois{api.KernelContext_GetOutput(
|
||||
context, 2, out_num_rois_dims.data(), out_num_rois_dims.size())};
|
||||
#endif
|
||||
int32_t* out_num_rois_data = out_num_rois.GetTensorMutableData<int32_t>();
|
||||
|
||||
std::vector<std::map<int, std::vector<int>>> all_indices;
|
||||
for (size_t i = 0; i < batch_size; ++i) {
|
||||
@@ -205,20 +211,26 @@ void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
||||
}
|
||||
std::vector<int64_t> out_box_dims = {num_nmsed_out, 6};
|
||||
std::vector<int64_t> out_index_dims = {num_nmsed_out, 1};
|
||||
OrtValue* out_box = ort_.KernelContext_GetOutput(
|
||||
context, 0, out_box_dims.data(), out_box_dims.size());
|
||||
OrtValue* out_index = ort_.KernelContext_GetOutput(
|
||||
context, 1, out_index_dims.data(), out_index_dims.size());
|
||||
|
||||
#if ORT_API_VERSION >= 14
|
||||
auto out_box = ort_context.GetOutput(0, out_box_dims);
|
||||
auto out_index = ort_context.GetOutput(1, out_index_dims);
|
||||
#else
|
||||
Ort::Unowned<Ort::Value> out_box{api.KernelContext_GetOutput(
|
||||
context, 0, out_box_dims.data(), out_box_dims.size())};
|
||||
Ort::Unowned<Ort::Value> out_index{api.KernelContext_GetOutput(
|
||||
context, 1, out_index_dims.data(), out_index_dims.size())};
|
||||
#endif
|
||||
|
||||
if (num_nmsed_out == 0) {
|
||||
int32_t* out_num_rois_data =
|
||||
ort_.GetTensorMutableData<int32_t>(out_num_rois);
|
||||
int32_t* out_num_rois_data = out_num_rois.GetTensorMutableData<int32_t>();
|
||||
for (size_t i = 0; i < batch_size; ++i) {
|
||||
out_num_rois_data[i] = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
float* out_box_data = ort_.GetTensorMutableData<float>(out_box);
|
||||
int32_t* out_index_data = ort_.GetTensorMutableData<int32_t>(out_index);
|
||||
float* out_box_data = out_box.GetTensorMutableData<float>();
|
||||
int32_t* out_index_data = out_index.GetTensorMutableData<int32_t>();
|
||||
|
||||
int count = 0;
|
||||
for (size_t i = 0; i < batch_size; ++i) {
|
||||
@@ -249,14 +261,26 @@ void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
||||
}
|
||||
|
||||
void MultiClassNmsKernel::GetAttribute(const OrtKernelInfo* info) {
|
||||
#if ORT_API_VERSION >= 14
|
||||
Ort::ConstKernelInfo ort_info{info};
|
||||
background_label = ort_info.GetAttribute<int64_t>("background_label");
|
||||
keep_top_k = ort_info.GetAttribute<int64_t>("keep_top_k");
|
||||
nms_eta = ort_info.GetAttribute<float>("nms_eta");
|
||||
nms_threshold = ort_info.GetAttribute<float>("nms_threshold");
|
||||
nms_top_k = ort_info.GetAttribute<int64_t>("nms_top_k");
|
||||
normalized = ort_info.GetAttribute<int64_t>("normalized");
|
||||
score_threshold = ort_info.GetAttribute<float>("score_threshold");
|
||||
#else
|
||||
Ort::CustomOpApi api{ort_};
|
||||
background_label =
|
||||
ort_.KernelInfoGetAttribute<int64_t>(info, "background_label");
|
||||
keep_top_k = ort_.KernelInfoGetAttribute<int64_t>(info, "keep_top_k");
|
||||
nms_eta = ort_.KernelInfoGetAttribute<float>(info, "nms_eta");
|
||||
nms_threshold = ort_.KernelInfoGetAttribute<float>(info, "nms_threshold");
|
||||
nms_top_k = ort_.KernelInfoGetAttribute<int64_t>(info, "nms_top_k");
|
||||
normalized = ort_.KernelInfoGetAttribute<int64_t>(info, "normalized");
|
||||
score_threshold = ort_.KernelInfoGetAttribute<float>(info, "score_threshold");
|
||||
api.KernelInfoGetAttribute<int64_t>(info, "background_label");
|
||||
keep_top_k = api.KernelInfoGetAttribute<int64_t>(info, "keep_top_k");
|
||||
nms_eta = api.KernelInfoGetAttribute<float>(info, "nms_eta");
|
||||
nms_threshold = api.KernelInfoGetAttribute<float>(info, "nms_threshold");
|
||||
nms_top_k = api.KernelInfoGetAttribute<int64_t>(info, "nms_top_k");
|
||||
normalized = api.KernelInfoGetAttribute<int64_t>(info, "normalized");
|
||||
score_threshold = api.KernelInfoGetAttribute<float>(info, "score_threshold");
|
||||
#endif
|
||||
}
|
||||
} // namespace fastdeploy
|
||||
|
||||
|
@@ -30,11 +30,10 @@ struct MultiClassNmsKernel {
|
||||
int64_t nms_top_k;
|
||||
bool normalized;
|
||||
float score_threshold;
|
||||
Ort::CustomOpApi ort_;
|
||||
OrtApi ort_;
|
||||
|
||||
public:
|
||||
MultiClassNmsKernel(Ort::CustomOpApi ort, const OrtKernelInfo* info)
|
||||
: ort_(ort) {
|
||||
MultiClassNmsKernel(OrtApi ort, const OrtKernelInfo* info) : ort_(ort) {
|
||||
GetAttribute(info);
|
||||
}
|
||||
|
||||
@@ -50,7 +49,7 @@ struct MultiClassNmsKernel {
|
||||
|
||||
struct MultiClassNmsOp
|
||||
: Ort::CustomOpBase<MultiClassNmsOp, MultiClassNmsKernel> {
|
||||
void* CreateKernel(Ort::CustomOpApi api, const OrtKernelInfo* info) const {
|
||||
void* CreateKernel(OrtApi api, const OrtKernelInfo* info) const {
|
||||
return new MultiClassNmsKernel(api, info);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user