Skip to content

nav: the material trainer on the kinematic bicycle (P8b, first item) - #642

Merged
transfix merged 2 commits into
masterfrom
feat/material-train-bicycle
Oct 17, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/material-train-bicycle

Conversation

@transfix

Copy link
Copy Markdown
Owner

The first step of moving training off the point mass (vehicle-physics roadmap P8b). The material trainer (CoefEnergyNetMaterial) differentiated only integrate_surrogate_material, a point mass. This adds a rollout with the same force law, driven through the deployed kinematic bicycle.

What

integrate_bicycle_material (src/cvc/nav/material_bike_rollout.cpp)

  • Force law, unchanged from the trainer: per-obstacle IPC barriers on the discs, the goal spring, damping, and the soft/hard material forces sampled from the 6-channel patch.
  • Controller and integrator: drive.cpp's bicycle_rollout_material, one substep per step, in drive.cpp's op order:
    • longitudinal a = (F·heading − γ·sp)/mass, clamped;
    • pure pursuit to the goal, plus the k_steer·tanh((F_rep + F_mat)·left) steering bias;
    • the corner, stop, creep and reverse governors;
    • d_cap and the kinematic update.
  • Governor input: the nearest disc's clearance and normal take the role the SDF sample plays in deployment, as in the footprint path's binding-disc rule.

integrate_bicycle_material_vjp

  • A hand-written adjoint over a per-step record of every intermediate and every branch the forward took.
  • Gradient flows through the taken branch, as autograd treats min/max/where.

Trainer

  • material_loss_config::integrator is surrogate (the default, so nothing changes) or bicycle, with bicycle_material_params in the batch's length units and validate().
  • material_batch gains optional th0/sp0; by default the bicycle starts at speed |v0| along v0.
  • L_vel compares sp·(cos θ, sin θ).
  • L_multi stays a point-mass geometry rollout.
  • The bicycle runs on the CPU even under use_cuda; it has no device twin yet.

pycvc

  • nav_material_trainer_set_bicycle / nav_material_trainer_integrator on the trainer handle, so GRL-SNAM campaigns can switch.
  • nav_integrate_bicycle_material and _vjp, for a torch twin's forward and gradient parity.

Verification (Windows, MSVC 2022, local cvcpkg prefix)

  • nav_material_bike_rollout_test, 6/6:
    • Gradchecks. Kink-aware finite-difference checks of the adjoint over every differentiable output (oT, θT, spT, min_clear, cum_risk, arc). The short batch checks 33 coefficients, worst 0.36%. The longer no-reverse batch checks 33 and skips 2 whose forward and backward differences disagree, worst 1.2%.
    • Why skip. The controller has real discontinuities (behind, the hard-steer floor, the approach cut-off). For one coefficient, the forward difference jumps between +1e-3 and +2e-3, while every difference at 1e-3 and below converges on the analytic value.
    • Behaviour. 1 vs 8 threads bit-identical; drives to its goal and stays in its speed range; keeps clear of an obstacle on the line to the goal; the soft material force cuts collected risk by more than 5%.
  • nav_material_train_test: the full loss → weights gradcheck through the bicycle passes (dir_rel 7e-4, worst 0.6%, 64 weights). The existing gradchecks are unchanged.
  • Training descends: nav_material_optim_test and test_pycvc_nav_train.py show Adam reducing the loss through the bicycle.
  • Existing suites: nav material, rollout, deploy, coef_train and geom_rollout all pass.

Measured, and worth knowing for P8a

Gradients through the bicycle are sparse. A saturated acceleration limit, steering tanh or speed limit passes no gradient, as autograd would. On the shared fixture:

gradient norm loss after 70 Adam steps
point mass 242 −74%
bicycle 2.8 −5%

That matches coef_train's note that the bicycle holds the basin rather than beating it. Learning rate and force scales matter more here. The dynamic model (P8a) may want smooth or straight-through clamps in training.

Not in this PR: the GRL-SNAM torch twin and parity test (next, using the new bindings), and a CUDA twin.

The material trainer (CoefEnergyNetMaterial) only ever differentiated a point
mass. This adds a rollout that keeps its force law but drives the deployed
kinematic bicycle, so training sees the vehicle it deploys on.

- The force law kept from the trainer: per-obstacle IPC barriers on the discs,
  the goal spring, damping, and the soft/hard material forces from the
  6-channel patch.
- integrate_bicycle_material: drive.cpp's bicycle_rollout_material controller,
  one substep per step:
  - pure pursuit with the (F_rep + F_mat) steering bias;
  - the corner, stop, creep and reverse governors;
  - d_cap and the kinematic update.
  The governor reads the nearest disc's clearance and normal, the role the SDF
  sample plays in the deployed drive.
- integrate_bicycle_material_vjp: a hand-written adjoint over a per-step
  record of every intermediate and every branch taken. Gradient flows through
  the taken branch, as autograd treats min/max/where.
- material_loss_config::integrator (surrogate | bicycle; default unchanged)
  and bicycle_material_params, in the batch's length units, with validate().
  material_batch gains an optional th0/sp0 (default: speed |v0| along v0).
  L_vel compares sp * (cos th, sin th). L_multi stays a point-mass geometry
  rollout. The bicycle runs on the CPU even with use_cuda (no device twin
  yet).
- pycvc:
  - nav_material_trainer_set_bicycle / nav_material_trainer_integrator on
    the trainer handle;
  - nav_integrate_bicycle_material and its _vjp, for a torch twin's
    forward and gradient parity.

Tests:
- nav_material_bike_rollout_test:
  - kink-aware finite-difference gradchecks of the adjoint: 33 coefficients,
    worst 0.4%; and 33 with 2 skipped where the nudge straddled a controller
    discontinuity, worst 1.2%;
  - 1 vs 8 threads bit-identical;
  - drives to its goal within its speed range;
  - keeps clear of an obstacle in the way;
  - the soft material force cuts collected risk.
- nav_material_train_test: the full loss -> weights gradcheck through the
  bicycle (dir_rel 7e-4, worst 0.6%, 64 weights).
- nav_material_optim_test and test_pycvc_nav_train.py: Adam descends through
  the bicycle.

Measured: gradients through the bicycle are sparse. A saturated acceleration
limit, steering tanh or speed limit passes none, so the fixture's gradient
norm is 2.8 against 242 on the point mass, and its loss falls 5% against 74%
over 70 steps. The P8a design should take that into account.
With material_integrator::bicycle, the multi-start robustness term now rolls its
short rollouts out on the bicycle instead of the point mass.
- multi_start_penalty_bicycle (material.h) uses the same starts and the same
  softplus(-min_clear / tau) score, with each agent keeping its heading and
  speed. As before it is geometry only (lam = 0), and its gradient comes from
  integrate_bicycle_material_vjp.
- The start construction moves out of multi_start_penalty into
  multi_start_origins (geom_rollout.h), unchanged. nav_geom_rollout_grad_test,
  with its Python goldens, still passes.
- Test: MultiStartPenaltyGradcheck: three sampled starts, 9 coefficients
  checked, worst 4e-4, 1 skipped at a controller discontinuity. The full
  loss -> weights gradcheck through the bicycle, now including this term,
  passes.
transfix added a commit to CVC-Lab/GRL-SNAM that referenced this pull request Oct 17, 2026
…cvc; trainer on the bicycle (#118)

libcvc P8b moves the material trainer off the point mass: integrate_bicycle_material
keeps the surrogate's force law and drives the deployed kinematic bicycle
(transfix/libcvc#642).
- grl_snam/material_bike.py: integrate_bicycle_material in torch, the same
  controller with every branch a torch.where. Autograd follows the branch
  each step took, as the C++ adjoint does. Guarded sqrt/norm keep an
  unselected zero from producing NaN gradients.
- nav_native: integrate_bicycle_material / _vjp wrappers over the new
  pycvc entry points, gated by HAS_BICYCLE_MATERIAL_ROLLOUT.
- MaterialTrainer(bicycle=BicycleParams(...)) takes the loss through the
  bicycle, gated by HAS_MATERIAL_TRAINER_BICYCLE; .integrator reports which.
  The module still imports without torch.
- tests/test_material_bike_parity.py:
  - forward (rtol 1e-4) and gradient (rtol 1e-3) parity against libcvc;
  - a pure-torch check that the twin drives to its goal.
  Measured against a local libcvc build: forward within 5e-7 relative,
  gradient within 3e-6, so two independent derivations agree.
- tests/test_material_train_native.py: the trainer descends on the bicycle,
  and a zero wheelbase is refused.

The native tests skip until a pycvc with the bindings is installed.
@transfix
transfix merged commit e8cba2f into master Oct 17, 2026
19 of 20 checks passed
@transfix
transfix deleted the feat/material-train-bicycle branch October 17, 2026 07:57
transfix added a commit that referenced this pull request Oct 17, 2026
Minor release. It carries:
- the world clock: lockstep and batch, followers, the sim.* binding, and nav
  on the clock (#629, #630, #631, #640);
- the units registry with SI <-> normalized-frame conversions (#632);
- cvc::physics with the Jolt backend (#641);
- the material trainer on the kinematic bicycle and its pycvc bindings
  (#642).
Bumps project(VERSION) 3.5.1 -> 3.6.0 and every cvcpkg recipe's
upstream_version to match. cvc_revision stays 1, per the recipe-version gate.
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