Repository navigation
cvc::ph training: native calibration of component laws, the C-PHAST DGU study (Phase 4) - #1560
Merged
Merged
Conversation
…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
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.
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.
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.varwithout 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.doubleit is the runtime law to rounding; the test holds the step and efforts to 1e-15.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.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 afterLocalStep(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.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 ascvc-ph-law/1.pycvc.ph_train_dgu(...).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):
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):CvcCliTest.PhTrainDguFitsAndWritesALaw.test_pycvc_ph.pygains a training section; it now runs 38 checks.Checked locally on Linux Release, GCC 13:
ph_train_test7/7, CLI ph 7/7,pycvc_ph;clang++-18 -Wall -Wextraclean on the new files;git clang-formatclean;Not here