es-ude / es-ude/OnDeviceTraining
Microbatch shape contract asserts in loss forward/backward
- Dominant language
- C
- Stars
- 1
- Forks
- 3
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 8
Description
Sub-issue of #152.
Add debug-only `assert(modelOutput->shape->dimensions[0] >= 1)` at the entry of each loss family's forward and backward (MSE, CrossEntropy, plus future loss types). The assert documents the microbatch-shape contract executably and protects against `1/0` divisions when MEAN reduction encounters an empty microbatch.
## Why deferred from #135
Under B = 1 only, the assert is effectively a no-op: today every caller passes a shape with `dimensions[0] = 1` either implicitly or explicitly. The protective value materialises only when B > 1 becomes real, alongside the rest of #152's work. Bundling here keeps the doc-side microbatch contract (already shipped with #135 in `docs/CONVENTIONS.md`) and the runtime enforcement together.
## Implementation sketch
- `#include ` in `src/loss_functions/MSE.c` and `src/loss_functions/CrossEntropy.c`
- One-line assert at the top of each:
- `mseLossForwardFloat`, `mseLossForwardSymInt32`
- `mseLossBackwardFloat`, `mseLossBackwardSymInt32`
- `crossEntropyForwardFloat`
- `crossEntropySoftmaxBackwardFloat`, `crossEntropySoftmaxBackwardAsym`
- No new tests (Unity has no death-test framework). Doxygen note added in #135 already documents the contract.
## Out of scope
The umbrella's other sub-issues (DataLoader B > 1, layer batched ops, etc.).
Contributor guide
Assessment
This issue has not been assessed yet.