multi_gpu_model.h 3.7 KB

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