Skip to content

cvc::nav: opt-in sampled multi-start starts (GRL-SNAM #113) + semi-implicit geometry rollout - #596

Merged
transfix merged 1 commit into
masterfrom
feat/nav-multistart-sampling
Oct 12, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/nav-multistart-sampling

Conversation

@transfix

@transfix transfix commented Oct 9, 2026

Copy link
Copy Markdown
Owner

CVC-Lab/GRL-SNAM#113 (merged 2026-10-09) fixed multi_start_penalty's deterministic start. Before it, frac = 0.9 on every iteration, so the ms_count rollouts were one rollout repeated. Now each start draws frac ~ U(frac_range) per start and per sample, default (0.8, 0.98). cvc::nav::multi_start_penalty still mirrors the old single start. This PR adds the sampling as an opt-in: the defaults are byte-identical to today, so existing results, gradchecks and TACC numbers don't move.

What changed

multi_start_params gains ms_count, frac_lo/frac_hi, seed and semi_implicit. The defaults (1, 0.9/0.9, 0, false) are the old single explicit-Euler start.

  • The draw is torch's, bit for bit. multi_start_fracs uses std::mt19937(seed) and keeps the low 24 bits, which is exactly what CPU torch.rand(B, generator=torch.Generator().manual_seed(seed), dtype=float32) draws (checked on torch 2.8, including a 100k-element stream). It then computes the Python's lo + (hi - lo) * u with the same float rounding. The bounds are double so (hi - lo) rounds the way the Python's scalar does.
  • The ms_count rollouts run as one batch of ms_count * B agents (inputs tiled, grads summed back over the copies). A degenerate range makes every start identical, so it is evaluated once whatever ms_count is, and the seed is ignored.
  • Fixed seed means fixed function. The draw doesn't advance between calls, so material_loss and material_loss_and_grad score the same starts, and the FD gradchecks stay deterministic. The pycvc trainer's step k draws with seed + k; _loss (validation) uses the base seed.
  • A bad range throws std::invalid_argument. The valid range is 0 <= lo <= hi < 1, same as the Python's ValueError. The binding validates at create so nothing throws inside the GIL-free step.

semi_implicit (forward + adjoint). The two Python references disagree on the integrator:

  • the material fork (SetasAditya/material-aware-grl-snam, full_code/surrogate_robust.py), which train_material.py (this trainer's normative source) imports, is explicit Euler: the position steps with the old velocity. That remains the default here.
  • GRL-SNAM's grl_snam/surrogate_robust.py has been semi-implicit since f5a015f (Dec 2025): velocity first, then the position steps with v_{n+1}.

Without the flag, frac_range=(0.9, 0.9) matches native only in its start point, not in its rollout. The #113 test docstring says it matches native exactly; that is true of the start but not of the integrator. With semi_implicit = true, native now matches GRL-SNAM's function to float tolerance. In the adjoint, v_{n+1} also collects the position step's adjoint (c * a_o).

NaN fix (separate from sampling). In a padded batch (N > 0), an agent whose obstacle slots are all padding started at o0 - 0.9 * inf * 0 = NaN. The loss was fine (its clearance is +inf, so softplus gives 0), but the adjoint's 0 * NaN made that agent's g_beta/g_gamma NaN. Through backward_one that poisons the whole weight gradient. The agent now stays at o0 and contributes exactly 0, as the Python's 1e6 sentinel does.

pycvc: nav_material_trainer_create gets five trailing args (ms_count, frac_lo, frac_hi, seed, ms_semi_implicit), with defaults matching the C++ ones.

Tests

nav_geom_rollout_grad_test:

  • the rollout VJP gradcheck now runs for both integrators;
  • the multi-start FD gradcheck runs in 4 configurations ({legacy, sampled ms_count=4, (0.8, 0.98), fixed seed} × {explicit, semi-implicit});
  • same seed gives bit-identical L and grads, including across 1 vs 4 threads; a different seed gives a different L;
  • a degenerate range with ms_count=10 is bit-identical to the legacy single start;
  • ms_count=0 returns 0; bad ranges throw;
  • the no-obstacle agent case is a NaN regression.

NavGeomRolloutParity, against goldens from GRL-SNAM at the #113 merge (b34c0ae), regenerated by the checked-in src/cvc/tests/gen_nav_geom_multistart_golden.py:

  • the 20 sampled fractions match torch's draw bit for bit;
  • L and all coefficient grads match torch autograd for 4 configurations ({legacy, sampled} × {GRL-SNAM's semi-implicit function, the same function with the fork's explicit update order}). The scene covers penetrating starts, the feasibility fallback (an agent already inside an obstacle) and a no-obstacle agent.

nav_material_train_test:

  • FullChainGradcheck gains sampled explicit and sampled semi-implicit variants (full loss → weights FD at a fixed seed);
  • SampledMultiStartReachesTheLoss checks that loss and grad agree on the draw and that the seed changes L;
  • the header no longer claims L_multi is out of scope. The default w_multi = 0.5 was always in the FD'd loss.

test_pycvc_nav_train.py:

  • the degenerate range reproduces the legacy loss; the sampled range changes it;
  • validation is deterministic;
  • step k draws seed + k (checked at lr=0 against a seed + 1 handle);
  • an inverted range is rejected at create.

Verified locally (Windows, MSVC 14.44 + CUDA 12.9 on an RTX 3050 Ti)

  • Byte-identity vs origin/master. A scratch harness compiles master's geom_rollout.cpp (namespace renamed) beside this branch's, with the library's flags. Over 400 random batches it compares the default rollout, the VJP (accumulating into non-zero buffers) and the multi-start loss and grads (default and varied ms_h/ms_dt_mult/tau; zeroed and non-zero grad buffers). 0 differences. These batches give every agent at least one valid obstacle; master gives NaN otherwise.
  • nav_geom_rollout_grad_test: 15/15. Parity: the draws match bit for bit, and L/grads match torch at worst_rel 1.4e-7 to 3.0e-7 (cos = 1.000000000) in all four configurations. FD: dir_rel ≤ 1.7e-4, worst ≤ 2.9e-3.
  • nav_material_train_test: 4/4. The two sampled full-chain gradchecks match the legacy one (dir_rel 2.2e-4). Their worst_rel of 4.985e-2 is the same fuser.layers.0.linear2.weight point the legacy check hits on master. This batch's L_multi gradient is near zero (its minimum clearance sits at the data start), so the penetrating cases are covered by the geometry-level checks, not here.
  • nav_material_train_cuda_test: 4/4 on the device.
  • test_pycvc_nav_train.py passes against an in-tree pycvc build of this branch (dbg-deps Python 3.12 / SWIG 4.4.1).

One choice in the semi-implicit FD checks. They run at dt' = 2.5·dt instead of the explicit-tuned 4·dt. Semi-implicit applies each step's acceleration immediately, so at 4·dt that batch's starts land in the barrier's stiff, kinked band. An eps sweep there shows the central difference converging onto the analytic gradient param by param (e.g. 103 → 493 → 779 → 790.3 vs 790.9 as eps goes 2e-3 → 2e-5), but only below about 2e-5, which is too fine for a float rollout across every parameter. The 4·dt semi-implicit gradient is pinned against torch autograd by the parity test.

Not in this PR

  • GRL-SNAM wrapper. grl_snam/material_train_native.py should expose ms_count / frac_range / seed / semi_implicit once a pycvc with this binding is published. Its surrogate_robust.py / test docstrings should also stop saying (0.9, 0.9) matches native exactly, since that needs semi_implicit=True. That is a GRL-SNAM PR after the release.
  • The fork's barrier. The material fork still carries the pre-M10 IPC derivative. cvc::nav and GRL-SNAM both use the M10-corrected one, so the "explicit" goldens use GRL-SNAM's barrier with the fork's update order. That is what native computes.
  • Defaults. Turning sampling on by default (ms_count = 10, (0.8, 0.98)) would change the native TACC objective. Post-change runs couldn't be compared with older ones, so it is left as a separate decision.

…rollout

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
transfix merged commit 6fd5071 into master Oct 12, 2026
14 checks passed
@transfix
transfix deleted the feat/nav-multistart-sampling branch October 29, 2026 20:52
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