Fuse DBE's per-patch entropy filter into a native kernel - #25
Merged
Merged
Conversation
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>
3 tasks
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.
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--autosets toentropy_bg=Truefor most target types) loops over up toConfig.DBE_MAX_SAMPLES(4000) candidate patches callingnp.histogramper patch — 4.2s on its own in a real profiled run, despite the neighboringdbe_sample_patchesdocstring describing it as "cheap".patch_entropy_batchnative 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._filter_sampled_patcheswith a transparent scipy/numpy fallback.Test plan
ruff check .andtools/lint_conventions.pyclean_filter_sampled_patchesastro_native bumped 0.34.0 -> 0.35.0.
🤖 Generated with Claude Code