BatchNorm1d / Reduce: float32 sums over batch×time use one sequential accumulator

Open
#548 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
54/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c

Research direction

Read meanOverTrailingAxesFloat32 and varianceBiasedOverTrailingAxesFloat32 in src/arithmetic/Reduce.c, then inspect bnBackward in src/layer/BatchNorm1d.c. Compare pairwise or blocked float32 summation approaches and check the existing BatchNorm and Reduce tests against their current tolerances; done means the sums use the chosen approach while those tests still pass.

Written by the indexing model from the issue text.

Description

Problem

BatchNorm1d sums over batch × time with one float32 accumulator, element by element:

  • forward statistics: meanOverTrailingAxesFloat32 and varianceBiasedOverTrailingAxesFloat32 in src/arithmetic/Reduce.c (one float acc per channel);
  • backward: sumDy / sumDyXhat in src/layer/BatchNorm1d.c (bnBackward, same pattern).

For a TC-ResNet8 stem on 12-class keyword spotting (batch 100, T = 98) that is N = 9,800 terms per channel. The rounding error of a running float32 sum grows with N (worst case ~N·ε, typically ~√N·ε), while a pairwise or blocked sum grows with log N.

Evidence

Same start, C vs a PyTorch float32 twin of the same model (identical initial weights, data order, Dropout off):

  • step 1 losses agree to < 1e-4 relative;
  • the per-step loss then drifts above 1e-4 relative at step 4–13 (4 seeds), while PyTorch float32 vs float64 drifts only at step 7–61;
  • a PyTorch twin that copies C's sequential BN summation reproduces C's departure steps, so the BN sums are the main source.

The effect on final accuracy is within seed noise (10-seed comparison), so this is precision, not a correctness bug.

Options

  1. Pairwise (or fixed-size blocked) summation in the Reduce float32 kernels and the BN backward sums. Same op count, a small stack or loop change; helps every Reduce user (LayerNorm, GroupNorm, PPCA replay).
  2. Kahan summation: ~4× the adds per element; costly on an MCU.
  3. Leave as is and document it.

Option 1 looks like the right cost/benefit for an MCU. Any change re-captures no goldens by itself; the existing BN/Reduce tests must stay within their tolerances.

Dominant language
C
Stars
2
Forks
3
Avg merge
13h 6m
Merged PRs (30d)
9

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from es-ude/OnDeviceTraining

All issues in es-ude/OnDeviceTraining

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.