Skip to content

Fuse DBE's per-patch entropy filter into a native kernel - #25

Merged
hd152 merged 1 commit into
mainfrom
native-dbe-patch-entropy
Sep 22, 2026
Merged

hd152 merged 1 commit into
mainfrom
native-dbe-patch-entropy

Conversation

@hd152

@hd152 hd152 commented Sep 22, 2026

Copy link
Copy Markdown
Owner

Summary

Continuing this session's profiling work (#22), profiled Phase 1 (Quality+Load) on a real long-sub session as planned — didn't find a Phase 1 bug (its cost there is simply linear in frame count, as expected, while Phase 4 is O(1)), but the same profiling run surfaced a fresh Phase 4 hotspot: DBE's entropy filter.

  • _filter_sampled_patches's entropy filter (use_entropy_weights, which --auto sets to entropy_bg=True for most target types) loops over up to Config.DBE_MAX_SAMPLES (4000) candidate patches calling np.histogram per patch — 4.2s on its own in a real profiled run, despite the neighboring dbe_sample_patches docstring describing it as "cheap".
  • New patch_entropy_batch native kernel: one rayon-parallel pass reproducing the exact patch-boundary mapping and Shannon-entropy formula. Not bit-exact against numpy (its histogram fast path has a floating-point edge-correction step this doesn't replicate) — entropy values agree to ~3e-4 absolute on a real-shaped case, immaterial for the median+MAD threshold this feeds.
  • Wired into _filter_sampled_patches with a transparent scipy/numpy fallback.
  • Measured on a real Omega Nebula session subset: Post-process 21.1s → 18.5s.

Test plan

  • Full test suite passes (1650 tests)
  • ruff check . and tools/lint_conventions.py clean
  • 4 new tests: isolated kernel parity vs. the Python reference, shape-mismatch rejection, end-to-end native-vs-fallback agreement through _filter_sampled_patches
  • Measured on real session data, not just synthetic

astro_native bumped 0.34.0 -> 0.35.0.

🤖 Generated with Claude Code

Profiling a real --auto run (entropy_bg=True by default for most
target types) found _filter_sampled_patches's Python loop -- up to
Config.DBE_MAX_SAMPLES (4000) candidate patches, each calling
np.histogram -- costing 4.2s on its own, despite dbe_sample_patches's
own docstring describing the entropy filter as "cheap, operates on the
small per-patch result". True per-call, but it runs on every DBE pass
regardless of session size, so the Python-loop overhead adds up.

Added patch_entropy_batch: one rayon-parallel native pass over the
sampled-patch coordinates, reproducing the exact patch-boundary
mapping and Shannon-entropy formula. Not bit-exact against numpy --
np.histogram's uniform-bin fast path has a floating-point edge-
correction step (compares each value against the actual linspace
edges and nudges the bin index where rounding put it off by one) that
this doesn't replicate, so entropy values agree to ~3e-4 absolute on a
real-shaped case, not bit-for-bit. Immaterial for what this feeds (a
median+MAD outlier threshold over thousands of patches).

Wired into src/background.py's _filter_sampled_patches with a
transparent fallback to the original Python loop. Measured on a real
Omega Nebula session subset: Post-process 21.1s -> 18.5s.

astro_native bumped to 0.35.0. 4 new tests (isolated kernel parity,
shape-mismatch rejection, end-to-end native-vs-fallback agreement);
full suite (1650 tests) passes.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@hd152
hd152 merged commit 5ba3b14 into main Sep 22, 2026
7 checks passed
@hd152
hd152 deleted the native-dbe-patch-entropy branch September 22, 2026 20:53
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