Skip to content

cvc::ph training: native calibration of component laws, the C-PHAST DGU study (Phase 4) - #1560

Merged
transfix merged 2 commits into
masterfrom
feat/ph-train
Nov 27, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/ph-train

Conversation

@transfix

@transfix transfix commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Phase 4 of the C-PHAST port (docs/roadmap/PH-COMPOSE-ROADMAP.md): native training of component laws, starting with the paper's isolated-DGU calibration (Section 4.1, Appendix A.5). No Python and no torch are involved. The C-PHAST licence note on #1227 still applies: the authors' LGPL-2.1 permission has not been recorded.

What it adds

  • inc/cvc/ph/ad.h. A reverse-mode tape. Each operation records its local partials, and one backward sweep gives every adjoint.
    • The tape belongs to the caller; nothing is global. Threaded gradients use one tape per thread.
    • A var without a tape is a constant and records nothing.
  • inc/cvc/ph/train.h, src/cvc/ph/train.cpp.
    • canonical_t<T>: canonical_law's arithmetic over a scalar type.
      • With double it is the runtime law to rounding; the test holds the step and efforts to 1e-15.
      • With ad::var, the loss is differentiated through the potential's and the mass's input derivatives and through the implicit midpoint's Picard iterations.
    • trainable_parameters / with_parameters: the flattening.
    • The isolated-DGU calibration:
      • make_dgu_dataset: the reference DGU (q = 2.2 (v + v³/3), 1.8 H, 1.2 Ω) integrated with RK4 at dt/16. Starts are v ∈ [−1.5, 1.5] V and φ ∈ [−1, 1] Wb, inputs 0.1 A and 1 V, all six channels observed.
      • fit_linear_dgu / linear_dgu_spec: the fitted-linear control.
      • warm_start_dgu: the trained family at those values, a quadratic plus an MLP for V, and positive MLPs for M and D.
      • dgu_loss: L_constraint (Eq. 73) and L_step (Eq. 74, the six channels after LocalStep(dt, 2)), weighted 1 / 0.01 / 1e-8.
      • train_dgu: Adam (optax-exact, optim.h) with the combined or constraint-first schedule.
      • evaluate_dgu: held-out open-loop rollout RMSE.
  • CLI: cvc ph train-dgu <out> runs it all with the paper's defaults (32 × 200 training, 16 × 500 held out, 32/32 · 32/32 · 32 nets, batch 128, Adam 1e-4) and writes the law as cvc-ph-law/1.
  • Python: pycvc.ph_train_dgu(...).
  • Docs: the "Training" section of docs/PH_COMPOSE.md, and the roadmap's Phase 4.

Measured

  • The tape: the loss gradient matches central finite differences to 5.5e-6 relative (the difference quotient's own error). Four threads give the same gradient to 1e-12.

  • A short run (373 parameters, 400 updates, 1.4 s on 4 threads): the joint loss falls 500-fold (0.0154 → 3.0e-5). On held-out 2 s rollouts it reaches 24 mV / 9.6 mA, against the fitted-linear warm start's 128 mV / 53 mA.

  • The paper-size family has 2,405 parameters; the paper's has 2,407, of which 2,404 are optimized. On 5 s held-out rollouts its fitted-linear control scores 107 mV / 58 mA; Table 3's fitted linear is 82–103 mV / 43–63 mA.

  • The paper's full protocol, one fit, held-out 5 s rollouts (capacitor voltage / inductor current RMSE):

    law voltage RMSE current RMSE training
    fitted linear 107.3 mV 57.6 mA none
    combined loss, 100,000 updates 0.359 mV 0.171 mA 73 min, 12 threads
    constraint-first, 25,000 + 75,000 0.355 mV 0.168 mA 63 min, 12 threads
    C-PHAST Table 3, 5 fits: median (range) 1.2 mV (0.13–3.2) 0.74 mA (0.05–1.9)

    The fit lies within the paper's range. As in the paper, the two schedules differ by about 1%. The constraint-first law is committed as src/cvc/tests/data/ph_dgu_trained.json, and a test holds it to its score.

One limit, measured: with very little data (4 trajectories) the trained law beats the training loss but loses to linear on held-out starts it has not seen. The nets extrapolate. The paper's 32 trajectories, and the tests' 8, generalize.

Tests

  • ph_train_test, 7 tests (about 3 s):
    • the tape against finite differences;
    • the law over double against the runtime law (n = 1 and 2);
    • the parameters round-trip;
    • the loss gradient against finite differences, and threads agreeing;
    • the linear fit recovering a linear DGU's C, L and R exactly;
    • the short run beating the linear warm start on held-out rollouts;
    • the full protocol's committed law keeping its held-out score (0.355 mV / 0.168 mA).
  • CvcCliTest.PhTrainDguFitsAndWritesALaw.
  • test_pycvc_ph.py gains a training section; it now runs 38 checks.

Checked locally on Linux Release, GCC 13:

  • ph_train_test 7/7, CLI ph 7/7, pycvc_ph;
  • clang++-18 -Wall -Wextra clean on the new files;
  • git clang-format clean;
  • the writable-data scan finds nothing new.

Not here

  • Calibration on source networks (Eqs. 76–77).
  • The Go2 leg blocks, which need Phase 5's mechanical blocks first.

…GU study (Phase 4)

cvc/ph/ad.h: a reverse-mode tape owned by the caller (no global state).
cvc/ph/train.h: canonical_t<T>, canonical_law's arithmetic over a scalar
type (with double it is the runtime law to rounding; over the tape its step
is differentiated through the potential's and mass's input derivatives and
the Picard iterations); trainable-parameter flattening; the isolated-DGU
calibration: a dataset from the reference DGU, the fitted-linear control and
warm start, the constraint and one-step losses (1 / 0.01 / 1e-8), Adam with
the combined or constraint-first schedule, held-out rollout scores, and
threaded gradients. cvc ph train-dgu; pycvc ph_train_dgu.

The tape's loss gradient matches finite differences to 5.5e-6 relative. A
400-update run (373 parameters, 1.4 s) beats its fitted-linear warm start on
held-out rollouts: 24 mV / 9.6 mA against 128 mV / 53 mA.
@transfix
transfix enabled auto-merge (squash) November 26, 2026 20:43
…d law as a fixture

cvc ph train-dgu with the paper's settings: held-out 5 s rollouts at 0.359 mV
/ 0.171 mA (combined loss, 100k updates) and 0.355 mV / 0.168 mA
(constraint-first, 25k + 75k), against the fitted-linear control's 107 mV /
58 mA and Table 3's C-PHAST medians of 1.2 mV / 0.74 mA. The constraint-first
law is tests/data/ph_dgu_trained.json; ph_train_test holds it to its score.
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