mat_batch.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2022 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 "ultra_infer/vision/common/processors/mat.h"
  16. #ifdef WITH_GPU
  17. #include <cuda_runtime_api.h>
  18. #endif
  19. namespace ultra_infer {
  20. namespace vision {
  21. enum FDMatBatchLayout { NHWC, NCHW };
  22. /*! @brief FDMatBatch contains batch data for preprocess
  23. */
  24. struct ULTRAINFER_DECL FDMatBatch {
  25. FDMatBatch() = default;
  26. // MatBatch is initialized with a list of mats,
  27. // the data is stored in the mats separately.
  28. // Call Tensor() function to get a batched 4-dimension tensor.
  29. explicit FDMatBatch(std::vector<FDMat> *_mats) {
  30. mats = _mats;
  31. layout = FDMatBatchLayout::NHWC;
  32. mat_type = ProcLib::OPENCV;
  33. }
  34. // Get the batched 4-dimension tensor.
  35. FDTensor *Tensor();
  36. void SetTensor(FDTensor *tensor);
  37. private:
  38. #ifdef WITH_GPU
  39. cudaStream_t stream = nullptr;
  40. #endif
  41. std::shared_ptr<FDTensor> fd_tensor = std::make_shared<FDTensor>();
  42. public:
  43. // When using CV-CUDA/CUDA, please set input/output cache,
  44. // refer to manager.cc
  45. FDTensor *input_cache;
  46. FDTensor *output_cache;
  47. #ifdef WITH_GPU
  48. cudaStream_t Stream() const { return stream; }
  49. void SetStream(cudaStream_t s);
  50. #endif
  51. std::vector<FDMat> *mats = nullptr;
  52. // Used by pybind, since python cannot pass list as pointer or reference
  53. std::vector<FDMat> mats_holder;
  54. ProcLib mat_type = ProcLib::OPENCV;
  55. FDMatBatchLayout layout = FDMatBatchLayout::NHWC;
  56. Device device = Device::CPU;
  57. ProcLib proc_lib = ProcLib::DEFAULT;
  58. // False: the data is stored in the mats separately
  59. // True: the data is stored in the fd_tensor continuously in 4 dimensions
  60. bool has_batched_tensor = false;
  61. };
  62. // Create a batched input tensor on GPU and save into input_cache.
  63. // If the MatBatch is on GPU, return the Tensor() directly.
  64. // If the MatBatch is on CPU, then copy the CPU tensors to GPU and get a GPU
  65. // batched input tensor.
  66. FDTensor *CreateCachedGpuInputTensor(FDMatBatch *mat_batch);
  67. } // namespace vision
  68. } // namespace ultra_infer