Skip to content

tenzr::Shape

Shape of an array: the ordered extents of its dimensions.

#include <shape.h>

Public Functions

Name
Shape() =default
Shape(std::initializer_list< int64_t > dims)
Shape(std::vector< int64_t > dims)
size_t Rank() const
Number of dimensions (rank).
int64_t Dim(size_t index) const
Extent of dimension index.
int64_t operator[](size_t index) const
const std::vector< int64_t > & Dims() const
The raw dimension extents in order.
int64_t NumElements() const
Total number of elements (product of all extents).
std::vector< int64_t > ComputeContiguousStrides() const
Row-major (C-order) contiguous strides for this shape.
bool operator==(const Shape & other) const
bool operator!=(const Shape & other) const
std::string ToString() const
Human-readable form such as "(2, 3)".
std::string ToMlirTensorType(DType dtype) const
MLIR tensor type for this shape with element type dtype.
Shape Broadcast(const Shape & a, const Shape & b)
Broadcast a and b to a common shape following NumPy rules.
bool CanBroadcast(const Shape & a, const Shape & b)
Whether a and b are broadcast-compatible.

Public Functions Documentation

function Shape

Shape() =default

function Shape

Shape(
    std::initializer_list< int64_t > dims
)

Parameters:

  • dims Dimension sizes, outermost first.

Construct a shape from a brace-enclosed list of dimensions.

function Shape

explicit Shape(
    std::vector< int64_t > dims
)

Parameters:

  • dims Dimension sizes, outermost first.

Construct a shape by taking ownership of a dimension vector.

function Rank

inline size_t Rank() const

Number of dimensions (rank).

function Dim

int64_t Dim(
    size_t index
) const

Extent of dimension index.

Parameters:

  • index Zero-based dimension index.

Return: The dimension size.

Size of the dimension at index.

function operator[]

inline int64_t operator[](
    size_t index
) const

function Dims

inline const std::vector< int64_t > & Dims() const

The raw dimension extents in order.

function NumElements

int64_t NumElements() const

Total number of elements (product of all extents).

Return: Total number of elements.

Product of all dimensions, treating an empty shape as the scalar value 1.

function ComputeContiguousStrides

std::vector< int64_t > ComputeContiguousStrides() const

Row-major (C-order) contiguous strides for this shape.

Return: Stride per dimension, in elements.

Row-major (C-order) strides for this shape.

function operator==

inline bool operator==(
    const Shape & other
) const

function operator!=

inline bool operator!=(
    const Shape & other
) const

function ToString

std::string ToString() const

Human-readable form such as "(2, 3)".

Return: Human-readable shape string.

Render the shape as a bracketed, comma-separated list.

function ToMlirTensorType

std::string ToMlirTensorType(
    DType dtype
) const

MLIR tensor type for this shape with element type dtype.

Parameters:

  • dtype Element type of the tensor.

Return: MLIR tensor type string.

Emit the StableHLO/MLIR tensor<...> type for this shape and dtype.

function Broadcast

static Shape Broadcast(
    const Shape & a,
    const Shape & b
)

Broadcast a and b to a common shape following NumPy rules.

Parameters:

  • a Left operand shape.
  • b Right operand shape.

Return: The broadcast result shape.

Broadcast a and b together, throwing if their dimensions are incompatible.

function CanBroadcast

static bool CanBroadcast(
    const Shape & a,
    const Shape & b
)

Whether a and b are broadcast-compatible.

Parameters:

  • a Left operand shape.
  • b Right operand shape.

Return: true when the shapes can be broadcast together.

Test whether a and b are broadcast-compatible without throwing.