Skip to content

train-coef: fix the deterministic multi-start, the dropped penetration penalty, and the integrate_surrogate export - #113

Merged
transfix merged 1 commit into
mainfrom
fix/training-loss-audit
Oct 9, 2026
Merged

transfix merged 1 commit into
mainfrom
fix/training-loss-audit

Conversation

@transfix

@transfix transfix commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Fixes three defects in the CoefEnergyNet training path (grl-snam train-coef / grl_snam.train_coef_energy) found by inspection on 2026-10-08 at 1885bdb. Each one was confirmed with a failing test before it was fixed. origin/main has since moved to b934fcd (#112, recipes only), so the line numbers below are unchanged.

Findings → confirmation → fix

1. multi_start_penalty is deterministic, so its 20 rollouts are identical

grl_snam/surrogate_robust.py:188-217: the loop sets frac = 0.9 on every iteration and draws no randomness. With the trainer's ms_count=20, it runs one rollout twenty times at 20× the compute.

  • Repro: a spy on the auxiliary rollouts records 20 starts with 1 distinct.
  • Fix: each start moves frac ~ U(frac_range) of the clearance toward the nearest obstacle, drawn per start and per sample.
    • The default (0.8, 0.98) is centred on the old 0.9, so w_multi keeps its scale.
    • A degenerate range (f, f) draws nothing; (0.9, 0.9) reproduces the old value exactly.
    • generator= makes the draw reproducible.
  • Found while fixing: a per-sample [B] robot_radius crashed it whenever B != N (it broadcast against the obstacle axis). That's fixed too.

2. The penetration penalty is computed but never added to the loss

grl_snam/train_coef_energy.py:553-559: pen is computed, but the line that adds it is commented out. It has been since the initial import.

  • Repro: on a sample whose rollout penetrates an obstacle, pen ≈ 9, yet the logged loss equals the sum of the other terms.
  • Fix: L += w_prox * pen, with explicit TrainCfg.w_prox = 0.1 and prox_tau = 0.05. These are the values the commented-out getattr carried. A --w_prox flag is added; w_prox=0 restores the old objective.

3. grl_snam.integrate_surrogate exports the legacy explicit-Euler integrator

grl_snam/__init__.py:36-40 pointed at train_coef_energy.integrate_surrogate (:386-438), whose position step uses v_n. The trainer (:500), the DBG runtime stepper and cvc::nav all integrate with the semi-implicit integrate_surrogate_v2.

  • Repro: the export's one-step output is exactly o0 + dt·v0.
  • Found while fixing: integrate_surrogate_v2 crashed on its own default robot_radius=0.0 (it indexed a 0-d tensor), so it could not have been dropped in as the export.
  • Decision: the export should be the semi-implicit integrator, i.e. what training uses.
    • grl_snam.integrate_surrogate is now integrate_surrogate_v2, which accepts a float or a [B] radius.
    • The legacy function is renamed train_coef_energy.integrate_surrogate_explicit and kept only for the version != 2 trainer path.
    • I found no consumer of the old export in any CVC checkout, including grl-snam-dbg, libcvc and the engagement docs.

Tests

tests/test_training_loss_audit.py has 15 tests. On origin/main, 14 fail and 1 passes (a gradient guard). On this branch all pass.

Full suite on this box: 452 passed, 19 failed. The same 19 tests fail on an origin/main archive (437 passed, 19 failed). They are this box's native-pycvc parity, missing material_raster dependencies, and the libm tripwire, and are unrelated to this change. black 26.5.1 (the CI pin) and ruff are clean.

Did shipped weights or published results depend on these defects?

Nothing was re-run.

  • grl-snam-weights (coef_sdf.cvcnav / .pt, v1.0.0): not affected. It is produced by grl-snam train (grl_snam/tools/train.py → sdf_nav only), which imports neither train_coef_energy nor multi_start_penalty.
  • libcvc share/cvc/nav/coef_mlp.cvcnav: not affected. It is the untrained bias-basin seed.
  • Anything trained with train_coef_energy, i.e. CoefEnergyNet on the legacy train-coef track, was affected by items 1 and 2. That includes the original research-code results, if any are still cited.
    • It trained with no penetration penalty.
    • Its multi-start term was effectively a single start.
    • No such weights ship from this repo. Re-run before citing those numbers against the fixed objective.
  • libcvc native trainer: cvc::nav::multi_start_penalty (src/cvc/nav/geom_rollout.cpp, used by the material trainer) is a faithful port of the old deterministic draw. It computes one start at 0.9, so it wasted no compute, and its value equals the old Python value. After this PR the Python default samples, and frac_range=(0.9, 0.9) is the parity setting. Any .cvcnm from the native trainer used the deterministic start. Making libcvc sample too is a separate libcvc change.
  • Item 3: export only. The trainer never called it, so no results depended on it.

Shipping

The grl-snam-cp31X recipe vendors the source, so this reaches the cvcpkg catalog only with a cvc_revision bump and a publish. Neither is done here.

Do not merge without review.

… audit

multi_start_penalty drew no randomness (frac was a constant 0.9), so its
ms_count=20 rollouts were one rollout repeated twenty times. Each start
now moves a U(frac_range) fraction of the clearance toward the nearest
obstacle, drawn per start and per sample. The default (0.8, 0.98) is
centred on the old 0.9 so w_multi keeps its scale; (0.9, 0.9) reproduces
the old value exactly and draws nothing; generator= makes it
reproducible. A per-sample [B] robot_radius also crashed it whenever
B != N (broadcast against the obstacle axis).

Trainer.step_batch computed the penetration penalty and never added it
to the loss (the line was commented out). It is now L += w_prox * pen,
with explicit TrainCfg.w_prox = 0.1 / prox_tau = 0.05 (the values the
commented-out getattr carried) and a --w_prox flag; 0 restores the old
objective.

grl_snam.integrate_surrogate exported the legacy explicit-Euler
integrator while the trainer, the runtime stepper and cvc::nav all use
the semi-implicit integrate_surrogate_v2. The export is now v2; the
legacy one stays as train_coef_energy.integrate_surrogate_explicit for
the version-1 trainer path. v2 itself crashed on its own default
robot_radius=0.0 (indexed a 0-d tensor); it now takes a float or [B].

Regression tests: tests/test_training_loss_audit.py (14 of 15 fail on
the parent commit; the 15th is a gradient guard).
@transfix
transfix merged commit b34c0ae into main Oct 9, 2026
3 checks passed
@transfix
transfix deleted the fix/training-loss-audit branch October 9, 2026 20:10
transfix added a commit to transfix/libcvc that referenced this pull request Oct 12, 2026
…rollout (#596)

CVC-Lab/GRL-SNAM#113 made multi_start_penalty draw frac ~ U(frac_range)
per start and per sample; the old fixed frac = 0.9 made its ms_count
rollouts one rollout repeated. The native port mirrored that single
start. multi_start_params now carries ms_count, frac_lo/frac_hi, seed
and semi_implicit; the defaults are the old single explicit start,
byte-identical to master (checked over 400 random batches).

The draw is torch.rand's, bit for bit: std::mt19937(seed), low 24 bits,
then the Python's lo + (hi - lo) * u with the same float rounding
(multi_start_fracs). The ms_count rollouts run as one batch of
ms_count*B agents; a degenerate range is evaluated once. A fixed seed
is a fixed function, so the loss and gradient score the same starts and
the finite-difference checks stay deterministic; the pycvc trainer's
step k draws with seed + k and _loss scores the base seed.

semi_implicit (forward + adjoint) selects GRL-SNAM's integrator, which
has updated the velocity first since f5a015f; the default explicit order
is the material fork's, which train_material.py imports. Without it,
frac_range=(0.9, 0.9) matched native's start but not its rollout.

Also fixes NaN coefficient grads for a padded agent with no valid
obstacle: its start was o0 - frac * inf * 0, and the adjoint's 0 * NaN
reached g_beta/g_gamma. It now contributes 0, like the Python's 1e6
sentinel.

pycvc: nav_material_trainer_create takes trailing ms_count, frac_lo,
frac_hi, seed and ms_semi_implicit, validated before any GIL-free step.

Tests: rollout and multi-start FD gradchecks for both integrators and
sampled starts at a fixed seed; determinism, degenerate-range, bad-range
and no-obstacle cases; NavGeomRolloutParity pins the draw bit for bit
and L/grads to ~3e-7 against torch autograd on GRL-SNAM #113
(gen_nav_geom_multistart_golden.py regenerates the goldens); full-chain
trainer gradchecks with sampled starts; pycvc trainer arg coverage.
transfix added a commit that referenced this pull request Nov 7, 2026
* release: 0.3.0

95 commits since v0.2.1 (PRs #66 to #158), with new features, so the
pre-1.0 minor version goes up. The main changes:

- Ariadne hosts: GRL-SNAM's demos run as Python hosts of Ariadne documents
  (#147, #149, #153). `grl-snam ariadne cars` runs libcvc's multi-car
  template (#155), and fleet and live take its settings, worlds, plants,
  panels and log (#157). These need pycvc-gl 3.7.12.
- Vehicle plants: capture, the demos, the fog-of-war stories and training
  run on kinematic, dynamic and Jolt plants (#119, #130, #143, #145, #154).
- Torch twins of libcvc, held to its goldens: the material rollout (#118),
  terrain (#120), dyn_bike and dyn4 (#121, #123, #134-#136), the external
  force (#122, #124), the material trainer (#125), belief material
  (#128, #129, #131), U_nav (#133), and the port-Hamiltonian form, its
  energy books, wheel spin and compliant contact (#138, #139, #146).
- The vehicle-plant port-Hamiltonian whitepaper (#137 to #158).
- Training and evaluation: the base scorecard and selection (#82-#98),
  `grl-snam coef-train` (#102), the learned lam head and .cvcnav v2
  (#94, #105), and the 2026-10 audit fixes (#113).
- Data: grl-snam-weights, plus the Austin scene and Humvee as runtime
  dependencies, so `grl-snam demo` runs from a bare install (#72-#77).
- The research core moves under grl_snam/ (#103). The world clock is
  shared with libcvc's (#116, #117).

The version lives in pyproject.toml. The three cvcpkg columns mirror it in
upstream_version and in the package.files dist-info path. cvc_revision goes
back to 1 for the new upstream version. The publish job asks the server for
the next revision in any case.

* material_train_native: pass semi_implicit=False explicitly (libcvc #981)

libcvc #981 (in 3.7.12) changed nav_material_trainer_create's
ms_semi_implicit default from 0 to 1. MaterialTrainer left the L_multi
sampling args out for the legacy single start, so on pycvc-gl 3.7.12
semi_implicit=False quietly ran semi-implicit Euler. #159's CI was the
first run on 3.7.12, and four tests failed on cp311, cp312 and cp313:

- test_sampled_multi_start_args: "semi_implicit did not reach the
  rollout", because the default and semi_implicit=True were now the same
  rollout;
- test_live_native_trainer_agrees[legacy] and [legacy+ext], and
  test_live_native_trainer_agrees_on_the_belief_net[surrogate]: the
  explicit torch trainer and the native one differed by 1.3e-3 to 1.5e-3
  (tolerance 1e-5).

Now a binding that takes the args always gets them, so the integrator is
the one asked for on any pycvc. An older binding still gets the legacy
start through the old signature. GRL-SNAM's default stays explicit Euler.

tests/test_material_train_native_args.py checks both bindings. Its new
case fails without this change.
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