Skip to main content

module nn

The module system: Module (one forward), Sequential (module chaining), Linear (an affine layer over held weights), and VarBuilder: dotted-key weight loading from a checkpoint, with pp scoping into sub-modules.

use clika_rt::nn::{Linear, Module, VarBuilder};
let vb = VarBuilder::from_safetensors(&api, "model.safetensors")?;
let fc1 = Linear::new(&vb.pp("fc1"))?;
let fc2 = Linear::new(&vb.pp("fc2"))?;
let x = api.tensor_full(&[1, 32], 1.0, f32_)?;
let y = x.apply(&fc1)?.apply(&fc2)?;

Linear (struct)

An affine layer: y = x · Wᵀ (+ b). Holds its weights; every forward reads them through fresh shared handles, so the layer serves any number of calls.

Implements: Module

from_tensors

fn from_tensors(weight: Tensor, bias: Option<Tensor>) -> Self

Build from explicit tensors.

new

fn new(vb: &VarBuilder) -> Result<Self>

Load weight (and bias, when the checkpoint carries it) from a VarBuilder scope: Linear::new(&vb.pp("fc1")).

Sequential (struct)

Runs its modules in order, feeding each output into the next.

Implements: Default, Module

add

fn add(self, module: impl Module) -> Self

Append a module; returns self so construction chains.

is_empty

fn is_empty(self) -> bool

len

fn len(self) -> usize

new

fn new() -> Self

VarBuilder (struct)

Dotted-key weight access over a loaded checkpoint. pp narrows the key scope (vb.pp("decoder").pp("fc1").get("weight") reads decoder.fc1.weight); scopes share one underlying checkpoint, so pp is cheap and a builder clones freely.

Implements: Clone

contains

fn contains(self, name: &str) -> Result<bool>

Whether the checkpoint carries <prefix>.<name>.

from_container

fn from_container(container: TensorsContainer) -> Self

Wrap an already-open checkpoint.

from_safetensors

fn from_safetensors(api: &Api, path: &str) -> Result<Self>

Open a safetensors checkpoint on the default placement.

get

fn get(self, name: &str) -> Result<Tensor>

The tensor at <prefix>.<name>.

pp

fn pp(self, name: &str) -> VarBuilder

A child scope: keys resolve under <prefix>.<name>.

Module (trait)

A neural-network building block: one forward from input to output. The input rides a shared reference; a forward never invalidates its argument.