NVIDIA DeepStream SDK API Reference

9.2 Release
sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h File Reference

Go to the source code of this file.

Data Structures

struct  Tensor
 Structure to store tensor information. More...
 
class  TensorCache
 Class to manage multiple tensors with string-based lookup. More...
 

Functions

std::string torch_tensor_sizes_to_string (const torch::Tensor &tensor)
 Provide a string representation of the sizes of a torch tensor. More...
 
std::string torch_tensor_to_string (const torch::Tensor &tensor, int max_values=10)
 Convert torch tensor to string representation of its values. More...
 
void processDataType (std::ifstream &file, Tensor *result, const std::string &dtype, int64_t num_elements, const std::vector< int64_t > &shape_int64, const std::string &file_path, int gpu_id)
 Helper function to process tensor data based on its type. More...
 
Tensor * read_tensor_from_file (const std::string &file_path, int gpu_id=0)
 Read a tensor from a binary file created by dump_tensor_to_file. More...
 
void torch_tensor_display (const torch::Tensor &tensor, const std::string &name="Tensor", bool print_data=false, size_t max_elements=100, int precision=4)
 Display a PyTorch tensor in a formatted table. More...
 

Function Documentation

◆ processDataType()

void processDataType ( std::ifstream &  file,
Tensor *  result,
const std::string &  dtype,
int64_t  num_elements,
const std::vector< int64_t > &  shape_int64,
const std::string &  file_path,
int  gpu_id 
)
inline

Helper function to process tensor data based on its type.

This function is used by read_tensor_from_file to handle different data types. It allocates memory, reads data from file, and transfers it to GPU memory.

Parameters
fileOpen file stream to read data from
resultTensor to populate
dtypeData type string from file
num_elementsTotal number of elements in tensor
shape_int64Shape of tensor as int64_t vector
file_pathPath to file (for logging)
gpu_idGPU ID where tensor is allocated

Definition at line 995 of file sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h.

References Tensor::buffer, Tensor::data_type, NvDsDataType_FP32, and Tensor::size.

Referenced by read_tensor_from_file().

◆ read_tensor_from_file()

Tensor* read_tensor_from_file ( const std::string &  file_path,
int  gpu_id = 0 
)
inline

Read a tensor from a binary file created by dump_tensor_to_file.

This function reads a tensor from a binary file that was saved using the dump_tensor_to_file function in Python. It handles the file format that includes tensor shape and data type information. The data is loaded into GPU memory.

Supported data types:

  • float32 (converted to NvDsDataType_FP32)
  • float64 (converted to NvDsDataType_FP32)
  • float16/half (converted to NvDsDataType_FP16)
  • int32 (converted to NvDsDataType_INT32)
  • int64 (converted to NvDsDataType_INT64)
  • int8 (converted to NvDsDataType_INT8)
  • uint8 (converted to NvDsDataType_UINT8)
Parameters
file_pathPath to the binary file containing the tensor data
gpu_idGPU ID where the tensor should be allocated (default 0)
Returns
Tensor* Pointer to a heap-allocated custom Tensor struct with data on GPU The caller is responsible for freeing this memory

Definition at line 1057 of file sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h.

References half_float::detail::binary, Tensor::buffer, Tensor::gpu_id, Tensor::name, processDataType(), and Tensor::shape.

◆ torch_tensor_display()

void torch_tensor_display ( const torch::Tensor &  tensor,
const std::string &  name = "Tensor",
bool  print_data = false,
size_t  max_elements = 100,
int  precision = 4 
)
inline

Display a PyTorch tensor in a formatted table.

This function displays a PyTorch tensor in the same formatted table style as the Tensor::display method. It shows tensor properties and optionally the tensor values.

Parameters
tensorThe PyTorch tensor to display
nameOptional name for the tensor (defaults to "Tensor")
print_dataWhether to print the tensor data values (defaults to false)
max_elementsMaximum number of elements to display (defaults to 100)
precisionNumber of decimal places for floating point values (defaults to 4)

Definition at line 1136 of file sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h.

References min().

◆ torch_tensor_sizes_to_string()

std::string torch_tensor_sizes_to_string ( const torch::Tensor &  tensor)
inline

Provide a string representation of the sizes of a torch tensor.

Converts the dimensions of a PyTorch tensor to a human-readable string format. For example, a tensor with dimensions [3, 224, 224] would return "[3x224x224]".

Parameters
tensorPyTorch tensor whose dimensions to convert
Returns
String representation of tensor dimensions in format [dim1xdim2x...]

Definition at line 882 of file sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h.

References pydeepstreamdoc::tensor::TensorDoc::size.

◆ torch_tensor_to_string()

std::string torch_tensor_to_string ( const torch::Tensor &  tensor,
int  max_values = 10 
)
inline

Convert torch tensor to string representation of its values.

This function creates a human-readable string representation of a tensor's values. It handles different data types and allows limiting the number of values displayed.

Parameters
tensorPyTorch tensor to convert to string
max_valuesMaximum number of values to include in the string, or -1 for all values
Returns
String representation of tensor values in format [val1, val2, ...]

Definition at line 909 of file sources/gst-plugins/gst-nvdsvideotemplate/ds-sparse4d/utils/tensor.h.

References min().