NVIDIA DeepStream SDK API Reference

9.1 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-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __TENSOR_HPP__
19 #define __TENSOR_HPP__
20 
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 namespace ds3d { namespace v2xinfer {
30 
31 static inline std::string format(const char* fmt, ...) {
32  char buffer[2048];
33  va_list vl;
34  va_start(vl, fmt);
35  vsnprintf(buffer, sizeof(buffer), fmt, vl);
36  return buffer;
37 }
38 
39 enum class DataType : int {
40  None = 0,
41  Int32 = 1,
42  Float16 = 2,
43  Float32 = 3,
44  Int64 = 4,
45  UInt64 = 5,
46  UInt32 = 6,
47  Int8 = 7,
48  UInt8 = 8,
49  UInt16 = 9,
50  Int16 = 10
51 };
52 
53 const char* dtype_string(DataType dtype);
54 size_t dtype_bytes(DataType dtype);
55 
56 template <typename _T>
57 std::string format_shape(const std::vector<_T>& shape);
58 
59 struct TensorData {
60  void* data = nullptr;
62  size_t bytes = 0;
63  bool device = true;
64  bool owner = false;
65 
66  bool empty() const { return data == nullptr; }
67  void reference(void* data, size_t bytes, DataType dtype, bool device);
68  void free();
69  virtual ~TensorData();
70 
71  static TensorData* reference_new(void* data, size_t bytes, DataType dtype, bool device);
72  static TensorData* create(size_t bytes, DataType dtype, bool device);
73 };
74 
75 struct Tensor {
76  std::vector<int64_t> shape;
77  std::shared_ptr<TensorData> data;
78  size_t numel = 0;
79  size_t ndim = 0;
80 
81  template <typename T>
82  T* ptr() const {
83  self_byte_check(sizeof(T));
84  return data ? (T*)data->data : nullptr;
85  }
86  void* ptr() const { return data ? data->data : nullptr; }
87 
88  int64_t size(int index) const { return shape[index]; }
89  size_t bytes() const { return data ? data->bytes : 0; }
90  bool empty() const { return data == nullptr || data->empty(); }
91  DataType dtype() const { return data ? data->dtype : DataType::None; }
92  bool device() const { return data ? data->device : false; }
93  void reference(void* data, std::vector<int64_t> shape, DataType dtype, bool device = true);
94  void to_device_(void* stream = nullptr);
95  void to_host_(void* stream = nullptr);
96  Tensor to_device(void* stream = nullptr) const;
97  Tensor to_host(void* stream = nullptr) const;
98  Tensor to_float(void* stream = nullptr) const;
99  Tensor to_half(void* stream = nullptr) const;
100  void create_(std::vector<int64_t> shape, DataType dtype, bool device = true);
101  bool save(const std::string& file, void* stream = nullptr) const;
102  void print(const char* prefix = "Tensor", size_t offset = 0, size_t num_per_line = 10, size_t lines = 1) const;
103  void memset(unsigned char value = 0, void* stream = nullptr);
104  void arange(void* stream = nullptr);
105  void release();
106  void self_byte_check(size_t type_bytes) const;
107  Tensor clone(void* stream) const;
108  void copy_from_host(const void* data, void* stream);
109  void copy_from_device(const void* data, void* stream);
110  unsigned int offset(unsigned int idim) const;
111 
112  Tensor() = default;
113  Tensor(std::vector<int64_t> shape, DataType dtype, bool device = true);
114 
115  static Tensor create(std::vector<int64_t> shape, DataType dtype, bool device = true);
116  static Tensor from_data_reference(void* data, std::vector<int64_t> shape, DataType dtype, bool device = true);
117  static Tensor from_data(void* data, std::vector<int64_t> shape, DataType dtype, bool device = true, void* stream = nullptr);
118  static Tensor load(const std::string& file, bool device = true, void* stream = nullptr);
119  static Tensor loadbinary(const std::string& file, std::vector<int64_t> shape, DataType dtype, bool device = true);
120  static bool save(const Tensor& tensor, const std::string& file, void* stream = nullptr);
121 };
122 
123 }}; // namespace ds3d::v2xinfer
124 
125 #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:59
ds3d::v2xinfer::TensorData::empty
bool empty() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:66
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:64
ds3d::DataType
DataType
Definition: sources/includes/ds3d/common/idatatype.h:82
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:82
ds3d::v2xinfer::format_shape
std::string format_shape(const std::vector< _T > &shape)
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:86
ds3d::v2xinfer::Tensor
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:75
ds3d::v2xinfer::Tensor::numel
size_t numel
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:78
ds3d::v2xinfer::Tensor::data
std::shared_ptr< TensorData > data
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:77
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:61
ds3d::v2xinfer::DataType::Float32
@ Float32
ds3d::v2xinfer::Tensor::shape
std::vector< int64_t > shape
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:76
ds3d::v2xinfer::Tensor::device
bool device() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:92
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:62
ds3d::v2xinfer::Tensor::size
int64_t size(int index) const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:88
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:39
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:90
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::bytes
size_t bytes() const
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:89
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:91
ds3d::v2xinfer::Tensor::arange
void arange(void *stream=nullptr)
ds3d::v2xinfer::Tensor::to_device
Tensor to_device(void *stream=nullptr) const
ds3d::v2xinfer::DataType::Float16
@ Float16
ds3d::v2xinfer::dtype_string
const char * dtype_string(DataType dtype)
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:63
ds3d::v2xinfer::Tensor::create_
void create_(std::vector< int64_t > shape, DataType dtype, bool device=true)
ds3d::v2xinfer::TensorData::data
void * data
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:60
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: sources/includes/ds3d/common/abi_dataprocess.h:25
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::Tensor::ndim
size_t ndim
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:79
ds3d::v2xinfer::format
static std::string format(const char *fmt,...)
Definition: sources/libs/ds3d/inference_custom_lib/ds3d_v2x_infer_custom_preprocess/tensor.hpp:31
ds3d::v2xinfer::Tensor::self_byte_check
void self_byte_check(size_t type_bytes) const