Skip to content

Microbatch shape contract asserts in loss forward/backward #153

@LeoBuron

Description

@LeoBuron

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 <assert.h> 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 Loss API: unify forward+backward reduction semantics for microbatching with batch dimension #135 already documents the contract.

Out of scope

The umbrella's other sub-issues (DataLoader B > 1, layer batched ops, etc.).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions