Skip to content
class

TensorImpl

TensorImpl

Reference-counted tensor implementation owned by every Python-level lucid.Tensor.

The C++ op layer receives and returns TensorImplPtr (shared_ptr<TensorImpl>) directly; the Python wrapper is a thin layer that forwards attribute access here. TensorImpl extends std::enable_shared_from_this so member functions can hand out additional shared_ptr references to *this safely (e.g. when building autograd graph nodes that capture the producing tensor).

See Also

Storage — backing memory variant. TensorMeta — shape / stride / dtype / device bundle. AutogradMeta — autograd bookkeeping.

Attributes

storageStorage
Backing memory variant — CpuStorage, GpuStorage, or SharedStorage. Shared with every view.
offsetstd::size_t
Byte offset of this tensor's first element from the start of storage. Non-zero only for views created by make_view.
Shape, stride, dtype, and device. Views carry their own TensorMeta independent of the base.
autogradstd::optional<AutogradMeta>
Autograd bookkeeping (requires_grad, is_leaf, version, grad_fn, accumulated gradient). std::nullopt for tensors that have never required grad nor been written in place.

Constructors

1
ctor

TensorImpl

void TensorImpl(int storage, int shape, Dtype dtype, Device device, bool requires_grad)

Constructs a TensorImpl from an already-allocated Storage.

The C-contiguous (row-major) stride is computed automatically from shape and dtype. When requires_grad is true an AutogradMeta is constructed immediately; otherwise it is left unallocated until needed.

Parameters

storageStorage
Backing memory (any variant). Ownership is moved in.
shapeShape
Dimension vector. May be empty for a scalar.
dtypeDtype
Element type.
deviceDevice
CPU or GPU. Must match the variant of storage.
requires_gradbool
Whether the tensor should track gradients from the start.

Static methods

9
static

add_to_family

void add_to_family(const int & base, const int & view)

Puts view into base's view family, making one if base has none yet.

static

detach_gpu_grad_slot

int detach_gpu_grad_slot(int s)

Returns a GpuStorage whose array is a fresh shared_ptr without any GpuGradSlot deleter attached.

static

from_bytes

→std::shared_ptr<TensorImpl>
int from_bytes(int data, int shape, Dtype dtype, Device device, bool requires_grad)

Reconstructs a TensorImpl from a raw bytes blob + metadata.

Inverse of to_bytes. When device == Device::GPU the buffer is uploaded to MLX before returning.

Parameters

datapy::bytes
Row-major byte payload. Must be shape_numel(shape) * dtype_size(dtype) bytes long.
shapeShape
Target shape.
dtypeDtype
Element type of the payload.
deviceDevice
Target device. GPU triggers an MLX upload.
requires_gradbool
Whether the reconstructed tensor tracks gradients.

Returns

std::shared_ptr<TensorImpl>

Newly constructed tensor.

static

from_numpy

→std::shared_ptr<TensorImpl>
int from_numpy(int arr, Device device, bool requires_grad)

Constructs a TensorImpl by copying data from a NumPy array.

The data is always copied into a fresh CpuStorage buffer. When device == Device::GPU the buffer is subsequently uploaded to MLX via the GPU bridge, yielding a GpuStorage-backed tensor.

Parameters

arrpy::array
Source NumPy array. Must be a signed / floating / complex dtype.
deviceDevice
Target device for the constructed tensor.
requires_gradbool
Whether the resulting tensor tracks gradients.

Returns

std::shared_ptr<TensorImpl>

Newly constructed tensor.

Raises

DtypeError
The array has an unsigned dtype. The caller must cast to a signed integer or floating-point type first.
static

make_allocation_view

int make_allocation_view(const int & base, int shape, int stride, int allocation_byte_offset)

Creates a view over base's full underlying allocation at allocation_byte_offset bytes from the allocation start.

Used by as_strided_op so a window taken from a narrowed CPU block view (or a Metal engine view-family member) can address any element of the root allocation while remaining in base's ViewFamily.

static

make_block_view

→std::shared_ptr<TensorImpl>
int make_block_view(const int & base, int shape, int byte_offset)

A view of a contiguous run of base's buffer: byte_offset bytes in, shape's elements long — what a leading-dimension slice reads.

Its storage points at the run itself (an aliasing shared_ptr into base's buffer, sharing its ownership), so the view is dense and every op reads it from its first byte, as it reads any tensor. It joins base's ViewFamily. base must be a dense CPU tensor and the run must lie inside its buffer.

Parameters

basestd::shared_ptr<TensorImpl>
Dense CPU tensor whose buffer the view reads.
shapeShape
Shape of the view; its elements are the run's, in order.
byte_offsetstd::size_t
Where the run starts in base's storage.

Returns

std::shared_ptr<TensorImpl>

The view, with no autograd state of its own.

static

make_dtype_view

int make_dtype_view(const int & base, Dtype dtype, int shape, int stride, int offset_bytes, bool join_family)

Creates a dtype-reinterpreting view over base's storage (used by real, imag, view_as_real, and view_as_complex).

static

make_view

→std::shared_ptr<TensorImpl>
int make_view(const int & base, int shape, int stride, int offset_bytes, bool join_family)

Creates a new TensorImpl that aliases base's Storage with a different shape, stride, and optional byte offset.

No data is copied. The view starts with no autograd state of its own; a caller that makes a differentiable view wires its grad_fn. The view joins base's ViewFamily — one version counter, in-place writes into the buffer (on Metal, into the family's root) — unless join_family is false; a Metal view outside the family takes base's elements under its geometry as a value of its own.

Parameters

basestd::shared_ptr<TensorImpl>
Tensor whose storage the view aliases.
shapeShape
Shape of the new view.
strideStride
Byte-stride vector for the new view. Need not be contiguous.
offset_bytesstd::size_t= None
Byte offset from base's storage origin. Defaults to 0.
join_familybool= None
Whether the view joins base's family. Defaults to true.

Returns

std::shared_ptr<TensorImpl>

New view tensor sharing base's storage.

static

sync_gpu_grad_slot

void sync_gpu_grad_slot(GpuStorage & slot, int next_arr)

Updates a GPU gradient slot's array while preserving any live .grad view-family handle attached to it.

Methods

92