skip to content
==> tests/cubic.c <==
1/* A golden digest from the Rust (mesh-ref lib.rs): the samples, a 64-point grid, sin_pi over 200 000
2 * inputs, 300 SGD steps (sums, loss, parameters, the cubic at a point) and a warp sum. */
3#include "mesh/ref/cubic.h"
4
5#include "mesh/abi/view_train.h"
6#include "mesh/ftz.h"
7
8#include <stdio.h>
9
10static uint64_t fh(uint64_t h, uint64_t w) { return (h ^ w) * 0x100000001b3ull; }
11
12static uint32_t mix(uint32_t x) {
13    x ^= x >> 16;
14    x *= 0x7feb352du;
15    x ^= x >> 15;
16    x *= 0x846ca68bu;
17    return x ^ (x >> 16);
18}
19
20static uint32_t gen(uint32_t m, uint32_t i) {
21    uint32_t h = mix((i * 0x9e3779b9u) ^ (m * 0x85ebca6bu));
22    uint32_t h2 = mix(h ^ 0x27d4eb2fu);
23    if (m == 0) return h;
24    if (m == 1) return (h & 0x80000000u) | ((h2 % 41) << 23) | (h & 0x007fffffu);
25    return (h & 0x80000000u) | ((50 + h2 % 31) << 23) | (h & 0x007fffffu);
26}
27
28int main(void) {
29    uint64_t h = 0xcbf29ce484222325ull;
30    float xs[MESH_TRAIN_SAMPLES], ys[MESH_TRAIN_SAMPLES], g[64];
31    mesh_cubic_samples(xs, ys);
32    for (uint32_t i = 0; i < MESH_TRAIN_SAMPLES; i++) h = fh(h, mesh_ftz_bits(xs[i]));
33    for (uint32_t i = 0; i < MESH_TRAIN_SAMPLES; i++) h = fh(h, mesh_ftz_bits(ys[i]));
34    mesh_cubic_grid(64, g);
35    for (int i = 0; i < 64; i++) h = fh(h, mesh_ftz_bits(g[i]));
36    for (uint32_t i = 0; i < 100000; i++) {
37        h = fh(h, mesh_ftz_bits(mesh_cubic_sin_pi(mesh_ftz_from_bits(gen(i % 3, i)))));
38        h = fh(h, mesh_ftz_bits(mesh_cubic_sin_pi(((float)i - 50000.0f) / 20000.0f)));
39    }
40    float p[4] = {0.1f, -0.2f, 0.3f, 0.05f}, s[5];
41    for (uint32_t step = 0; step < 300; step++) {
42        mesh_cubic_step_sums(p, xs, ys, step, s);
43        for (int k = 0; k < 5; k++) h = fh(h, mesh_ftz_bits(s[k]));
44        h = fh(h, mesh_ftz_bits(mesh_cubic_loss(s, 1.0f / 32.0f)));
45        mesh_cubic_apply(p, s, 0.005f, 1.0f / 32.0f);
46        for (int k = 0; k < 4; k++) h = fh(h, mesh_ftz_bits(p[k]));
47        h = fh(h, mesh_ftz_bits(mesh_cubic(p, mesh_ftz_from_bits(gen(step % 3, step)))));
48    }
49    float v[32];
50    for (uint32_t l = 0; l < 32; l++) v[l] = mesh_ftz_from_bits(gen(1, l));
51    h = fh(h, mesh_ftz_bits(mesh_cubic_warp_sum(v)));
52    if (h != 0xc34d0ef23c7cd3f0ull) {
53        fprintf(stderr, "cubic %#018llx, want 0xc34d0ef23c7cd3f0\n", (unsigned long long)h);
54        return 1;
55    }
56    return 0;
57}