Skip to content

tenzr::apiutils

Functions

Name
TENZR_API int64_t CanonicalizeAxis(int64_t axis, size_t rank)
TENZR_API std::vector< int64_t > CanonicalizeAxes(const std::vector< int64_t > & axes, size_t rank)
TENZR_API std::vector< int64_t > ComputeContiguousStrides(const Shape & shape)
TENZR_API int64_t ComputeElementOffset(const std::vector< int64_t > & indices, const std::vector< int64_t > & strides, int64_t base_offset =0)
TENZR_API bool AreBroadcastCompatible(const Shape & a, const Shape & b)
TENZR_APIShape BroadcastShapes(const Shape & a, const Shape & b)
TENZR_APIShape BroadcastShapes(const std::vector< Shape > & shapes)
TENZR_APIShape SqueezeShape(const Shape & shape, const std::vector< int64_t > & axes)
TENZR_APIShape SqueezeAllUnitDims(const Shape & shape)
TENZR_APIShape ExpandDimsShape(const Shape & shape, int64_t axis)
TENZR_APIShape FlattenShape(const Shape & shape)

Functions Documentation

function CanonicalizeAxis

TENZR_API int64_t CanonicalizeAxis(
    int64_t axis,
    size_t rank
)

Parameters:

  • axis The axis to normalize.
  • rank The tensor rank.
  • axis Axis index, negative values count from the end.
  • rank Tensor rank, which must be positive.

Return:

  • The canonical non-negative axis.
  • The canonical non-negative axis.

Normalizes a negative or positive axis to a canonical non-negative index in [0, rank - 1]. Throws std::invalid_argument if axis is outside [-rank, rank - 1]. Normalize a possibly negative axis into [0, rank).

function CanonicalizeAxes

TENZR_API std::vector< int64_t > CanonicalizeAxes(
    const std::vector< int64_t > & axes,
    size_t rank
)

Parameters:

  • axes The axes to normalize.
  • rank The tensor rank.
  • axes Axes to normalize.
  • rank Tensor rank.

Return:

  • The canonical non-negative axes.
  • Unique, canonicalized axes.

Normalizes a vector of axes to unique canonical non-negative indices in [0, rank - 1]. Canonicalize a list of axes and remove duplicates, preserving sorted order.

function ComputeContiguousStrides

TENZR_API std::vector< int64_t > ComputeContiguousStrides(
    const Shape & shape
)

Parameters:

  • shape The shape to derive strides for.
  • shape Shape to stride.

Return:

  • The row-major strides.
  • Stride per dimension; empty for a scalar.

Computes standard C-contiguous row-major strides for a given shape. Compute row-major strides for shape.

function ComputeElementOffset

TENZR_API int64_t ComputeElementOffset(
    const std::vector< int64_t > & indices,
    const std::vector< int64_t > & strides,
    int64_t base_offset =0
)

Parameters:

  • indices The multidimensional coordinates.
  • strides The per-dimension strides.
  • base_offset Offset added to the result.
  • indices Per-dimension indices.
  • strides Strides matching indices.
  • base_offset Offset added to the computed index.

Return:

  • The flat element offset.
  • Flat element offset.

Computes the flat 1D buffer element offset from multidimensional coordinates and strides. Map multi-dimensional indices to a flat element offset.

function AreBroadcastCompatible

TENZR_API bool AreBroadcastCompatible(
    const Shape & a,
    const Shape & b
)

Parameters:

  • a The first shape.
  • b The second shape.
  • a Left operand shape.
  • b Right operand shape.

Return:

  • true if the shapes broadcast together.
  • true when broadcast-compatible.

Validates whether two shapes are strictly broadcast-compatible following NumPy/JAX rules. Test whether a and b can be broadcast together, without throwing.

function BroadcastShapes

TENZR_APIShape BroadcastShapes(
    const Shape & a,
    const Shape & b
)

Parameters:

  • a The first shape.
  • b The second shape.
  • a Left operand shape.
  • b Right operand shape.

Return:

  • The broadcasted shape.
  • The broadcast result shape.

Computes the broadcasted output shape for two input shapes. Throws std::invalid_argument if the shapes cannot be broadcast together. Broadcast two shapes, throwing on incompatible dimensions.

function BroadcastShapes

TENZR_APIShape BroadcastShapes(
    const std::vector< Shape > & shapes
)

Parameters:

  • shapes The input shapes.
  • shapes Shapes to broadcast together; an empty list yields a scalar shape.

Return:

  • The broadcasted shape.
  • The combined broadcast shape.

Computes the broadcasted output shape across an arbitrary list of shapes. Left-fold broadcasting across a list of shapes.

function SqueezeShape

TENZR_APIShape SqueezeShape(
    const Shape & shape,
    const std::vector< int64_t > & axes
)

Parameters:

  • shape The input shape.
  • axes The axes to remove.
  • shape Shape to squeeze.
  • axes Axes to remove.

Return:

  • The squeezed shape.
  • Squeezed shape.

Removes specified dimensions of size 1 from a shape. Compute the shape after squeezing the given axes, each of which must have size 1.

function SqueezeAllUnitDims

TENZR_APIShape SqueezeAllUnitDims(
    const Shape & shape
)

Parameters:

  • shape The input shape.
  • shape Shape to squeeze.

Return:

  • The squeezed shape.
  • Shape with all unit dimensions dropped.

Removes all dimensions of size 1 from a shape. Remove every dimension of size 1.

function ExpandDimsShape

TENZR_APIShape ExpandDimsShape(
    const Shape & shape,
    int64_t axis
)

Parameters:

  • shape The input shape.
  • axis Where to insert the new dimension.
  • shape Shape to expand.
  • axis Insertion position, negative values count from the end.

Return:

  • The expanded shape.
  • Shape with the new unit dimension.

Inserts a new unit dimension of size 1 at the specified axis. Insert a unit dimension at axis, allowing an index one past the last dimension.

function FlattenShape

TENZR_APIShape FlattenShape(
    const Shape & shape
)

Parameters:

  • shape The input shape.
  • shape Shape to flatten.

Return:

  • The flattened shape.
  • Rank-1 shape.

Flattens a multidimensional shape into a 1D shape with num_elements(). Collapse a shape to a single dimension of its total element count.