From 7191d2da20bfc632b3f2ddbdfd8c8774d7b83d91 Mon Sep 17 00:00:00 2001 From: Horror Proton <107091537+horror-proton@users.noreply.github.com> Date: Thu, 15 Jun 2023 21:35:58 +0800 Subject: [PATCH] [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> --- .../backends/ort/ops/adaptive_pool2d.cc | 60 +++++++----- .../backends/ort/ops/adaptive_pool2d.h | 6 +- .../backends/ort/ops/multiclass_nms.cc | 96 ++++++++++++------- .../runtime/backends/ort/ops/multiclass_nms.h | 7 +- 4 files changed, 102 insertions(+), 67 deletions(-) diff --git a/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.cc b/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.cc index 47bd6e39d..39c23657d 100644 --- a/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.cc +++ b/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.cc @@ -17,13 +17,6 @@ #include "adaptive_pool2d.h" namespace fastdeploy { -struct OrtTensorDimensions : std::vector { - OrtTensorDimensions(Ort::CustomOpApi ort, const OrtValue* value) { - OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value); - std::vector::operator=(ort.GetTensorShape(info)); - ort.ReleaseTensorTypeAndShapeInfo(info); - } -}; void AdaptivePool2dKernel::CpuAdaptivePool( const std::vector& 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 input{ + const_cast(api.KernelContext_GetInput(context, 0))}; +#endif + auto input_data = input.GetTensorData(); + auto input_dim = input.GetTensorTypeAndShapeInfo().GetShape(); - const float* input_data = - reinterpret_cast(ort_.GetTensorData(input)); - - OrtTensorDimensions input_dim(ort_, input); output_size_[0] = input_dim[0]; std::vector 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(output); +#if ORT_API_VERSION >= 14 + auto output = ort_context.GetOutput(0, output_size_); +#else + Ort::Unowned output{api.KernelContext_GetOutput( + context, 0, output_size_.data(), output_size_.size())}; +#endif + float* output_data = output.GetTensorMutableData(); 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,14 +106,20 @@ void AdaptivePool2dKernel::Compute(OrtKernelContext* context) { } void AdaptivePool2dKernel::GetAttribute(const OrtKernelInfo* info) { - pooling_type_ = - ort_.KernelInfoGetAttribute(info, "pooling_type"); +#if ORT_API_VERSION >= 14 + Ort::ConstKernelInfo ort_info{info}; + pooling_type_ = ort_info.GetAttribute("pooling_type"); + output_size_ = ort_info.GetAttributes("output_size"); +#else + Ort::CustomOpApi api{ort_}; + pooling_type_ = api.KernelInfoGetAttribute(info, "pooling_type"); output_size_ = - ort_.KernelInfoGetAttribute>(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>(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 -#endif \ No newline at end of file +#endif diff --git a/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.h b/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.h index e800c2a00..2d68dae10 100755 --- a/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.h +++ b/fastdeploy/runtime/backends/ort/ops/adaptive_pool2d.h @@ -33,12 +33,12 @@ struct AdaptivePool2dKernel { protected: std::string pooling_type_ = "avg"; std::vector 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 { 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_); } diff --git a/fastdeploy/runtime/backends/ort/ops/multiclass_nms.cc b/fastdeploy/runtime/backends/ort/ops/multiclass_nms.cc index 8b8c0d4ff..6577f5a69 100644 --- a/fastdeploy/runtime/backends/ort/ops/multiclass_nms.cc +++ b/fastdeploy/runtime/backends/ort/ops/multiclass_nms.cc @@ -23,14 +23,6 @@ namespace fastdeploy { -struct OrtTensorDimensions : std::vector { - OrtTensorDimensions(Ort::CustomOpApi ort, const OrtValue* value) { - OrtTensorTypeAndShapeInfo* info = ort.GetTensorTypeAndShape(value); - std::vector::operator=(ort.GetTensorShape(info)); - ort.ReleaseTensorTypeAndShapeInfo(info); - } -}; - template bool SortScorePairDescend(const std::pair& pair1, const std::pair& 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(ort_.GetTensorData(boxes)); - const float* scores_data = - reinterpret_cast(ort_.GetTensorData(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 boxes{ + const_cast(api.KernelContext_GetInput(context, 0))}; + Ort::Unowned scores{ + const_cast(api.KernelContext_GetInput(context, 1))}; +#endif + + auto boxes_data = boxes.GetTensorData(); + auto scores_data = scores.GetTensorData(); + + 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 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(out_num_rois); +#if ORT_API_VERSION >= 14 + auto out_num_rois = ort_context.GetOutput(2, out_num_rois_dims); +#else + Ort::Unowned 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(); std::vector>> all_indices; for (size_t i = 0; i < batch_size; ++i) { @@ -205,20 +211,26 @@ void MultiClassNmsKernel::Compute(OrtKernelContext* context) { } std::vector out_box_dims = {num_nmsed_out, 6}; std::vector 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 out_box{api.KernelContext_GetOutput( + context, 0, out_box_dims.data(), out_box_dims.size())}; + Ort::Unowned 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(out_num_rois); + int32_t* out_num_rois_data = out_num_rois.GetTensorMutableData(); for (size_t i = 0; i < batch_size; ++i) { out_num_rois_data[i] = 0; } return; } - float* out_box_data = ort_.GetTensorMutableData(out_box); - int32_t* out_index_data = ort_.GetTensorMutableData(out_index); + float* out_box_data = out_box.GetTensorMutableData(); + int32_t* out_index_data = out_index.GetTensorMutableData(); int count = 0; for (size_t i = 0; i < batch_size; ++i) { @@ -249,15 +261,27 @@ 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("background_label"); + keep_top_k = ort_info.GetAttribute("keep_top_k"); + nms_eta = ort_info.GetAttribute("nms_eta"); + nms_threshold = ort_info.GetAttribute("nms_threshold"); + nms_top_k = ort_info.GetAttribute("nms_top_k"); + normalized = ort_info.GetAttribute("normalized"); + score_threshold = ort_info.GetAttribute("score_threshold"); +#else + Ort::CustomOpApi api{ort_}; background_label = - ort_.KernelInfoGetAttribute(info, "background_label"); - keep_top_k = ort_.KernelInfoGetAttribute(info, "keep_top_k"); - nms_eta = ort_.KernelInfoGetAttribute(info, "nms_eta"); - nms_threshold = ort_.KernelInfoGetAttribute(info, "nms_threshold"); - nms_top_k = ort_.KernelInfoGetAttribute(info, "nms_top_k"); - normalized = ort_.KernelInfoGetAttribute(info, "normalized"); - score_threshold = ort_.KernelInfoGetAttribute(info, "score_threshold"); + api.KernelInfoGetAttribute(info, "background_label"); + keep_top_k = api.KernelInfoGetAttribute(info, "keep_top_k"); + nms_eta = api.KernelInfoGetAttribute(info, "nms_eta"); + nms_threshold = api.KernelInfoGetAttribute(info, "nms_threshold"); + nms_top_k = api.KernelInfoGetAttribute(info, "nms_top_k"); + normalized = api.KernelInfoGetAttribute(info, "normalized"); + score_threshold = api.KernelInfoGetAttribute(info, "score_threshold"); +#endif } } // namespace fastdeploy -#endif \ No newline at end of file +#endif diff --git a/fastdeploy/runtime/backends/ort/ops/multiclass_nms.h b/fastdeploy/runtime/backends/ort/ops/multiclass_nms.h index 747b92577..0a23fa8b8 100644 --- a/fastdeploy/runtime/backends/ort/ops/multiclass_nms.h +++ b/fastdeploy/runtime/backends/ort/ops/multiclass_nms.h @@ -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 { - void* CreateKernel(Ort::CustomOpApi api, const OrtKernelInfo* info) const { + void* CreateKernel(OrtApi api, const OrtKernelInfo* info) const { return new MultiClassNmsKernel(api, info); }