tinymind 0.3.8

Neural networks for embedded Rust: a no_std, allocation-free port of the tinymind (C++) library, bit-exact with it
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// NeuralNetwork over f32 and f64 against tinymind (C++)'s NeuralNetwork
// instantiated with float and double. tests/golden/float_mlp/stdout.txt is the
// output of scripts/cpp/float_mlp.cpp (built with -ffp-contract=off), which
// trains nine networks with tinymind (C++)'s floating-point activations and
// transfer-function bundle (learning rate 0.15, momentum 0.5, acceleration
// 0.1), five with SGD and four with its AdamOptimizerFloat and
// RmsPropOptimizerFloat (learning rate 0.01), plus four LstmNeuralNetwork
// runs (SGD and Adam) on short sequences, and hashes every weight after
// every step; scripts/compare-with-cpp.sh
// rebuilds it and checks the golden is current. tanh and exp come from the
// platform's math library on both sides (feature `std`), so on a glibc host
// every step is bit-identical. The committed golden holds the f64 lines only;
// see the end of the test.
#![cfg(feature = "std")]

use std::fmt::Write;

use tinymind::rand::GlibcRand;
use tinymind::recurrent::LstmNetwork;
use tinymind::{
    Activation, AdamFloat, Dense, Linear, NeuralNetwork, Optimizer, Relu, RmsPropFloat, Scalar,
    Sgd, Sigmoid, Tanh,
};

/// What the test needs from f32 and f64 beyond `Scalar`.
trait Float: Scalar + core::fmt::Debug {
    fn from_f64(x: f64) -> Self;
    fn from_i32(x: i32) -> Self;
    fn absorb(self, hash: &mut u32);
    fn bits(self) -> String;
    /// The C++ program's `ZeroToleranceCalculator` specialization: |x| < 0.004.
    fn is_within_zero_tolerance(self) -> bool;
}

impl Float for f64 {
    fn from_f64(x: f64) -> Self {
        x
    }
    fn from_i32(x: i32) -> Self {
        f64::from(x)
    }
    fn absorb(self, hash: &mut u32) {
        for b in self.to_bits().to_le_bytes() {
            *hash = (*hash ^ u32::from(b)).wrapping_mul(0x0100_0193);
        }
    }
    fn bits(self) -> String {
        format!("{:016X}", self.to_bits())
    }
    fn is_within_zero_tolerance(self) -> bool {
        self == 0.0 || (self < 0.004 && self > -0.004)
    }
}

impl Float for f32 {
    fn from_f64(x: f64) -> Self {
        x as f32
    }
    fn from_i32(x: i32) -> Self {
        x as f32
    }
    fn absorb(self, hash: &mut u32) {
        for b in self.to_bits().to_le_bytes() {
            *hash = (*hash ^ u32::from(b)).wrapping_mul(0x0100_0193);
        }
    }
    fn bits(self) -> String {
        format!("{:08X}", self.to_bits())
    }
    fn is_within_zero_tolerance(self) -> bool {
        self == 0.0 || (self < 0.004 && self > -0.004)
    }
}

/// Every weight in the C++ program's order: each layer's weights by source,
/// then its bias weights, from the input side.
trait AllWeights<V> {
    fn all_weights(&self) -> Vec<V>;
}

impl<V: Copy, A0, A1, const I: usize, const H: usize, const O: usize> AllWeights<V>
    for (Dense<V, A0, I, H>, Dense<V, A1, H, O>)
{
    fn all_weights(&self) -> Vec<V> {
        let mut w: Vec<V> = self.0.weights().iter().flatten().copied().collect();
        w.extend(self.0.bias_weights());
        w.extend(self.1.weights().iter().flatten().copied());
        w.extend(self.1.bias_weights());
        w
    }
}

impl<V: Copy, A0, A1, A2, const I: usize, const H1: usize, const H2: usize, const O: usize>
    AllWeights<V>
    for (
        Dense<V, A0, I, H1>,
        Dense<V, A1, H1, H2>,
        Dense<V, A2, H2, O>,
    )
{
    fn all_weights(&self) -> Vec<V> {
        let mut w: Vec<V> = self.0.weights().iter().flatten().copied().collect();
        w.extend(self.0.bias_weights());
        w.extend(self.1.weights().iter().flatten().copied());
        w.extend(self.1.bias_weights());
        w.extend(self.2.weights().iter().flatten().copied());
        w.extend(self.2.bias_weights());
        w
    }
}

struct Lcg(u32);

impl Lcg {
    fn next(&mut self) -> u32 {
        self.0 = self.0.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
        self.0
    }
}

/// The C++ program's sample for `IN` inputs (see its `sample`).
fn sample<V: Float, const IN: usize, const OUT: usize>(g: &mut Lcg) -> ([V; IN], [V; OUT]) {
    let mut x = [V::ZERO; IN];
    let mut t = [V::ZERO; OUT];
    if IN == 2 {
        let a = ((g.next() >> 16) & 1) as i32;
        let b = ((g.next() >> 16) & 1) as i32;
        x[0] = V::from_i32(a);
        x[1] = V::from_i32(b);
        t[0] = V::from_i32(a ^ b);
        return (x, t);
    }
    let mut v = [0i32; IN];
    for vi in &mut v {
        *vi = ((g.next() >> 8) % 65_536) as i32;
    }
    if IN == 3 {
        for (xi, &vi) in x.iter_mut().zip(&v) {
            *xi = V::from_i32(vi - 32_768) / V::from_i32(32_768);
        }
        t[0] = V::from_i32(v[0] - v[2]) / V::from_i32(65_536);
        t[1 % OUT] = V::from_i32(v[1] + v[2]) / V::from_i32(131_072);
        return (x, t);
    }
    for (xi, &vi) in x.iter_mut().zip(&v) {
        *xi = V::from_i32(vi) / V::from_i32(65_536);
    }
    let s = [v[0] + v[1], v[2] + v[3], v[0] + v[3]];
    let mut best = 0;
    for k in 1..3 {
        if s[k] > s[best] {
            best = k;
        }
    }
    for (o, to) in t.iter_mut().enumerate() {
        *to = V::from_i32(i32::from(o == best));
    }
    (x, t)
}

fn run<V, L, const IN: usize, const OUT: usize>(name: &str, seed: u32, steps: u32, out: &mut String)
where
    V: Float,
    L: tinymind::Layers<Value = V, Inputs = [V; IN], Outputs = [V; OUT]> + Default + AllWeights<V>,
    Sgd: Optimizer<V>,
{
    run_with::<V, L, Sgd, IN, OUT>(name, seed, steps, Sgd, 0.15, out);
}

/// One configuration with optimizer `opt` and learning rate `lr` (the C++
/// program gives the adaptive runs 0.01).
fn run_with<V, L, O, const IN: usize, const OUT: usize>(
    name: &str,
    seed: u32,
    steps: u32,
    opt: O,
    lr: f64,
    out: &mut String,
) where
    V: Float,
    L: tinymind::Layers<Value = V, Inputs = [V; IN], Outputs = [V; OUT]> + Default + AllWeights<V>,
    O: Optimizer<V>,
{
    let mut rand = GlibcRand::new(seed);
    let mut nn: NeuralNetwork<L, O> = NeuralNetwork::with_optimizer(
        || V::from_f64(f64::from(rand.next_i32()) / f64::from(i32::MAX) - 0.5),
        opt,
    );
    // tinymind (C++)'s floating-point transfer-function bundle.
    nn.set_learning_rate(V::from_f64(lr));
    nn.set_momentum_rate(V::from_f64(0.5));
    nn.set_acceleration_rate(V::from_f64(0.1));
    let mut g = Lcg(seed);
    let mut hash = 0x811C_9DC5u32;
    for step in 1..=steps {
        let (x, t) = sample::<V, IN, OUT>(&mut g);
        nn.feed_forward(&x);
        let error = nn.calculate_error(&t);
        nn.train(&t);
        for w in nn.layers().all_weights() {
            w.absorb(&mut hash);
        }
        if step % 500 == 0 {
            writeln!(
                out,
                "{name} step={step} error= {} hash=0x{hash:08X}",
                error.bits()
            )
            .unwrap();
        }
    }
    for k in 0..4 {
        let (x, _) = sample::<V, IN, OUT>(&mut g);
        nn.feed_forward(&x);
        write!(out, "{name} final{k}").unwrap();
        for y in nn.learned_values() {
            write!(out, " {}", y.bits()).unwrap();
        }
        writeln!(out).unwrap();
    }
}

/// The C++ program's `runLstm`: an LSTM network on sequences of 1 to 12 steps.
fn run_lstm<V, H, O, const IN: usize, const HID: usize, const OUT: usize, Opt>(
    name: &str,
    seed: u32,
    steps: i32,
    opt: Opt,
    lr: f64,
    out: &mut String,
) where
    V: Float,
    H: Activation<V>,
    O: Activation<V>,
    Sigmoid: Activation<V>,
    Tanh: Activation<V>,
    Opt: Optimizer<V>,
{
    let mut rand = GlibcRand::new(seed);
    let mut nn = LstmNetwork::<V, H, O, IN, HID, OUT, Opt>::with_optimizer(
        || V::from_f64(f64::from(rand.next_i32()) / f64::from(i32::MAX) - 0.5),
        opt,
    );
    nn.set_learning_rate(V::from_f64(lr));
    nn.set_momentum_rate(V::from_f64(0.5));
    nn.set_acceleration_rate(V::from_f64(0.1));
    let mut g = Lcg(seed);
    let mut hash = 0x811C_9DC5u32;
    let mut step = 0;
    while step < steps {
        let length = 1 + ((g.next() >> 8) % 12) as i32;
        let mut previous = 0;
        nn.reset_state();
        let mut k = 0;
        while k < length && step < steps {
            k += 1;
            step += 1;
            let mut v = [0i32; IN];
            for vi in &mut v {
                *vi = ((g.next() >> 8) % 65_536) as i32 - 32_768;
            }
            let x = v.map(|vi| V::from_i32(vi) / V::from_i32(32_768));
            let t: [V; OUT] = core::array::from_fn(|o| {
                let bit = if o == 0 {
                    previous > 0
                } else {
                    (previous > 0) != (v[0] > 0)
                };
                V::from_i32(i32::from(bit))
            });
            previous = v[0];
            nn.feed_forward(&x);
            let error = nn.calculate_error(&t);
            if step % 7 != 0 && !Float::is_within_zero_tolerance(error) {
                nn.train(&t);
            }
            for y in nn.learned_values() {
                y.absorb(&mut hash);
            }
            for w in nn.input_weights().iter().flatten().flatten() {
                w.absorb(&mut hash);
            }
            for w in nn.input_bias_weights().iter().flatten() {
                w.absorb(&mut hash);
            }
            for w in nn.recurrent_weights().iter().flatten().flatten() {
                w.absorb(&mut hash);
            }
            for w in nn.hidden_weights().iter().flatten() {
                w.absorb(&mut hash);
            }
            for w in nn.hidden_bias_weights() {
                w.absorb(&mut hash);
            }
            if step % 250 == 0 {
                writeln!(
                    out,
                    "{name} step={step} error= {} hash=0x{hash:08X}",
                    error.bits()
                )
                .unwrap();
            }
        }
    }
}

#[test]
fn float_networks_match_cpp() {
    let mut out = String::new();
    run::<f64, (Dense<f64, Tanh, 2, 4>, Dense<f64, Sigmoid, 4, 1>), 2, 1>(
        "f64 2-4-1 tanh/sigmoid",
        1,
        3000,
        &mut out,
    );
    run::<
        f32,
        (
            Dense<f32, Tanh, 2, 4>,
            Dense<f32, Tanh, 4, 4>,
            Dense<f32, Sigmoid, 4, 1>,
        ),
        2,
        1,
    >("f32 2-4-4-1 tanh/sigmoid", 2, 3000, &mut out);
    run::<
        f64,
        (
            Dense<f64, Relu, 4, 8>,
            Dense<f64, Relu, 8, 6>,
            Dense<f64, Sigmoid, 6, 3>,
        ),
        4,
        3,
    >("f64 4-8-6-3 relu/sigmoid", 3, 2000, &mut out);
    run::<f32, (Dense<f32, Tanh, 4, 8>, Dense<f32, Sigmoid, 8, 3>), 4, 3>(
        "f32 4-8-3 tanh/sigmoid",
        4,
        2000,
        &mut out,
    );
    run::<f64, (Dense<f64, Relu, 3, 6>, Dense<f64, Linear, 6, 2>), 3, 2>(
        "f64 3-6-2 relu/linear",
        5,
        2000,
        &mut out,
    );
    // tinymind (C++)'s floating-point Adam and RMSprop.
    run_with::<f64, (Dense<f64, Tanh, 2, 4>, Dense<f64, Sigmoid, 4, 1>), _, 2, 1>(
        "f64 2-4-1 tanh/sigmoid adam",
        6,
        2000,
        AdamFloat::default(),
        0.01,
        &mut out,
    );
    run_with::<
        f32,
        (
            Dense<f32, Tanh, 4, 8>,
            Dense<f32, Tanh, 8, 6>,
            Dense<f32, Sigmoid, 6, 3>,
        ),
        _,
        4,
        3,
    >(
        "f32 4-8-6-3 tanh/sigmoid adam",
        7,
        2000,
        AdamFloat::default(),
        0.01,
        &mut out,
    );
    run_with::<f64, (Dense<f64, Relu, 3, 6>, Dense<f64, Linear, 6, 2>), _, 3, 2>(
        "f64 3-6-2 relu/linear rmsprop",
        8,
        2000,
        RmsPropFloat::default(),
        0.01,
        &mut out,
    );
    run_with::<
        f32,
        (
            Dense<f32, Tanh, 2, 4>,
            Dense<f32, Tanh, 4, 4>,
            Dense<f32, Sigmoid, 4, 1>,
        ),
        _,
        2,
        1,
    >(
        "f32 2-4-4-1 tanh/sigmoid rmsprop",
        9,
        2000,
        RmsPropFloat::default(),
        0.01,
        &mut out,
    );
    // LSTM networks.
    run_lstm::<f64, Tanh, Sigmoid, 3, 6, 2, _>(
        "f64 lstm 3-6-2 tanh/sigmoid",
        10,
        1500,
        Sgd,
        0.15,
        &mut out,
    );
    run_lstm::<f32, Tanh, Sigmoid, 2, 4, 2, _>(
        "f32 lstm 2-4-2 tanh/sigmoid",
        11,
        1500,
        Sgd,
        0.15,
        &mut out,
    );
    run_lstm::<f64, Relu, Linear, 2, 5, 1, _>(
        "f64 lstm 2-5-1 relu/linear adam",
        12,
        1500,
        AdamFloat::default(),
        0.01,
        &mut out,
    );
    run_lstm::<f32, Tanh, Sigmoid, 2, 3, 2, _>(
        "f32 lstm 2-3-2 tanh/sigmoid adam",
        13,
        1500,
        AdamFloat::default(),
        0.01,
        &mut out,
    );
    // With TINYMIND_FLOAT_MLP_CPP set (scripts/compare-with-cpp.sh sets it),
    // every line is compared with the C++ program's output from the same
    // machine. Otherwise the f64 lines are compared with the committed golden:
    // the f32 ones depend on the C library's tanhf and expf, which glibc 2.41
    // replaced with correctly rounded versions, so they differ between hosts.
    match std::env::var("TINYMIND_FLOAT_MLP_CPP") {
        Ok(path) => {
            let cpp = std::fs::read_to_string(path).expect("the C++ program's output");
            assert_eq!(out, cpp);
        }
        Err(_) => {
            let f64_lines: String = out
                .lines()
                .filter(|l| l.starts_with("f64 "))
                .map(|l| format!("{l}\n"))
                .collect();
            assert_eq!(f64_lines, include_str!("golden/float_mlp/stdout.txt"));
        }
    }
}