Skip to content

nav: 3-4x faster exact EDT/SDF rebuilds in sim_world (bit-identical) - #503

Merged
transfix merged 5 commits into
masterfrom
perf/nav-sdf-rebuild
Oct 1, 2026
Merged

transfix merged 5 commits into
masterfrom
perf/nav-sdf-rebuild

Conversation

@transfix

Copy link
Copy Markdown
Owner

Profiling a multi-plane sim_world workload showed step()'s plane rebuilds dominated by rebuild_plane → build_sdf → edt2_squared → edt1d_line. This makes them 3–4× faster with every output bit unchanged.

Changes

  • Exact binary-mask fast path for edt2_squared and build_sdf:
    • The column pass becomes two vectorized integer sweeps, done in row order, over the mask and its complement together. This is exact because seed breakpoints fall on (q+v)/2, and 1e20 parabolas never win a cell.
    • The row pass runs the old kernel's exact arithmetic, but only over seeded columns.
    • Per-thread scratch is reused across calls.
    • No float substitution and no FMA reassociation in the double EDT.
  • build_sdf into caller buffers (a new overload). sim_world::rebuild_plane builds straight into field_, then clips in place.
  • to_occupancy reuses the previous cell's decision when the log-odds float bits repeat. Every distinct value still goes through the same sigmoid.
  • Rebuild trigger: a plane is rebuilt only when its composited occupancy changes, since the field is a pure function of it. field_version() now bumps if and only if some field actually changed. This removed hundreds of spurious bumps in the fuzz, though it's a no-op for the default log-odds increments. plane_version() docs now say what it counts: belief sign flips, not every occupancy change.

Numbers (single thread, 384² synthetic city)

before after
build_sdf per call 11.8 ms 2.8 ms
edt2_squared per call 5.0 ms 0.8 ms
build_sdf 1024² 121 ms 33 ms
sense tick, 24 agents over 4 belief planes 128 ms 34 ms

Exactness

  • NavEdtExact: a verbatim copy of the old kernel checked bit-for-bit against the new edt2_squared, both build_sdf overloads and clearance_cost, over 468 grids plus a city layout. Grids cover 1×N, N×1, odd sizes, all-free, all-set, single seed or hole, noise and blocks.
  • NavBeliefOccupancy.ToOccupancyMatchesPerCellReference: signed zeros, infinities, NaN, denormals, and one-ulp steps around each threshold.
  • NavSimWorld.VersionOnlyFlipDoesNotRebuildButOccupancyChangeDoes.
  • Independent adversarial review: 0 mismatches over 42,000+ fuzzed EDT, SDF and occupancy cases and 1,800 random sim_world step sequences (including set_live_knobs, add_obstacle, dynamic TTL, pooled runs).
  • Python reference: a torch-free comparison with the numpy EDT, build_sdf and belief code gave 0 mismatches over 800+ cases.
  • CUDA: NavSimWorldCuda passes on a GTX 1650. drive.cu uses the host build_sdf.
  • Totals: 18 nav test binaries, 167 tests, all pass.

ABI

Removing sim_world::last_version_ changes the class layout, so pycvc, the cvcGL examples and downstream consumers need a rebuild and a revision bump when republished.

sim_world::step() rebuilt plane m's SDF when version_[m] had moved OR the
composited occupancy differed. version_ counts log-odds sign flips (p
crossing 0.5), which need not cross the planning threshold: under the
pessimistic policy a decaying phantom wall flips at p ~0.4 while it is
still occupied, and with tuned l_occ/l_free a cell can flip inside the
optimistic band. Those "version-only" rebuilds redo the full EDT pair to
produce a bit-identical field (rebuild_plane is a pure function of the
occ_ plane plus fixed cfg bounds/scale), and bump field_version, which
field_version() consumers treat as a re-mesh / replan signal.

Trigger on the occupancy compare alone and drop last_version_, which
existed only for the old trigger. plane_version() is unchanged (it
still reports the belief flip counter). field_version now bumps exactly
when some plane's occupancy, and therefore its field, changed.

Synthetic 384^2 city, 24 agents over 4 belief planes, 400 ticks:
- default config (all-free prior, optimistic): 0 version-only rebuilds,
  no change (374 rebuilds either way);
- phantom-wall prior + pessimistic: rebuilds 68 -> 56, field_version
  bumps 49 -> 41;
- l_occ 0.85: rebuilds 393 -> 376.
Final poses, fields and belief rasters hash identically before/after.

The new test walks a single phantom cell through a version-only flip
and then an occupancy change; it fails on the old trigger at the flip.
Profiling a multi-plane sim_world workload showed step()'s plane
rebuilds dominated by rebuild_plane -> build_sdf -> edt2_squared ->
edt1d_line. The kernel ran the generic Felzenszwalb-Huttenlocher line
transform down every column (stride-cols walk) and across every row,
twice per build_sdf, allocating two vectors per line.

The inputs are binary (f = mask ? 0 : 1e20), which makes most of that
work provably irrelevant while every output bit stays the same:
- column pass: with a seed in the column the seed envelope is exact
  ((q+v)/2 breakpoints) and 1e20 parabolas break at ~1e20/(2n), outside
  the grid, so the output is exactly (nearest seed distance)^2; two
  vectorized row-order integer sweeps compute it for the mask and its
  complement at once. Seed-free columns keep the generic kernel's value.
- row pass: 1e20 inputs never win a cell and never disturb a finite
  breakpoint, so the kernel's own arithmetic (same expressions,
  comparisons, order) runs over just the seeded columns; a seed-free
  grid falls back to the generic kernel.
- build_sdf forms phi per row while both distance rows are hot, takes
  one sqrt per cell (one of the two squared distances is 0; the
  expression is bit-equal and falls back to two sqrts otherwise), and
  hoists grad1d's edge branches so the normal loop vectorizes with the
  same float expressions.
- scratch is per-thread and reused; no per-line or per-call allocation
  besides the returned buffers.
No float substitution or FP-contraction change: the double EDT has no
multiply-add, and the normal expression is textually unchanged.

NavEdtExact compares edt2_squared, build_sdf and clearance_cost bit for
bit against a verbatim copy of the old kernel on 468 randomized and
degenerate grids (1xN, Nx1, odd sizes, all-free, all-set, single seed /
hole, noise, block cities) plus a 300x260 city; it fails on injected
sweep and sign mutations.

Single-thread, median per call, Xeon E5-2650 v2 (old | new):
- 384^2 belief planes captured from a synthetic-city sim_world run:
  build_sdf 11.8 | 2.8 ms, edt2_squared 5.0 | 0.8 ms
- 384^2 dense synthetic city: build_sdf 13.3 | 4.9 ms
- 1024^2 synthetic city: build_sdf 121 | 33 ms, edt2_squared 52 | 15 ms
- sim_world sense tick (4 planes, 384^2): ~65-70 | 23 ms; final poses,
  fields and belief rasters hash identically.
drive.cu's CUDA twin builds its static planes with the host build_sdf,
so it picks this up unchanged.
rebuild_plane() took a freshly allocated sdf_field (three rows*cols
float vectors) from build_sdf and then copied it, with the phi clip,
into the plane's [3,H,W] slice of field_. With the EDT itself now a few
ms, that allocate + first-touch + copy was ~1 ms of every plane rebuild
on a 384^2 grid.

Add a build_sdf overload that writes phi / normal_x / normal_y into
caller-owned buffers (the sdf_field overload now wraps it), and have
rebuild_plane build straight into field_ and clip phi in place. The clip
still runs after the normals are taken from the unclipped phi, so the
field is bit-identical; the NavEdtExact build_sdf test now checks the
new overload against the reference kernel too.

sim_world, 4 belief planes on a 384^2 synthetic city, single thread:
rebuild per sense tick 20.3-22.0 -> 16.3-18.4 ms over three runs; the
final poses / fields / belief rasters hash is unchanged.
…pancy

sim_world::step() composites every belief plane on every sense tick to
find the planes whose occupancy changed, and to_occupancy evaluated a
float32 expf sigmoid per cell to do it. With the EDT now cheap, that
was ~1.5 ms of the ~4.5 ms per-plane rebuild on a 384^2 grid, and it is
paid even by planes that end up unchanged.

The decision is a pure function of the cell's float bits, and a belief
plane is mostly long runs of one value (the +-l_clamp saturated prior,
cells sensed back to the clamp). Reuse the previous cell's decision
when the bits repeat; every distinct value still goes through the same
float32 sigmoid and double-threshold compare, so the raster is
identical. The new test checks both policies against the per-cell
expression on runs of signed zeros, infinities, NaN, denormals and
ulp ladders straddling both thresholds, through to_occupancy and
composite_occupancy.

to_occupancy on a 384^2 plane: 1.47 -> 0.35 ms. sim_world, 4 belief
planes on a 384^2 synthetic city, single thread: rebuild per sense
tick 17.5-18.6 -> 12.9-13.6 ms over three runs; final poses / fields /
belief rasters hash unchanged.
The comment said plane_version(m) bumps "when plane m's belief/occupancy
raster changes" and suggested gating raster re-uploads on it. It counts
log-odds sign flips (one bump per sensing agent with any flip). The
planning raster belief_occ(m) also changes on threshold crossings that
are not flips (the pessimistic clear in
VersionOnlyFlipDoesNotRebuildButOccupancyChangeDoes happens on a tick
with no bump) and when an add_obstacle() stamp enters or expires.
ever_seen / last_visible change whenever a field of view moves. A
consumer that trusts the old comment can show a stale raster.

Say what it counts, list the changes it misses, and point at
field_version(), which after the occupancy-only rebuild trigger is the
exact (world-level) occupancy/field change signal. Comment only; no
behaviour change.
@transfix
transfix merged commit 1314d8b into master Oct 1, 2026
13 checks passed
@transfix
transfix deleted the perf/nav-sdf-rebuild branch October 29, 2026 20:54
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