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
storageStorageCpuStorage, GpuStorage, or SharedStorage. Shared with every view.offsetstd::size_tstorage. Non-zero only for views created by make_view.metaTensorMetaTensorMeta independent of the base.autogradstd::optional<AutogradMeta>requires_grad, is_leaf, version, grad_fn, accumulated gradient). std::nullopt for tensors that have never required grad nor been written in place.Constructors
1TensorImpl
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
storageStorageshapeShapedtypeDtypedeviceDevicestorage.requires_ gradboolStatic methods
9Puts view into base's view family, making one if base has none yet.
Returns a GpuStorage whose array is a fresh shared_ptr without any GpuGradSlot deleter attached.
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::bytesshape_numel(shape) * dtype_size(dtype) bytes long.shapeShapedtypeDtypedeviceDevicerequires_ gradboolReturns
std::shared_ptr<TensorImpl>Newly constructed tensor.
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::arraydeviceDevicerequires_ gradboolReturns
std::shared_ptr<TensorImpl>Newly constructed tensor.
Raises
DtypeErrormake_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.
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>shapeShapebyte_ offsetstd::size_tbase's storage.Returns
std::shared_ptr<TensorImpl>The view, with no autograd state of its own.
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).
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>shapeShapestrideStrideoffset_ bytesstd::size_t= Nonebase's storage origin. Defaults to 0.join_ familybool= Nonebase's family. Defaults to true.Returns
std::shared_ptr<TensorImpl>New view tensor sharing base's storage.
Updates a GPU gradient slot's array while preserving any live .grad view-family handle attached to it.
Methods
92The phantom write rule (absorbs_phantom_write) for this tensor's gradient slot, held by every sink a gradient lands in — a leaf's AccumulateGrad, a retained gradient, accumulate_grad_impl — before it stores or adds anything.
Returns
booltrue for a phantom tensor: it takes any gradient and keeps no values, so an empty slot gets a phantom of this tensor's geometry (.grad then reads as one) and the caller stores nothing.
Raises
LucidErrorgrad a phantom — the first gradient included, which an empty slot would otherwise take as its own.Accumulates a graph-mode gradient.
First call sets the gradient outright; subsequent calls add g
into the existing grad_as_impl via the engine's add op
so the accumulation is differentiable when create_graph=true.
Parameters
gstd::shared_ptr<TensorImpl>Host/device base address of the start of the underlying allocation.
Total byte size of the underlying allocation (the user-facing storage capacity).
Byte offset of this tensor's first element from the start of the underlying allocation (the user-facing storage coordinate frame).
An in-place assignment as a user means it, as opposed to copy_from, which is the raw buffer write every internal caller uses — an optimiser step, a load_state_dict, the compiled executable's ghost-grad placeholders. Putting the graph handling into copy_from itself broke all of those at once, because the two want opposite things from the same function.
Here it is the assignment: the values are overwritten, so the
tensor's place in the graph becomes the source's where there is
one — that is what routes the gradient to z in
y.copy_(z) — and ends where there is not. A leaf that
requires grad is refused for the same reason every other in-place
op refuses one.
Increments the autograd version counter.
Called by every in-place op so autograd can detect mutations of
tensors that were saved for backward. The tensor's own count moves
whether or not it has an AutogradMeta yet. Having none does
not mean the tensor is outside every graph: w * buf saves buf
to differentiate with respect to w, though buf never required
grad. When the count stayed put for such a tensor, a CPU write into
its buffer (fill_, copy_, the index writes) went unseen, and
backward computed w's gradient from the values written after the
forward. The meta lives inside the tensor (std::optional), so
making one here allocates nothing, and a forward that writes nothing
never makes one.
A Metal shared buffer's counter moves too, because another alias of the same bytes may have been saved. So does a view family's, for the same reason.
Refuses, before anything is written, a tensor mutable_storage would refuse: a Metal view of part of its family's root. A writer that updates several tensors in one step checks them all first, so a refusal leaves every one of them — and the writer's own state — as it was.
Raises
NotImplementedErrorDrops the backward function pointer without removing the AutogradMeta.
Used when an in-place op detaches a tensor from the autograd graph
(tensor.detach_()-style semantics).
Returns a new TensorImpl sharing the same storage and view family with the requested requires_grad flag and no grad_fn.
Copies data from other into this tensor.
Supports CpuStorage↔CpuStorage,
GpuStorage↔GpuStorage, and all combinations
involving SharedStorage. Bumps the version counter on
success.
Parameters
otherTensorImplRaises
DeviceMismatchother.device() != device().DtypeMismatchother.dtype() != dtype().ShapeMismatchother.shape() != shape().copy_from, raising and refusing in the name of the op that asked: assign_from passes its caller's (fill_ may write an overlapping view, which a copy may not).
Whether this tensor reads every element of its family's root, in the root's order — a reshape of it rather than a part.
Returns a view of this TensorImpl used when a custom Function returns an input as-is (or returns the same tensor twice).
Returns the alias .data hands out: a member of this tensor's view family with detached_alias_ set so writes through it are untracked.
Returns the tensor data as a NumPy array.
For CPU tensors the returned array is a zero-copy view backed by a
py::capsule that keeps the TensorImpl alive. For
GPU tensors the data is synchronously downloaded to a temporary CPU
buffer first.
See Also
to_bytes — NumPy-free alternative.
tolist — nested Python list alternative.
Returns
py::objectA NumPy array (one of the supported dtypes, except for Dtype::C64 which is represented as a complex64 array).
Detaches this TensorImpl in place from the autograd graph so every holder of the same TensorImpl (including p.grad) sees grad_fn cleared and requires_grad set to false.
Returns the device tag. Backs the Python .device attribute.
Mutable device accessor — returns the underlying Device by reference. Rarely used outside engine internals; pairs with the public const device.
Notes
Same footgun as dtype_field: changing the device WITHOUT also
migrating storage_ to the new device produces a TensorImpl that
claims to live on one stream while its bytes live on another. Only
call from paths that have already moved the storage.
Drops every member's cached elements, and any value a member held before the family formed: the root is being replaced.
Stops tracking the shared buffer, folding its counter into this tensor's own so version() never moves backwards.
Returns the element dtype. Backs the Python .dtype attribute.
Mutable dtype accessor — returns the underlying Dtype by reference. Rarely used outside engine internals; pairs with the public const dtype.
Notes
Changing the dtype WITHOUT also reinterpreting / reallocating
storage_ is a footgun: the bytes on disk no longer match the
declared element type, so subsequent ops will read garbage. Only call
from paths that have already updated storage to match the new dtype.
Returns a pointer to the AutogradMeta, constructing it in-place if it does not yet exist. The returned pointer is stable (optional stores in-place) as long as autograd_ is not reset.
Forces evaluation of this tensor's lazy MLX computation graph.
GPU tensors: calls mlx::core::eval() on the underlying array,
materialising the buffer.
CPU tensors: no-op (CPU storage is always materialised).
Notes
Required before any path that wants to dereference the GPU buffer
through a CPU pointer (e.g. to_bytes, data_as_python).
Stable non-zero identity shared by every member of this tensor's view family (on both CPU and Metal), or 0 when the tensor belongs to no view family.
An identity shared by every member of this tensor's Metal view family and by nothing else — where autograd measures a member's elements from when it re-derives the family after a write — or nullptr when the tensor is not a Metal family member.
Whether this tensor reads a Metal view family's root.
int grad_as_impl()Returns the graph-mode gradient as a full TensorImpl.
Populated when backward(create_graph=True) was called — allowing
the gradient itself to participate in further autograd (Hessian-vector
products, MAML, second-order ops).
Returns
std::shared_ptr<TensorImpl>The graph-mode gradient tensor, or nullptr when no graph-mode gradient has been accumulated.
Returns the accumulated gradient as a NumPy array.
Identical download semantics to data_as_python.
Returns
py::objectThe gradient array, or py::none when no gradient has been computed yet.
Returns the backward function pointer for this tensor.
Returns
const std::shared_ptr<Node>&Non-null for non-leaf tensors produced by a differentiable op, nullptr for leaves and tensors with no AutogradMeta.
Returns the output index that connects this tensor to its grad_fn.
Multi-output ops produce several tensors that share one
Node; this index tells the backward pass which of the
node's outputs the tensor corresponds to.
Returns
std::uint32_tOutput index, or 0 when no AutogradMeta exists.
Returns the accumulated gradient Storage, if any.
Returns
const std::optional<Storage>&std::nullopt when no gradient has been accumulated, or when the tensor has no AutogradMeta at all.
int grad_to_tensor()Wraps the tensor's accumulated gradient as a TensorImpl that reads the underlying buffer.
Prefers the graph-mode grad_impl when present; otherwise wraps
the standard grad Storage directly. Replaces the
older NumPy round-trip used by the Python-side Tensor.grad
accessor. On the CPU the same impl comes back while it lives, and an
in-place op on it writes into the gradient (take_storage_from).
Returns
std::shared_ptr<TensorImpl>Tensor view of the gradient, or nullptr when no gradient has been accumulated.
Installs an assigned gradient tensor g and its validated slot buffer on this tensor, keeping g's Metal view family attached so subsequent in-place writes through .grad alias g.
Predicate: does another live tensor read this one's buffer as a view of it — a member of the same ViewFamily?
Such a tensor is written in place rather than handed a new storage
slot, so its views see the write (write_through). Unlike
storage_is_shared, a NumPy array or a storage saved for
backward does not count: neither is a view.
Returns
booltrue while at least one other member of the family is alive.
Predicate: does the current stride equal the row-major contiguous stride for this shape and dtype?
Returns
booltrue for canonically laid-out tensors; false for views that were transposed / sliced / otherwise reordered.
Predicate: is this tensor a view returned as an output of a tracked custom Function? In-place writes directly into such a view are refused while grad mode is enabled, and in-place writes into its base or another view of its family invalidate its grad_fn (LCD-558).
Predicate: contiguous, at the first byte of its storage, and covering all of it — the layout every backend kernel assumes when it reads a Storage from byte 0. A contiguous view at an offset, or one smaller than its buffer, is contiguous but not dense.
Predicate: is this a detached alias — what .data hands out — rather than a view autograd follows? Other clone_with_grad aliases are not detached: a factory's requires_grad=True tensor is one, and it has to stay an ordinary member once the temporary it aliased is gone.
It reads the same buffer as the rest of its family and shares their
version counter, but a write through it is untracked by design: the
values change and no graph is re-derived, as with the reference's
.data. Reshape-family views are not detached.
Returns the leaf-status flag.
A leaf tensor was constructed directly by the user; a non-leaf
tensor is the output of a differentiable op and carries a non-null
grad_fn.
Returns
booltrue for leaves, true (default) when there is no AutogradMeta yet.
Predicate: is this a phantom tensor — shape, dtype, device and autograd state, and a PhantomStorage in place of values?
Made by every factory and op while PhantomMode is on. A
phantom stays one after the mode is off: the kernels take it as an
input and give a phantom back, and a read of its values raises. It is
never a Metal family member, so its own storage is its storage.
Extracts a single-element tensor's value as a Python scalar.
GPU tensors are downloaded to CPU first. The Dtype::F16
IEEE-754 binary16 → float conversion is performed engine-side so
the Python wrapper avoids duplicating the bit-fiddling logic from
to_string.
Returns
py::objectint / float / bool / complex depending on dtype.
Raises
ValueErrornumel() != 1.A leaf's gradient hooks (autograd/TensorHooks.h), or null.
A Metal member that is the only one left takes its values as its own storage and leaves the family, so it behaves as any Metal tensor.
Returns a const reference to the TensorMeta bundle.
const SharedStorage * metal_shared()The Metal shared buffer this tensor still is, or nullptr.
Non-null only while the storage is the whole buffer, contiguous from
its first byte in its own dtype — the one case a relabel to the other
device can alias rather than copy. nullptr for views that cover
less, and once an op has replaced the storage.
Returns
const SharedStorage*The descriptor, or nullptr.
Returns a mutable reference to the gradient Storage, constructing the AutogradMeta on demand.
Used by the engine's gradient accumulation paths.
Returns
std::optional<Storage>&Mutable reference; the optional may still be empty even though AutogradMeta is now allocated.
The leaf-hook slot itself, constructing the AutogradMeta on demand — for registering the first hook.
Returns a mutable reference to the backing Storage variant.
Used by engine writers that replace the values wholesale (the C++ optimisers, BatchNorm's running statistics, the compiled step); callers are responsible for bumping the version counter when they mutate the underlying bytes. A Metal family member that reads its whole root hands out the root itself, reshaped to this tensor's shape, so what the caller writes is what every member reads.
Raises
NotImplementedErrorReturns the total byte footprint of the tensor's logical data.
Equal to numel() * dtype_size(dtype()); does not include any
extra storage capacity beyond the tensor's view.
Returns
std::size_tLogical byte size of the tensor data.
Returns the total number of elements (product of shape dimensions).
Returns
std::size_t1 for a scalar (empty shape).
The backing Storage as it is, before any packing: the whole buffer a view reads into, to be walked with stride and storage_offset. For a Metal family member, the family's root.
What reads of the values go through: a Metal member's elements, or the storage as it is (the CPU readers walk the geometry themselves).
Bumps old's version counter for an in-place rebind and carries the bumped version (and any saved-output version baseline on this tensor's grad_fn) onto this tensor.
Predicate: does a write into this tensor's family re-derive this member's graph (inplace::rebase_views)?
Not for a detached alias, which autograd does not follow, and not for a leaf that requires grad: gradient accumulates there, so the leaf keeps its place and only its values move.
Returns the current requires_grad flag.
Returns
boolfalse when there is no AutogradMeta at all (the common inference path — avoids an indirection).
Returns whether this tensor will retain its gradient even when it is a non-leaf.
Mirrors the reference framework's tensor.retain_grad() API.
Returns
booltrue when the engine should accumulate gradients into grad_storage for this tensor regardless of leaf status.
Returns a Storage snapshot suitable for saving this TensorImpl as an op's output on a backward node: on the CPU, splits CpuStorage::ptr onto a separate control block so subsequent in-place ops on this tensor are not refused as shared storage before the version check at backward.
Marks this tensor as a custom Function output view.
Marks this tensor as a detached alias; see is_detached_alias.
Sets the device field. Does not move data; used after an in-place device transfer has updated storage_.
Sets the dtype field. Does not touch the underlying bytes; used by pure metadata reinterpretation paths.
Attaches the backward function pointer.
Marks the tensor as non-leaf (callers usually pair this with
set_leaf(false)).
Parameters
fnstd::shared_ptr<Node>Sets the graph-mode gradient tensor.
Parameters
gstd::shared_ptr<TensorImpl>Sets the output index associated with grad_fn.
Parameters
nrstd::uint32_tNode.Stores a gradient Storage directly.
Parameters
gradStorageSets the leaf-status flag, constructing AutogradMeta if it does not yet exist.
Parameters
vboolis_leaf value.Enables or disables gradient tracking.
Constructs AutogradMeta lazily when v is true.
Setting to false does not destroy an already-existing
AutogradMeta — only the flag is cleared, so historical
version counters / saved tensors remain valid.
Parameters
vboolrequires_grad value.Toggles gradient retention for non-leaf tensors.
Parameters
vboolretain_grad value.Returns the shape vector. Backs the Python .shape attribute.
Private mutable references to the meta fields — used only within TensorImpl's own methods; callers should use the public set_* functions.
The backing Storage, as a dense row-major buffer of exactly this tensor's elements.
Nearly every op hands storage() straight to a backend kernel that
reads it as dense, from its first byte. A CPU view at an offset or
with strides read the wrong bytes there — about a hundred ops did, and
the audit's layout axis found them once it fed them real views. So a
non-dense CPU tensor answers with a packed copy of the elements it
reads, rebuilt whenever its version moves (a write through it or
through any member of its view family). Every other tensor answers
with its own buffer, as before.
A member of a Metal view family answers with its elements read out of the family's root, cached the same way — or with the root itself when it reads all of it, in the root's shape.
Code that walks the geometry itself — contiguous, the NumPy bridge,
data_ptr, buffer-identity and overlap tests — reads
raw_storage instead.
Predicate: is this tensor's CPU storage aliased by another tensor?
Returns
booltrue when the underlying CpuStorage ptr is referenced by more than one shared_ptr — i.e. at least one other view of this tensor exists. Always false for GpuStorage and SharedStorage variants (those variants do not currently report sharing through this hook).
Returns the byte offset of this tensor's first element from the start of raw_storage (the handle coordinate frame).
Rule: anything that shows a position or size to the user, or takes one
from the user (storage_offset, untyped_storage, as_strided),
uses the allocation coordinate frame (allocation_offset,
allocation_nbytes, allocation_base). storage_offset
and raw_storage are the handle frame for code that walks bytes.
Returns
std::size_t0 for owning tensors and block views; non-zero for strided views created with make_view.
Returns the byte-stride vector. Backs the Python .stride() method.
Mutable stride accessor — returns the underlying Stride by reference so engine-side ops can rewrite strides in place without going through the full set_meta path. Pairs with the public const stride.
Notes
Modifying the stride WITHOUT also touching storage_ or shape_
produces an invalid TensorImpl — only call this from internal kernel
code that holds the necessary invariants. Does NOT bump the version
counter; callers that change tensor geometry through this hook are
responsible for whatever bookkeeping autograd requires.
Take out's contents after an in-place op.
Ordinarily a swap of the storage slot — see
ops/utils/InplaceGraph.h for why the slot and not the buffer.
A tensor over a Metal shared buffer writes the new values into the
buffer instead, so every alias of it sees them — but only when out
recorded no graph, since a node that saved the pre-op input shares the
buffer. A CPU tensor with live views always writes into its buffer
(write_through), and so does one read from .grad
(grad_to_tensor), whose buffer is its owner's gradient.
Otherwise the swap happens and the tensor stops being shared.
Parameters
outTensorImpl&nameconst char*Raises
LucidErrorReturns the tensor data as a contiguous bytes blob, in row-major order.
GPU tensors are downloaded to a temporary CPU buffer first. No NumPy
dependency, by design — this path is what the serialization layer and
__repr__ use to avoid pulling NumPy into import lucid.
Returns
py::bytesRow-major bytes blob of length nbytes().
Renders the tensor data as a human-readable string suitable for __repr__.
The format roughly mirrors NumPy's array2string defaults but is
implemented entirely engine-side so neither Lucid nor its consumers
need NumPy.
Parameters
precisionint= None4.thresholdstd::size_t= Nonenumel > threshold, an edge-summary is rendered instead of the full data. Defaults to 1000.edgeitemsstd::size_t= None3.Returns
std::stringHuman-readable rendering of the tensor.
Converts the tensor to a nested Python list (or a Python scalar for 0-d tensors).
NumPy-free — mirrors item's dtype dispatch but walks the full
shape. Handles every supported dtype including Dtype::C64
(yields Python complex), so callers never need to fall back to
the NumPy bridge. GPU tensors are downloaded to a temporary
CpuStorage via the to_bytes snapshot path; the heavy
lifting then runs against a contiguous row-major byte buffer.
Returns
py::objectNested list of Python scalars, or a single scalar when the tensor is 0-d.
int transfer_to_device(Device target, bool requires_grad)Cross-device copy without round-tripping through Python or NumPy.
Dispatch summary:
- CPU → GPU: contiguous-snapshot the source view, then
gpu::upload_cpu_to_gpuinto a fresh GPU-private MLX array.- GPU → CPU:
gpu::download_gpu_to_cpuinto a freshCpuStorage. SharedStorageis handled by the separatetransfer_storageAPI (zero-copy relabel); calling this method on aSharedStoragetensor falls back to a contiguous CPU snapshot path.
- GPU → CPU:
Parameters
targetDevicerequires_ gradboolReturns
std::shared_ptr<TensorImpl>New tensor on target with the same shape and dtype.
Returns the autograd version counter.
Bumped by bump_version on every in-place mutation; autograd
snapshots this at forward time and checks it at backward time to
catch illegal mutations. A tensor over a Metal shared buffer adds the
buffer's own counter, which every alias of the buffer bumps — so a
write through the CPU view invalidates a GPU alias saved for backward,
and the other way round.
Returns
std::int64_tCurrent version. 0 for a tensor that nothing has written in place — no AutogradMeta exists until something does (bump_version) or the tensor joins a graph.
Returns a shared version counter that tracks every in-place version bump on this tensor (and its view family) even after this TensorImpl dies.
Predicate: does a view of this tensor keep its strides over the same buffer — the CPU's rule? A Metal tensor views through its family's root, or copies. A phantom follows its device, so its views have the strides, and a view the refusals, of the tensor it stands in for.
Predicate: was this tensor made, through any chain of views, from a live leaf that requires grad?
A write through such a view moves the leaf's values, so autograd refuses it as it refuses a write to the leaf itself. Only the bases the chain passes through count, as in the reference: writing the leaf's own base, or a view made beside the leaf, is allowed. A detached alias ends the chain, since its writes are untracked.
write_gpu_member
void write_gpu_member(const TensorImpl & src, const char * name, bool raw)Replaces the Metal family root with src's values laid into this member's elements, and bumps the version. raw is copy_from's write rather than an in-place op's: no autograd refusals, and with no graph to protect it writes into a Metal shared buffer the root reads.
Writes src's values into the shared buffer. False when this tensor no longer is the buffer or src does not match it.
Predicate: would an in-place op that records a graph write into the bytes this tensor reads now, rather than hand it a new storage slot?
The two cases take_storage_from writes through: a CPU tensor
with live views, and a tensor read from .grad whose write is let
into its gradient's buffer. An op that keeps a handle on the pre-write
tensor for backward needs to know, because a handle sharing those bytes
would read the new values back — and, for the gradient, would itself be
one more reader that has the write refused.
Returns
booltrue when the write lands in this tensor's current buffer.
Writes src's values into this tensor's buffer, in place.
The write an in-place op makes to a tensor with live views: into the bytes the views read, where a new storage slot would leave them reading the old ones. A Metal family member writes into the family's root instead. Bumps the version, which every member shares.
Parameters
srcTensorImplnameconst char*Raises
ShapeMismatchsrc has another shape.LucidErrorsrc has another dtype, a view of a leaf that requires grad would be modified while autograd records, the elements overlap and name is not fill_, or the buffer is a gradient's that something else also reads.NotImplementedErrorClears the accumulated gradient.
Sets grad to std::nullopt but leaves the
AutogradMeta itself in place so subsequent backward passes
continue to accumulate.