micrograd.zig/src/nn.zig
2026-06-09 20:56:26 +03:00

252 lines
9.2 KiB
Zig

const std = @import("std");
const engine = @import("engine.zig");
// Karpathy's micrograd `nn` module, ported to Zig with the network shape known
// at comptime. The layer sizes are a comptime `[]const usize`, so every
// structural array (weights, neurons, layers, parameter list) has a fixed,
// comptime-known length. The ONLY runtime allocation happens inside engine's
// `Value`, which keeps the whole autograd graph in a static arena.
/// Small PRNG wrapper that yields uniform values in the unit interval (floats)
/// or the full range (ints). Held by value so each `MLP` seeds its own stream.
pub fn Random(comptime T: type) type {
return struct {
const Self = @This();
prng: std.Random.DefaultPrng,
pub fn init(seed: u64) Self {
return .{ .prng = std.Random.DefaultPrng.init(seed) };
}
pub fn uniform(self: *Self) T {
return switch (@typeInfo(T)) {
.float => self.prng.random().float(T),
.int => self.prng.random().int(T),
else => @compileError("Only integer and floating-point types are supported."),
};
}
};
}
/// A single neuron with `in` weights and a bias. Weights are initialised
/// uniformly in (-1, 1) (Karpathy's default); the bias starts at 0.
pub fn Neuron(comptime T: type, comptime in: usize) type {
const Value = engine.Value(T);
return struct {
const Self = @This();
/// Number of `*Value` parameters this neuron contributes (weights + bias).
pub const param_count = in + 1;
weights: [in]*Value,
bias: *Value,
pub fn init(rng: *Random(T)) Self {
var w: [in]*Value = undefined;
for (&w) |*wi| wi.* = Value.new(rng.uniform() * 2 - 1);
return .{ .weights = w, .bias = Value.new(0) };
}
/// Build a neuron from saved parameters (weights then bias), consuming
/// `param_count` entries from the front of `buf`. Pairs with `parameters`.
pub fn fromParams(buf: []const T) Self {
var w: [in]*Value = undefined;
inline for (0..in) |i| w[i] = Value.new(buf[i]);
return .{ .weights = w, .bias = Value.new(buf[in]) };
}
/// w·x + b, optionally passed through ReLU. `nonlin` is comptime so the
/// branch is resolved away — output neurons compile to the linear path.
pub fn forward(self: *const Self, x: [in]*Value, comptime nonlin: bool) *Value {
var sum = self.bias;
inline for (self.weights, x) |w, xi| {
sum = sum.add(w.mul(xi));
}
return if (nonlin) sum.relu() else sum;
}
/// Writes this neuron's parameters into `buf` and returns the count used.
pub fn parameters(self: *const Self, buf: []*Value) usize {
inline for (self.weights, 0..) |w, i| buf[i] = w;
buf[in] = self.bias;
return param_count;
}
};
}
/// A dense layer: `out` neurons, each taking `in` inputs.
pub fn Layer(comptime T: type, comptime in: usize, comptime out: usize) type {
const Value = engine.Value(T);
const N = Neuron(T, in);
return struct {
const Self = @This();
pub const param_count = out * N.param_count;
neurons: [out]N,
pub fn init(rng: *Random(T)) Self {
var ns: [out]N = undefined;
for (&ns) |*n| n.* = N.init(rng);
return .{ .neurons = ns };
}
/// Build a layer from saved parameters, consuming `param_count` entries.
pub fn fromParams(buf: []const T) Self {
var ns: [out]N = undefined;
inline for (0..out) |i| ns[i] = N.fromParams(buf[i * N.param_count ..]);
return .{ .neurons = ns };
}
pub fn forward(self: *const Self, x: [in]*Value, comptime nonlin: bool) [out]*Value {
var y: [out]*Value = undefined;
inline for (&self.neurons, 0..) |*n, i| y[i] = n.forward(x, nonlin);
return y;
}
pub fn parameters(self: *const Self, buf: []*Value) usize {
var n: usize = 0;
inline for (&self.neurons) |*neu| n += neu.parameters(buf[n..]);
return n;
}
};
}
/// A multi-layer perceptron. `sizes[0]` is the input width; each subsequent
/// entry is a layer's output width. Hidden layers use ReLU; the final layer is
/// linear (Karpathy's `nonlin = i != len-1`).
///
/// const Net = MLP(f32, &.{ 3, 4, 4, 1 });
/// var net = Net.init(alloc); // owns the engine Value arena
/// defer net.deinit();
/// const out = net.forward(.{ x0, x1, x2 }); // [1]*Value
pub fn MLP(comptime T: type, comptime sizes: []const usize) type {
if (sizes.len < 2) @compileError("MLP needs at least an input size and one layer.");
const Value = engine.Value(T);
const n_layers = sizes.len - 1;
// Layers have heterogeneous sizes, so they can't share a uniform array
// type. A comptime-built tuple holds one concrete `Layer(...)` per slot.
const LayersTuple = blk: {
var lt: [n_layers]type = undefined;
for (0..n_layers) |i| lt[i] = Layer(T, sizes[i], sizes[i + 1]);
break :blk std.meta.Tuple(&lt);
};
return struct {
const Self = @This();
pub const in_size = sizes[0];
pub const out_size = sizes[sizes.len - 1];
pub const param_count = blk: {
var c: usize = 0;
for (0..n_layers) |i| c += sizes[i + 1] * (sizes[i] + 1);
break :blk c;
};
layers: LayersTuple,
/// Initialise the engine Value arena and build weights.
pub fn init(alloc: std.mem.Allocator) Self {
const seed = 42;
Value.init(alloc);
var rng = Random(T).init(seed);
var self: Self = undefined;
inline for (0..n_layers) |i| {
self.layers[i] = @TypeOf(self.layers[i]).init(&rng);
}
return self;
}
/// Build the net in a fresh Value arena from a saved parameter array
/// (same layout as `parameters()`). Used by training loops that reset the
/// arena each step: keep weights as plain `[]T`, rematerialise per step.
pub fn fromParams(alloc: std.mem.Allocator, data: []const T) Self {
Value.init(alloc);
var self: Self = undefined;
var off: usize = 0;
inline for (0..n_layers) |i| {
const LT = @TypeOf(self.layers[i]);
self.layers[i] = LT.fromParams(data[off..]);
off += LT.param_count;
}
return self;
}
/// Tears down the engine Value arena (frees every Value ever created).
pub fn deinit(self: *Self) void {
_ = self;
Value.deinit();
}
pub fn forward(self: *const Self, x: [in_size]*Value) [out_size]*Value {
return self.forwardFrom(0, x);
}
// Comptime recursion threads the typed activation array through layers;
// `i` being comptime is what lets the array type shrink/grow per layer.
fn forwardFrom(self: *const Self, comptime i: usize, x: [sizes[i]]*Value) [out_size]*Value {
const nonlin = i != n_layers - 1;
const y = self.layers[i].forward(x, nonlin);
if (i == n_layers - 1) return y;
return self.forwardFrom(i + 1, y);
}
/// All trainable parameters as a fixed-size array (no allocation).
pub fn parameters(self: *const Self) [param_count]*Value {
var buf: [param_count]*Value = undefined;
var n: usize = 0;
inline for (0..n_layers) |i| n += self.layers[i].parameters(buf[n..]);
return buf;
}
pub fn zeroGrad(self: *const Self) void {
for (self.parameters()) |p| p.grad = 0;
}
};
}
test "param_count and parameters() length agree" {
const Net = MLP(f32, &.{ 3, 4, 4, 1 });
var net = Net.initSeed(std.testing.allocator, 42);
defer net.deinit();
// 4*(3+1) + 4*(4+1) + 1*(4+1) = 16 + 20 + 5 = 41
try std.testing.expectEqual(@as(usize, 41), Net.param_count);
try std.testing.expectEqual(@as(usize, 41), net.parameters().len);
}
test "forward produces the output-layer width" {
const Value = engine.Value(f32);
const Net = MLP(f32, &.{ 3, 4, 4, 1 });
var net = Net.initSeed(std.testing.allocator, 7);
defer net.deinit();
const x: [3]*Value = .{ Value.new(1), Value.new(-2), Value.new(3) };
const y = net.forward(x);
try std.testing.expectEqual(@as(usize, 1), y.len);
}
test "one gradient-descent step lowers the loss" {
const Value = engine.Value(f32);
const Backprop = engine.Backprop(Value);
const Net = MLP(f32, &.{ 2, 8, 1 });
var net = Net.initSeed(std.testing.allocator, 123);
defer net.deinit();
var bp = Backprop.init(std.testing.allocator);
defer bp.deinit();
const x: [2]*Value = .{ Value.new(0.5), Value.new(-0.5) };
const target: f32 = 1.0;
const loss0 = net.forward(x)[0].sub(target).pow(2);
net.zeroGrad();
bp.backprop(loss0);
const before = loss0.data;
for (net.parameters()) |p| p.data -= 0.01 * p.grad;
const after = net.forward(x)[0].sub(target).pow(2).data;
try std.testing.expect(after < before);
}