docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
sdfg::math::tensor::TensorLayout Class Reference

The metadata associated with a addressing a tensor in elements Meant to be used with TensorNodes to describe their input's and outputs layouts and ease handling of such descriptions If the TensorType will keep existing it should also be switched to use this. More...

#include <sdfg/data_flow/library_nodes/math/tensor/tensor_layout.h>

Public Types

enum  TensorLayoutType { LAYOUT_OTHER = 0 , LAYOUT_ROW_MAJOR = 1 , LAYOUT_COL_MAJOR = 2 , LAYOUT_ROW_OR_COL_MAJOR = 3 }

Public Member Functions

 TensorLayout (const symbolic::MultiExpression &shape, const symbolic::MultiExpression &strides={}, const symbolic::Expression offset=symbolic::integer(0))
const symbolic::MultiExpression & shape () const
const symbolic::MultiExpression & strides () const
const symbolic::Expression & offset () const
void serialize_to_json (nlohmann::json &j) const
std::string toStr () const
symbolic::Expression total_elements () const
symbolic::Expression memory_span () const
symbolic::MultiExpression linear_strides () const
symbolic::Expression resolve_element (const symbolic::MultiExpression &indices, bool require_to_element=true) const
 apply the index to the layout, returning an "element-address"
std::optional< symbolic::MultiExpression > partition_offset_into_dimensions () const
 Partition the layout's offset() into one coordinate offset per dimension.
symbolic::Expression max_accessed_byte_offset_from_ptr (types::PrimitiveType element_type) const
 For memory access ranges, this should return the maximum offset from the pointer that is possible, in elements.
void collect_symbols (symbolic::SymbolSet &set) const
void replace_symbols (const symbolic::Expression &old, const symbolic::Expression &new_expr)
void replace_symbols (const symbolic::ExpressionMapping &replacements)
const symbolic::Expression & get_dim (int i) const
symbolic::Expression get_dim_innermost (int i) const
symbolic::Expression get_stride (int i) const
symbolic::Expression get_stride_innermost (int i) const
int dims () const
bool is_scalar () const
bool has_linear_accesses () const
bool has_linear_accesses_no_padding () const
bool has_transposed_strides_no_padding () const
bool operator== (const TensorLayout &other) const
std::unique_ptr< TensorLayout > newaxis (size_t axis) const
std::unique_ptr< TensorLayout > flip (size_t axis) const
std::unique_ptr< TensorLayout > unsqueeze (size_t axis) const
std::unique_ptr< TensorLayout > squeeze (size_t axis) const
std::unique_ptr< TensorLayout > squeeze () const
std::unique_ptr< TensorLayout > reshape (const symbolic::MultiExpression &new_shape) const
std::unique_ptr< TensorLayout > broadcast (const symbolic::MultiExpression &ref_shape) const
 Broadcast this layout to a bigger shape with strides correctly set.
TensorLayoutType is_2d_col_or_row_major () const
 Checks if the layout is 2D and either column-major or row-major.
bool is_last_dims_expressible_as_row_major () const
 Whatever the dimesionality of the entire Tensor, if it has at least 2 dimensions, and the innermost has a stride of "1" Useful to check of certain (hardware) APIs that only support 1 stride parameter and implicitly expect the other stride to be 1 can be used with this Tensor.

Static Public Member Functions

static symbolic::MultiExpression linear_strides (const symbolic::MultiExpression &shape)
static TensorLayout deserialize_from_json (const nlohmann::json &j)
static bool has_linear_accesses (symbolic::MultiExpression shape, symbolic::MultiExpression strides)
static bool has_linear_accesses_no_padding (symbolic::MultiExpression shape, symbolic::MultiExpression strides, symbolic::Expression offset)
static std::ostream & emit_symbolic_list (std::ostream &stream, const symbolic::MultiExpression &list)
static types::PrimitiveType get_tensor_indvar_type_for_shape (const std::vector< symbolic::Expression > &shape)
static TensorLayoutType is_last_dims_col_or_row_major (const symbolic::MultiExpression &strides)
 Checks if the given strides can be looked at as col-major or row-major.

Detailed Description

The metadata associated with a addressing a tensor in elements Meant to be used with TensorNodes to describe their input's and outputs layouts and ease handling of such descriptions If the TensorType will keep existing it should also be switched to use this.

Datatype is not part of it, as that can change independent of the layout. As long as the layout is strictly in elements, not bytes, there is no conflict

Member Enumeration Documentation

◆ TensorLayoutType

Enumerator
LAYOUT_OTHER 
LAYOUT_ROW_MAJOR 
LAYOUT_COL_MAJOR 
LAYOUT_ROW_OR_COL_MAJOR 

Constructor & Destructor Documentation

◆ TensorLayout()

sdfg::math::tensor::TensorLayout::TensorLayout ( const symbolic::MultiExpression & shape,
const symbolic::MultiExpression & strides = {},
const symbolic::Expression offset = symbolic::integer(0) )

Member Function Documentation

◆ broadcast()

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::broadcast ( const symbolic::MultiExpression & ref_shape) const

Broadcast this layout to a bigger shape with strides correctly set.

◆ collect_symbols()

void sdfg::math::tensor::TensorLayout::collect_symbols ( symbolic::SymbolSet & set) const

◆ deserialize_from_json()

TensorLayout sdfg::math::tensor::TensorLayout::deserialize_from_json ( const nlohmann::json & j)
static

◆ dims()

int sdfg::math::tensor::TensorLayout::dims ( ) const
inline

◆ emit_symbolic_list()

std::ostream & sdfg::math::tensor::TensorLayout::emit_symbolic_list ( std::ostream & stream,
const symbolic::MultiExpression & list )
static

◆ flip()

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::flip ( size_t axis) const

◆ get_dim()

const symbolic::Expression & sdfg::math::tensor::TensorLayout::get_dim ( int i) const
inline
Parameters
ithe dimension / entry in shape. 0 is outermost

◆ get_dim_innermost()

symbolic::Expression sdfg::math::tensor::TensorLayout::get_dim_innermost ( int i) const
inline
Parameters
i0 is innermost dim, 1 next level out etc.

◆ get_stride()

symbolic::Expression sdfg::math::tensor::TensorLayout::get_stride ( int i) const
inline
Parameters
ithe dimension / entry in strides. 0 is outermost

◆ get_stride_innermost()

symbolic::Expression sdfg::math::tensor::TensorLayout::get_stride_innermost ( int i) const
inline
Parameters
i0 is innermost dim, 1 next level out etc.

◆ get_tensor_indvar_type_for_shape()

types::PrimitiveType sdfg::math::tensor::TensorLayout::get_tensor_indvar_type_for_shape ( const std::vector< symbolic::Expression > & shape)
static

◆ has_linear_accesses() [1/2]

bool sdfg::math::tensor::TensorLayout::has_linear_accesses ( ) const

◆ has_linear_accesses() [2/2]

bool sdfg::math::tensor::TensorLayout::has_linear_accesses ( symbolic::MultiExpression shape,
symbolic::MultiExpression strides )
static

◆ has_linear_accesses_no_padding() [1/2]

bool sdfg::math::tensor::TensorLayout::has_linear_accesses_no_padding ( ) const

◆ has_linear_accesses_no_padding() [2/2]

bool sdfg::math::tensor::TensorLayout::has_linear_accesses_no_padding ( symbolic::MultiExpression shape,
symbolic::MultiExpression strides,
symbolic::Expression offset )
static

◆ has_transposed_strides_no_padding()

bool sdfg::math::tensor::TensorLayout::has_transposed_strides_no_padding ( ) const

◆ is_2d_col_or_row_major()

TensorLayoutType sdfg::math::tensor::TensorLayout::is_2d_col_or_row_major ( ) const

Checks if the layout is 2D and either column-major or row-major.

A layout is considered column-major if the stride of the first dimension is 1, and row-major if the stride of the second dimension is 1. If neither condition is met, then it is not mappable to code that expects col/row major and the outer line size, like blas GEMM or MMA cores

Returns
LAYOUT_OTHER(0), LAYOUT_ROW_MAJOR(1), LAYOUT_COL_MAJOR(-1)

◆ is_last_dims_col_or_row_major()

TensorLayoutType sdfg::math::tensor::TensorLayout::is_last_dims_col_or_row_major ( const symbolic::MultiExpression & strides)
static

Checks if the given strides can be looked at as col-major or row-major.

A layout is considered column-major if the stride of the first dimension is 1, and row-major if the stride of the second dimension is 1. If neither condition is met, then it is not mappable to code that expects col/row major and the outer line size, like blas GEMM or MMA cores Only considers the last 2 dimensions

Parameters
stridesat least the innermost 2 strides
Returns

◆ is_last_dims_expressible_as_row_major()

bool sdfg::math::tensor::TensorLayout::is_last_dims_expressible_as_row_major ( ) const

Whatever the dimesionality of the entire Tensor, if it has at least 2 dimensions, and the innermost has a stride of "1" Useful to check of certain (hardware) APIs that only support 1 stride parameter and implicitly expect the other stride to be 1 can be used with this Tensor.

◆ is_scalar()

bool sdfg::math::tensor::TensorLayout::is_scalar ( ) const

◆ linear_strides() [1/2]

symbolic::MultiExpression sdfg::math::tensor::TensorLayout::linear_strides ( ) const

◆ linear_strides() [2/2]

symbolic::MultiExpression sdfg::math::tensor::TensorLayout::linear_strides ( const symbolic::MultiExpression & shape)
static

◆ max_accessed_byte_offset_from_ptr()

symbolic::Expression sdfg::math::tensor::TensorLayout::max_accessed_byte_offset_from_ptr ( types::PrimitiveType element_type) const

For memory access ranges, this should return the maximum offset from the pointer that is possible, in elements.

This is used to determine the size of a pointer access

◆ memory_span()

symbolic::Expression sdfg::math::tensor::TensorLayout::memory_span ( ) const

◆ newaxis()

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::newaxis ( size_t axis) const

◆ offset()

const symbolic::Expression & sdfg::math::tensor::TensorLayout::offset ( ) const
inline

◆ operator==()

bool sdfg::math::tensor::TensorLayout::operator== ( const TensorLayout & other) const

◆ partition_offset_into_dimensions()

std::optional< symbolic::MultiExpression > sdfg::math::tensor::TensorLayout::partition_offset_into_dimensions ( ) const

Partition the layout's offset() into one coordinate offset per dimension.

Attempts to express offset() as sum_d strides[d] * dim_offset[d] and returns the per-dimension dim_offset (same size as shape). Only possible when every stride is an integer immediate and every summand of the offset is imm * x with imm divisible by some stride (the largest such stride is used); returns nullopt otherwise. Resolving coordinates with these offsets added to them yields the same element address as this layout.

◆ replace_symbols() [1/2]

void sdfg::math::tensor::TensorLayout::replace_symbols ( const symbolic::Expression & old,
const symbolic::Expression & new_expr )

◆ replace_symbols() [2/2]

void sdfg::math::tensor::TensorLayout::replace_symbols ( const symbolic::ExpressionMapping & replacements)

◆ reshape()

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::reshape ( const symbolic::MultiExpression & new_shape) const

◆ resolve_element()

symbolic::Expression sdfg::math::tensor::TensorLayout::resolve_element ( const symbolic::MultiExpression & indices,
bool require_to_element = true ) const

apply the index to the layout, returning an "element-address"

Parameters
require_to_elementwhether the result must be a single element or could just be a sub-tensor. If true, the indices must match the shape size
Returns
the offset from the pointer to the element in elements

◆ serialize_to_json()

void sdfg::math::tensor::TensorLayout::serialize_to_json ( nlohmann::json & j) const

◆ shape()

const symbolic::MultiExpression & sdfg::math::tensor::TensorLayout::shape ( ) const
inline

◆ squeeze() [1/2]

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::squeeze ( ) const

◆ squeeze() [2/2]

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::squeeze ( size_t axis) const

◆ strides()

const symbolic::MultiExpression & sdfg::math::tensor::TensorLayout::strides ( ) const
inline

◆ toStr()

std::string sdfg::math::tensor::TensorLayout::toStr ( ) const

◆ total_elements()

symbolic::Expression sdfg::math::tensor::TensorLayout::total_elements ( ) const

◆ unsqueeze()

std::unique_ptr< TensorLayout > sdfg::math::tensor::TensorLayout::unsqueeze ( size_t axis) const

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