vkML 0.1.0

vkml.Tensor

The Python handle to a value in the computation graph.

class Tensorbindings/module.cpp:349

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.

ⓘ Note

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.

⚠ Warning

.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

shape(self) -> tuplebindings/module.cpp:352

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

size(self) -> intbindings/module.cpp:354

The total number of elements, as numpy.ndarray.size — not the shape, and not a method. Bound to C++ numel().

ndim

ndim(self) -> intbindings/module.cpp:353

dtype

dtype(self) -> vkml._vkml_core.dtypebindings/module.cpp:355

device

device(self) -> vkml._vkml_core.devicebindings/module.cpp:289

is_contiguous

is_contiguous(self) -> boolbindings/module.cpp:360

defined

defined(self) -> boolbindings/module.cpp:361

False for a default-constructed / cleared tensor (e.g. after zero_grad).

Views — no copy

reshape

reshape(self, shape: collections.abc.Sequence[int]) -> vkml._vkml_core.Tensorbindings/module.cpp:391

A view. Reinterprets the same storage with new extents; the element count must be unchanged. Takes a sequence, not varargs.

view

view(self, shape: collections.abc.Sequence[int]) -> vkml._vkml_core.Tensorbindings/module.cpp:394

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

T(self) -> vkml._vkml_core.Tensorbindings/module.cpp:399

A view with the last two axes exchanged — transpose(-2, -1). No copy, and the result is non-contiguous.

permute

permute(self, dims: collections.abc.Sequence[int]) -> vkml._vkml_core.Tensorbindings/module.cpp:396

transpose

transpose(self, dim0: int, dim1: int) -> vkml._vkml_core.Tensorbindings/module.cpp:398

squeeze

squeeze(self, dim: int) -> vkml._vkml_core.Tensorbindings/module.cpp:400

unsqueeze

unsqueeze(self, dim: int) -> vkml._vkml_core.Tensorbindings/module.cpp:401

broadcast_to

broadcast_to(self, shape: collections.abc.Sequence[int]) -> vkml._vkml_core.Tensorbindings/module.cpp:402

__getitem__

__getitem__(self, key: object) -> vkml._vkml_core.Tensorbindings/module.cpp:408

Basic slicing, producing a view. This is the Python spelling of C++ slice.

Copies and conversion

contiguous

contiguous(self) -> vkml._vkml_core.Tensorbindings/module.cpp:405

to

to(self, dtype: vkml._vkml_core.dtype) -> vkml._vkml_core.Tensorbindings/module.cpp:406

Converts dtype only. Does not move between devices.

astype

astype(self, dtype: vkml._vkml_core.dtype) -> vkml._vkml_core.Tensorbindings/module.cpp:407

Converts dtype. Bound to the same C++ function as to, so the two are interchangeable; astype is the NumPy spelling.

assign_

assign_(self, src: vkml._vkml_core.Tensor) -> Nonebindings/module.cpp:418

In-place overwrite. Used by optimizers; see the C++ docs for the mid-graph hazard.

Observation

numpy

numpy(self) -> numpy.ndarray[]bindings/module.cpp:382

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

item(self) -> floatbindings/module.cpp:381

The single element of a scalar tensor, as a Python float. Realizes the graph.

Autograd

requires_grad

requires_grad(self) -> boolbindings/module.cpp:412

grad

grad(self) -> vkml._vkml_core.Tensorbindings/module.cpp:414

backward

backward(self) -> Nonebindings/module.cpp:415

Accumulate gradients into every leaf that requires grad.

detach

detach(self) -> vkml._vkml_core.Tensorbindings/module.cpp:421

A view of the same data that carries no gradient history.

realize

realize(self) -> vkml._vkml_core.Tensorbindings/module.cpp:376

Maths, in method form

abs

abs(self) -> vkml._vkml_core.Tensorbindings/module.cpp:508

exp

exp(self) -> vkml._vkml_core.Tensorbindings/module.cpp:501

log

log(self) -> vkml._vkml_core.Tensorbindings/module.cpp:502

sqrt

sqrt(self) -> vkml._vkml_core.Tensorbindings/module.cpp:503

matmul

matmul(self, other: vkml._vkml_core.Tensor) -> vkml._vkml_core.Tensorbindings/module.cpp:511

relu

relu(self) -> vkml._vkml_core.Tensorbindings/module.cpp:500

gelu

gelu(self) -> vkml._vkml_core.Tensorbindings/module.cpp:506

sigmoid

sigmoid(self) -> vkml._vkml_core.Tensorbindings/module.cpp:505

silu

silu(self) -> vkml._vkml_core.Tensorbindings/module.cpp:507

tanh

tanh(self) -> vkml._vkml_core.Tensorbindings/module.cpp:504

softmax

softmax(self, dim: int = -1) -> vkml._vkml_core.Tensorbindings/module.cpp:509

log_softmax

log_softmax(self, dim: int = -1) -> vkml._vkml_core.Tensorbindings/module.cpp:510

Reductions, in method form

sum

sum(self, dim: object | None = None, keepdim: bool = False) -> vkml._vkml_core.Tensorbindings/module.cpp:476

mean

mean(self, dim: object | None = None, keepdim: bool = False) -> vkml._vkml_core.Tensorbindings/module.cpp:482

max

max(self, dim: object | None = None, keepdim: bool = False) -> vkml._vkml_core.Tensorbindings/module.cpp:488

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

min(self, dim: object | None = None, keepdim: bool = False) -> vkml._vkml_core.Tensorbindings/module.cpp:494

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.

See also Tensor, tensor, realize, backward, detach

vkML — Vulkan-first machine learning in C++20. Apache-2.0. Signatures on this page are generated from the installed module.