NVIDIA DeepStream SDK API Reference

8.0 Release
sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef __TENSOR_HPP__
14 #define __TENSOR_HPP__
15 
16 #include <assert.h>
17 #include <stdarg.h>
18 #include <stdlib.h>
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 namespace ds3d { namespace v2xinfer {
25 
26 static inline std::string format(const char* fmt, ...) {
27  char buffer[2048];
28  va_list vl;
29  va_start(vl, fmt);
30  vsnprintf(buffer, sizeof(buffer), fmt, vl);
31  return buffer;
32 }
33 
34 enum class DataType : int {
35  None = 0,
36  Int32 = 1,
37  Float16 = 2,
38  Float32 = 3,
39  Int64 = 4,
40  UInt64 = 5,
41  UInt32 = 6,
42  Int8 = 7,
43  UInt8 = 8,
44  UInt16 = 9,
45  Int16 = 10
46 };
47 
48 const char* dtype_string(DataType dtype);
49 size_t dtype_bytes(DataType dtype);
50 
51 template <typename _T>
52 std::string format_shape(const std::vector<_T>& shape);
53 
54 struct TensorData {
55  void* data = nullptr;
57  size_t bytes = 0;
58  bool device = true;
59  bool owner = false;
60 
61  bool empty() const { return data == nullptr; }
62  void reference(void* data, size_t bytes, DataType dtype, bool device);
63  void free();
64  virtual ~TensorData();
65 
66  static TensorData* reference_new(void* data, size_t bytes, DataType dtype, bool device);
67  static TensorData* create(size_t bytes, DataType dtype, bool device);
68 };
69 
70 struct Tensor {
71  std::vector<int64_t> shape;
72  std::shared_ptr<TensorData> data;
73  size_t numel = 0;
74  size_t ndim = 0;
75 
76  template <typename T>
77  T* ptr() const {
78  self_byte_check(sizeof(T));
79  return data ? (T*)data->data : nullptr;
80  }
81  void* ptr() const { return data ? data->data : nullptr; }
82 
83  int64_t size(int index) const { return shape[index]; }
84  size_t bytes() const { return data ? data->bytes : 0; }
85  bool empty() const { return data == nullptr || data->empty(); }
86  DataType dtype() const { return data ? data->dtype : DataType::None; }
87  bool device() const { return data ? data->device : false; }
88  void reference(void* data, std::vector<int64_t> shape, DataType dtype, bool device = true);
89  void to_device_(void* stream = nullptr);
90  void to_host_(void* stream = nullptr);
91  Tensor to_device(void* stream = nullptr) const;
92  Tensor to_host(void* stream = nullptr) const;
93  Tensor to_float(void* stream = nullptr) const;
94  Tensor to_half(void* stream = nullptr) const;
95  void create_(std::vector<int64_t> shape, DataType dtype, bool device = true);
96  bool save(const std::string& file, void* stream = nullptr) const;
97  void print(const char* prefix = "Tensor", size_t offset = 0, size_t num_per_line = 10, size_t lines = 1) const;
98  void memset(unsigned char value = 0, void* stream = nullptr);
99  void arange(void* stream = nullptr);
100  void release();
101  void self_byte_check(size_t type_bytes) const;
102  Tensor clone(void* stream) const;
103  void copy_from_host(const void* data, void* stream);
104  void copy_from_device(const void* data, void* stream);
105  unsigned int offset(unsigned int idim) const;
106 
107  Tensor() = default;
108  Tensor(std::vector<int64_t> shape, DataType dtype, bool device = true);
109 
110  static Tensor create(std::vector<int64_t> shape, DataType dtype, bool device = true);
111  static Tensor from_data_reference(void* data, std::vector<int64_t> shape, DataType dtype, bool device = true);
112  static Tensor from_data(void* data, std::vector<int64_t> shape, DataType dtype, bool device = true, void* stream = nullptr);
113  static Tensor load(const std::string& file, bool device = true, void* stream = nullptr);
114  static Tensor loadbinary(const std::string& file, std::vector<int64_t> shape, DataType dtype, bool device = true);
115  static bool save(const Tensor& tensor, const std::string& file, void* stream = nullptr);
116 };
117 
118 }}; // namespace ds3d::v2xinfer
119 
120 #endif // __TENSOR_HPP__
ds3d::v2xinfer::Tensor::copy_from_device
void copy_from_device(const void *data, void *stream)
ds3d::v2xinfer::DataType::UInt8
@ UInt8
ds3d::v2xinfer::TensorData
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:54
ds3d::v2xinfer::TensorData::empty
bool empty() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:61
ds3d::v2xinfer::Tensor::to_host_
void to_host_(void *stream=nullptr)
ds3d::v2xinfer::DataType::UInt16
@ UInt16
ds3d::v2xinfer::TensorData::owner
bool owner
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:59
ds3d::DataType
DataType
Definition: idatatype.h:77
ds3d::v2xinfer::TensorData::reference
void reference(void *data, size_t bytes, DataType dtype, bool device)
ds3d::v2xinfer::TensorData::free
void free()
ds3d::v2xinfer::Tensor::clone
Tensor clone(void *stream) const
ds3d::v2xinfer::Tensor::ptr
T * ptr() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:77
ds3d::v2xinfer::format_shape
std::string format_shape(const std::vector< _T > &shape)
ds3d::v2xinfer::Tensor::shape
std::vector< int64_t > shape
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:71
ds3d::v2xinfer::TensorData::reference_new
static TensorData * reference_new(void *data, size_t bytes, DataType dtype, bool device)
ds3d::v2xinfer::Tensor::from_data_reference
static Tensor from_data_reference(void *data, std::vector< int64_t > shape, DataType dtype, bool device=true)
ds3d::v2xinfer::Tensor::ptr
void * ptr() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:81
ds3d::v2xinfer::Tensor
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:70
ds3d::v2xinfer::Tensor::numel
size_t numel
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:73
ds3d::v2xinfer::Tensor::memset
void memset(unsigned char value=0, void *stream=nullptr)
ds3d::v2xinfer::TensorData::dtype
DataType dtype
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:56
ds3d::v2xinfer::DataType::Float32
@ Float32
ds3d::v2xinfer::Tensor::device
bool device() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:87
ds3d::v2xinfer::Tensor::loadbinary
static Tensor loadbinary(const std::string &file, std::vector< int64_t > shape, DataType dtype, bool device=true)
ds3d::v2xinfer::Tensor::load
static Tensor load(const std::string &file, bool device=true, void *stream=nullptr)
ds3d::v2xinfer::TensorData::bytes
size_t bytes
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:57
ds3d::v2xinfer::Tensor::size
int64_t size(int index) const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:83
ds3d::v2xinfer::Tensor::reference
void reference(void *data, std::vector< int64_t > shape, DataType dtype, bool device=true)
ds3d::v2xinfer::Tensor::print
void print(const char *prefix="Tensor", size_t offset=0, size_t num_per_line=10, size_t lines=1) const
ds3d::v2xinfer::Tensor::to_float
Tensor to_float(void *stream=nullptr) const
ds3d::v2xinfer::DataType
DataType
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:34
ds3d::v2xinfer::DataType::Int16
@ Int16
ds3d::v2xinfer::Tensor::empty
bool empty() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:85
ds3d::v2xinfer::Tensor::Tensor
Tensor()=default
ds3d::v2xinfer::Tensor::save
bool save(const std::string &file, void *stream=nullptr) const
ds3d::v2xinfer::Tensor::offset
unsigned int offset(unsigned int idim) const
ds3d::v2xinfer::Tensor::release
void release()
ds3d::v2xinfer::TensorData::~TensorData
virtual ~TensorData()
ds3d::v2xinfer::Tensor::data
std::shared_ptr< TensorData > data
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:72
ds3d::v2xinfer::Tensor::bytes
size_t bytes() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:84
ds3d::v2xinfer::Tensor::to_host
Tensor to_host(void *stream=nullptr) const
ds3d::v2xinfer::Tensor::dtype
DataType dtype() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:86
ds3d::v2xinfer::Tensor::arange
void arange(void *stream=nullptr)
ds3d::v2xinfer::TensorData::data
void * data
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:55
ds3d::v2xinfer::Tensor::to_device
Tensor to_device(void *stream=nullptr) const
ds3d::v2xinfer::DataType::Float16
@ Float16
ds3d::v2xinfer::DataType::Int8
@ Int8
ds3d::v2xinfer::DataType::UInt32
@ UInt32
ds3d::v2xinfer::TensorData::device
bool device
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:58
ds3d::v2xinfer::Tensor::create_
void create_(std::vector< int64_t > shape, DataType dtype, bool device=true)
ds3d::v2xinfer::Tensor::from_data
static Tensor from_data(void *data, std::vector< int64_t > shape, DataType dtype, bool device=true, void *stream=nullptr)
ds3d::v2xinfer::Tensor::to_half
Tensor to_half(void *stream=nullptr) const
ds3d::v2xinfer::Tensor::copy_from_host
void copy_from_host(const void *data, void *stream)
ds3d::v2xinfer::Tensor::to_device_
void to_device_(void *stream=nullptr)
ds3d::v2xinfer::Tensor::create
static Tensor create(std::vector< int64_t > shape, DataType dtype, bool device=true)
ds3d::v2xinfer::DataType::Int32
@ Int32
ds3d::v2xinfer::DataType::Int64
@ Int64
ds3d
Definition: abi_dataprocess.h:21
ds3d::v2xinfer::DataType::None
@ None
ds3d::v2xinfer::TensorData::create
static TensorData * create(size_t bytes, DataType dtype, bool device)
ds3d::v2xinfer::dtype_bytes
size_t dtype_bytes(DataType dtype)
ds3d::v2xinfer::DataType::UInt64
@ UInt64
ds3d::v2xinfer::dtype_string
const char * dtype_string(DataType dtype)
ds3d::v2xinfer::Tensor::ndim
size_t ndim
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:74
ds3d::v2xinfer::format
static std::string format(const char *fmt,...)
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:26
ds3d::v2xinfer::Tensor::self_byte_check
void self_byte_check(size_t type_bytes) const