vkml.Tensor
The Python handle to a value in the computation graph.
Defined in C++ and exposed through nanobind, so this page is generated by introspecting the built extension and every signature below is the one the installed module actually has. Member links point at bindings/module.cpp — the binding site, which is where the Python name is chosen and therefore the only place a rename is visible.
The semantics — lazy evaluation, views sharing storage, cheap handle copies — are the C++ ones, and are described on the Tensor page. What follows is the surface as Python sees it, and the places where the two disagree.
Shape arguments are sequences, not varargs. x.reshape([3, 2]) and x.view([3, 2]) are correct; the PyTorch spelling x.reshape(3, 2) raises TypeError. This applies to reshape, view, broadcast_to and permute.
to() converts dtype, not device. There is no device-transfer method on either surface — Tensor::to has a single DType overload. Place a tensor by passing device= when you create it, as the MNIST example does: V.tensor(xb_np, device=device). astype is bound to the same C++ function as to and is an exact alias.
.size is the element count, not the shape, following NumPy rather than PyTorch. It is a property, so x.size() raises TypeError: 'int' object is not callable, and x.size(0) is not available. The C++ class has a different function of the same name — Tensor::size(int axis), a per-axis extent — which is not bound here. Use x.shape for the shape and x.shape[0] for one axis. The rename happens at bindings/module.cpp:338, where Python's size is bound to C++ numel; numel itself is not a Python name.
Shape and layout¶
shape¶
Extents as a tuple, outermost axis first. This is the member to reach for when porting PyTorch code that calls x.size() or x.size(0).
strides¶
Strides in bytes, as NumPy reports them. A list, not a tuple.
size¶
The total number of elements, as numpy.ndarray.size — not the shape, and not a method. Bound to C++ numel().
ndim¶
dtype¶
device¶
is_contiguous¶
defined¶
False for a default-constructed / cleared tensor (e.g. after zero_grad).
Views — no copy¶
reshape¶
A view. Reinterprets the same storage with new extents; the element count must be unchanged. Takes a sequence, not varargs.
view¶
A view, and an exact alias of reshape: both are bound to C++ reshape, so unlike PyTorch there is no contiguity precondition that distinguishes them. Takes a sequence.
T¶
A view with the last two axes exchanged — transpose(-2, -1). No copy, and the result is non-contiguous.
permute¶
transpose¶
squeeze¶
unsqueeze¶
broadcast_to¶
__getitem__¶
Basic slicing, producing a view. This is the Python spelling of C++ slice.
Copies and conversion¶
contiguous¶
to¶
Converts dtype only. Does not move between devices.
astype¶
Converts dtype. Bound to the same C++ function as to, so the two are interchangeable; astype is the NumPy spelling.
assign_¶
In-place overwrite. Used by optimizers; see the C++ docs for the mid-graph hazard.
Observation¶
numpy¶
Copies to a new numpy.ndarray, realizing the graph first. This is the principal way data leaves the framework, and it is always a copy — the array does not alias tensor storage, so writing to it is safe and has no effect on the tensor.
item¶
The single element of a scalar tensor, as a Python float. Realizes the graph.
Autograd¶
requires_grad¶
grad¶
backward¶
Accumulate gradients into every leaf that requires grad.
detach¶
A view of the same data that carries no gradient history.
realize¶
Maths, in method form¶
abs¶
exp¶
log¶
sqrt¶
matmul¶
relu¶
gelu¶
sigmoid¶
silu¶
tanh¶
softmax¶
log_softmax¶
Reductions, in method form¶
sum¶
mean¶
max¶
Maximum over dim, or over every element when dim is None. Returns a Tensor — unlike torch.max, it does not return a (values, indices) pair, and there is no argmax on this surface. Equivalent to the free function amax.
min¶
Minimum over dim, or over every element when dim is None. Returns a Tensor, with no indices — see max. Equivalent to the free function amin.