OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
opennn::TensorView Struct Reference

Non-owning view over a tensor: pointer, shape, and data type with rich reshape helpers. More...

#include <tensor_utilities.h>

Public Member Functions

 TensorView (void *new_data=nullptr, const Shape &new_shape={}, Type new_dtype=Type::FP32) noexcept
 Constructs a view from an external buffer, shape, and dtype.
 
Index get_rank () const noexcept
 Number of dimensions in the view.
 
Index size () const noexcept
 Total element count.
 
Index byte_size () const noexcept
 Total byte count (size() * sizeof(dtype)).
 
bool empty () const noexcept
 Returns true if the shape is empty.
 
template<typename T>
T * as () const noexcept
 Reinterprets the view's data as a pointer to T (no type checking).
 
float * as_float () const noexcept
 Reinterprets the view's data as a float pointer.
 
cudaDataType_t cuda_dtype () const noexcept
 Returns the CUDA data type tag corresponding to this view's dtype.
 
template<typename F>
void dispatch (F &&fn) const
 Dispatches a callable on the concrete element type (FP32 or BF16).
 
TensorView reshape (const Shape &new_shape) const
 Returns a new view over the same memory with a different shape.
 
MatrixMap as_matrix () const
 Maps the view to an Eigen matrix: rows = first dim, cols = product of the rest.
 
MatrixMap as_matrix (Index batch_index) const
 Maps a single batch slice of the view to an Eigen matrix.
 
MatrixMap as_flat_matrix () const
 Maps the view to an Eigen matrix flattened across all leading dimensions.
 
MatrixMap as_flat_matrix (Index batch_index) const
 Flat-matrix view of a single batch slice.
 
VectorMap as_vector () const
 Maps the view to a flat Eigen vector.
 
template<int Rank>
TensorMapR< Rank > as_tensor () const
 Maps the view to an Eigen Tensor of the given rank.
 
template<int Rank>
TensorMapR< Rank > as_tensor (Index batch_index) const
 Maps a single batch slice of the view to an Eigen Tensor of rank Rank.
 
void fill (float value)
 Sets every element of the view to the given value, dispatching CPU/GPU as needed.
 
void setZero ()
 Zeros every element of the view.
 

Public Attributes

void * data = nullptr
 
Shape shape
 
Type type = Type::FP32
 

Detailed Description

Non-owning view over a tensor: pointer, shape, and data type with rich reshape helpers.

Constructor & Destructor Documentation

◆ TensorView()

opennn::TensorView::TensorView ( void * new_data = nullptr,
const Shape & new_shape = {},
Type new_dtype = Type::FP32 )
inlinenoexcept

Constructs a view from an external buffer, shape, and dtype.

Member Function Documentation

◆ as()

template<typename T>
T * opennn::TensorView::as ( ) const
inlinenodiscardnoexcept

Reinterprets the view's data as a pointer to T (no type checking).

◆ as_flat_matrix() [1/2]

MatrixMap opennn::TensorView::as_flat_matrix ( ) const
inlinenodiscard

Maps the view to an Eigen matrix flattened across all leading dimensions.

◆ as_flat_matrix() [2/2]

MatrixMap opennn::TensorView::as_flat_matrix ( Index batch_index) const
inlinenodiscard

Flat-matrix view of a single batch slice.

◆ as_float()

float * opennn::TensorView::as_float ( ) const
inlinenodiscardnoexcept

Reinterprets the view's data as a float pointer.

◆ as_matrix() [1/2]

MatrixMap opennn::TensorView::as_matrix ( ) const
inlinenodiscard

Maps the view to an Eigen matrix: rows = first dim, cols = product of the rest.

◆ as_matrix() [2/2]

MatrixMap opennn::TensorView::as_matrix ( Index batch_index) const
inlinenodiscard

Maps a single batch slice of the view to an Eigen matrix.

◆ as_tensor() [1/2]

template<int Rank>
TensorMapR< Rank > opennn::TensorView::as_tensor ( ) const
inlinenodiscard

Maps the view to an Eigen Tensor of the given rank.

◆ as_tensor() [2/2]

template<int Rank>
TensorMapR< Rank > opennn::TensorView::as_tensor ( Index batch_index) const
inlinenodiscard

Maps a single batch slice of the view to an Eigen Tensor of rank Rank.

◆ as_vector()

VectorMap opennn::TensorView::as_vector ( ) const
inlinenodiscard

Maps the view to a flat Eigen vector.

◆ byte_size()

Index opennn::TensorView::byte_size ( ) const
inlinenodiscardnoexcept

Total byte count (size() * sizeof(dtype)).

◆ cuda_dtype()

cudaDataType_t opennn::TensorView::cuda_dtype ( ) const
inlinenodiscardnoexcept

Returns the CUDA data type tag corresponding to this view's dtype.

◆ dispatch()

template<typename F>
void opennn::TensorView::dispatch ( F && fn) const
inline

Dispatches a callable on the concrete element type (FP32 or BF16).

◆ empty()

bool opennn::TensorView::empty ( ) const
inlinenodiscardnoexcept

Returns true if the shape is empty.

◆ fill()

void opennn::TensorView::fill ( float value)
inline

Sets every element of the view to the given value, dispatching CPU/GPU as needed.

◆ get_rank()

Index opennn::TensorView::get_rank ( ) const
inlinenodiscardnoexcept

Number of dimensions in the view.

◆ reshape()

TensorView opennn::TensorView::reshape ( const Shape & new_shape) const
inlinenodiscard

Returns a new view over the same memory with a different shape.

◆ setZero()

void opennn::TensorView::setZero ( )
inline

Zeros every element of the view.

◆ size()

Index opennn::TensorView::size ( ) const
inlinenodiscardnoexcept

Total element count.

Member Data Documentation

◆ data

void* opennn::TensorView::data = nullptr

◆ shape

Shape opennn::TensorView::shape

◆ type

Type opennn::TensorView::type = Type::FP32