Skip to content

fix: IPC barrier force is the gradient of the barrier (M10) - #61

Merged
transfix merged 3 commits into
mainfrom
fix/ipc-barrier-derivative
Sep 8, 2026
Merged

transfix merged 3 commits into
mainfrom
fix/ipc-barrier-derivative

Conversation

@transfix

@transfix transfix commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

The bug

The obstacle-barrier derivative was not the derivative of the barrier it names. For b(d) = -(d-d̂)² log(d/d̂), the returned dbdd exceeded the true derivative by exactly (d - d̂) + 1 — verified against autograd (max|dbdd - autograd(b)| ≈ 1.0 on main, matching the (d-d̂)+1 signature). That +1 makes the force nonzero at detection range where the true derivative vanishes, and flips its sign over an outer band, producing an attraction band over ~(0.39–0.80)·d̂ where the obstacle barrier pulls the agent toward the obstacle. The force field was therefore the gradient of no potential — no energy or passivity argument transfers to the running code.

Numerically, on the shipped code: 1057 / 4001 sample points in (0, d̂) had dbdd > 0 (the attraction band), max dbdd = +1.0.

The fix

Replace dbdd with the analytic derivative of b: -(2(d-d̂)·log(d/d̂) + (d-d̂)²/d). Matches autograd to machine zero and is purely repulsive — 0 attraction-band points. Applied at the four Python sites carrying the closed form (train_coef_energy, surrogate_robust fallback, sdf_nav._ipc_dbdd, spline_stagewise6). The softplus SDF hazard barrier was already exact and is untouched.

Verification

  • test_ipc_barrier_is_gradient.py (new): dbdd == autograd(b) to 1e-9; no point has dbdd > 0.
  • test_material_nav.py: the hand-written integrator oracle re-derived to the corrected form.
  • Full suite: 289 passed, 26 skipped (native pycvc/CUDA parity + VTK, unavailable here).

Two honest notes

  1. A pre-existing ~5e-7 nondeterminism in SdfNavigator.step (multithreaded reduction; present on main, single-thread too) is exposed by the corrected trajectory, which the buggy trajectory dodged. test_vehicle_refinements's bitwise torch.equal is relaxed to allclose(1e-4) — inertness is an O(metres) claim, 4 orders above the noise — with the real nondeterminism tracked as a separate task.
  2. This is the Python half of M10. The C++ cvc::nav copies (material_rollout.h, drive.cpp/.cu, diff_rollout.h) + their two analytic VJP adjoints + a force-law provenance byte in the .cvcnav/.cvcnm headers land as the coordinated libcvc PR. The C++ parity tests skip locally (pycvc not installed), so this Python change is independently green.

Trained checkpoints were optimised against the old force law and are stale for tight-clearance regimes → retrain + a planner v1-vs-v2 delta is Contract-2 T0. The behavioural delta is scenario-dependent and negligible on easy routes (a doorway episode was byte-identical); it concentrates where an agent hugs an obstacle.

The obstacle-barrier derivative was not the derivative of the barrier it names.
For b(d) = -(d-d_hat)^2 log(d/d_hat), the returned dbdd exceeded the true
derivative by exactly (d - d_hat) + 1 — verified against autograd. That put an
attraction band over ~(0.39-0.80)*d_hat where the barrier pulled the agent
toward the obstacle, and made the force field the gradient of no potential.

Corrected at the four Python sites that carry the closed form:
  - train_coef_energy.ipc_piecewise (canonical, value+derivative)
  - surrogate_robust.ipc_piecewise (ImportError fallback copy)
  - sdf_nav._ipc_dbdd (derivative-only; the .clamp(max=0) workaround at the
    steering site is now a no-op but is left in place)
  - scripts/spline_stagewise6.IPCBarrier._piecewise
The new form is the analytic derivative of b, matching autograd to machine zero
and purely repulsive on (0, d_hat). The softplus SDF hazard barrier
(sdf_barrier_grad) was already exact and is untouched.

test_material_nav's hand-written integrator oracle re-derived to match.
test_ipc_barrier_is_gradient locks the property in (dbdd == autograd(b) to 1e-9;
no attraction band). One incidental: the corrected trajectory in
test_vehicle_refinements exposes a pre-existing ~5e-7 nondeterminism in
SdfNavigator.step (present on main, single-thread too) that the buggy trajectory
dodged; its bitwise assert is relaxed to allclose(1e-4) — inertness is an
O(metres) claim — with the real nondeterminism tracked separately.

Suite: 289 passed, 26 skipped (native pycvc/CUDA parity, VTK). This is the
Python half of M10; the C++ cvc::nav copies (material_rollout.h, drive.cpp/.cu,
diff_rollout.h) plus their two analytic VJP adjoints and a force-law provenance
byte in the .cvcnav/.cvcnm headers land as the coordinated libcvc PR.
test_navigator_defaults_are_inert compares two SdfNavigators built by the
_navigator() helper. That helper constructs sdf_nav.CoefMLP() without seeding,
so the two agents were drawing DIFFERENT random policy weights from the global
RNG stream -- the ~5e-7 divergence the M10 stopgap saw was two different
policies, not a nondeterministic op.

SdfNavigator.step is fully deterministic: given identical weights it is
bit-identical run-to-run, single- vs multi-threaded, across processes, and
under torch.use_deterministic_algorithms(True) (nothing is flagged). The
manual_seed(0) calls the test placed *after* construction were inert -- step()
consumes no RNG -- so they never equalised the weights. The strict torch.equal
assertion passed before M10 only because the pre-M10 barrier (the buggy +1
attraction band) left this open-field trajectory insensitive enough to the
coefficients to come out bit-identical despite the differing weights; the
corrected M10 barrier is coefficient-sensitive here (the wall barrier is active,
d=0.31 < d_hat=0.35), which surfaced the difference.

Fix the root cause in the helper: seed torch before CoefMLP() so both agents
carry identical policy weights. This also strengthens
test_navigator_threads_footprint_and_grip_into_the_drive (plain vs refined now
differ only by the refinements, not by their init). Restore the exact
torch.equal(a.o, b.o) / torch.equal(a.th, b.th) assertion and replace the
"multithreaded-reduction nondeterminism" comment with what was actually going on.
The M10 commit added this file in a form that fails CI's
`black --check grl_snam tests` and `ruff check` (blank line after the module
docstring, third-party/first-party import grouping, and the two multi-line
asserts). Pure formatting, no behaviour change; the two tests still pass.
@transfix
transfix merged commit 772dfec into main Sep 8, 2026
3 of 9 checks passed
transfix added a commit that referenced this pull request Sep 8, 2026
…ed barrier (#63)

train_native() hardcoded the surrogate refinement lr at 2e-4 — the pre-M10 value —
which OVERRODE the C++ coef_trainer's own default when dispatching to the native
backend. The M10 barrier fix (#61) made the IPC barrier steeper (b'' gains +1/d^2),
so 2e-4 now overshoots and DEGRADES the trained policy: on the city scene (seed 0),
trained reach falls 71% -> 26%. Retuned to 5e-5, matching libcvc's coef_train.h
default (1e-4..1e-5 all recover; 5e-5 is mid-band). The bicycle path keeps its own 1e-5.

The torch imitation trainer (lr=1e-3) and scripts/train_on_geometry.py (lr=3e-4) are
different regimes not covered by the surrogate sweep and are left unchanged pending
their own validation. See the training guide's corrected-barrier section.
@transfix transfix mentioned this pull request Sep 8, 2026
transfix added a commit that referenced this pull request Sep 8, 2026
Two fixes since v0.2.0, both parts of the M10 IPC-barrier correction:

- #61: the IPC wall-barrier FORCE is now the true gradient of the barrier
  b(d) = -(d-d_hat)^2 * log(d/d_hat). The shipped force carried a spurious
  (d-d_hat)+1 factor that opened an attraction band inside d_hat; corrected
  to the analytic derivative -(2*(d-d_hat)*log(d/d_hat) + (d-d_hat)^2/d),
  verified against autograd.
- #63: the native-surrogate trainer default lr retuned 2e-4 -> 5e-5. The
  corrected (steeper) barrier made the old rate overshoot and DEGRADE the
  policy (city seed-0 trained reach 71% -> 26% at 2e-4; 1e-4..1e-5 recover).

Fixes only, pre-1.0 -> patch bump. Version lives in pyproject.toml; the three
cvcpkg columns mirror it in upstream_version and the package.files dist-info
path. cvc_revision stays 1 (a new upstream version restarts its revision
count, and 0.2.0 was already revision 1).
transfix added a commit that referenced this pull request Sep 28, 2026
…ed barrier (#106)

train_native() hardcoded the surrogate refinement lr at 2e-4 — the pre-M10 value —
which OVERRODE the C++ coef_trainer's own default when dispatching to the native
backend. The M10 barrier fix (#61) made the IPC barrier steeper (b'' gains +1/d^2),
so 2e-4 now overshoots and DEGRADES the trained policy: on the city scene (seed 0),
trained reach falls 71% -> 26%. Retuned to 5e-5, matching libcvc's coef_train.h
default (1e-4..1e-5 all recover; 5e-5 is mid-band). The bicycle path keeps its own 1e-5.

The torch imitation trainer (lr=1e-3) and scripts/train_on_geometry.py (lr=3e-4) are
different regimes not covered by the surrogate sweep and are left unchanged pending
their own validation. See the training guide's corrected-barrier section.
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