NForge
Tensor library
Loading...
Searching...
No Matches
Tensor::View Class Reference

#include <tensor_view.h>

Public Member Functions

 View (Tensor &parent)
 Full view at the origin.
 
 View (Tensor &parent, const std::vector< size_t > &position)
 View of a sub tensor starting at position.
 
 View (Tensor &parent, const std::vector< size_t > &position, const TensorLayout &layout)
 View with explicit position and TensorLayout.
 
 View (const Tensor &parent)
 Implicit conversion from Tensor.
 
void print () const
 Prints the view to stdout.
 
TensorgetParent () const
 Returns the referenced tensor.
 
std::vector< size_t > getPosition () const
 Returns the origin position within the parent tensor.
 
size_t getOffset () const
 Returns the element offset from the parent's data start.
 
Tensor::Shape getShape () const
 Returns the shape of the viewed region.
 
std::vector< size_t > getStride () const
 Returns the logical stride per dimension, normalized by the parent's base stride.
 
const TensorLayoutgetLayout () const
 Returns the underlying physical layout.
 
std::string getBackendString () const
 Returns "CPU" or "CUDA".
 
Backend getBackend () const
 Returns the backend enum.
 
Tensor copy () const
 Deep copies the viewed region into a new tensor.
 
std::vector< float > toVector () const
 Copies the viewd elements into a flat vector.
 
Tensor operator+ (const Tensor::View &rhs) const
 Elementwise addition with a tensor or view. Copies then computes.
 
Tensor operator- (const Tensor::View &rhs) const
 Elementwise subtraction with a tensor or view. Copies then computes.
 
Tensor operator* (const Tensor::View &rhs) const
 Elementwise multiplication with a tensor or view. Copies then computes.
 
Tensor operator/ (const Tensor::View &rhs) const
 Elementwise division by a tensor or view. Copies then computes.
 
void operator+= (const Tensor::View &rhs)
 In-place elementwise addition with a tensor or view. Modifies the parent tensor.
 
void operator-= (const Tensor::View &rhs)
 In-place elementwise subtraction with a tensor or view. Modifies the parent tensor.
 
void operator*= (const Tensor::View &rhs)
 In-place elementwise multiplication with a tensor or view. Modifies the parent tensor.
 
void operator/= (const Tensor::View &rhs)
 In-place elementwise division by a tensor or view. Modifies the parent tensor.
 
Tensor mean (size_t dim=0) const
 Reduces dimensions [dim, rank) by averaging. Result shape is shape[0:dim].
 
Tensor sum (size_t dim=0) const
 Reduces dimensions [dim, rank) by summation. Result shape is shape[0:dim].
 
Tensor min (size_t dim=0) const
 Reduces dimensions [dim, rank) by taking the minimum. Result shape is shape[0:dim].
 
Tensor max (size_t dim=0) const
 Reduces dimensions [dim, rank) by taking the maximum. Result shape is shape[0:dim].
 
Tensor prod (size_t dim=0) const
 Reduces dimensions [dim, rank) by taking the product. Result shape is shape[0:dim].
 
Tensor norm () const
 L2 norm (scalar tensor equal to sqrt(sum(x^2))).
 
Tensor::View operator= (const Tensor &rhs)
 Copies data from a tensor into the referenced position of this view.
 
Tensor::View operator= (const Tensor::View &rhs)
 Copies data from a view into the referenced position of this view.
 
Tensor::View operator= (float scalar)
 
Tensor::View operator[] (size_t idx) const
 Indexes into the first dimension of this view.
 
Tensor matmul (const Tensor::View &rhs) const
 
Tensor::View subsample (std::vector< size_t > strides) const
 Strided subsampling view. Views every strides[i]-th element along dim i.
 
bool operator== (const Tensor &rhs) const
 
bool operator== (const Tensor::View &rhs) const
 
bool operator!= (const Tensor &rhs) const
 
bool operator!= (const Tensor::View &rhs) const
 
Tensor operator< (const Tensor::View &rhs) const
 Elementwise less than. Returns a tensor of 0.0 / 1.0.
 
Tensor operator<= (const Tensor::View &rhs) const
 Elementwise less or equal. Returns a tensor of 0.0 / 1.0.
 
Tensor operator> (const Tensor::View &rhs) const
 Elementwise greater than. Returns a tensor of 0.0 / 1.0.
 
Tensor operator>= (const Tensor::View &rhs) const
 Elementwise greater or equal. Returns a tensor of 0.0 / 1.0.
 
Tensor isClose (const Tensor::View &rhs, float tolerance=1e-5f) const
 

Static Public Member Functions

static Tensor::View broadcast (Tensor &source, const Tensor::Shape &shape)
 
static Tensor::View subsample (const View &src, const std::vector< size_t > &factors)
 

Detailed Description

Non-owning reference to a sub region of a Tensor.

A View describes a sub region of a Tensor via an position, shape, and stride layout. Multiple views can reference the same underlying data at different offsets and shapes.

Member Function Documentation

◆ broadcast()

Tensor::View Tensor::View::broadcast ( Tensor source,
const Tensor::Shape shape 
)
static

Creates a broadcast view of source to shape. Size-1 dimensions are broadcast by setting their stride to 0.

◆ isClose()

Tensor Tensor::View::isClose ( const Tensor::View rhs,
float  tolerance = 1e-5f 
) const

Elementwise closeness check within tolerance. Returns a tensor of 0.0 / 1.0.

Parameters
toleranceMaximum absolute difference (default: 1e-5).

◆ matmul()

Tensor Tensor::View::matmul ( const Tensor::View rhs) const

Matrix multiplication. Inputs must be 2D or 3D tensors. 2D: (N, M) @ (M, K) => (N, K).

3D: (B, N, M) @ (B, M, K) => (B, N, K).

Batch dims must be broadcastable, match or be 1.

◆ operator!=() [1/2]

bool Tensor::View::operator!= ( const Tensor rhs) const

Returns true if any element differs from rhs.

Note
Exact match, which is unstable for floats. Consider using .isClose()

◆ operator!=() [2/2]

bool Tensor::View::operator!= ( const Tensor::View rhs) const

Returns true if any element differs from rhs.

Note
Exact match, which is unstable for floats. Consider using .isClose()

◆ operator=()

Tensor::View Tensor::View::operator= ( float  scalar)

Assigns a pure float to a View.

Note
Only works on scalar-shaped views, e.g views with only 1 element.

◆ operator==() [1/2]

bool Tensor::View::operator== ( const Tensor rhs) const

Returns true if shape and every element matches rhs.

Note
Exact match, which is unstable for floats. Consider using .isClose()

◆ operator==() [2/2]

bool Tensor::View::operator== ( const Tensor::View rhs) const

Returns true if shape and every element matches rhs.

Note
Exact match, which is unstable for floats. Consider using .isClose()

◆ subsample()

Tensor::View Tensor::View::subsample ( const View src,
const std::vector< size_t > &  factors 
)
static

Creates a subsampled view of src. A factor of 0 freezes that dimension to a single element (stride 0).


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