Skip to content

hdr: recover moments_u32 / MomentsU32 and Cascade::observe_batch (HDR-only harvest from #325) - #327

Merged
AdaWorldAPI merged 2 commits into
masterfrom
claude/llvm-codegen-polyfill-gni3cw
Sep 24, 2026
Merged

AdaWorldAPI merged 2 commits into
masterfrom
claude/llvm-codegen-polyfill-gni3cw

Conversation

@AdaWorldAPI

@AdaWorldAPI AdaWorldAPI commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

What

A recovery of the HDR/popcount hunks that #326 reverted. Nothing else from #325 returns.

Hamming distances → moments_u32 → Welford / Chan merge → Cascade state

The first commit, 1c31868, applies the #325 hunks unchanged: its diffs of the two recovered files are byte-identical to #325's. The second commit, 92adbd4, fixes two defects that Codex review found in that recovered code (see Review fixes), so the final diff is no longer byte-identical to #325.

#325 hunks recovered

file hunk contents
src/hpc/statistics.rs @@ -360,3 +360,203 MomentsU32 (exact integer n / Σx / Σx², an exact order-independent merge, exact u128 variance) and moments_u32 (U64x8 lanes, split 32-bit squares, 2²⁸-chunk drain), plus tests
src/hpc/cascade.rs @@ -208,6 +208,56 Cascade::observe_batch (Chan–Golub–LeVeque merge, per-batch drift alert)
src/hpc/cascade.rs @@ -768,6 +818,76 the 3 observe_batch tests
src/simd.rs from @@ -617,6 +617,8 the moments_u32 / MomentsU32 re-export line only; the hunk was split

Review fixes (92adbd4)

  • observe_batch alert gate. The recovered code tested old_n > 10. observe counts the new element before it tests > 10, so it alerts with exactly 10 prior observations. observe_batch(&[x]) therefore stayed silent where observe(x) alerted. The gate is now old_n >= 10. Test: observe_batch_singleton_matches_observe_alert_gate.
  • MomentsU32::variance fallback. When n·Σx² or (Σx)² overflowed u128, the fallback subtracted two floats of magnitude around 2⁶⁴ and could cancel the variance to 0. Example: 2³³ values split between u32::MAX and u32::MAX - 1 returned 0.0 instead of 0.25. The fix centres on q = ⌊Σx/n⌋ exactly in u128 first. Test: variance_fallback_does_not_cancel.

Both tests failed before the fix and pass after it.

#325 hunks intentionally left reverted

  • src/hpc/zspace.rs (whole file): ZGamma, ln_det, fisher_z, hyperbolic_depth, hamming_null_z, and the golden digests.
  • src/hpc/mod.rs @@ -25,6 +25,9: the pub mod zspace line.
  • src/simd.rs: the zspace re-export line.
  • src/hpc/reliability.rs, all 4 hunks: the *_z accessors and their doc and test.
  • crates/wasm-simd-parity/src/lib.rs, both hunks: the ZGamma golden check.

Validation

  • Scope: 3 files; nothing is removed.
  • Forbidden terms: none of ZGamma|zspace|fisher|hyperbolic|similarity_z|palette appear in the added lines. The one whole-file hit, cascade.rs "Berry-Esseen Fisher efficiency", is an existing master doc line.
  • Palette256 / bgz-tensor: no files touched.
  • Tests (hpc::statistics + hpc::cascade): 35/35 on native, config-v3 (AVX2), config-v4 (AVX-512), and aarch64 under qemu.
  • Lints and formatting: clippy -D warnings and fmt are clean.
  • Benchmarks: zspace: Fisher-Z entry tax without materializing z (ZGamma codes) + binomial Hamming null; batch Welford recovered from #323 #325 contained no committed benchmark for this path.

🤖 Generated with Claude Code

https://claude.ai/code/session_019HnekoM1EidTwQLS3oFVFm

Summary by CodeRabbit

  • New Features
    • Added integer-sample statistics for calculating exact counts, sums, and sums of squares, along with mean and population variance. Results can be combined across samples, and the functionality is available through the SIMD interface.
  • Bug Fixes
    • Batch observations now trigger drift alerts under the same minimum-observation threshold as individual observations, including for single-item batches.

…325

Mechanical recovery of the HDR/popcount hunks that #326 reverted, applied
unchanged:
- src/hpc/statistics.rs: MomentsU32 (exact integer n / Σx / Σx², order-
  independent merge, exact u128 variance) and moments_u32 (U64x8 lanes,
  split 32-bit squares, 2^28-chunk drain) with their tests.
- src/hpc/cascade.rs: Cascade::observe_batch (Chan-Golub-LeVeque merge of
  batch moments into the rolling floor, per-batch drift alert) with its
  three tests.
- src/simd.rs: the moments_u32 / MomentsU32 re-export line only.

Everything else from #325 stays reverted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019HnekoM1EidTwQLS3oFVFm
@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Essentials

Run ID: d65421bd-c60a-440d-8a12-06f1c8af1c5a

📥 Commits

Reviewing files that changed from the base of the PR and between 1c31868 and 92adbd4.

📒 Files selected for processing (2)
  • src/hpc/cascade.rs
  • src/hpc/statistics.rs

Included review availability: 1 review is currently available. Your included PR review attempts over the past 7 days set your current allowance at 3 reviews per hour.


📝 Walkthrough

Walkthrough

The change adds public u32 moment calculations with SIMD accumulation and merging. It also changes the batch drift-alert threshold in Cascade to include cases with exactly 10 prior observations.

Changes

Integer Moments

Layer / File(s) Summary
Define moment values and arithmetic
src/hpc/statistics.rs
Adds MomentsU32 with exact integer count, sum, and sum-of-squares fields, plus merge, mean, and variance methods.
Compute and expose u32 moments
src/hpc/statistics.rs, src/simd.rs
Adds SIMD-based moments_u32, tests for computation and merging, and public facade re-exports.

Cascade Alert Threshold

Layer / File(s) Summary
Align batch alert threshold
src/hpc/cascade.rs
Changes the batch alert condition from more than 10 to at least 10 prior observations. Updates the documentation and adds a test comparing observe with a singleton observe_batch at the boundary.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Suggested reviewers: claude

Merge Risk: 🔵 Low · up to 92adb

Very large sparse aggregates can report zero variance incorrectly. The impact is narrow and does not appear to block merging.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 73.91% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 23 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main changes: restoring moments_u32, MomentsU32, and Cascade::observe_batch. It also states that the changes are an HDR-only recovery from #325.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

A rabbit counts each sum with care
Eight swift lanes race through numbers there
The moments merge, then mean takes flight
Ten old steps can flag a shift
A batch and single match just right
The rabbit hops into the night

Comment @coderabbitai help to get the list of available commands.

@cursor

cursor Bot commented Sep 24, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_e2c61841-6f3b-4707-882a-675485386313)

@AdaWorldAPI
AdaWorldAPI marked this pull request as ready for review September 24, 2026 17:02
@cursor

cursor Bot commented Sep 24, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_c5b89a13-8743-4d84-b23d-95fb501e2fc1)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1c31868327

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/hpc/statistics.rs Outdated
Comment thread src/hpc/cascade.rs Outdated
Two defects in the code recovered from #325, both found in review:

- observe_batch gated its drift alert on `old_n > 10`, while observe
  counts the new element first and tests `> 10`, i.e. >= 10 prior. At
  exactly 10 prior observations, observe_batch(&[x]) stayed silent where
  observe(x) alerted. Gate is now `old_n >= 10`.
- MomentsU32::variance's fallback (when n*sum_sq or sum^2 overflows
  u128) subtracted two ~2^64-scale f64 values and could cancel the
  variance to 0 (2^33 values split between u32::MAX and u32::MAX-1:
  0.0 instead of 0.25). It now centres on q = floor(sum/n) exactly in
  u128 first, so the float step works on variance-sized quantities.

Each fix has a regression test that failed before the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019HnekoM1EidTwQLS3oFVFm
@AdaWorldAPI
AdaWorldAPI merged commit 031f5c8 into master Sep 24, 2026
26 checks passed
AdaWorldAPI pushed a commit that referenced this pull request Sep 24, 2026
Adds hpc::rolling_floor, the adaptive half of the HDR exposure meter,
harvested from lance-graph graph/blasgraph/hdr.rs (the behavioural
reference). Constants, cadence, drift rule and reset semantics carry over
unchanged; the arithmetic underneath is #327's exact MomentsU32 instead of
the reference's approximate integer Welford.

- isqrt_u32: the reference integer Newton square root.
- ReservoirU32: deterministic Algorithm-R reservoir (splitmix replacement
  hash keyed on the observation count), u32 empirical quantile, Pearson
  second skewness, kurtosis x100. Order-defined; no merge law.
- RollingFloor: calibrated (mu, sigma), sigma floors mu - k sigma,
  empirical floors at the reference percentiles, shape evaluation every
  1000 observations after the first 1000 (reservoir >= 100), normal iff
  |skew| < 2 and 200 < kurt < 500, drift at |dmu| > sigma/2 or
  |dsigma| > sigma/4, recalibration resets moments, reservoir and shape.
- observe_batch folds moments_u32 between checkpoints and stops after the
  first shift, so any batching reproduces the scalar observe/recalibrate
  loop exactly.
- MomentsU32::observe: scalar fold equal to merging a singleton batch.

A test keeps the legacy integer Welford as an oracle: across 95
checkpoints on five streams the (mu, sigma) the drift rule sees are
identical.

examples/hdr_rolling_floor_bench.rs separates the hot path from the
periodic shape path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019HnekoM1EidTwQLS3oFVFm
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.

2 participants