ppinference_engine.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "model_deploy/engine/include/ppinference_engine.h"
  15. namespace PaddleDeploy {
  16. bool Model::PaddleEngineInit(const PaddleEngineConfig& engine_config) {
  17. infer_engine_ = std::make_shared<PaddleInferenceEngine>();
  18. InferenceConfig config("paddle");
  19. *(config.paddle_config) = engine_config;
  20. return infer_engine_->Init(config);
  21. }
  22. bool PaddleInferenceEngine::Init(const InferenceConfig& infer_config) {
  23. const PaddleEngineConfig& engine_config = *(infer_config.paddle_config);
  24. paddle_infer::Config config;
  25. if ("" == engine_config.key) {
  26. config.SetModel(engine_config.model_filename,
  27. engine_config.params_filename);
  28. } else {
  29. #ifdef PADDLEX_DEPLOY_ENCRYPTION
  30. std::string model = decrypt_file(engine_config.model_filename.c_str(),
  31. engine_config.key.c_str());
  32. std::string params = decrypt_file(engine_config.params_filename.c_str(),
  33. engine_config.key.c_str());
  34. config.SetModelBuffer(model.c_str(),
  35. model.size(),
  36. params.c_str(),
  37. params.size());
  38. #else
  39. std::cerr << "Don't open with_encryption on compile" << std::endl;
  40. return false;
  41. #endif // PADDLEX_DEPLOY_ENCRYPTION
  42. }
  43. if (engine_config.use_mkl && !engine_config.use_gpu) {
  44. config.EnableMKLDNN();
  45. config.SetCpuMathLibraryNumThreads(engine_config.mkl_thread_num);
  46. config.SetMkldnnCacheCapacity(10);
  47. }
  48. if (engine_config.use_gpu) {
  49. config.EnableUseGpu(100, engine_config.gpu_id);
  50. } else {
  51. config.DisableGpu();
  52. }
  53. config.SwitchUseFeedFetchOps(false);
  54. config.SwitchSpecifyInputNames(true);
  55. config.SwitchIrOptim(engine_config.use_ir_optim);
  56. config.EnableMemoryOptim();
  57. if (engine_config.use_trt && engine_config.use_gpu) {
  58. paddle_infer::PrecisionType precision;
  59. if (engine_config.precision == 0) {
  60. precision = paddle_infer::PrecisionType::kFloat32;
  61. } else if (engine_config.precision == 1) {
  62. precision = paddle_infer::PrecisionType::kHalf;
  63. } else if (engine_config.precision == 2) {
  64. precision = paddle_infer::PrecisionType::kInt8;
  65. } else {
  66. std::cerr << "Can not support the set precision" << std::endl;
  67. return false;
  68. }
  69. config.EnableTensorRtEngine(
  70. engine_config.max_workspace_size /* workspace_size*/,
  71. engine_config.max_batch_size /* max_batch_size*/,
  72. engine_config.min_subgraph_size /* min_subgraph_size*/,
  73. precision /* precision*/,
  74. engine_config.use_static /* use_static*/,
  75. engine_config.use_calib_mode /* use_calib_mode*/);
  76. if (engine_config.min_input_shape.size() != 0) {
  77. config.SetTRTDynamicShapeInfo(engine_config.min_input_shape,
  78. engine_config.max_input_shape,
  79. engine_config.optim_input_shape);
  80. }
  81. }
  82. predictor_ = std::move(paddle_infer::CreatePredictor(config));
  83. return true;
  84. }
  85. bool PaddleInferenceEngine::Infer(const std::vector<DataBlob>& inputs,
  86. std::vector<DataBlob>* outputs) {
  87. if (inputs.size() == 0) {
  88. std::cerr << "empty input image on PaddleInferenceEngine" << std::endl;
  89. return true;
  90. }
  91. auto input_names = predictor_->GetInputNames();
  92. for (int i = 0; i < inputs.size(); i++) {
  93. auto in_tensor = predictor_->GetInputHandle(input_names[i]);
  94. in_tensor->Reshape(inputs[i].shape);
  95. if (inputs[i].dtype == FLOAT32) {
  96. float* im_tensor_data;
  97. im_tensor_data = (float*)(inputs[i].data.data()); // NOLINT
  98. in_tensor->CopyFromCpu(im_tensor_data);
  99. } else if (inputs[i].dtype == INT64) {
  100. int64_t* im_tensor_data;
  101. im_tensor_data = (int64_t*)(inputs[i].data.data()); // NOLINT
  102. in_tensor->CopyFromCpu(im_tensor_data);
  103. } else if (inputs[i].dtype == INT32) {
  104. int* im_tensor_data;
  105. im_tensor_data = (int*)(inputs[i].data.data()); // NOLINT
  106. in_tensor->CopyFromCpu(im_tensor_data);
  107. } else if (inputs[i].dtype == INT8) {
  108. uint8_t* im_tensor_data;
  109. im_tensor_data = (uint8_t*)(inputs[i].data.data()); // NOLINT
  110. in_tensor->CopyFromCpu(im_tensor_data);
  111. } else {
  112. std::cerr << "There's unexpected input dtype: " << inputs[i].dtype
  113. << std::endl;
  114. return false;
  115. }
  116. }
  117. // predict
  118. predictor_->Run();
  119. // get output
  120. auto output_names = predictor_->GetOutputNames();
  121. for (const auto output_name : output_names) {
  122. auto output_tensor = predictor_->GetOutputHandle(output_name);
  123. auto output_tensor_shape = output_tensor->shape();
  124. DataBlob output;
  125. output.name = output_name;
  126. output.shape.assign(output_tensor_shape.begin(), output_tensor_shape.end());
  127. output.dtype = paddle_infer::DataType(output_tensor->type());
  128. output.lod = output_tensor->lod();
  129. int size = 1;
  130. for (const auto &i : output_tensor_shape) {
  131. size *= i;
  132. }
  133. if (output.dtype == 0) {
  134. output.data.resize(size * sizeof(float));
  135. output_tensor->CopyToCpu(reinterpret_cast<float *>(output.data.data()));
  136. } else if (output.dtype == 1) {
  137. output.data.resize(size * sizeof(int64_t));
  138. output_tensor->CopyToCpu(reinterpret_cast<int64_t *>(output.data.data()));
  139. } else if (output.dtype == 2) {
  140. output.data.resize(size * sizeof(int));
  141. output_tensor->CopyToCpu(reinterpret_cast<int *>(output.data.data()));
  142. } else if (output.dtype == 3) {
  143. output.data.resize(size * sizeof(uint8_t));
  144. output_tensor->CopyToCpu(reinterpret_cast<uint8_t *>(output.data.data()));
  145. }
  146. outputs->push_back(std::move(output));
  147. }
  148. return true;
  149. }
  150. } // namespace PaddleDeploy