Skip to content

v18.1 — Q8.8 training works again: fixed-point SGD steps round to nearest

Choose a tag to compare

@danmcleran danmcleran released this 10 Nov 23:52
· 34 commits to master since this release
6013e5f

v18.1 — Q8.8 training works again: fixed-point SGD steps round to nearest

Fix

  • Fixed-point SGD no longer drifts downward. The learningRate x gradient step was rounded with the format's own rounding policy. The default TruncatePolicy floors, so a step smaller than one LSB became 0 when positive and a full -1 LSB when negative, and every weight with a small gradient drifted down by about half an LSB per training step. In Q8.8 that swamps the gradient: after v18.0 removed a momentum bug that had been masking it, the xor example built in Q8.8 learned XOR from only 25 of 100 seeds (2 of 100 at learning rate 0.1 with momentum 0.9). It now learns from 100 of 100 in both settings, and from 99 of 100 with no momentum. (#267)
  • The gradient step now rounds to nearest, with ties away from zero, at every SGD update: NeuralNetwork weights, gated weights and biases, KAN, and the standalone Conv1D, Conv2D, DepthwiseConv2D, PointwiseConv2D, SelfAttention1D, BatchNorm1D, BinaryDense and TernaryDense layers. The momentum and acceleration terms still round toward zero, as in v18.0, so a delta that no gradient refreshes decays to exactly zero.
  • Rounding the gradient step toward zero was tried first and rejected: its one-LSB dead zone stalled recurrent Q16.16 training. Rounding to nearest halves the dead zone and keeps the step symmetric in sign.
  • New header cpp/include/tinymind_rounding.hpp holds multiplyRoundNearest and multiplyRoundTowardZero; the QValue overloads live in qformat.hpp. Floating-point training and int8 inference are bit-identical to v18.0.

Examples

  • har_activity: the learning rate goes from 0.05 to 0.1. The old value had been tuned against the downward drift; with unbiased steps it left 8 of 10 seeds at chance. At 0.1, mean test accuracy over seeds 1 to 10 is 0.90 ± 0.10 with no seed at chance, against 0.76 ± 0.16 with three at chance before. The default seed scores 76.5% (it was 92.5%, the best of the old seeds), and the docs report both numbers.
  • dqn_maze: the network's Q values are now within 0.0005 RMS of the exact optimum Q*, down from 0.0017.
  • The other fixed-point examples move within seed noise (10-seed means): predictive_maintenance on the real AI4I CSV gives precision / recall / F1 of 0.65 / 0.80 / 0.71 (was 0.67 / 0.78 / 0.72). Examples that use RoundUpPolicy (lstm_sinusoid, lstm_sinusoid_float, elman_temporal_xor) are bit-identical.

Tests

  • unit_test/nn/sgd_step_rounding_tests.hpp drives exact gradients through every standalone layer, and checks that every LSTM, GRU, self-attention and KAN parameter moves by exactly its gradient step rounded to nearest. A Q8.8 xor case must learn from 20 of 20 seeds. Planting either the old floor or a toward-zero step fails a test at every update site.

Docs

  • docs/neural-networks.md documents the rounding rules; the affected example pages, READMEs and 15 plots are re-measured and regenerated.
  • The README project tree lists the new header. (#268)

Documentation, architecture guides, and example gallery: https://danmcleran.github.io/tinymind/

Full changelog: v18.0...v18.1