Module Kaun
Neural networks as typed parameter records.
Kaun has no layer or trainer abstraction. A layer is a plain record of tensors with an apply function; a model is a record of layers with hand-written one-line traversals (Nx.Ptree.S plus checkpoint names, see Linear for the pattern); a training step composes Rune.value_and_grad with a Vega optimizer update; the training loop is ordinary Seq iteration over Data minibatches.
let step (params, ostate) (x, y) =
let loss p = Loss.softmax_cross_entropy_sparse (Model.apply p x) y in
let l, grads = Rune.value_and_grad (module Model) loss params in
let params, ostate =
Vega.adamw_step (module Model) ~lr:1e-3 ostate ~params ~grads
in
((params, ostate), Nx.item [] l)Random initialization and shuffling draw from the implicit RNG scope; wrap the program in Nx.Rng.run for reproducibility. Randomness inside a jitted training step (Dropout) instead takes an explicit Rune.Rng key threaded through the step's inputs.
Layers
Parameterized building blocks. Each module pairs a parameter record with constructors (Linear.init, Linear.make), an apply function, and the traversals that make it compose into differentiable, checkpointable models.
module Linear : sig ... endDense (fully connected) layers, and the model-as-record pattern.
module Embedding : sig ... endToken-id to dense-vector lookup tables.
module Conv : sig ... end2-D convolution layers (NCHW).
module Attention : sig ... endMulti-head self-attention, and the pure Attention.scaled_dot_product_attention core.
module Layer_norm : sig ... endLayer normalization over the feature axis.
module Batch_norm : sig ... endBatch normalization, with running statistics as explicit state.
Stateless functions
Pure operations with no parameters: activations, pooling, dropout.
module Fn : sig ... endActivation functions (relu, gelu, softmax, ...).
module Pool : sig ... end2-D max and average pooling.
module Init : sig ... endWeight initializers (Glorot, He, LeCun, variance scaling).
Training
module Loss : sig ... endScalar training objectives: regression and classification losses.
module Data : sig ... endMinibatch Seq.ts over in-memory tensors, with shuffling.
module Metric : sig ... endEvaluation metrics: accuracy, precision/recall/F1, AUC-ROC.
Persistence
module Checkpoint : sig ... endSave and load parameter structures as safetensors checkpoints.