|
Tannic
A C++ Tensor Library
|
A multidimensional, strided tensor data structure. More...
#include <tensor.hpp>
Public Types | |
| using | rank_type = uint8_t |
| Type used for rank (number of dimensions). | |
| using | size_type = std::size_t |
| Type used for size and shape dimensions. | |
Public Member Functions | |
| Tensor (type dtype, Shape shape) | |
| Constructs an uninitialized tensor with default (contiguous) strides. | |
| Tensor (type dtype, Shape shape, Strides strides, std::ptrdiff_t offset=0) | |
| Constructs an uninitialized tensor with custom strides and offset. | |
| template<Expression Expression> | |
| Tensor (const Expression &expression) | |
Constructs a tensor by forwarding an Expression-like object. | |
| template<Expression Expression> | |
| Tensor & | operator= (const Expression &expression) |
| Assigns the result of an expression to the tensor. | |
| Tensor (type dtype, Shape shape, std::ptrdiff_t offset, std::shared_ptr< Buffer > storage) | |
| Tensor (type dtype, Shape shape, Strides strides, std::ptrdiff_t offset, std::shared_ptr< Buffer > storage) | |
| Node * | node () const |
Metadata Access (May be constexpr in the future.) | |
| type | dtype () const |
| Returns the tensor's data type. | |
| rank_type | rank () const |
| Returns the number of dimensions (rank) of the tensor. | |
| Shape const & | shape () const |
| Returns the tensor's shape (dimension sizes per dimension). | |
| Shape::size_type | size (int dimension) const |
| Returns the tensor's size at a given dimension. | |
| Strides const & | strides () const |
| Returns the tensor's strides (step sizes per dimension). | |
| std::ptrdiff_t | offset () const |
| Returns the offset of the tensor in the current buffer. | |
| std::size_t | nelements () const |
| Returns the total number of elements of the tensor. | |
| std::size_t | nbytes () const |
| Returns the total number of bytes occupied by the tensor's elements. | |
| bool | is_contiguous () const |
| Returns whether the tensor's elements are in contiguous layout or not. | |
| bool | is_singleton () const |
| Tensor & | forward () |
| Returns a reference to this tensor (const-qualified). | |
| Tensor const & | forward () const |
| Returns a reference to this tensor (non-const). | |
Memory Management (Always runtime.) | |
| void | initialize (Environment environment=Host{}) const |
| Allocates the memory buffer for the tensor. | |
| std::byte * | bytes () const |
| Returns a pointer to the beginning of the tensor's data (accounting for offset). | |
| bool | is_initialized () const |
| Checks whether the tensor has been initialized with memory. | |
| Environment const & | environment () const |
| Returns a reference to the environment variant used to allocate this tensor's buffer. | |
| template<typename T > | |
| Tensor (std::initializer_list< T > const &values) | |
| @ | |
| template<typename T > | |
| Tensor (std::initializer_list< std::initializer_list< T > > const &values) | |
| Constructs a 2D tensor from a nested initializer list. | |
| template<typename T > | |
| Tensor (std::initializer_list< std::initializer_list< std::initializer_list< T > > > const &values) | |
| Constructs a 3D tensor from a triple-nested initializer list. | |
| template<typename T > | |
| Tensor (std::initializer_list< std::initializer_list< std::initializer_list< std::initializer_list< T > > > > const &values) | |
| Constructs a 4D tensor from a quadruple-nested initializer list. | |
| template<typename T > | |
| void | initialize (std::initializer_list< T > values, Environment environment=Host{}) |
| Assigns values to a 1D tensor from an initializer list. | |
| template<typename T > | |
| void | initialize (std::initializer_list< std::initializer_list< T > > const &values, Environment environment=Host{}) |
| Assigns values to a 2D tensor from a nested initializer list. | |
| template<typename T > | |
| void | initialize (std::initializer_list< std::initializer_list< std::initializer_list< T > > > const &values, Environment environment=Host{}) |
| Assigns values to a 3D tensor from a triple-nested initializer list. | |
| template<typename T > | |
| void | initialize (std::initializer_list< std::initializer_list< std::initializer_list< std::initializer_list< T > > > > values, Environment environment=Host{}) |
| Assigns values to a 4D tensor from a quadruple-nested initializer list. | |
Indexing, Slicing and Views. | |
| template<Integral Index> | |
| auto | operator[] (Index index) const |
| Indexes the tensor with a single integral index. | |
| auto | operator[] (indexing::Range range) const |
Indexes the tensor with a Range object. | |
| template<class ... Indexes> | |
| auto | operator[] (Indexes... indexes) const |
| Indexes the tensor with multiple indices (e.g., integers or ranges). | |
| auto | transpose (int first=-1, int second=-2) const |
| Returns a view of the tensor with two dimensions transposed. | |
| template<Integral ... Indexes> | |
| auto | permute (Indexes... indexes) const |
| Returns a view of the tensor with dimensions permuted. | |
| template<Integral ... Sizes> | |
| auto | view (Sizes... sizes) const |
| Returns a view of the tensor with given sizes. | |
| auto | squeeze () const |
| Returns a view of the tensor with all dimensions of size 1 removed. | |
| template<Integral... Axes> | |
| auto | unsqueeze (Axes... axes) |
| Returns a view of the tensor with a dimension of size 1 inserted at the given axis. | |
Protected Member Functions | |
| void | assign (std::byte const *, std::ptrdiff_t) |
| void | assign (bool const *, std::ptrdiff_t) |
| bool | compare (std::byte const *, std::ptrdiff_t) const |
Friends | |
| template<Expression Source, class... Indexes> | |
| class | expression::Slice |
| template<Expression Source> | |
| class | expression::Transpose |
| template<Expression Source> | |
| class | expression::View |
| template<Expression Source> | |
| class | expression::Squeeze |
| template<Expression Source> | |
| class | expression::Unsqueeze |
| template<Expression Source, Integral... Indexes> | |
| class | expression::Permutation |
| template<Expression Source> | |
| class | expression::Expansion |
| template<Expression Source> | |
| class | expression::Flatten |
| template<class Coordinates , Expression... Sources> | |
| class | expression::Complexification |
| template<Expression Source> | |
| class | expression::Realification |
A multidimensional, strided tensor data structure.
The Tensor class is the primary data structure in the Tannic Tensor Library. It represents a dynamic typed intention of a computation. It supports slicing, transposition, and expression composition.
constexpr evaluation mode is planned for future versions, enabling compile-time computational graphs.Host() or Device().initialize(Host()) or initialize(Device()).This example demonstrates eager initialization, advanced indexing, broadcasting, and composite expression evaluation.
| using tannic::Tensor::rank_type = uint8_t |
Type used for rank (number of dimensions).
| using tannic::Tensor::size_type = std::size_t |
Type used for size and shape dimensions.
|
inline |
Constructs an uninitialized tensor with default (contiguous) strides.
| dtype | Data type of the tensor. |
| shape | Shape (dimensions) of the tensor. |
|
inline |
Constructs a tensor by forwarding an Expression-like object.
| Expression | Expression type satisfying the Expression concept. |
| expression | An expression to evaluate and store as a tensor. |
|
inline |
@
Constructs a 1D tensor from an initializer list.
| T | Element type (deduced from the initializer list). |
| values | A list of values to populate the tensor. |
This constructor allows direct construction of 1D tensors using brace-enclosed lists:
The tensor is immediately initialized on host, contiguous in memory, and its dtype is deduced from T.
|
inline |
Constructs a 2D tensor from a nested initializer list.
| T | Element type (deduced from the nested initializer list). |
| values | A nested list of values (rows) to populate the tensor. |
This constructor allows direct construction of 2D tensors:
All inner lists (rows) must have the same length. The tensor is contiguous in memory.
|
inline |
Constructs a 3D tensor from a triple-nested initializer list.
| T | Element type (deduced from the nested lists). |
| values | A triple-nested list of values (matrices) to populate the tensor. |
This constructor allows direct construction of 3D tensors:
All matrices must have the same number of rows, and all rows must have the same number of columns. The tensor is contiguous in memory.
|
inline |
Constructs a 4D tensor from a quadruple-nested initializer list.
| T | Element type (deduced from the nested lists). |
| values | A quadruple-nested list of values (tensors) to populate the 4D tensor. |
This constructor allows direct construction of 4D tensors:
All inner tensors must have consistent dimensions. The tensor is contiguous in memory.
|
inline |
|
inline |
|
protected |
|
protected |
|
inline |
Returns a pointer to the beginning of the tensor's data (accounting for offset).
|
protected |
|
inline |
Returns the tensor's data type.
|
inline |
Returns a reference to the environment variant used to allocate this tensor's buffer.
|
inline |
Returns a reference to this tensor (const-qualified).
|
inline |
Returns a reference to this tensor (non-const).
| void tannic::Tensor::initialize | ( | Environment | environment = Host{} | ) | const |
Allocates the memory buffer for the tensor.
| environment | Memory environment (defaults to Host{}). |
|
inline |
Assigns values to a 4D tensor from a quadruple-nested initializer list.
| T | Element type of the nested initializer list. |
| values | A quadruple-nested list of values to assign to the tensor. |
This operator assigns values to an existing 4D tensor:
Requirements:
Type conversion:
|
inline |
Assigns values to a 3D tensor from a triple-nested initializer list.
| T | Element type of the nested initializer list. |
| values | A triple-nested list of values (matrices) to assign to the tensor. |
| environment | Target environment for initialization (default: Host). |
This operator assigns values to an existing 3D tensor:
Requirements:
Type conversion:
|
inline |
Assigns values to a 2D tensor from a nested initializer list.
| T | Element type of the nested initializer list. |
| values | A nested list of values to assign to the tensor (rows). |
This operator assigns values to an existing 2D tensor:
Requirements:
Type conversion:
|
inline |
Assigns values to a 1D tensor from an initializer list.
| T | Element type of the initializer list. |
| values | A list of values to assign to the tensor. |
This operator assigns values to an existing 1D tensor:
Requirements:
values must match the tensor shape.Type conversion:
|
inline |
Returns whether the tensor's elements are in contiguous layout or not.
|
inline |
Checks whether the tensor has been initialized with memory.
|
inline |
|
inline |
Returns the total number of bytes occupied by the tensor's elements.
|
inline |
Returns the total number of elements of the tensor.
|
inline |
|
inline |
Returns the offset of the tensor in the current buffer.
|
inline |
Assigns the result of an expression to the tensor.
| Expression | Expression type satisfying the Expression concept. |
| expression | Expression to evaluate. |
*this.
|
inline |
Indexes the tensor with a single integral index.
| index | The index to access. |
|
inline |
Indexes the tensor with multiple indices (e.g., integers or ranges).
| Indexes | Variadic index types. |
| indexes | Indices to apply. |
|
inline |
Indexes the tensor with a Range object.
| range | Range to apply to the first dimension. |
|
inline |
Returns a view of the tensor with dimensions permuted.
| indexes | Indices to permute |
|
inline |
Returns the number of dimensions (rank) of the tensor.
|
inline |
Returns the tensor's shape (dimension sizes per dimension).
|
inline |
Returns the tensor's size at a given dimension.
|
inline |
Returns a view of the tensor with all dimensions of size 1 removed.
|
inline |
Returns the tensor's strides (step sizes per dimension).
|
inline |
Returns a view of the tensor with two dimensions transposed.
| first | First dimension to swap (default: -1). |
| second | Second dimension to swap (default: -2). |
|
inline |
Returns a view of the tensor with a dimension of size 1 inserted at the given axis.
| axis | Dimension index where the new axis is inserted (supports negative indexing). |
|
inline |
Returns a view of the tensor with given sizes.
| sizes | sizes of the new shape of the tensor |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |
|
friend |