NVIDIA DeepStream SDK API Reference

9.2 Release
TensorCache Class Reference

Detailed Description

Class to manage multiple tensors with string-based lookup.

This class provides a central repository for caching tensors, allowing easy retrieval by name and automatic memory management. All tensors added to the cache are owned by the cache and will be automatically released when the cache is destroyed.

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

Public Member Functions

 TensorCache ()
 Default constructor. More...
 
 ~TensorCache ()
 Destructor that automatically releases all cached tensors. More...
 
bool addTensor (Tensor *tensor)
 Add a tensor to the cache. More...
 
Tensor * getTensor (const std::string &name) const
 Get a tensor by name. More...
 
void * getTensorBuffer (const std::string &name) const
 Get a tensor's buffer by name. More...
 
size_t getTensorSize (const std::string &name) const
 Get a tensor's size by name. More...
 
std::vector< int > getTensorShape (const std::string &name) const
 Get a tensor's shape by name. More...
 
int getTensorGpuId (const std::string &name) const
 Get a tensor's gpu_id by name. More...
 
int getTensorFrameNum (const std::string &name) const
 Get a tensor's frame_num by name. More...
 
torch::Tensor getTorchTensor (const std::string &name) const
 Get a tensor directly as PyTorch tensor by name. More...
 
bool updateTensorBuffer (const std::string &name, void *newBuffer)
 Update only a tensor's buffer pointer, keeping all other properties the same. More...
 
Tensor * cloneTensorCPU (const std::string &name)
 Clone a tensor to CPU memory. More...
 
torch::Tensor cloneTorchTensorCPU (const std::string &name)
 Clone a tensor to CPU memory and convert to PyTorch tensor. More...
 
bool hasTensor (const std::string &name) const
 Check if a tensor with the given name exists. More...
 
bool removeTensor (const std::string &name)
 Remove a tensor from the cache and release its resources. More...
 
void releaseAll ()
 Release all tensors and clear the cache. More...
 
size_t size () const
 Get the number of tensors in the cache. More...
 
std::vector< std::string > getTensorNames () const
 Get a list of all tensor names in the cache. More...
 
const std::unordered_map< std::string, Tensor * > & getTensors () const
 Get all tensors in the cache. More...
 
void display (bool print_data=false) const
 Display information about all cached tensors. More...
 
void displayTensor (const std::string &name, bool print_data=false) const
 Display information about given tensor. More...
 
 TensorCache ()
 Default constructor. More...
 
 ~TensorCache ()
 Destructor that automatically releases all cached tensors. More...
 
bool addTensor (Tensor *tensor)
 Add a tensor to the cache. More...
 
Tensor * getTensor (const std::string &name) const
 Get a tensor by name. More...
 
void * getTensorBuffer (const std::string &name) const
 Get a tensor's buffer by name. More...
 
size_t getTensorSize (const std::string &name) const
 Get a tensor's size by name. More...
 
std::vector< int > getTensorShape (const std::string &name) const
 Get a tensor's shape by name. More...
 
int getTensorGpuId (const std::string &name) const
 Get a tensor's gpu_id by name. More...
 
int getTensorFrameNum (const std::string &name) const
 Get a tensor's frame_num by name. More...
 
torch::Tensor getTorchTensor (const std::string &name) const
 Get a tensor directly as PyTorch tensor by name. More...
 
bool updateTensorBuffer (const std::string &name, void *newBuffer)
 Update only a tensor's buffer pointer, keeping all other properties the same. More...
 
Tensor * cloneTensorCPU (const std::string &name)
 Clone a tensor to CPU memory. More...
 
torch::Tensor cloneTorchTensorCPU (const std::string &name)
 Clone a tensor to CPU memory and convert to PyTorch tensor. More...
 
bool hasTensor (const std::string &name) const
 Check if a tensor with the given name exists. More...
 
bool removeTensor (const std::string &name)
 Remove a tensor from the cache and release its resources. More...
 
void releaseAll ()
 Release all tensors and clear the cache. More...
 
size_t size () const
 Get the number of tensors in the cache. More...
 
std::vector< std::string > getTensorNames () const
 Get a list of all tensor names in the cache. More...
 
const std::unordered_map< std::string, Tensor * > & getTensors () const
 Get all tensors in the cache. More...
 
void display (bool print_data=false) const
 Display information about all cached tensors. More...
 
void displayTensor (const std::string &name, bool print_data=false) const
 Display information about given tensor. More...
 

Constructor & Destructor Documentation

◆ TensorCache() [1/2]

TensorCache::TensorCache ( )
inline

Default constructor.

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

◆ ~TensorCache() [1/2]

TensorCache::~TensorCache ( )
inline

Destructor that automatically releases all cached tensors.

Ensures that all memory allocated for tensors is properly freed when the cache is destroyed.

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

References releaseAll().

◆ TensorCache() [2/2]

TensorCache::TensorCache ( )
inline

Default constructor.

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

◆ ~TensorCache() [2/2]

TensorCache::~TensorCache ( )
inline

Destructor that automatically releases all cached tensors.

Ensures that all memory allocated for tensors is properly freed when the cache is destroyed.

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

References releaseAll().

Member Function Documentation

◆ addTensor() [1/2]

bool TensorCache::addTensor ( Tensor *  tensor)
inline

Add a tensor to the cache.

Parameters
tensorPointer to tensor object (ownership transferred to cache)
Returns
True if tensor was added, false if tensor is nullptr
Note
The cache takes ownership of the tensor and will delete it when appropriate

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

References Tensor::name.

◆ addTensor() [2/2]

bool TensorCache::addTensor ( Tensor *  tensor)
inline

Add a tensor to the cache.

Parameters
tensorPointer to tensor object (ownership transferred to cache)
Returns
True if tensor was added, false if tensor is nullptr
Note
The cache takes ownership of the tensor and will delete it when appropriate

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

References Tensor::name.

◆ cloneTensorCPU() [1/2]

Tensor* TensorCache::cloneTensorCPU ( const std::string &  name)
inline

Clone a tensor to CPU memory.

Creates a new Tensor object with a CPU buffer and copies data from the existing tensor. Works regardless of whether the source tensor is on CPU or GPU.

Parameters
nameName of the tensor to clone
Returns
Pointer to new tensor on CPU, or nullptr if source tensor not found Caller is responsible for deleting the returned tensor

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

References Tensor::buffer, Tensor::data_type, Tensor::frame_num, getTensor(), Tensor::gpu_id, hasTensor(), Tensor::name, Tensor::shape, and Tensor::size.

Referenced by cloneTorchTensorCPU().

◆ cloneTensorCPU() [2/2]

Tensor* TensorCache::cloneTensorCPU ( const std::string &  name)
inline

Clone a tensor to CPU memory.

Creates a new Tensor object with a CPU buffer and copies data from the existing tensor. Works regardless of whether the source tensor is on CPU or GPU.

Parameters
nameName of the tensor to clone
Returns
Pointer to new tensor on CPU, or nullptr if source tensor not found Caller is responsible for deleting the returned tensor

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

References Tensor::buffer, Tensor::data_type, Tensor::frame_num, getTensor(), Tensor::gpu_id, hasTensor(), Tensor::name, Tensor::shape, and Tensor::size.

◆ cloneTorchTensorCPU() [1/2]

torch::Tensor TensorCache::cloneTorchTensorCPU ( const std::string &  name)
inline

Clone a tensor to CPU memory and convert to PyTorch tensor.

Creates a CPU copy of the specified tensor and converts it to a PyTorch tensor. Works regardless of whether the source tensor is on CPU or GPU. The function handles memory management internally, freeing the cloned tensor after creating the PyTorch tensor.

Parameters
nameName of the tensor to clone
Returns
torch::Tensor PyTorch tensor on CPU, or empty tensor if source tensor not found

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

References cloneTensorCPU(), and Tensor::to_torch_tensor().

◆ cloneTorchTensorCPU() [2/2]

torch::Tensor TensorCache::cloneTorchTensorCPU ( const std::string &  name)
inline

Clone a tensor to CPU memory and convert to PyTorch tensor.

Creates a CPU copy of the specified tensor and converts it to a PyTorch tensor. Works regardless of whether the source tensor is on CPU or GPU. The function handles memory management internally, freeing the cloned tensor after creating the PyTorch tensor.

Parameters
nameName of the tensor to clone
Returns
torch::Tensor PyTorch tensor on CPU, or empty tensor if source tensor not found

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

References cloneTensorCPU(), and Tensor::to_torch_tensor().

◆ display() [1/2]

void TensorCache::display ( bool  print_data = false) const
inline

Display information about all cached tensors.

Parameters
print_dataWhether to print the tensor data values

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

◆ display() [2/2]

void TensorCache::display ( bool  print_data = false) const
inline

Display information about all cached tensors.

Parameters
print_dataWhether to print the tensor data values

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

◆ displayTensor() [1/2]

void TensorCache::displayTensor ( const std::string &  name,
bool  print_data = false 
) const
inline

Display information about given tensor.

Parameters
nameName of the tensor to display
print_dataWhether to print the tensor data values

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

◆ displayTensor() [2/2]

void TensorCache::displayTensor ( const std::string &  name,
bool  print_data = false 
) const
inline

Display information about given tensor.

Parameters
nameName of the tensor to display
print_dataWhether to print the tensor data values

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

◆ getTensor() [1/2]

Tensor* TensorCache::getTensor ( const std::string &  name) const
inline

Get a tensor by name.

Parameters
nameName of the tensor to retrieve
Returns
Pointer to the tensor, or nullptr if not found
Note
The returned tensor is still owned by the cache

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

◆ getTensor() [2/2]

Tensor* TensorCache::getTensor ( const std::string &  name) const
inline

Get a tensor by name.

Parameters
nameName of the tensor to retrieve
Returns
Pointer to the tensor, or nullptr if not found
Note
The returned tensor is still owned by the cache

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

Referenced by cloneTensorCPU(), getTorchTensor(), and updateTensorBuffer().

◆ getTensorBuffer() [1/2]

void* TensorCache::getTensorBuffer ( const std::string &  name) const
inline

Get a tensor's buffer by name.

Parameters
nameName of the tensor whose buffer to retrieve
Returns
void* Pointer to the tensor buffer, or nullptr if tensor not found or has no buffer

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

◆ getTensorBuffer() [2/2]

void* TensorCache::getTensorBuffer ( const std::string &  name) const
inline

Get a tensor's buffer by name.

Parameters
nameName of the tensor whose buffer to retrieve
Returns
void* Pointer to the tensor buffer, or nullptr if tensor not found or has no buffer

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

◆ getTensorFrameNum() [1/2]

int TensorCache::getTensorFrameNum ( const std::string &  name) const
inline

Get a tensor's frame_num by name.

Parameters
nameName of the tensor whose frame_num to retrieve
Returns
int Frame number of the tensor, or -1 if tensor not found

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

◆ getTensorFrameNum() [2/2]

int TensorCache::getTensorFrameNum ( const std::string &  name) const
inline

Get a tensor's frame_num by name.

Parameters
nameName of the tensor whose frame_num to retrieve
Returns
int Frame number of the tensor, or -1 if tensor not found

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

◆ getTensorGpuId() [1/2]

int TensorCache::getTensorGpuId ( const std::string &  name) const
inline

Get a tensor's gpu_id by name.

Parameters
nameName of the tensor whose gpu_id to retrieve
Returns
int GPU ID of the tensor, or -1 if tensor not found or is on CPU

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

◆ getTensorGpuId() [2/2]

int TensorCache::getTensorGpuId ( const std::string &  name) const
inline

Get a tensor's gpu_id by name.

Parameters
nameName of the tensor whose gpu_id to retrieve
Returns
int GPU ID of the tensor, or -1 if tensor not found or is on CPU

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

◆ getTensorNames() [1/2]

std::vector<std::string> TensorCache::getTensorNames ( ) const
inline

Get a list of all tensor names in the cache.

Returns
Vector of tensor names

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

◆ getTensorNames() [2/2]

std::vector<std::string> TensorCache::getTensorNames ( ) const
inline

Get a list of all tensor names in the cache.

Returns
Vector of tensor names

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

◆ getTensors() [1/2]

const std::unordered_map<std::string, Tensor*>& TensorCache::getTensors ( ) const
inline

Get all tensors in the cache.

Returns
Reference to the map containing all tensor name-pointer pairs

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

◆ getTensors() [2/2]

const std::unordered_map<std::string, Tensor*>& TensorCache::getTensors ( ) const
inline

Get all tensors in the cache.

Returns
Reference to the map containing all tensor name-pointer pairs

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

◆ getTensorShape() [1/2]

std::vector<int> TensorCache::getTensorShape ( const std::string &  name) const
inline

Get a tensor's shape by name.

Parameters
nameName of the tensor whose shape to retrieve
Returns
std::vector<int> Shape of the tensor (dimensions), or empty vector if tensor not found

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

◆ getTensorShape() [2/2]

std::vector<int> TensorCache::getTensorShape ( const std::string &  name) const
inline

Get a tensor's shape by name.

Parameters
nameName of the tensor whose shape to retrieve
Returns
std::vector<int> Shape of the tensor (dimensions), or empty vector if tensor not found

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

◆ getTensorSize() [1/2]

size_t TensorCache::getTensorSize ( const std::string &  name) const
inline

Get a tensor's size by name.

Parameters
nameName of the tensor whose size to retrieve
Returns
size_t Size of the tensor buffer in bytes, or 0 if tensor not found

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

◆ getTensorSize() [2/2]

size_t TensorCache::getTensorSize ( const std::string &  name) const
inline

Get a tensor's size by name.

Parameters
nameName of the tensor whose size to retrieve
Returns
size_t Size of the tensor buffer in bytes, or 0 if tensor not found

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

◆ getTorchTensor() [1/2]

torch::Tensor TensorCache::getTorchTensor ( const std::string &  name) const
inline

Get a tensor directly as PyTorch tensor by name.

Parameters
nameName of the tensor to retrieve
Returns
torch::Tensor PyTorch tensor created from the cached tensor, or empty tensor if not found
Note
The returned PyTorch tensor shares memory with the original tensor buffer

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

References Tensor::buffer, getTensor(), and Tensor::to_torch_tensor().

◆ getTorchTensor() [2/2]

torch::Tensor TensorCache::getTorchTensor ( const std::string &  name) const
inline

Get a tensor directly as PyTorch tensor by name.

Parameters
nameName of the tensor to retrieve
Returns
torch::Tensor PyTorch tensor created from the cached tensor, or empty tensor if not found
Note
The returned PyTorch tensor shares memory with the original tensor buffer

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

References Tensor::buffer, getTensor(), and Tensor::to_torch_tensor().

◆ hasTensor() [1/2]

bool TensorCache::hasTensor ( const std::string &  name) const
inline

Check if a tensor with the given name exists.

Parameters
nameName of the tensor to check
Returns
True if tensor exists, false otherwise

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

Referenced by cloneTensorCPU().

◆ hasTensor() [2/2]

bool TensorCache::hasTensor ( const std::string &  name) const
inline

Check if a tensor with the given name exists.

Parameters
nameName of the tensor to check
Returns
True if tensor exists, false otherwise

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

◆ releaseAll() [1/2]

void TensorCache::releaseAll ( )
inline

Release all tensors and clear the cache.

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

Referenced by ~TensorCache().

◆ releaseAll() [2/2]

void TensorCache::releaseAll ( )
inline

Release all tensors and clear the cache.

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

◆ removeTensor() [1/2]

bool TensorCache::removeTensor ( const std::string &  name)
inline

Remove a tensor from the cache and release its resources.

Parameters
nameName of the tensor to remove
Returns
True if tensor was found and removed, false otherwise

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

◆ removeTensor() [2/2]

bool TensorCache::removeTensor ( const std::string &  name)
inline

Remove a tensor from the cache and release its resources.

Parameters
nameName of the tensor to remove
Returns
True if tensor was found and removed, false otherwise

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

◆ size() [1/2]

size_t TensorCache::size ( ) const
inline

Get the number of tensors in the cache.

Returns
Number of tensors

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

◆ size() [2/2]

size_t TensorCache::size ( ) const
inline

Get the number of tensors in the cache.

Returns
Number of tensors

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

◆ updateTensorBuffer() [1/2]

bool TensorCache::updateTensorBuffer ( const std::string &  name,
void *  newBuffer 
)
inline

Update only a tensor's buffer pointer, keeping all other properties the same.

Parameters
nameName of the tensor to update
newBufferPointer to the new buffer (must be GPU memory with same size/layout)
Returns
True if tensor was found and updated, false otherwise

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

References Tensor::buffer, and getTensor().

◆ updateTensorBuffer() [2/2]

bool TensorCache::updateTensorBuffer ( const std::string &  name,
void *  newBuffer 
)
inline

Update only a tensor's buffer pointer, keeping all other properties the same.

Parameters
nameName of the tensor to update
newBufferPointer to the new buffer (must be GPU memory with same size/layout)
Returns
True if tensor was found and updated, false otherwise

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

References Tensor::buffer, and getTensor().


The documentation for this class was generated from the following file: