Skip to content
fn

clone_state_storage

→std::shared_ptr<TensorImpl>
int clone_state_storage(const int & src, const int & shape, Dtype dtype, Device device)

Deep-copy an optimizer state buffer into a new owning TensorImpl.

Snapshots a state Storage (CPU or GPU) into a freshly allocated TensorImpl whose backing buffer is independent of the original. This is the primitive used by derived optimizers when implementing state_buffers() so that the returned tensors survive any future in-place updates by step().

See Also

overwrite_state_storage : in-place inverse used by load_state_buffers.

Parameters

srcconst Storage&
Source state buffer. Must be either CpuStorage or GpuStorage. For GpuStorage the underlying MLX array is materialised via eval() before copying.
shapeconst Shape&
Logical shape to attach to the returned TensorImpl. Must match the element count implied by src.
dtypeDtype
Dtype tag to record on the new TensorImpl.
deviceDevice
Device tag to record on the new TensorImpl.

Returns

std::shared_ptr<TensorImpl>

A non-leaf tensor whose storage is a deep copy of src.

Raises

:runtime_error
If src is neither a CpuStorage nor a GpuStorage.

Notes

CPU copies use allocate_aligned_bytes followed by memcpy; GPU copies build an independent MLX array. In both cases the resulting buffer can be mutated freely without affecting the source optimizer state.