Tensor
Tensors are the primary data structure in HEAL. They represent multi-dimensional arrays with fixed shape and data type. Every function in the HEAL runtime operates over tensors, which are passed as pointers into device memory.
This page introduces how tensors are structured in HEAL: their data types, shapes, strides, and broadcasting behavior.
Data Types
HEAL tensors support integer types (int32_t
;int64_t
)
Every tensor must have a clearly defined scalar type. The type is determined at tensor creation and cannot be changed.
Example:
This declares a tensor of 32-bit integers, managed on the device.
Shape
A tensor’s shape defines its dimensionality - the size of the tensor along each axis.
Example:
A tensor with shape [2, 3]
contains 2 rows and 3 columns:
Shape is immutable unless explicitly changed (e.g. via
reshape
).Shape compatibility is enforced in all HEAL operations.
Strides
Strides describe how tensor elements are laid out in memory. Each stride tells you how many memory steps to take to move between elements along a particular axis.
Strides are especially important when tensors are non-contiguous, such as after slicing, reshaping, or transposing.
Example:
A tensor with shape [2, 3]
laid out in row-major order will have strides [3, 1]
:
Move 3 units in memory to go from row 0 to row 1.
Move 1 unit to move across columns.
Strides allow for memory-efficient views and slicing without copying data.
Broadcasting Semantics
Broadcasting allows element-wise operations between tensors of different shapes, as long as the shapes are broadcastable. This enables operations without explicitly reshaping tensors.
Broadcasting Rules
Two tensors are broadcastable if:
Both have at least one dimension.
Starting from the last (trailing) dimension and comparing backward:
Dimensions are equal
or
One of the dimensions is
1
or
One of the tensors has fewer dimensions, in which case it's left-padded with 1s to match the other tensor's rank.
If these rules hold for every dimension, the shapes are broadcastable. The resulting shape is the maximum size in each dimension.
✓ Valid Broadcasting Example
✘ Invalid Broadcasting Example
Additional Note
In-place operations (where input and output refer to the same tensor) are only allowed if broadcasting does not change the input's shape.
If a tensor needs to be broadcast to a larger shape, it cannot safely be used as the output - doing so would write outside its allocated memory.
Example:
Last updated