Repository navigation
cvc::nav: opt-in sampled multi-start starts (GRL-SNAM #113) + semi-implicit geometry rollout - #596
Merged
Merged
Conversation
…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.
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.
CVC-Lab/GRL-SNAM#113 (merged 2026-10-09) fixed
multi_start_penalty's deterministic start. Before it,frac = 0.9on every iteration, so thems_countrollouts were one rollout repeated. Now each start drawsfrac ~ U(frac_range)per start and per sample, default(0.8, 0.98).cvc::nav::multi_start_penaltystill 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_paramsgainsms_count,frac_lo/frac_hi,seedandsemi_implicit. The defaults (1, 0.9/0.9, 0, false) are the old single explicit-Euler start.multi_start_fracsusesstd::mt19937(seed)and keeps the low 24 bits, which is exactly what CPUtorch.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'slo + (hi - lo) * uwith the same float rounding. The bounds aredoubleso(hi - lo)rounds the way the Python's scalar does.ms_countrollouts run as one batch ofms_count * Bagents (inputs tiled, grads summed back over the copies). A degenerate range makes every start identical, so it is evaluated once whateverms_countis, and the seed is ignored.material_lossandmaterial_loss_and_gradscore the same starts, and the FD gradchecks stay deterministic. The pycvc trainer's stepkdraws withseed + k;_loss(validation) uses the base seed.std::invalid_argument. The valid range is0 <= lo <= hi < 1, same as the Python'sValueError. The binding validates atcreateso nothing throws inside the GIL-free step.semi_implicit(forward + adjoint). The two Python references disagree on the integrator:SetasAditya/material-aware-grl-snam,full_code/surrogate_robust.py), whichtrain_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/surrogate_robust.pyhas been semi-implicit sincef5a015f(Dec 2025): velocity first, then the position steps withv_{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. Withsemi_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 ato0 - 0.9 * inf * 0 = NaN. The loss was fine (its clearance is+inf, so softplus gives 0), but the adjoint's0 * NaNmade that agent'sg_beta/g_gammaNaN. Throughbackward_onethat poisons the whole weight gradient. The agent now stays ato0and contributes exactly 0, as the Python's1e6sentinel does.pycvc:
nav_material_trainer_creategets 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:ms_count=4,(0.8, 0.98), fixed seed} × {explicit, semi-implicit});ms_count=10is bit-identical to the legacy single start;ms_count=0returns 0; bad ranges throw;NavGeomRolloutParity, against goldens from GRL-SNAM at the #113 merge (b34c0ae), regenerated by the checked-insrc/cvc/tests/gen_nav_geom_multistart_golden.py:nav_material_train_test:FullChainGradcheckgains sampled explicit and sampled semi-implicit variants (full loss → weights FD at a fixed seed);SampledMultiStartReachesTheLosschecks that loss and grad agree on the draw and that the seed changes L;w_multi = 0.5was always in the FD'd loss.test_pycvc_nav_train.py:kdrawsseed + k(checked at lr=0 against aseed + 1handle);Verified locally (Windows, MSVC 14.44 + CUDA 12.9 on an RTX 3050 Ti)
origin/master. A scratch harness compiles master'sgeom_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 variedms_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 samefuser.layers.0.linear2.weightpoint 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.pypasses 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·dtinstead of the explicit-tuned4·dt. Semi-implicit applies each step's acceleration immediately, so at4·dtthat 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. The4·dtsemi-implicit gradient is pinned against torch autograd by the parity test.Not in this PR
grl_snam/material_train_native.pyshould exposems_count/frac_range/seed/semi_implicitonce a pycvc with this binding is published. Itssurrogate_robust.py/ test docstrings should also stop saying(0.9, 0.9)matches native exactly, since that needssemi_implicit=True. That is a GRL-SNAM PR after the release.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.