vkML 0.1.0

Module

Base class for layers: named parameters, buffers and children.

class Modulepython/vkml/nn.py:47

Written in Python rather than C++ on purpose. A Module holds references and iterates dicts, none of which is hot; the hot part is the tensor operators it calls, and those are already in C++.

The structure — named children plus named parameters, recursive with a dotted prefix — exists so parameter names line up with a state_dict for loading and comparison. It mirrors torch.nn closely enough that PyTorch code reads across unchanged.

Assigning a Tensor attribute makes a parameter, and assigning a Module makes a child. That is done in __setattr__, so self.weight = ... registers without any explicit call. register_buffer is the explicit opt-out for state that must travel with the module but must not be trained.

Construction

__init__

def __init__selfpython/vkml/nn.py:50

register_buffer

def register_bufferself, name: str, tensor: V.Tensor -> Nonepython/vkml/nn.py:81

Records persistent state that is not trained. A buffer appears in state_dict, so it saves, loads and interoperates with a torch checkpoint — but never in parameters(), so an optimiser cannot see it. Batch normalisation's running statistics are the motivating case: they carry no gradient, and letting an optimiser "train" them would destroy the estimate. Raises if the tensor has requires_grad.

Traversal

named_parameters

def named_parametersself, prefix: str='' -> Iterator[tuple[str, V.Tensor]]python/vkml/nn.py:101

parameters

def parametersself -> Iterator[V.Tensor]python/vkml/nn.py:107

named_buffers

def named_buffersself, prefix: str='' -> Iterator[tuple[str, V.Tensor]]python/vkml/nn.py:111

named_modules

def named_modulesself, prefix: str='' -> Iterator[tuple[str, 'Module']]python/vkml/nn.py:117

Yields self first, then children depth-first, so a caller can match on the root as well as the leaves.

State

state_dict

def state_dictself -> dict[str, _np.ndarray]python/vkml/nn.py:122

Parameters and buffers, by dotted name. Buffers are included because that is what makes a checkpoint complete: a batch-normalised model restored without its running statistics evaluates against the wrong distribution while looking perfectly healthy.

load_state_dict

def load_state_dictself, state: dict[str, _np.ndarray] -> Nonepython/vkml/nn.py:134

Copies values in by name, in place, preserving each entry's device, dtype and requires_grad. Every key must match — a missing or unexpected one raises rather than being ignored.

Placement and mode

to

def toself, device -> 'Module'python/vkml/nn.py:200

Moves every parameter and buffer to a device, in place.

Call this before constructing an optimiser. The optimiser captures the parameter list when it is built, and to replaces each parameter with a new tensor — so an optimiser made first would keep updating the old ones while the model used the new. torch has the same ordering constraint for the same reason.

Transfer goes through the host, because that is what a transfer to a discrete device is. Gradients move with their parameters; dropping them would leave a subsequent optimiser step silently updating nothing.

train

def trainself, mode: bool=True -> 'Module'python/vkml/nn.py:227

Sets training mode recursively. Only Dropout and BatchNorm2d behave differently between modes.

eval

def evalself -> 'Module'python/vkml/nn.py:233

train(False).

zero_grad

def zero_gradself -> Nonepython/vkml/nn.py:196

Clears every parameter's gradient by assigning an undefined tensor. Required between steps, because backward accumulates.

Interface

forward

def forwardself, *args, **kwargspython/vkml/nn.py:238

__call__

def __call__self, *args, **kwargspython/vkml/nn.py:241

__repr__

def __repr__self -> strpython/vkml/nn.py:244

Attribute plumbing

__setattr__

def __setattr__self, name, valuepython/vkml/nn.py:58

Routes a Tensor into _parameters and a Module into _modules, which is what makes self.weight = ... register without a call.

__getattr__

def __getattr__self, namepython/vkml/nn.py:67

Only invoked when normal lookup fails, so parameters, buffers and children resolve without shadowing real attributes.

See also backward, save_module, load_module

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