未验证 提交 f289cf85 编写于 作者: W Wilber 提交者: GitHub

inference c_api support std::string (#38667)

* c_api support std::string

* update

* update

* add NOTE

* fix delete error.
上级 1456b02d
...@@ -459,12 +459,10 @@ __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses( ...@@ -459,12 +459,10 @@ __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses(
std::vector<std::string> passes = config->pass_builder()->AllPasses(); std::vector<std::string> passes = config->pass_builder()->AllPasses();
return paddle_infer::CvtVecToOneDimArrayCstr(passes); 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; CHECK_AND_CONVERT_PD_CONFIG;
auto sum_str = config->Summary(); auto sum_str = config->Summary();
char* c = reinterpret_cast<char*>(malloc(sum_str.length() + 1)); return paddle_infer::CvtStrToCstr(sum_str);
snprintf(c, sum_str.length() + 1, "%s", sum_str.c_str());
return c;
} }
} // extern "C" } // extern "C"
...@@ -705,7 +705,7 @@ PADDLE_CAPI_EXPORT extern __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses( ...@@ -705,7 +705,7 @@ PADDLE_CAPI_EXPORT extern __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses(
/// ///
/// \return Return config info. /// \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); __pd_keep PD_Config* pd_config);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -34,6 +34,11 @@ typedef struct PD_OneDimArrayCstr { ...@@ -34,6 +34,11 @@ typedef struct PD_OneDimArrayCstr {
char** data; char** data;
} PD_OneDimArrayCstr; // std::vector<std::string> } PD_OneDimArrayCstr; // std::vector<std::string>
typedef struct PD_Cstr {
size_t size;
char* data;
} PD_Cstr; // std::string
typedef struct PD_TwoDimArraySize { typedef struct PD_TwoDimArraySize {
size_t size; size_t size;
PD_OneDimArraySize** data; PD_OneDimArraySize** data;
......
...@@ -78,6 +78,17 @@ void PD_OneDimArrayCstrDestroy(__pd_take PD_OneDimArrayCstr* array) { ...@@ -78,6 +78,17 @@ void PD_OneDimArrayCstrDestroy(__pd_take PD_OneDimArrayCstr* array) {
delete 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 { namespace paddle_infer {
__pd_give PD_OneDimArrayCstr* CvtVecToOneDimArrayCstr( __pd_give PD_OneDimArrayCstr* CvtVecToOneDimArrayCstr(
...@@ -101,6 +112,18 @@ std::vector<std::string> CvtOneDimArrayToVecCstr( ...@@ -101,6 +112,18 @@ std::vector<std::string> CvtOneDimArrayToVecCstr(
return vec; 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 } // namespace paddle_infer
#define DESTROY_TWO_DIM_ARRAY(type) \ #define DESTROY_TWO_DIM_ARRAY(type) \
......
...@@ -65,6 +65,15 @@ PADDLE_CAPI_EXPORT extern void PD_OneDimArraySizeDestroy( ...@@ -65,6 +65,15 @@ PADDLE_CAPI_EXPORT extern void PD_OneDimArraySizeDestroy(
PADDLE_CAPI_EXPORT extern void PD_TwoDimArraySizeDestroy( PADDLE_CAPI_EXPORT extern void PD_TwoDimArraySizeDestroy(
__pd_take PD_TwoDimArraySize* array); __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 #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif
...@@ -114,6 +114,14 @@ __pd_give PD_TwoDimArraySize* CvtVecToTwoDimArraySize( ...@@ -114,6 +114,14 @@ __pd_give PD_TwoDimArraySize* CvtVecToTwoDimArraySize(
std::vector<std::vector<size_t>> CvtTwoDimArrayToVecSize( std::vector<std::vector<size_t>> CvtTwoDimArrayToVecSize(
__pd_keep const PD_TwoDimArraySize* array); __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' /// \brief Convert the 'PD_PlaceType' object to a 'paddle_infer::PlaceType'
/// object. /// object.
......
...@@ -833,7 +833,7 @@ func (config *Config) AllPasses() []string { ...@@ -833,7 +833,7 @@ func (config *Config) AllPasses() []string {
/// ///
func (config *Config) Summary() string { func (config *Config) Summary() string {
cSummary := C.PD_ConfigSummary(config.c) cSummary := C.PD_ConfigSummary(config.c)
summary := C.GoString(cSummary) summary := C.GoString(cSummary.data)
C.free(unsafe.Pointer(cSummary)) C.PD_CstrDestroy(cSummary)
return summary return summary
} }
...@@ -18,7 +18,9 @@ limitations under the License. */ ...@@ -18,7 +18,9 @@ limitations under the License. */
#include <string> #include <string>
#include <vector> #include <vector>
#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_inference_api.h"
#include "paddle/fluid/inference/capi_exp/pd_utils.h"
#include "paddle/fluid/inference/tests/api/tester_helper.h" #include "paddle/fluid/inference/tests/api/tester_helper.h"
namespace paddle { namespace paddle {
...@@ -34,6 +36,8 @@ void predictor_run() { ...@@ -34,6 +36,8 @@ void predictor_run() {
PD_ConfigSetCpuMathLibraryNumThreads(config, 10); PD_ConfigSetCpuMathLibraryNumThreads(config, 10);
PD_ConfigSwitchIrDebug(config, TRUE); PD_ConfigSwitchIrDebug(config, TRUE);
PD_ConfigSetModel(config, prog_file.c_str(), params_file.c_str()); 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_Predictor *predictor = PD_PredictorCreate(config);
PD_Tensor *tensor = PD_PredictorGetInputHandle(predictor, "data"); PD_Tensor *tensor = PD_PredictorGetInputHandle(predictor, "data");
...@@ -51,6 +55,7 @@ void predictor_run() { ...@@ -51,6 +55,7 @@ void predictor_run() {
delete[] input; delete[] input;
PD_TensorDestroy(tensor); PD_TensorDestroy(tensor);
PD_CstrDestroy(config_summary);
PD_PredictorDestroy(predictor); PD_PredictorDestroy(predictor);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册