Skip to content
fn

fresh

→TensorImplPtr
int fresh(int && s, int shape, Dtype dt, Device device)

Wraps an existing Storage and tensor geometry into a new TensorImpl with autograd disabled.

See Also

helpers::allocate_cpu — typical Storage producer for the first argument. TensorImpl — concrete wrapper type returned.

Parameters

sStorage&&
Storage holding the tensor data. Moved into the new TensorImpl — no copy of the underlying buffer occurs. May be CPU, GPU, or Shared storage.
shapeShape
Logical shape of the resulting tensor.
dtDtype
Element dtype. Must be consistent with the storage's element layout.
deviceDevice
Device on which the storage resides. Must match the storage's actual location.

Returns

TensorImplPtr

std::shared_ptr<TensorImpl> to a freshly constructed tensor. The stride is initialised to the C-contiguous default by the TensorImpl constructor; requires_grad is false.

Notes

"Fresh" here means newly produced by an op kernel — neither a view onto an existing tensor nor an autograd leaf that needs gradient tracking. Callers that want a gradient-tracking output must set the autograd metadata after construction.

Storage is moved (rvalue reference), so the input is consumed. This matches the kernel pattern where storage and tensor are produced in adjacent statements and the storage handle is no longer needed after wrapping.

Examples

Build a kernel output tensor in one statement::
    auto out = helpers::fresh(
        helpers::allocate_cpu(out_shape, dt),
        out_shape, dt, Device::CPU);