mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-12 20:11:20 +08:00
[Other] Optimize runtime module (#1211)
* modify ort option * update code * Unify backend
This commit is contained in:
@@ -341,53 +341,9 @@ void Runtime::CreateOpenVINOBackend() {
|
||||
}
|
||||
|
||||
void Runtime::CreateOrtBackend() {
|
||||
FDASSERT(option.device == Device::CPU || option.device == Device::GPU,
|
||||
"Backend::ORT only supports Device::CPU/Device::GPU.");
|
||||
FDASSERT(option.model_format == ModelFormat::PADDLE ||
|
||||
option.model_format == ModelFormat::ONNX,
|
||||
"OrtBackend only support model format of ModelFormat::PADDLE / "
|
||||
"ModelFormat::ONNX.");
|
||||
#ifdef ENABLE_ORT_BACKEND
|
||||
auto ort_option = OrtBackendOption();
|
||||
ort_option.graph_optimization_level = option.ort_graph_opt_level;
|
||||
ort_option.intra_op_num_threads = option.cpu_thread_num;
|
||||
ort_option.inter_op_num_threads = option.ort_inter_op_num_threads;
|
||||
ort_option.execution_mode = option.ort_execution_mode;
|
||||
ort_option.use_gpu = (option.device == Device::GPU) ? true : false;
|
||||
ort_option.gpu_id = option.device_id;
|
||||
ort_option.external_stream_ = option.external_stream_;
|
||||
backend_ = utils::make_unique<OrtBackend>();
|
||||
auto casted_backend = dynamic_cast<OrtBackend*>(backend_.get());
|
||||
if (option.model_format == ModelFormat::ONNX) {
|
||||
if (option.model_from_memory_) {
|
||||
FDASSERT(casted_backend->InitFromOnnx(option.model_file, ort_option),
|
||||
"Load model from ONNX failed while initliazing OrtBackend.");
|
||||
ReleaseModelMemoryBuffer();
|
||||
} else {
|
||||
std::string model_buffer = "";
|
||||
FDASSERT(ReadBinaryFromFile(option.model_file, &model_buffer),
|
||||
"Fail to read binary from model file");
|
||||
FDASSERT(casted_backend->InitFromOnnx(model_buffer, ort_option),
|
||||
"Load model from ONNX failed while initliazing OrtBackend.");
|
||||
}
|
||||
} else {
|
||||
if (option.model_from_memory_) {
|
||||
FDASSERT(casted_backend->InitFromPaddle(option.model_file,
|
||||
option.params_file, ort_option),
|
||||
"Load model from Paddle failed while initliazing OrtBackend.");
|
||||
ReleaseModelMemoryBuffer();
|
||||
} else {
|
||||
std::string model_buffer = "";
|
||||
std::string params_buffer = "";
|
||||
FDASSERT(ReadBinaryFromFile(option.model_file, &model_buffer),
|
||||
"Fail to read binary from model file");
|
||||
FDASSERT(ReadBinaryFromFile(option.params_file, ¶ms_buffer),
|
||||
"Fail to read binary from parameter file");
|
||||
FDASSERT(casted_backend->InitFromPaddle(model_buffer, params_buffer,
|
||||
ort_option),
|
||||
"Load model from Paddle failed while initliazing OrtBackend.");
|
||||
}
|
||||
}
|
||||
FDASSERT(backend_->Init(option), "Failed to initialize Backend::ORT.");
|
||||
#else
|
||||
FDASSERT(false,
|
||||
"OrtBackend is not available, please compiled with "
|
||||
|
Reference in New Issue
Block a user