Skip to content

nav: an external-force port in the differentiable rollout, step and trajectory - #728

Merged
transfix merged 8 commits into
masterfrom
feat/diff-ext-force
Oct 24, 2026
Merged

transfix merged 8 commits into
masterfrom
feat/diff-ext-force

Conversation

@transfix

@transfix transfix commented Oct 19, 2026 •

Copy link
Copy Markdown
Owner

The external-force port in the differentiable rollout: the deployed drive's ext_force, differentiated, so a trainer can learn through a force a caller supplies. It is the roadmap's P8 item before P8d (Joe, 2026-10-19: "start the ext-force port"; "ext-force port before P8c is fine, P8c after P8d").

One step: diff::ext_in (inc/cvc/nav/detail/diff_rollout.h)

  • Fields: fx, fy (the force at this step's pose, in the normalized drive frame), an optional position Jacobian J = dF_ext/do, and steer.
  • Through: bike_control, bike_step, bike_step_bwd, bike_control_bwd, dyn_bike_step and dyn_bike_step_bwd. Each takes const ext_in *ext = nullptr; the reverses take float *g_fext too.
  • Semantics, those of rollout_impl: the force joins the barrier and goal forces in the longitudinal command, and the repulsive force in the steering bias when it steers. The behind branch's barrier term stays barrier-only.
  • Reverse: returns dL/dF_ext and, given J, adds J^T dL/dF_ext into the position gradient.
  • Null: a null ext_in is the controller as it was. Every existing caller is unchanged.

A trajectory: inc/cvc/nav/ext_rollout.h

  • Calls: diff::bike_rollout_ext / bike_rollout_ext_bwd, and the dynamic twin dyn_rollout_ext / dyn_rollout_ext_bwd.
  • Per-step inputs: goals and coefficients, exogenous (the deployed carrot and coef_mlp's per-tick output), with scalar overloads.
  • The force: a callback gives F and J at a pose, every hold steps, held between. hold = 1 is the deployed drive's per-substep force; hold > 1 is a harness's per-frame force.
  • The tape: every step's state, and the force and J it used.
  • The reverse: takes dL/dstate at every step and returns dL/dF_ext per step, dL/dcoef (optional) and dL/d(initial state). A hold window's dL/dF goes through J at its sampling step.
  • Scope: host only, one agent per call.

Tests (nav_dyn_bike_test: DiffExtForce, DiffExtRollout)

Check Result
One step against the deployed bicycle_rollout_ext, 64 agents, with and without steering identical (worst 0.0)
A zero force against no force, forward and backward identical
dL/dF_ext against central differences, one step 9.4e-4 kinematic, 5.7e-5 dynamic; 1.6e-2 through a position-dependent force
10-step kinematic trajectory, hold 1 / 3: dL/dinit, dL/dF, dL/dbeta 1.8e-3 / 1.7e-3, 3.6e-3 / 4.8e-3, 1.6e-2 / 9.7e-3
The dynamic twin: dL/dinit, dL/dF 1.5e-2 / 1.4e-2, 1.1e-3 / 8.3e-4
The recorded trajectory against the steps with their held forces identical

Locally: 362/362 nav/physics/plant ctest cases outside the heavy suites, plus the new tests. Documented in docs/NAV_TRAINING.md ("An external force in the differentiable rollout").

Not in this PR

  • The GRL-SNAM torch twin: batched, float64, runs on CUDA, parity at 1e-9.
  • A coupled n-agent rollout with cross-agent Jacobians (a convoy), noted for later.

…ed ext_force's twin)

diff::ext_in carries a per-agent force for one step (fx, fy in the drive frame, an optional
position Jacobian, and whether it steers) into bike_control, bike_step, bike_step_bwd,
bike_control_bwd, dyn_bike_step and dyn_bike_step_bwd. It joins the barrier and goal forces in
the longitudinal command and, when it steers, the steering bias, exactly as rollout_impl adds
ext_force; the behind branch's barrier term stays barrier-only. The reverse returns dL/dF_ext
and, given J, adds J^T dL/dF_ext into the position gradient. A null ext_in is the controller as
it was. For cvcdbg's comm-network trainer; signatures agreed with the DBG session.

DiffExtForce: a zero force is the controller without it, forward and backward; one step matches
the deployed bicycle_rollout_ext exactly (worst 0.0, with and without steering); dL/dF_ext
against central differences 9.4e-4 (kinematic) and 5.7e-5 (dynamic), and through a
position-dependent force 1.6e-2.
…verse (ext_rollout.h)

diff::bike_rollout_ext / bike_rollout_ext_bwd (inc/cvc/nav/ext_rollout.h), and the dynamic twin
dyn_rollout_ext / dyn_rollout_ext_bwd: one agent's trajectory with per-step goals and
coefficients (exogenous), a force sampled by callback at the step's pose every `hold` steps and
held between, and a tape of every step's state, force and Jacobian. The reverse takes dL/dstate
at every step and returns dL/dF_ext at every step, dL/dcoef (optional) and dL/d(initial state);
a hold window's dL/dF goes through J at the step where F was sampled. A null callback is the
plain rollout. Interface agreed with cvcdbg's DBG session for its comm-network trainer.

DiffExtRollout: the trajectory is the steps with their held forces, bit for bit; the reverse
against central differences over 10 steps, hold 1 / 3: kinematic dL/dinit 1.8e-3 / 1.7e-3, dL/dF
3.6e-3 / 4.8e-3, dL/dbeta 1.6e-2 / 9.7e-3; dynamic dL/dinit 1.5e-2 / 1.4e-2, dL/dF 1.1e-3 /
8.3e-4. Documented in NAV_TRAINING.md. 362/362 nav/physics/plant ctest cases outside the heavy
suites.
bike_rollout_ext and its reverse on six fixed agents (12 steps, per-step goals and coefficients,
an affine force each, hold 1 or 3, one that does not steer) with the field they sample, written to
tests/data/ext_rollout_golden.json (CVC_EXT_ROLLOUT_GOLDEN_WRITE=1). GRL-SNAM's twin replays it in
float64 and holds this float reverse to autograd; this build reproduces the file to 1e-4.
…ard library's draws

#728's macOS Debug recipe failed DiffExtForce.GradientsMatchFiniteDifferences (0.148 through a
position-dependent force) and DiffExtRollout.ReverseMatchesFiniteDifferences (dL/dF 0.38 at hold 1),
which pass on MSVC and libstdc++. The scene's starts come from sample_starts_goals'
uniform_int_distribution, so libc++ checks other cases, and an unfiltered sum over cases is only as
good as its worst one.

- ext_cases jitters the starts off the SDF's grid nodes (the bilinear field's kinks).
- fd_sum takes each case's loss as a function of the offset and skips and counts a case whose
  difference measures no slope: forward and backward differences apart (a controller switch), or
  central differences at eps and eps / 2 apart (a slope peaked within the step: near a steep
  barrier 24.9 at 1e-3 against the reverse's 31.2, 31.3 at 1e-4). At most a quarter may skip.
- dL/dbeta at one step is differenced at 1e-2: its effect is small enough that float noise was a
  fair share of the 1e-3 difference.
- CVC_EXT_CASE_SEED replaces the draws: swept over 100 seeds, none fails; before, 2 of 40 did with
  jitter alone.

Docs: the measured table updated, and the method described.
The comments and docs describe ext_force as a hook for a caller's own force, without naming a downstream user.
# Conflicts:
#	docs/NAV_TRAINING.md
#	inc/cvc/nav/detail/diff_rollout.h
#	src/cvc/tests/nav_dyn_bike_test.cpp
@transfix
transfix merged commit 022264b into master Oct 24, 2026
24 checks passed
transfix added a commit that referenced this pull request Oct 24, 2026
transfix added a commit that referenced this pull request Oct 24, 2026
…dyn4_rollout_ext) (#780)

* nav: an external-force port on the four-wheel plant (dyn4_bike_step, dyn4_rollout_ext)

P8d's open item. The four-wheel vehicle (diff::dyn4_plant) takes the external force as the bicycle
and P8a's dyn_bike_step do since #728:

- dyn4_bike_step / dyn4_bike_step_bwd take an optional ext_in (the force joined to the
  controller's, steering or not) and return dL/dF_ext, adding J^T dL/dF_ext to the position's
  gradient; null is the step as it was.
- ext_rollout.h: dyn4_rollout_ext<G> / dyn4_rollout_ext_bwd<G>, the trajectory on a ground
  sampler, with a force sampled every `hold` steps and held between (dyn4_tape).

nav_dyn_4w_test: a zero force is the step bit for bit; one step's dL/dF (8.6e-5) and the
position's gradient through J (3.6e-4) against central differences; the trajectory is its steps,
a null sampler the plain rollout, and its reverse against central differences for hold 1 and 3,
cases on a controller switch skipped and counted (fd_acc); none of CVC_EXT_CASE_SEED 1..100 fails.
Dyn4ExtRollout.Golden writes tests/data/dyn4_ext_rollout_golden.json for GRL-SNAM's torch twin.

* nav_dyn_4w_test: the four-wheel force port's golden as the test writes it (not reformatted)

* docs: the external-force port is done on every plant (#728, #780); P8c is next
@transfix
transfix deleted the feat/diff-ext-force 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