Add unified trainer (PC + backprop + autoregressive) - #36
Open
aprotsenko24 wants to merge 1 commit into
Open
Conversation
One self-contained, algorithm-parameterized trainer consolidating the three
legacy trainers (train.py / train_backprop.py / train_autoregressive.py) behind
a single API:
train(params, structure, loader, optimizer, config, key,
algorithm="pc"|"backprop", autoregressive=False, ...)
evaluate(..., algorithm=..., autoregressive=False, ...)
The two learning rules differ in EXACTLY three funnels (_forward /
compute_gradients / _eval_step); all scaffolding (batch conversion, clamping,
optimizer update, the epoch/batch driver, sharding, eval reduction) is shared.
Autoregressive is an orthogonal boolean flag (causal-mask clamp + loss/perplexity
reporting), not a third algorithm. The gradient math is called VERBATIM from
fabricpc.core; the three legacy files are left untouched as the parity baseline.
Numerical parity (single device):
- scripts/_parity_check_unified.py: PC & backprop train+eval vs legacy, exact to 1e-6.
- scripts/_parity_check_ar.py: AR-PC/AR-backprop vs legacy; eager grads bit-identical
(atol=0); jitted params <=2e-3 (float artifact -- the legacy AR step computes an unused
cross-entropy inside its jit, which the unified version omits); plus a ragged-batch
eval assertion.
- tests/test_unified_trainer.py: PC & backprop train+eval parity, 1e-12.
Deferred (separate atomic step, after sign-off): wiring into training/__init__.py and
converting the legacy files to deprecation shims.
aprotsenko24
force-pushed
the
feature/unified-trainer
branch
from
June 22, 2026 03:17
22699cd to
7e47aac
Compare
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.
What
One self-contained, algorithm-parameterized trainer that consolidates the three legacy trainers (
train.py/train_backprop.py/train_autoregressive.py) behind a single API:Design
_forward/compute_gradients/_eval_step. Everything else (batch conversion, clamping, optimizer update, the epoch/batch driver, sharding, eval reduction) is shared.fabricpc.core(compute_local_weight_gradients,run_inference,initialize_graph_state) — not reimplemented.algorithm/autoregressive/use_causal_mask/metric_nameare explicit signature parameters (not config keys);configholds onlynum_epochs.Numerical parity (single device)
scripts/_parity_check_unified.py— PC & backprop, train + eval vs the legacy trainers, exact to 1e-6.scripts/_parity_check_ar.py— AR-PC / AR-backprop vs legacy: eager gradients bit-identical (atol=0); jitted params ≤2e-3 (a float artifact — the legacy AR step computes an unused cross-entropy inside its jit, which the unified version omits); includes a ragged-batch eval assertion.tests/test_unified_trainer.py— PC & backprop train + eval parity, 1e-12 (pytest gate).All green on CPU (
JAX_PLATFORMS=cpu).Known AR caveat (do not "fix")
AR eager gradients are bit-identical to the legacy formula; only the jitted params drift ≤2e-3, purely because the legacy AR step computes a dead cross-entropy inside the jit (changes XLA float-scheduling; Adam amplifies it). The unified step is cleaner and run-to-run deterministic.
Deferred (separate atomic step, after sign-off)
Wiring into
training/__init__.pyand converting the three legacy files to deprecation shims that delegate to this module.