FastDeploy  latest
Fast & Easy to Deploy!
mat_batch.h
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 "fastdeploy/vision/common/processors/mat.h"
16 
17 #ifdef WITH_GPU
18 #include <cuda_runtime_api.h>
19 #endif
20 
21 namespace fastdeploy {
22 namespace vision {
23 
24 enum FDMatBatchLayout { NHWC, NCHW };
25 
28 struct FASTDEPLOY_DECL FDMatBatch {
29  FDMatBatch() = default;
30 
31  // MatBatch is intialized with a list of mats,
32  // the data is stored in the mats separately.
33  // Call Tensor() function to get a batched 4-dimension tensor.
34  explicit FDMatBatch(std::vector<FDMat>* _mats) {
35  mats = _mats;
36  layout = FDMatBatchLayout::NHWC;
37  mat_type = ProcLib::OPENCV;
38  }
39 
40  // Get the batched 4-dimension tensor.
41  FDTensor* Tensor();
42 
43  void SetTensor(FDTensor* tensor);
44 
45  private:
46 #ifdef WITH_GPU
47  cudaStream_t stream = nullptr;
48 #endif
49  std::shared_ptr<FDTensor> fd_tensor = std::make_shared<FDTensor>();
50 
51  public:
52  // When using CV-CUDA/CUDA, please set input/output cache,
53  // refer to manager.cc
54  FDTensor* input_cache;
55  FDTensor* output_cache;
56 #ifdef WITH_GPU
57  cudaStream_t Stream() const { return stream; }
58  void SetStream(cudaStream_t s);
59 #endif
60 
61  std::vector<FDMat>* mats = nullptr;
62 
63  // Used by pybind, since python cannot pass list as pointer or reference
64  std::vector<FDMat> mats_holder;
65 
66  ProcLib mat_type = ProcLib::OPENCV;
67  FDMatBatchLayout layout = FDMatBatchLayout::NHWC;
68  Device device = Device::CPU;
69  ProcLib proc_lib = ProcLib::DEFAULT;
70 
71  // False: the data is stored in the mats separately
72  // True: the data is stored in the fd_tensor continuously in 4 dimensions
73  bool has_batched_tensor = false;
74 };
75 
76 // Create a batched input tensor on GPU and save into input_cache.
77 // If the MatBatch is on GPU, return the Tensor() directly.
78 // If the MatBatch is on CPU, then copy the CPU tensors to GPU and get a GPU
79 // batched input tensor.
80 FDTensor* CreateCachedGpuInputTensor(FDMatBatch* mat_batch);
81 
82 } // namespace vision
83 } // namespace fastdeploy
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
FDMatBatch contains batch data for preprocess.
Definition: mat_batch.h:28
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16