17 #include <cuda_runtime_api.h> 28 #include "fastdeploy/core/allocate.h" 29 #include "fastdeploy/core/fd_tensor.h" 30 #include "fastdeploy/utils/utils.h" 34 struct FDInferDeleter {
35 template <
typename T>
void operator()(T* obj)
const {
43 template <
typename T>
using FDUniquePtr = std::unique_ptr<T, FDInferDeleter>;
45 int64_t Volume(
const nvinfer1::Dims& d);
47 nvinfer1::Dims ToDims(
const std::vector<int>& vec);
48 nvinfer1::Dims ToDims(
const std::vector<int64_t>& vec);
50 size_t TrtDataTypeSize(
const nvinfer1::DataType& dtype);
52 FDDataType GetFDDataType(
const nvinfer1::DataType& dtype);
54 nvinfer1::DataType ReaderDtypeToTrtDtype(
int reader_dtype);
56 FDDataType ReaderDtypeToFDDtype(
int reader_dtype);
58 std::vector<int> ToVec(
const nvinfer1::Dims& dim);
61 std::ostream& operator<<(std::ostream& out, const std::vector<T>& vec) {
63 for (
size_t i = 0; i < vec.size(); ++i) {
64 if (i != vec.size() - 1) {
65 out << vec[i] <<
", ";
73 template <
typename AllocFunc,
typename FreeFunc>
class FDGenericBuffer {
78 explicit FDGenericBuffer(nvinfer1::DataType type = nvinfer1::DataType::kFLOAT)
79 : mSize(0), mCapacity(0), mType(type), mBuffer(nullptr),
80 mExternal_buffer(nullptr) {}
85 FDGenericBuffer(
size_t size, nvinfer1::DataType type)
86 : mSize(size), mCapacity(size), mType(type) {
87 if (!allocFn(&mBuffer, this->nbBytes())) {
88 throw std::bad_alloc();
95 FDGenericBuffer(
size_t size, nvinfer1::DataType type,
void* buffer)
96 : mSize(size), mCapacity(size), mType(type) {
97 mExternal_buffer = buffer;
100 FDGenericBuffer(FDGenericBuffer&& buf)
101 : mSize(buf.mSize), mCapacity(buf.mCapacity), mType(buf.mType),
102 mBuffer(buf.mBuffer) {
105 buf.mType = nvinfer1::DataType::kFLOAT;
106 buf.mBuffer =
nullptr;
109 FDGenericBuffer& operator=(FDGenericBuffer&& buf) {
113 mCapacity = buf.mCapacity;
115 mBuffer = buf.mBuffer;
119 buf.mBuffer =
nullptr;
128 if (mExternal_buffer !=
nullptr)
129 return mExternal_buffer;
136 const void* data()
const {
137 if (mExternal_buffer !=
nullptr)
138 return mExternal_buffer;
145 size_t size()
const {
return mSize; }
150 size_t nbBytes()
const {
return this->size() * TrtDataTypeSize(mType); }
155 nvinfer1::DataType dtype()
const {
return mType; }
160 void SetExternalData(
size_t size, nvinfer1::DataType type,
void* buffer) {
161 mSize = mCapacity = size;
163 mExternal_buffer =
const_cast<void*
>(buffer);
169 void SetExternalData(
const nvinfer1::Dims& dims,
const void* buffer) {
170 mSize = mCapacity = Volume(dims);
171 mExternal_buffer =
const_cast<void*
>(buffer);
178 void resize(
size_t newSize) {
179 mExternal_buffer =
nullptr;
181 if (mCapacity < newSize) {
183 if (!allocFn(&mBuffer, this->nbBytes())) {
184 throw std::bad_alloc{};
193 void resize(
const nvinfer1::Dims& dims) {
return this->resize(Volume(dims)); }
196 mExternal_buffer =
nullptr;
201 size_t mSize{0}, mCapacity{0};
202 nvinfer1::DataType mType;
204 void* mExternal_buffer;
209 using FDDeviceBuffer = FDGenericBuffer<FDDeviceAllocator, FDDeviceFree>;
210 using FDDeviceHostBuffer =
211 FDGenericBuffer<FDDeviceHostAllocator, FDDeviceHostFree>;
213 class FDTrtLogger :
public nvinfer1::ILogger {
215 static FDTrtLogger* logger;
216 static FDTrtLogger* Get() {
217 if (logger !=
nullptr) {
220 logger =
new FDTrtLogger();
223 void SetLog(
bool enable_info =
false,
bool enable_warning =
false) {
224 enable_info_ = enable_info;
225 enable_warning_ = enable_warning;
228 void log(nvinfer1::ILogger::Severity severity,
229 const char* msg) noexcept
override {
230 if (severity == nvinfer1::ILogger::Severity::kINFO) {
232 FDINFO << msg << std::endl;
234 }
else if (severity == nvinfer1::ILogger::Severity::kWARNING) {
235 if (enable_warning_) {
236 FDWARNING << msg << std::endl;
238 }
else if (severity == nvinfer1::ILogger::Severity::kERROR) {
239 FDERROR << msg << std::endl;
240 }
else if (severity == nvinfer1::ILogger::Severity::kINTERNAL_ERROR) {
241 FDASSERT(
false,
"%s", msg);
246 bool enable_info_ =
false;
247 bool enable_warning_ =
false;
250 struct ShapeRangeInfo {
251 explicit ShapeRangeInfo(
const std::vector<int64_t>& new_shape) {
252 shape.assign(new_shape.begin(), new_shape.end());
253 min.resize(new_shape.size());
254 max.resize(new_shape.size());
255 is_static.resize(new_shape.size());
256 for (
size_t i = 0; i < new_shape.size(); ++i) {
257 if (new_shape[i] > 0) {
258 min[i] = new_shape[i];
259 max[i] = new_shape[i];
270 std::vector<int64_t> shape;
271 std::vector<int64_t> min;
272 std::vector<int64_t> max;
273 std::vector<int64_t> opt;
274 std::vector<int8_t> is_static;
279 int Update(
const std::vector<int64_t>& new_shape);
280 int Update(
const std::vector<int>& new_shape) {
281 std::vector<int64_t> new_shape_int64(new_shape.begin(), new_shape.end());
282 return Update(new_shape_int64);
285 friend std::ostream& operator<<(std::ostream& out,
286 const ShapeRangeInfo& info) {
287 out <<
"Input name: " << info.name <<
", shape=" << info.shape
288 <<
", min=" << info.min <<
", max=" << info.max << std::endl;
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16