Skip to content

Add unified trainer (PC + backprop + autoregressive) - #36

Open
aprotsenko24 wants to merge 1 commit into
mainfrom
feature/unified-trainer
Open

Add unified trainer (PC + backprop + autoregressive)#36
aprotsenko24 wants to merge 1 commit into
mainfrom
feature/unified-trainer

Conversation

@aprotsenko24

@aprotsenko24 aprotsenko24 commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

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:

train(params, structure, loader, optimizer, config, key,
      algorithm="pc"|"backprop", autoregressive=False, ...)
evaluate(..., algorithm=..., autoregressive=False, ...)

Design

  • The two learning rules differ in exactly three funnels_forward / compute_gradients / _eval_step. Everything else (batch conversion, clamping, optimizer update, the epoch/batch driver, sharding, eval reduction) is shared.
  • Autoregressive is an orthogonal boolean flag, not a third algorithm: its only footprint is a causal-mask clamp + loss/perplexity reporting; it works with either algorithm.
  • The gradient math is called verbatim from fabricpc.core (compute_local_weight_gradients, run_inference, initialize_graph_state) — not reimplemented.
  • The three legacy trainer files are left untouched as the parity baseline (this is a new, standalone file).
  • algorithm / autoregressive / use_causal_mask / metric_name are explicit signature parameters (not config keys); config holds only num_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__.py and converting the three legacy files to deprecation shims that delegate to this module.

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
aprotsenko24 force-pushed the feature/unified-trainer branch from 22699cd to 7e47aac Compare June 22, 2026 03:17
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.

1 participant