mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-19 15:04:47 +08:00
[BUG FIX] fix memory leak for ort backend
This commit is contained in:
@@ -31,10 +31,10 @@ namespace fastdeploy {
|
|||||||
std::vector<OrtCustomOp*> OrtBackend::custom_operators_ =
|
std::vector<OrtCustomOp*> OrtBackend::custom_operators_ =
|
||||||
std::vector<OrtCustomOp*>();
|
std::vector<OrtCustomOp*>();
|
||||||
|
|
||||||
std::wstring ToWstring(const std::string &str) {
|
std::wstring ToWstring(const std::string& str) {
|
||||||
unsigned len = str.size() * 2;
|
unsigned len = str.size() * 2;
|
||||||
setlocale(LC_CTYPE, "");
|
setlocale(LC_CTYPE, "");
|
||||||
wchar_t *p = new wchar_t[len];
|
wchar_t* p = new wchar_t[len];
|
||||||
mbstowcs(p, str.c_str(), len);
|
mbstowcs(p, str.c_str(), len);
|
||||||
std::wstring wstr(p);
|
std::wstring wstr(p);
|
||||||
delete[] p;
|
delete[] p;
|
||||||
@@ -59,9 +59,10 @@ bool OrtBackend::BuildOption(const OrtBackendOption& option) {
|
|||||||
if (!option.optimized_model_filepath.empty()) {
|
if (!option.optimized_model_filepath.empty()) {
|
||||||
#if (defined(_WIN32) || defined(_WIN64))
|
#if (defined(_WIN32) || defined(_WIN64))
|
||||||
session_options_.SetOptimizedModelFilePath(
|
session_options_.SetOptimizedModelFilePath(
|
||||||
ToWstring(option.optimized_model_filepath).c_str());
|
ToWstring(option.optimized_model_filepath).c_str());
|
||||||
#else
|
#else
|
||||||
session_options_.SetOptimizedModelFilePath(option.optimized_model_filepath.c_str());
|
session_options_.SetOptimizedModelFilePath(
|
||||||
|
option.optimized_model_filepath.c_str());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,12 +208,17 @@ bool OrtBackend::InitFromPaddle(const std::string& model_buffer,
|
|||||||
std::strcpy(charStr, one_type.c_str());
|
std::strcpy(charStr, one_type.c_str());
|
||||||
disable_fp16_ops.push_back(charStr);
|
disable_fp16_ops.push_back(charStr);
|
||||||
}
|
}
|
||||||
if (!paddle2onnx::Export(
|
bool is_exported = paddle2onnx::Export(
|
||||||
model_buffer.c_str(), model_buffer.size(), params_buffer.c_str(),
|
model_buffer.c_str(), model_buffer.size(), params_buffer.c_str(),
|
||||||
params_buffer.size(), &model_content_ptr, &model_content_size, 11,
|
params_buffer.size(), &model_content_ptr, &model_content_size, 11, true,
|
||||||
true, verbose, true, true, true, ops.data(), 2, "onnxruntime",
|
verbose, true, true, true, ops.data(), 2, "onnxruntime", nullptr, 0, "",
|
||||||
nullptr, 0, "", &save_external, option.enable_fp16,
|
&save_external, option.enable_fp16, disable_fp16_ops.data(),
|
||||||
disable_fp16_ops.data(), option.ort_disabled_ops_.size())) {
|
option.ort_disabled_ops_.size());
|
||||||
|
for (auto& disable_fp16_op : disable_fp16_ops) {
|
||||||
|
delete[] disable_fp16_op;
|
||||||
|
}
|
||||||
|
disable_fp16_ops.clear();
|
||||||
|
if (!is_exported) {
|
||||||
FDERROR << "Error occured while export PaddlePaddle to ONNX format."
|
FDERROR << "Error occured while export PaddlePaddle to ONNX format."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return false;
|
return false;
|
||||||
@@ -258,6 +264,8 @@ bool OrtBackend::InitFromOnnx(const std::string& model_file,
|
|||||||
&model_content_ptr, &model_content_size);
|
&model_content_ptr, &model_content_size);
|
||||||
std::string onnx_model_proto(model_content_ptr,
|
std::string onnx_model_proto(model_content_ptr,
|
||||||
model_content_ptr + model_content_size);
|
model_content_ptr + model_content_size);
|
||||||
|
delete[] model_content_ptr;
|
||||||
|
model_content_ptr = nullptr;
|
||||||
onnx_model_buffer = onnx_model_proto;
|
onnx_model_buffer = onnx_model_proto;
|
||||||
} else {
|
} else {
|
||||||
onnx_model_buffer = model_file;
|
onnx_model_buffer = model_file;
|
||||||
|
Reference in New Issue
Block a user