Skip to content

Add mini-batch accumulation, mean reduction, and seeded shuffling (closes #42) - #53

Merged
w4ffl35 merged 1 commit into
masterfrom
fix/nir-minibatch
Oct 14, 2026
Merged

w4ffl35 merged 1 commit into
masterfrom
fix/nir-minibatch

Conversation

@w4ffl35

@w4ffl35 w4ffl35 commented Oct 14, 2026

Copy link
Copy Markdown
Member

Closes #42.

No-ABI-break path from the issue: the caller keeps looping over batch-1 tensors, and this module owns the reduction bookkeeping.

  • minibatch.h/minibatch.c: zero / per-sample gradient accumulate / 1/B scale over caller buffers, float64-accumulated mean loss, and seeded Fisher-Yates epoch shuffling (nir_sampler_arange + nir_sampler_shuffle) over nir_rng_t. Failure leaves accumulators untouched; NULL/empty/non-finite inputs fail.
  • Reduction contract documented in minibatch.h and pointed at from train.h/data.h (plus the stale Mini-batch training (gradient accumulation, batched loss reduction, seeded shuffling) #42 pointer in loss.h): batch loss/grad = mean of per-sample values, optimizers always see the mean gradient.
  • Batch-axis decision recorded in minibatch.h: no batch axis on nir_tensor_t; revisit only with measured evidence.
  • test_minibatch.c: accumulation matches summed affine-backward per-sample grads (float64 reference, plus the op's own repeated-call path bit-exact); mean loss/grad match single-sample cross-entropy; shuffling is seeded-reproducible with a pinned order on the tiny-MNIST fixture (seed 7 -> [1,2,0], labels read 3,9,7).

Ownership: minibatch module + tests + CMake registration only. No tape, checkpoint, driver, or example changes.

Local verification: Debug, Release, and ASan/UBSan builds each pass the full suite, 42/42 ctest.

…oses #42)

minibatch.h/minibatch.c: caller-loop helpers over batch-1 tensors -- zero, per-sample gradient accumulation, 1/B scaling, and float64-accumulated mean loss -- plus seeded Fisher-Yates epoch shuffling over nir_rng_t. Documents the mean-reduction contract (batch loss/grad = mean of per-sample values, optimizers always see the mean gradient) and records the #42 decision against a batch axis on nir_tensor_t.

test_minibatch.c: accumulation matches summed affine-backward per-sample grads (float64 reference and the op's own repeated-call path), mean loss/grad match single-sample cross-entropy, and shuffling is seeded-reproducible with a pinned order on the tiny-MNIST fixture.
@w4ffl35
w4ffl35 merged commit 8edadec into master Oct 14, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mini-batch training (gradient accumulation, batched loss reduction, seeded shuffling)

1 participant