FastDeploy  latest
Fast & Easy to Deploy!
fd_tensor.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 
16 #include <iostream>
17 #include <numeric>
18 #include <string>
19 #include <vector>
20 
21 #include "fastdeploy/core/allocate.h"
22 #include "fastdeploy/core/fd_scalar.h"
23 #include "fastdeploy/core/fd_type.h"
25 
26 namespace fastdeploy {
27 
31 struct FASTDEPLOY_DECL FDTensor {
45  void SetData(const std::vector<int64_t>& tensor_shape, const FDDataType& data_type, void* data_buffer, bool copy = false, const Device& data_device = Device::CPU, int data_device_id = -1) {
46  SetExternalData(tensor_shape, data_type, data_buffer, data_device, data_device_id);
47  if (copy) {
48  StopSharing();
49  }
50  }
51 
53  void* GetData() {
54  return MutableData();
55  }
57  const void* GetData() const {
58  return Data();
59  }
60 
62  void ExpandDim(int64_t axis = 0);
63 
65  void Squeeze(int64_t axis = 0);
66 
68  bool Reshape(const std::vector<int64_t>& new_shape);
69 
71  int Nbytes() const;
72 
74  int Numel() const;
75 
77  std::vector<int64_t> Shape() const { return shape; }
78 
80  FDDataType Dtype() const { return dtype; }
81 
90  void Allocate(const FDDataType& data_type, const std::vector<int64_t>& data_shape) {
91  Allocate(data_shape, data_type, name);
92  }
93 
95  void PrintInfo(const std::string& prefix = "Debug TensorInfo: ") const;
96 
98  std::string name = "";
99 
101  bool IsShared() { return external_data_ptr != nullptr; }
103  void StopSharing();
104 
105 
106  // ******************************************************
107  // The following member and function only used by inside FastDeploy, maybe removed in next version
108 
109  void* buffer_ = nullptr;
110  std::vector<int64_t> shape = {0};
111  FDDataType dtype = FDDataType::INT8;
112 
113  // This use to skip memory copy step
114  // the external_data_ptr will point to the user allocated memory
115  // user has to maintain the memory, allocate and release
116  void* external_data_ptr = nullptr;
117  // The internal data will be on CPU
118  // Some times, the external data is on the GPU, and we are going to use
119  // GPU to inference the model
120  // so we can skip data transfer, which may improve the efficience
121  Device device = Device::CPU;
122  // By default the device id of FDTensor is -1, which means this value is
123  // invalid, and FDTensor is using the same device id as Runtime.
124  int device_id = -1;
125 
126  // Whether the data buffer is in pinned memory, which is allocated
127  // with cudaMallocHost()
128  bool is_pinned_memory = false;
129 
130  // if the external data is not on CPU, we use this temporary buffer
131  // to transfer data to CPU at some cases we need to visit the
132  // other devices' data
133  std::vector<int8_t> temporary_cpu_buffer;
134 
135  // The number of bytes allocated so far.
136  // When resizing GPU memory, we will free and realloc the memory only if the
137  // required size is larger than this value.
138  size_t nbytes_allocated = 0;
139 
140  // Get data buffer pointer
141  void* MutableData();
142 
143  void* Data();
144 
145  const void* Data() const;
146 
147  // Use this data to get the tensor data to process
148  // Since the most senario is process data in CPU
149  // this function will return a pointer to cpu memory
150  // buffer.
151  // If the original data is on other device, the data
152  // will copy to cpu store in `temporary_cpu_buffer`
153  const void* CpuData() const;
154 
155  // void SetDataBuffer(const std::vector<int64_t>& new_shape, const FDDataType& data_type, void* data_buffer, bool copy = false, const Device& new_device = Device::CPU, int new_device_id = -1);
156  // Set user memory buffer for Tensor, the memory is managed by
157  // the user it self, but the Tensor will share the memory with user
158  // So take care with the user buffer
159  void SetExternalData(const std::vector<int64_t>& new_shape,
160  const FDDataType& data_type, void* data_buffer,
161  const Device& new_device = Device::CPU,
162  int new_device_id = -1);
163  // Initialize Tensor
164  // Include setting attribute for tensor
165  // and allocate cpu memory buffer
166  void Allocate(const std::vector<int64_t>& new_shape,
167  const FDDataType& data_type,
168  const std::string& tensor_name = "",
169  const Device& new_device = Device::CPU);
170 
171  void Resize(size_t nbytes);
172 
173  void Resize(const std::vector<int64_t>& new_shape);
174 
175  void Resize(const std::vector<int64_t>& new_shape,
176  const FDDataType& data_type, const std::string& tensor_name = "",
177  const Device& new_device = Device::CPU);
178 
179  bool ReallocFn(size_t nbytes);
180 
181  void FreeFn();
182 
183  FDTensor() {}
184  explicit FDTensor(const std::string& tensor_name);
185  explicit FDTensor(const char* tensor_name);
186 
187  // Deep copy
188  FDTensor(const FDTensor& other);
189  // Move constructor
190  FDTensor(FDTensor&& other);
191 
192  // Deep copy assignment
193  FDTensor& operator=(const FDTensor& other);
194  // Move assignment
195  FDTensor& operator=(FDTensor&& other);
196 
197  // Scalar to FDTensor
198  explicit FDTensor(const Scalar& scalar);
199 
200  ~FDTensor() { FreeFn(); }
201 
202  static void CopyBuffer(void* dst, const void* src, size_t nbytes,
203  const Device& device = Device::CPU,
204  bool is_pinned_memory = false);
205 };
206 
207 } // namespace fastdeploy
void SetData(const std::vector< int64_t > &tensor_shape, const FDDataType &data_type, void *data_buffer, bool copy=false, const Device &data_device=Device::CPU, int data_device_id=-1)
Set data buffer for a FDTensor, e.g std::vector<float> buffer(1 * 3 * 224 * 224, 0); FDTensor tensor;...
Definition: fd_tensor.h:45
FDDataType Dtype() const
Get dtype of tensor.
Definition: fd_tensor.h:80
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
A brief file description.
void Allocate(const FDDataType &data_type, const std::vector< int64_t > &data_shape)
Allocate cpu data buffer for a FDTensor, e.g FDTensor tensor; tensor.Allocate(FDDataType::FLOAT, {1, 3, 224, 224};.
Definition: fd_tensor.h:90
bool IsShared()
Whether the tensor is owned the data buffer or share the data buffer from outside.
Definition: fd_tensor.h:101
void * GetData()
Get data pointer of tensor.
Definition: fd_tensor.h:53
std::vector< int64_t > Shape() const
Get shape of tensor.
Definition: fd_tensor.h:77
const void * GetData() const
Get data pointer of tensor.
Definition: fd_tensor.h:57
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16