From 457fe72ce8380b4a8a167b72535b1d4218bc0e47 Mon Sep 17 00:00:00 2001 From: Wilber Date: Wed, 5 Jan 2022 18:39:58 +0800 Subject: [PATCH] inference c_api support std::string (#38667) (#38710) --- paddle/fluid/inference/capi_exp/pd_config.cc | 6 ++--- paddle/fluid/inference/capi_exp/pd_config.h | 2 +- paddle/fluid/inference/capi_exp/pd_types.h | 5 ++++ paddle/fluid/inference/capi_exp/pd_utils.cc | 23 +++++++++++++++++++ paddle/fluid/inference/capi_exp/pd_utils.h | 9 ++++++++ .../fluid/inference/capi_exp/utils_internal.h | 8 +++++++ paddle/fluid/inference/goapi/config.go | 4 ++-- .../tests/api/analyzer_capi_exp_tester.cc | 5 ++++ 8 files changed, 55 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/inference/capi_exp/pd_config.cc b/paddle/fluid/inference/capi_exp/pd_config.cc index b1ad2f6c87c..e342190fda1 100644 --- a/paddle/fluid/inference/capi_exp/pd_config.cc +++ b/paddle/fluid/inference/capi_exp/pd_config.cc @@ -459,12 +459,10 @@ __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses( std::vector passes = config->pass_builder()->AllPasses(); return paddle_infer::CvtVecToOneDimArrayCstr(passes); } -const char* PD_ConfigSummary(__pd_keep PD_Config* pd_config) { +__pd_give PD_Cstr* PD_ConfigSummary(__pd_keep PD_Config* pd_config) { CHECK_AND_CONVERT_PD_CONFIG; auto sum_str = config->Summary(); - char* c = reinterpret_cast(malloc(sum_str.length() + 1)); - snprintf(c, sum_str.length() + 1, "%s", sum_str.c_str()); - return c; + return paddle_infer::CvtStrToCstr(sum_str); } } // extern "C" diff --git a/paddle/fluid/inference/capi_exp/pd_config.h b/paddle/fluid/inference/capi_exp/pd_config.h index e8ab9357dc9..c314aca918f 100644 --- a/paddle/fluid/inference/capi_exp/pd_config.h +++ b/paddle/fluid/inference/capi_exp/pd_config.h @@ -705,7 +705,7 @@ PADDLE_CAPI_EXPORT extern __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses( /// /// \return Return config info. /// -PADDLE_CAPI_EXPORT extern const char* PD_ConfigSummary( +PADDLE_CAPI_EXPORT extern __pd_give PD_Cstr* PD_ConfigSummary( __pd_keep PD_Config* pd_config); #ifdef __cplusplus diff --git a/paddle/fluid/inference/capi_exp/pd_types.h b/paddle/fluid/inference/capi_exp/pd_types.h index a5da2913a9b..62c54616535 100644 --- a/paddle/fluid/inference/capi_exp/pd_types.h +++ b/paddle/fluid/inference/capi_exp/pd_types.h @@ -34,6 +34,11 @@ typedef struct PD_OneDimArrayCstr { char** data; } PD_OneDimArrayCstr; // std::vector +typedef struct PD_Cstr { + size_t size; + char* data; +} PD_Cstr; // std::string + typedef struct PD_TwoDimArraySize { size_t size; PD_OneDimArraySize** data; diff --git a/paddle/fluid/inference/capi_exp/pd_utils.cc b/paddle/fluid/inference/capi_exp/pd_utils.cc index 94362b8784b..efca350fbaf 100644 --- a/paddle/fluid/inference/capi_exp/pd_utils.cc +++ b/paddle/fluid/inference/capi_exp/pd_utils.cc @@ -78,6 +78,17 @@ void PD_OneDimArrayCstrDestroy(__pd_take PD_OneDimArrayCstr* array) { delete array; } } + +void PD_CstrDestroy(__pd_take PD_Cstr* cstr) { + if (cstr != NULL) { + if (cstr->size != 0) { + cstr->size = 0; + delete[] cstr->data; + cstr->data = NULL; + } + delete cstr; + } +} namespace paddle_infer { __pd_give PD_OneDimArrayCstr* CvtVecToOneDimArrayCstr( @@ -101,6 +112,18 @@ std::vector CvtOneDimArrayToVecCstr( return vec; } +__pd_give PD_Cstr* CvtStrToCstr(const std::string& str) { + PD_Cstr* cstr = new PD_Cstr; + if (str.empty()) { + cstr->size = 0; + cstr->data = NULL; + } else { + cstr->size = str.length() + 1; + cstr->data = new char[str.length() + 1]; + memcpy(cstr->data, str.c_str(), str.length() + 1); + } + return cstr; +} } // namespace paddle_infer #define DESTROY_TWO_DIM_ARRAY(type) \ diff --git a/paddle/fluid/inference/capi_exp/pd_utils.h b/paddle/fluid/inference/capi_exp/pd_utils.h index 68e519d4bb5..8743c58db76 100644 --- a/paddle/fluid/inference/capi_exp/pd_utils.h +++ b/paddle/fluid/inference/capi_exp/pd_utils.h @@ -65,6 +65,15 @@ PADDLE_CAPI_EXPORT extern void PD_OneDimArraySizeDestroy( PADDLE_CAPI_EXPORT extern void PD_TwoDimArraySizeDestroy( __pd_take PD_TwoDimArraySize* array); +/// +/// \brief Destroy the PD_Cstr object pointed to by the pointer. +/// NOTE: if input string is empty, the return PD_Cstr's size is +/// 0 and data is NULL. +/// +/// \param[in] cstr pointer to the PD_Cstr object. +/// +PADDLE_CAPI_EXPORT extern void PD_CstrDestroy(__pd_take PD_Cstr* cstr); + #ifdef __cplusplus } // extern "C" #endif diff --git a/paddle/fluid/inference/capi_exp/utils_internal.h b/paddle/fluid/inference/capi_exp/utils_internal.h index fbae512ecd8..95b16dbd599 100644 --- a/paddle/fluid/inference/capi_exp/utils_internal.h +++ b/paddle/fluid/inference/capi_exp/utils_internal.h @@ -114,6 +114,14 @@ __pd_give PD_TwoDimArraySize* CvtVecToTwoDimArraySize( std::vector> CvtTwoDimArrayToVecSize( __pd_keep const PD_TwoDimArraySize* array); +/// +/// \brief Convert the 'std::string' object to a 'PD_Cstr' object. +/// +/// \param[in] vec source object. +/// \return target object. +/// +__pd_give PD_Cstr* CvtStrToCstr(const std::string& vec); + /// /// \brief Convert the 'PD_PlaceType' object to a 'paddle_infer::PlaceType' /// object. diff --git a/paddle/fluid/inference/goapi/config.go b/paddle/fluid/inference/goapi/config.go index a95bb6bef6e..def26913b0a 100644 --- a/paddle/fluid/inference/goapi/config.go +++ b/paddle/fluid/inference/goapi/config.go @@ -833,7 +833,7 @@ func (config *Config) AllPasses() []string { /// func (config *Config) Summary() string { cSummary := C.PD_ConfigSummary(config.c) - summary := C.GoString(cSummary) - C.free(unsafe.Pointer(cSummary)) + summary := C.GoString(cSummary.data) + C.PD_CstrDestroy(cSummary) return summary } diff --git a/paddle/fluid/inference/tests/api/analyzer_capi_exp_tester.cc b/paddle/fluid/inference/tests/api/analyzer_capi_exp_tester.cc index 11de1a5a6fa..4b2852be861 100644 --- a/paddle/fluid/inference/tests/api/analyzer_capi_exp_tester.cc +++ b/paddle/fluid/inference/tests/api/analyzer_capi_exp_tester.cc @@ -18,7 +18,9 @@ limitations under the License. */ #include #include +#include "paddle/fluid/inference/capi_exp/pd_config.h" #include "paddle/fluid/inference/capi_exp/pd_inference_api.h" +#include "paddle/fluid/inference/capi_exp/pd_utils.h" #include "paddle/fluid/inference/tests/api/tester_helper.h" namespace paddle { @@ -34,6 +36,8 @@ void predictor_run() { PD_ConfigSetCpuMathLibraryNumThreads(config, 10); PD_ConfigSwitchIrDebug(config, TRUE); PD_ConfigSetModel(config, prog_file.c_str(), params_file.c_str()); + PD_Cstr *config_summary = PD_ConfigSummary(config); + LOG(INFO) << config_summary->data; PD_Predictor *predictor = PD_PredictorCreate(config); PD_Tensor *tensor = PD_PredictorGetInputHandle(predictor, "data"); @@ -51,6 +55,7 @@ void predictor_run() { delete[] input; PD_TensorDestroy(tensor); + PD_CstrDestroy(config_summary); PD_PredictorDestroy(predictor); } -- GitLab