|
NForge
Tensor library
|
#include <tensor.h>
Classes | |
| class | CPUImpl |
| class | CUDAImpl |
| class | Impl |
| class | Shape |
| class | View |
Public Member Functions | |
| Tensor (const Tensor::Shape &shape, Backend backend=Backend::CPU) | |
| Constructs a tensor with the given shape, zero-initialized. | |
| Tensor (const std::initializer_list< size_t > &shape, Backend backend=Backend::CPU) | |
| Constructs a tensor with the given shape, zero-initialized. | |
| Tensor (const Tensor::Shape &shape, float value, Backend backend=Backend::CPU) | |
Constructs a tensor and fills every element with value. | |
| Tensor (const std::initializer_list< size_t > &shape, float value, Backend backend=Backend::CPU) | |
Constructs a tensor and fills every element with value. | |
| Tensor (float value, Backend backend=Backend::CPU) | |
| Constructs a scalar tensor, shape {}. | |
| Tensor (const Tensor &tensor) | |
| Copy constructor. Performs a deep copy. | |
| Tensor (std::unique_ptr< Tensor::Impl > impl, Backend backend=Backend::CPU) | |
| ~Tensor () | |
| Destructor. | |
| void | to (Backend newBackend) |
| Transfers data to a different backend. No-op if already on that backend. | |
| void | fillAll (float value) |
Fills all elements with value. | |
| void | fillRand () |
| Fills all elements with random values in [-1, 1]. | |
| void | print () const |
| Prints the tensor to stdout. | |
| void | print (const std::vector< size_t > &position) const |
Prints the block starting at position to stdout. | |
| Tensor::Shape | getShape () const |
| Returns the tensor shape. | |
| std::string | getBackendString () const |
| Returns "CPU" or "CUDA". | |
| Backend | getBackend () const |
| Returns the backend enum. | |
| std::string | getDataString () const |
| Returns a string representation of the underlying data. | |
| size_t | getNumElements () const |
| Returns the total number of elements. | |
| std::vector< float > | toVector () const |
| Copies all elements into a flat vector (row-major order). | |
| void | set (const std::vector< size_t > &position, const Tensor::View &rhs) |
Replaces the block starting at position with the data from rhs. | |
| bool | compare (const Tensor::View &rhs) const |
Returns true if shape and every element matches rhs. | |
| bool | compare (const std::vector< size_t > &position, const Tensor::View &rhs) const |
Returns true if the block at position matches rhs. | |
| Tensor | operator+ (const Tensor::View &rhs) const |
| Elementwise addition with a tensor or view. | |
| Tensor | operator- (const Tensor::View &rhs) const |
| Elementwise subtraction with a tensor or view. | |
| Tensor | operator* (const Tensor::View &rhs) const |
| Elementwise multiplication with a tensor or view. | |
| Tensor | operator/ (const Tensor::View &rhs) const |
| Elementwise division by a tensor or view. | |
| Tensor | operator+ (float scalar) const |
| Elementwise addition with a pure float. | |
| Tensor | operator- (float scalar) const |
| Elementwise subtraction with a pure float. | |
| Tensor | operator* (float scalar) const |
| Elementwise multiplication with a pure float. | |
| Tensor | operator/ (float scalar) const |
| Elementwise division by a pure float. | |
| void | operator+= (const Tensor::View &rhs) |
| In-place elementwise addition with a tensor or view. | |
| void | operator-= (const Tensor::View &rhs) |
| In-place elementwise subtraction with a tensor or view. | |
| void | operator*= (const Tensor::View &rhs) |
| In-place elementwise multiplication with a tensor or view. | |
| void | operator/= (const Tensor::View &rhs) |
| In-place elementwise division by a tensor or view. | |
| 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 | matmul (const Tensor::View &rhs) const |
| Tensor::View | operator[] (size_t idx) const |
| Indexes into the first dimension, returning a view of the sub-tensor. | |
| Tensor::View | subsample (std::vector< size_t > strides) const |
| Tensor & | operator= (const Tensor &rhs) |
| Copies data from another tensor. | |
| Tensor & | operator= (const Tensor::View &rhs) |
| Copies data from a view. | |
| Tensor & | operator= (float scalar) |
| bool | operator== (const Tensor::View &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 |
Friends | |
| Tensor | operator+ (float scalar, const Tensor &rhs) |
| Elementwise addition of a pure float and a tensor. | |
| Tensor | operator- (float scalar, const Tensor &rhs) |
| Elementwise subtraction of a tensor from a pure float. | |
| Tensor | operator* (float scalar, const Tensor &rhs) |
| Elementwise multiplication of a pure float and a tensor. | |
| Tensor | operator/ (float scalar, const Tensor &rhs) |
| Elementwise division of a pure float by a tensor. | |
Multi-dimensional array with backend specific implementation.
A Tensor owns a contiguous block of float data managed by a backend specific Impl object. Elements can be accessed through Tensor::View, which describes a sub region via offset, shape, and stride layout.
| Tensor::Tensor | ( | std::unique_ptr< Tensor::Impl > | impl, |
| Backend | backend = Backend::CPU |
||
| ) |
Constructs a tensor by taking ownership of a backend implementation.
| impl | Backend implementation, ownership is transferred. |
| Tensor Tensor::isClose | ( | const Tensor::View & | rhs, |
| float | tolerance = 1e-5f |
||
| ) | const |
Elementwise closeness check within tolerance. Returns a tensor of 0.0 / 1.0.
| tolerance | Maximum absolute or relative difference (default: 1e-5). |
| Tensor Tensor::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.
| bool Tensor::operator!= | ( | const Tensor::View & | rhs | ) | const |
Returns true if any element differs from rhs.
.isClose() | Tensor & Tensor::operator= | ( | float | scalar | ) |
Assigns a pure float to a tensor.
| bool Tensor::operator== | ( | const Tensor::View & | rhs | ) | const |
Returns true if all elements equal those in rhs.
.isClose() | Tensor::View Tensor::subsample | ( | std::vector< size_t > | strides | ) | const |
Strided sub-sampling view. Views every strides[i]-th element along dim i.
| strides | Length must match rank of tensor or be scalar 0. |