refactor(optimizer)!: unify SGD step arms + updateMath knob (#308, #310)#313
Merged
Conversation
…ateless factory, plain-SGD arm deleted (#308) - sgdStepM: momentumFactor==0 runs one stateless update op per parameter and never reads optim->states (may be NULL) - sgdMCreateOptim: allocates no momentum-state buffers at momentumFactor==0 (optim->states == NULL); freeOptimSgdM handles the no-states case; the ten duplicated per-layer state-building blocks DRYed into one uniform post-loop - delete unreachable sgdStep + SGD enum value (verified unserialized); its three direct test callers (#279 dead-zone fixtures) ported byte-identically Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…GD update ops, FLOAT32 default, fail-fast otherwise (#310) - sgdMCreateOptim gains a by-value arithmetic_t updateMath (7th param, mirrors the layer-side per-op knobs), stored on sgd_t and fed into all three update opSpecs (previously hardcoded {ARITH_FLOAT32, HALF_AWAY}) - anything but ARITH_FLOAT32 fails fast at creation (sgdInit) AND step time (sgdStepM entry; the update kernels raw-cast float*) — both death-tested - mechanical sweep: 48 test + 9 example call sites, zero numeric movement; 7-arg _Static_assert signature contract updated - rounding ownership documented at the #282 seam (prologue = knob, inert for FLOAT32; OUT_WRITE epilogue = target qConfig, the #279 SR escape) - FEATURES.md optimizer section corrected; stale pre-#284 comments swept; mem_instrument guards states==NULL; momentumFactor creation-fixed doc note Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
084a672 to
bcd82e7
Compare
This was referenced Jul 8, 2026
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two-issue stack on the SGD update path, executed via SDD (per-task reviews + final whole-branch review, verdict: merge-ready).
#308 — unify SGD step arms
sgdStepMgains a momentum==0 fast path: one statelessexecuteOpper parameter (param -= lr*(grad + wd*param)), mathematically identical to the two-op momentum path at momentum==0, and never touchesoptim->states.sgdMCreateOptimallocates no momentum-state buffers at momentumFactor==0 (optim->states == NULL) — real RAM back on MCU targets;freeOptimSgdMhandles the no-states case. The ten duplicated per-layer state-building blocks collapsed into one uniform post-loop (−73 lines).sgdStepdeleted,optimizerType_tis now{ SGD_M }(verified unserialized anywhere), vtable has one entry. Its three direct test callers (the mutation-hardened optimizer: SYM_INT32 param/momentum update has a fixed-scale dead-zone (sub-ULP updates vanish) #279 dead-zone fixtures) are ported to the unified step with fixture math, iteration counts, seeds, and assertions byte-identical.momentumFactoris creation-fixed — it now decides state allocation (documented in SgdApi.h).#310 —
updateMathknobsgdMCreateOptimgains a 7th parameter, by-valuearithmetic_t updateMath, stored onsgd_tand fed into all three update opSpecs (previously hardcoded{ARITH_FLOAT32, HALF_AWAY}— a SYM-arithmetic update was structurally impossible).{ARITH_FLOAT32, HALF_AWAY}= zero numeric movement; all ~48 test call sites + 9 example call sites swept mechanically, no expectation changed anywhere.ARITH_FLOAT32fails fast at creation (sgdInit) AND at step time (sgdStepMentry, protects hand-assembled optimizers — the update kernels raw-castfloat*). Both guards death-tested; the creation death test doubles as an arg-forwarding pin.updateMath.roundingModegoverns the funnel prologue (inert for FLOAT32); the OUT_WRITE epilogue rounds by each target's own qConfig — where the optimizer: SYM_INT32 param/momentum update has a fixed-scale dead-zone (sub-ULP updates vanish) #279 SR dead-zone escape lives._Static_assertsignature contract. The first real second arm owns the distinguishing tests.Also in this PR
optimizerInitclaim; exercised-vs-implied dtype wording; knob + stateless-SGD documented).train_c_sym.c,mem_instrument.c(plus a NULL-states guard there), and two test files.Verification
ctest --preset unit_test_debug: 72/72 (re-run post-rebase), incl. 2 new death tests + 2 new optimizer: unify SGD step arms — plain sgdStep is unreachable; momentum==0 pays for momentum #308 contract tests; strict TDD evidence per task in.superpowers/sdd/task-*-report.md.cmake --build --preset examples: all example binaries build (rot guard).uv run pytest: 29/29.examples_memprofilebuild, nologs/writes (verified viafind -newer).Closes #308
Closes #310
Note: develop-merge does not auto-close — close both manually after merge. Follow-up to file:
sizeStatesrename (doubles as param count; misnomer sharpened now thatstatescan be NULL).🤖 Generated with Claude Code