Repository navigation
nav: 3-4x faster exact EDT/SDF rebuilds in sim_world (bit-identical) - #503
Merged
Merged
Conversation
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.
This was referenced Oct 1, 2026
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.
Profiling a multi-plane
sim_worldworkload showedstep()'s plane rebuilds dominated byrebuild_plane → build_sdf → edt2_squared → edt1d_line. This makes them 3–4× faster with every output bit unchanged.Changes
edt2_squaredandbuild_sdf:build_sdfinto caller buffers (a new overload).sim_world::rebuild_planebuilds straight intofield_, then clips in place.to_occupancyreuses the previous cell's decision when the log-odds float bits repeat. Every distinct value still goes through the same sigmoid.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)
build_sdfper calledt2_squaredper callbuild_sdf1024²Exactness
NavEdtExact: a verbatim copy of the old kernel checked bit-for-bit against the newedt2_squared, bothbuild_sdfoverloads andclearance_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.sim_worldstep sequences (includingset_live_knobs,add_obstacle, dynamic TTL, pooled runs).build_sdfand belief code gave 0 mismatches over 800+ cases.NavSimWorldCudapasses on a GTX 1650.drive.cuuses the hostbuild_sdf.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.