multi_gpu_model.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #pragma once
  15. #include <memory>
  16. #include <string>
  17. #include <thread> // NOLINT
  18. #include <vector>
  19. #include "model_deploy/common/include/model_factory.h"
  20. #include "model_deploy/engine/include/engine.h"
  21. namespace PaddleDeploy {
  22. class MultiGPUModel {
  23. private:
  24. std::vector<std::shared_ptr<Model>> models_;
  25. public:
  26. bool Init(const std::string& model_type,
  27. const std::string& cfg_file, size_t gpu_num = 1) {
  28. models_.clear();
  29. for (auto i = 0; i < gpu_num; ++i) {
  30. std::shared_ptr<Model> model =
  31. PaddleDeploy::ModelFactory::CreateObject(model_type);
  32. if (!model) {
  33. std::cerr << "no model_type: " << model_type << std::endl;
  34. return false;
  35. }
  36. std::cerr << i + 1 << " model start init" << std::endl;
  37. if (!model->Init(cfg_file)) {
  38. std::cerr << "model Init error" << std::endl;
  39. return false;
  40. }
  41. models_.push_back(model);
  42. }
  43. return true;
  44. }
  45. bool PaddleEngineInit(PaddleEngineConfig engine_config,
  46. const std::vector<int> gpu_ids) {
  47. if (gpu_ids.size() != models_.size()) {
  48. std::cerr << "Paddle Engine Init gpu_ids != MultiGPUModel Init gpu_num"
  49. << gpu_ids.size() << " != " << models_.size()
  50. << std::endl;
  51. return false;
  52. }
  53. engine_config.use_gpu = true;
  54. for (auto i = 0; i < gpu_ids.size(); ++i) {
  55. engine_config.gpu_id = gpu_ids[i];
  56. if (!models_[i]->PaddleEngineInit(engine_config)) {
  57. std::cerr << "Paddle Engine Init error:" << gpu_ids[i] << std::endl;
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. bool Predict(const std::vector<cv::Mat>& imgs,
  64. std::vector<Result>* results,
  65. int thread_num = 1) {
  66. results->clear();
  67. int model_num = models_.size();
  68. if (model_num <= 0) {
  69. std::cerr << "Please Init before Predict!" << std::endl;
  70. return false;
  71. }
  72. int imgs_size = imgs.size();
  73. int start = 0;
  74. std::vector<std::thread> threads;
  75. std::vector<std::vector<cv::Mat>> split_imgs;
  76. std::vector<std::vector<Result>> model_results;
  77. for (int i = 0; i < model_num; ++i) {
  78. int img_num = static_cast<int>(imgs_size / model_num);
  79. if (i < imgs_size % model_num) {
  80. img_num += 1;
  81. } else if (img_num <= 0) {
  82. // imgs.size < model_.size
  83. break;
  84. }
  85. std::vector<cv::Mat> new_imgs(imgs.begin() + start,
  86. imgs.begin() + start + img_num);
  87. split_imgs.push_back(new_imgs);
  88. start += img_num;
  89. }
  90. model_results.resize(split_imgs.size());
  91. for (int i = 0; i < split_imgs.size(); ++i) {
  92. threads.push_back(std::thread(&PaddleDeploy::Model::Predict, models_[i],
  93. std::ref(split_imgs[i]),
  94. &model_results[i], thread_num));
  95. }
  96. for (auto& thread : threads) {
  97. if (thread.joinable()) {
  98. thread.join();
  99. }
  100. }
  101. // merge result
  102. for (auto model_result : model_results) {
  103. results->insert(results->end(),
  104. model_result.begin(), model_result.end());
  105. }
  106. return true;
  107. }
  108. };
  109. } // namespace PaddleDeploy