TensorBuffer class

A multi-dimensional array view over typed data with shape and stride metadata.

TensorBuffer provides a NumPy-like interface for tensor operations. It uses a view/storage separation pattern where TensorStorage holds the physical data and TensorBuffer defines how to interpret it through shape and stride information.

This design enables O(1) operations like transpose by manipulating strides rather than copying data.

// Create a 3x4 tensor filled with zeros
final tensor = TensorBuffer.zeros([3, 4], dtype: DType.float32);

// Transpose without copying data
final transposed = tensor.transpose([1, 0]); // Now 4x3

// Reshape (requires contiguous memory)
final reshaped = tensor.reshape([2, 6]);
Available extensions

Constructors

TensorBuffer({required TensorStorage storage, required List<int> shape, List<int>? strides, int storageOffset = 0, MemoryFormat memoryFormat = MemoryFormat.contiguous})
Creates a new tensor buffer with the given storage, shape, and optional strides.

Properties

data TypedData
The underlying typed data for direct access.
no setter
dataAsFloat32List Float32List
The underlying data as a Float32List.
no setter
dtype DType
The data type of elements in this tensor.
no setter
hashCode int
The hash code for this object.
no setterinherited
isContiguous bool
Whether this tensor's data is stored contiguously in memory.
no setter
isViewable bool

Available on TensorBuffer, provided by the TensorViewExtension extension

Checks if this tensor can be represented as a contiguous view of another tensor's storage.
no setter
memoryFormat MemoryFormat
The memory layout format of this tensor.
final
numel int
The total number of elements in this tensor.
no setter
rank int
The number of dimensions in this tensor.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shape List<int>
The dimensions of this tensor.
final
sizeInBytes int
The total size of this tensor's data in bytes.
no setter
storage TensorStorage
The underlying storage containing the physical data.
final
storageOffset int
The offset into storage where this tensor's data begins.
final
strides List<int>
The number of elements to skip in storage for each dimension.
final

Methods

clone() TensorBuffer
Creates a deep copy of this tensor with its own storage.
contiguous() TensorBuffer
Returns a contiguous copy of this tensor if not already contiguous.
flatten() TensorBuffer

Available on TensorBuffer, provided by the TensorViewExtension extension

Creates a flat view of this tensor.
max() double

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns the maximum value among all elements in this tensor.
maxAxes(List<int> axes, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the maximum value along multiple axes.
maxAxis(int axis, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the maximum value along the specified axis.
mean() double

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns the arithmetic mean of all elements in this tensor.
meanAxes(List<int> axes, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the mean of elements along multiple axes.
meanAxis(int axis, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the mean of elements along the specified axis.
min() double

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns the minimum value among all elements in this tensor.
minAxes(List<int> axes, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the minimum value along multiple axes.
minAxis(int axis, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the minimum value along the specified axis.
narrow(int dim, int start, int length) TensorBuffer

Available on TensorBuffer, provided by the TensorViewExtension extension

Narrows the tensor along a dimension.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
reshape(List<int> newShape) TensorBuffer
Returns a view of this tensor with a new newShape.
select(int dim, int index) TensorBuffer

Available on TensorBuffer, provided by the TensorViewExtension extension

Selects a single index along a dimension, returning a view with reduced rank.
sliceFirst(int start, int end) TensorBuffer

Available on TensorBuffer, provided by the TensorViewExtension extension

Creates a view of a contiguous slice along the first dimension.
squeeze([int? dim]) TensorBuffer
Returns a view with all size-1 dimensions removed.
sum() double

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns the sum of all elements in this tensor.
sumAxes(List<int> axes, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the sum of elements along multiple axes.
sumAxis(int axis, {bool keepDims = false}) TensorBuffer

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns a tensor with the sum of elements along the specified axis.
toChannelsFirst() TensorBuffer

Available on TensorBuffer, provided by the TensorViewExtension extension

For a 4D NHWC tensor, converts to NCHW format without copying.
toChannelsLast() TensorBuffer

Available on TensorBuffer, provided by the TensorViewExtension extension

Returns a view with channels moved to a different position.
toList() List<double>

Available on TensorBuffer, provided by the TensorBufferReduce extension

Returns all elements as a List<double>.
topk(int k, {int axis = -1, bool largest = true, bool sorted = true}) TopKResult

Available on TensorBuffer, provided by the TopKExtension extension

Returns the k largest or smallest values and their indices along axis.
toString() String
A string representation of this object.
override
transpose(List<int> axes) TensorBuffer
Returns a view of this tensor with dimensions permuted according to axes.
unbind(int dim) List<TensorBuffer>

Available on TensorBuffer, provided by the TensorViewExtension extension

Splits the tensor along a dimension into multiple views.
unsqueeze(int dim) TensorBuffer
Returns a view with a size-1 dimension inserted at position dim.

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](List<int> indices) double
Returns the element at the given multi-dimensional indices.

Static Methods

arange({required double start, required double end, double step = 1.0, DType dtype = DType.float32}) TensorBuffer
Creates a 1D tensor with values in a range with a given step.
computeStrides(List<int> shape, MemoryFormat format) List<int>
Computes strides for a tensor with the given shape and format.
eye(int n, {int? m, DType dtype = DType.float32}) TensorBuffer
Creates a 2D identity matrix.
fromFloat32List(Float32List data, List<int> shape) TensorBuffer
Creates a tensor from an existing Float32List with the given shape.
fromFloat64List(Float64List data, List<int> shape) TensorBuffer
Creates a tensor from an existing Float64List with the given shape.
fromUint8List(Uint8List data, List<int> shape) TensorBuffer
Creates a tensor from an existing Uint8List with the given shape.
full(List<int> shape, {required double fillValue, DType dtype = DType.float32}) TensorBuffer
Creates a tensor filled with a specific value.
linspace(double start, double end, {required int steps, DType dtype = DType.float32}) TensorBuffer
Creates a 1D tensor with evenly spaced values.
ones(List<int> shape, {DType dtype = DType.float32}) TensorBuffer
Creates a tensor filled with ones.
randn(List<int> shape, {DType dtype = DType.float32, int? seed}) TensorBuffer
Creates a tensor with random values from a standard normal distribution N(0, 1).
random(List<int> shape, {DType dtype = DType.float32, int? seed}) TensorBuffer
Creates a tensor with random values uniformly distributed in [0, 1).
uninitialized(List<int> shape, {DType dtype = DType.float32, MemoryFormat memoryFormat = MemoryFormat.contiguous}) TensorBuffer
Creates a tensor buffer without initializing values.
zeros(List<int> shape, {DType dtype = DType.float32, MemoryFormat memoryFormat = MemoryFormat.contiguous}) TensorBuffer
Creates a tensor filled with zeros.