Skip to content
class

TensorMeta

TensorMeta

Geometric and type description of a tensor.

TensorMeta is stored by value inside TensorImpl (not on the heap) so that shape / dtype / device queries are a single pointer dereference. Views share the same Storage as their base tensor but carry an independent TensorMeta with potentially different shape, stride, or implicit byte offset (the offset is encoded in Storage).

Attributes

shapeShape
Per-dimension sizes. Empty shape denotes a 0-dim scalar.
strideStride
Per-dimension byte strides. Must have stride.size() == shape.size() to be considered a valid contiguous layout by is_contiguous_for.
dtypeDtype
Element type tag. Defaults to Dtype::F32.
deviceDevice
Memory domain owning the underlying storage. Defaults to Device::CPU.

Notes

Equality of two TensorMeta instances is structural — matching shape, stride, dtype, and device implies the two metas describe layout-equivalent tensors (modulo storage offset).

Methods

2
fnconst

is_contiguous_for

→bool
bool is_contiguous_for(int elem_size)

Returns whether the stored stride matches row-major (C-order) contiguous layout for elem_size-byte elements.

A tensor is C-contiguous when stride[i] = elem_size * prod(shape[i+1:]) for every axis i. A 0-dim scalar is contiguous by definition.

Parameters

elem_sizestd::size_t
Size in bytes of one element (e.g. sizeof(float) for F32). Must match the dtype width for the result to be meaningful.

Returns

bool

true if stride matches C-contiguous layout for the given element size, false otherwise. Returns false when stride.size() does not match shape.size().

Notes

Used by TensorImpl::is_contiguous and by op kernels that require contiguous input — non-contiguous tensors must be materialised via a copy before such kernels run.

fnconst

numel

→std::size_t
int numel()

Returns the total number of elements described by shape.

Computed as the product of all dimension sizes; a 0-dim scalar (empty shape) returns 1 by convention.

Math

numel=∏i=0N−1shapei\mathrm{numel} = \prod_{i=0}^{N-1} \mathrm{shape}_i

Returns

std::size_t

shape[0] * shape[1] * ... * shape[N-1], or 1 for a scalar.

Notes

shape_numel is the one element-count rule; a second one here cast a negative extent to 2^64 - 1 while allocation counted it as empty. A negative extent raises InvalidArgument.