mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-17 06:00:59 +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"
|
#include "adaptive_pool2d.h"
|
||||||
|
|
||||||
namespace fastdeploy {
|
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(
|
void AdaptivePool2dKernel::CpuAdaptivePool(
|
||||||
const std::vector<int64_t>& input_size,
|
const std::vector<int64_t>& input_size,
|
||||||
@@ -68,25 +61,38 @@ void AdaptivePool2dKernel::CpuAdaptivePool(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AdaptivePool2dKernel::Compute(OrtKernelContext* context) {
|
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];
|
output_size_[0] = input_dim[0];
|
||||||
std::vector<int64_t> input_size;
|
std::vector<int64_t> input_size;
|
||||||
for (auto i : input_dim) {
|
for (auto i : input_dim) {
|
||||||
input_size.push_back(i);
|
input_size.push_back(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
OrtValue* output = ort_.KernelContext_GetOutput(
|
#if ORT_API_VERSION >= 14
|
||||||
context, 0, output_size_.data(), output_size_.size());
|
auto output = ort_context.GetOutput(0, output_size_);
|
||||||
|
#else
|
||||||
float* output_data = ort_.GetTensorMutableData<float>(output);
|
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")) {
|
if (!strcmp(this->provider_, "CUDAExecutionProvider")) {
|
||||||
#ifdef WITH_GPU
|
#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,
|
CudaAdaptivePool(input_size, output_size_, output_data, input_data,
|
||||||
compute_stream, pooling_type_);
|
compute_stream, pooling_type_);
|
||||||
#else
|
#else
|
||||||
@@ -100,14 +106,20 @@ void AdaptivePool2dKernel::Compute(OrtKernelContext* context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AdaptivePool2dKernel::GetAttribute(const OrtKernelInfo* info) {
|
void AdaptivePool2dKernel::GetAttribute(const OrtKernelInfo* info) {
|
||||||
pooling_type_ =
|
#if ORT_API_VERSION >= 14
|
||||||
ort_.KernelInfoGetAttribute<std::string>(info, "pooling_type");
|
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_ =
|
output_size_ =
|
||||||
ort_.KernelInfoGetAttribute<std::vector<int64_t>>(info, "output_size");
|
api.KernelInfoGetAttribute<std::vector<int64_t>>(info, "output_size");
|
||||||
FDASSERT(
|
#endif
|
||||||
output_size_.size() == 4 && output_size_[2] > 0 && output_size_[3] > 0,
|
FDASSERT(output_size_.size() == 4 && output_size_[2] > 0 &&
|
||||||
"The output size of adaptive pool must be positive.");
|
output_size_[3] > 0,
|
||||||
|
"The output size of adaptive pool must be positive.");
|
||||||
}
|
}
|
||||||
} // namespace fastdeploy
|
} // namespace fastdeploy
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -33,12 +33,12 @@ struct AdaptivePool2dKernel {
|
|||||||
protected:
|
protected:
|
||||||
std::string pooling_type_ = "avg";
|
std::string pooling_type_ = "avg";
|
||||||
std::vector<int64_t> output_size_ = {};
|
std::vector<int64_t> output_size_ = {};
|
||||||
Ort::CustomOpApi ort_;
|
OrtApi ort_;
|
||||||
void* compute_stream_;
|
void* compute_stream_;
|
||||||
const char* provider_;
|
const char* provider_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AdaptivePool2dKernel(Ort::CustomOpApi ort, const OrtKernelInfo* info,
|
AdaptivePool2dKernel(OrtApi ort, const OrtKernelInfo* info,
|
||||||
const char* provider)
|
const char* provider)
|
||||||
: ort_(ort) {
|
: ort_(ort) {
|
||||||
GetAttribute(info);
|
GetAttribute(info);
|
||||||
@@ -57,7 +57,7 @@ struct AdaptivePool2dKernel {
|
|||||||
struct AdaptivePool2dOp
|
struct AdaptivePool2dOp
|
||||||
: Ort::CustomOpBase<AdaptivePool2dOp, AdaptivePool2dKernel> {
|
: Ort::CustomOpBase<AdaptivePool2dOp, AdaptivePool2dKernel> {
|
||||||
explicit AdaptivePool2dOp(const char* provider) : provider_(provider) {}
|
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_);
|
return new AdaptivePool2dKernel(api, info, provider_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -23,14 +23,6 @@
|
|||||||
|
|
||||||
namespace fastdeploy {
|
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>
|
template <class T>
|
||||||
bool SortScorePairDescend(const std::pair<float, T>& pair1,
|
bool SortScorePairDescend(const std::pair<float, T>& pair1,
|
||||||
const std::pair<float, T>& pair2) {
|
const std::pair<float, T>& pair2) {
|
||||||
@@ -165,14 +157,24 @@ int MultiClassNmsKernel::NMSForEachSample(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
||||||
const OrtValue* boxes = ort_.KernelContext_GetInput(context, 0);
|
#if ORT_API_VERSION >= 14
|
||||||
const OrtValue* scores = ort_.KernelContext_GetInput(context, 1);
|
Ort::KernelContext ort_context{context};
|
||||||
const float* boxes_data =
|
Ort::ConstValue boxes = ort_context.GetInput(0);
|
||||||
reinterpret_cast<const float*>(ort_.GetTensorData<float>(boxes));
|
Ort::ConstValue scores = ort_context.GetInput(1);
|
||||||
const float* scores_data =
|
#else
|
||||||
reinterpret_cast<const float*>(ort_.GetTensorData<float>(scores));
|
Ort::CustomOpApi api{ort_};
|
||||||
OrtTensorDimensions boxes_dim(ort_, boxes);
|
Ort::Unowned<const Ort::Value> boxes{
|
||||||
OrtTensorDimensions scores_dim(ort_, scores);
|
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();
|
int score_size = scores_dim.size();
|
||||||
|
|
||||||
int64_t batch_size = scores_dim[0];
|
int64_t batch_size = scores_dim[0];
|
||||||
@@ -183,12 +185,16 @@ void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
|||||||
FDASSERT(score_size == 3,
|
FDASSERT(score_size == 3,
|
||||||
"Require rank of input scores be 3, but now it's %d.", score_size);
|
"Require rank of input scores be 3, but now it's %d.", score_size);
|
||||||
FDASSERT(boxes_dim[2] == 4,
|
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);
|
box_dim);
|
||||||
std::vector<int64_t> out_num_rois_dims = {batch_size};
|
std::vector<int64_t> out_num_rois_dims = {batch_size};
|
||||||
OrtValue* out_num_rois = ort_.KernelContext_GetOutput(
|
#if ORT_API_VERSION >= 14
|
||||||
context, 2, out_num_rois_dims.data(), out_num_rois_dims.size());
|
auto out_num_rois = ort_context.GetOutput(2, out_num_rois_dims);
|
||||||
int32_t* out_num_rois_data = ort_.GetTensorMutableData<int32_t>(out_num_rois);
|
#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;
|
std::vector<std::map<int, std::vector<int>>> all_indices;
|
||||||
for (size_t i = 0; i < batch_size; ++i) {
|
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_box_dims = {num_nmsed_out, 6};
|
||||||
std::vector<int64_t> out_index_dims = {num_nmsed_out, 1};
|
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());
|
#if ORT_API_VERSION >= 14
|
||||||
OrtValue* out_index = ort_.KernelContext_GetOutput(
|
auto out_box = ort_context.GetOutput(0, out_box_dims);
|
||||||
context, 1, out_index_dims.data(), out_index_dims.size());
|
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) {
|
if (num_nmsed_out == 0) {
|
||||||
int32_t* out_num_rois_data =
|
int32_t* out_num_rois_data = out_num_rois.GetTensorMutableData<int32_t>();
|
||||||
ort_.GetTensorMutableData<int32_t>(out_num_rois);
|
|
||||||
for (size_t i = 0; i < batch_size; ++i) {
|
for (size_t i = 0; i < batch_size; ++i) {
|
||||||
out_num_rois_data[i] = 0;
|
out_num_rois_data[i] = 0;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
float* out_box_data = ort_.GetTensorMutableData<float>(out_box);
|
float* out_box_data = out_box.GetTensorMutableData<float>();
|
||||||
int32_t* out_index_data = ort_.GetTensorMutableData<int32_t>(out_index);
|
int32_t* out_index_data = out_index.GetTensorMutableData<int32_t>();
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (size_t i = 0; i < batch_size; ++i) {
|
for (size_t i = 0; i < batch_size; ++i) {
|
||||||
@@ -249,15 +261,27 @@ void MultiClassNmsKernel::Compute(OrtKernelContext* context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MultiClassNmsKernel::GetAttribute(const OrtKernelInfo* info) {
|
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 =
|
background_label =
|
||||||
ort_.KernelInfoGetAttribute<int64_t>(info, "background_label");
|
api.KernelInfoGetAttribute<int64_t>(info, "background_label");
|
||||||
keep_top_k = ort_.KernelInfoGetAttribute<int64_t>(info, "keep_top_k");
|
keep_top_k = api.KernelInfoGetAttribute<int64_t>(info, "keep_top_k");
|
||||||
nms_eta = ort_.KernelInfoGetAttribute<float>(info, "nms_eta");
|
nms_eta = api.KernelInfoGetAttribute<float>(info, "nms_eta");
|
||||||
nms_threshold = ort_.KernelInfoGetAttribute<float>(info, "nms_threshold");
|
nms_threshold = api.KernelInfoGetAttribute<float>(info, "nms_threshold");
|
||||||
nms_top_k = ort_.KernelInfoGetAttribute<int64_t>(info, "nms_top_k");
|
nms_top_k = api.KernelInfoGetAttribute<int64_t>(info, "nms_top_k");
|
||||||
normalized = ort_.KernelInfoGetAttribute<int64_t>(info, "normalized");
|
normalized = api.KernelInfoGetAttribute<int64_t>(info, "normalized");
|
||||||
score_threshold = ort_.KernelInfoGetAttribute<float>(info, "score_threshold");
|
score_threshold = api.KernelInfoGetAttribute<float>(info, "score_threshold");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
} // namespace fastdeploy
|
} // namespace fastdeploy
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -30,11 +30,10 @@ struct MultiClassNmsKernel {
|
|||||||
int64_t nms_top_k;
|
int64_t nms_top_k;
|
||||||
bool normalized;
|
bool normalized;
|
||||||
float score_threshold;
|
float score_threshold;
|
||||||
Ort::CustomOpApi ort_;
|
OrtApi ort_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MultiClassNmsKernel(Ort::CustomOpApi ort, const OrtKernelInfo* info)
|
MultiClassNmsKernel(OrtApi ort, const OrtKernelInfo* info) : ort_(ort) {
|
||||||
: ort_(ort) {
|
|
||||||
GetAttribute(info);
|
GetAttribute(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +49,7 @@ struct MultiClassNmsKernel {
|
|||||||
|
|
||||||
struct MultiClassNmsOp
|
struct MultiClassNmsOp
|
||||||
: Ort::CustomOpBase<MultiClassNmsOp, MultiClassNmsKernel> {
|
: 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);
|
return new MultiClassNmsKernel(api, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user