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

inference c_api support std::string (#38667) (#38710)

上级 dd339f4e
......@@ -459,12 +459,10 @@ __pd_give PD_OneDimArrayCstr* PD_ConfigAllPasses(
std::vector<std::string> 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<char*>(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"
......@@ -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
......
......@@ -34,6 +34,11 @@ typedef struct PD_OneDimArrayCstr {
char** data;
} PD_OneDimArrayCstr; // std::vector<std::string>
typedef struct PD_Cstr {
size_t size;
char* data;
} PD_Cstr; // std::string
typedef struct PD_TwoDimArraySize {
size_t size;
PD_OneDimArraySize** data;
......
......@@ -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<std::string> 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) \
......
......@@ -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
......@@ -114,6 +114,14 @@ __pd_give PD_TwoDimArraySize* CvtVecToTwoDimArraySize(
std::vector<std::vector<size_t>> 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.
......
......@@ -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
}
......@@ -18,7 +18,9 @@ limitations under the License. */
#include <string>
#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_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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册