Skip to content

simd: masked_sum_i32 sums dense words branch-free, walks sparse ones - #376

Merged
AdaWorldAPI merged 1 commit into
masterfrom
claude/masked-sum-hybrid
Oct 24, 2026
Merged

AdaWorldAPI merged 1 commit into
masterfrom
claude/masked-sum-hybrid

Conversation

@AdaWorldAPI

Copy link
Copy Markdown
Owner

What

masked_sum_i32 now chooses its method one 64-bit mask word at a time:

  • Empty word: skipped, as before.
  • Sparse word (at most 16 set bits): keeps the existing set-bit walk.
  • Dense full word (more than 16 set bits): sums all 64 lanes without branching. Each value is widened to i64 and masked before the add, so the i64 contract still holds and the loop can vectorize.
  • Partial final word: always takes the walk, so it never reads past the end of values.

The threshold of 16 and the expected speed-up come from the D-MPF-2b measurement in lance-graph #1752: 2–5× faster than the walk, and never slower.

Measured in the consumer (lance-graph #1763, P2 probe, 4096-slot fold, median)

mask density before after
full, AVX-512 2 341 ns 650 ns
full, AVX2 2 306 ns 559 ns
25 % 475 ns 468 ns

The 25 % masks barely move: at about 16 set bits per word, most words stay on the walk path.

Verification

  • New test masked_sum_i32_both_word_routes_match_reference covers words just below, at and above the threshold, plus random densities. It asserts that at least 5 words take each path, so neither path can go untested.
  • Disable runs, each with an asserted patch anchor; all three are red:
    • dropping the mask on the dense path breaks 2 tests;
    • accumulating the dense path in i32 breaks 3 tests, including the widening test;
    • removing the full-word guard breaks the reference test at non-multiple-of-64 lengths.
  • cargo test --lib: 2627 pass.
  • cargo clippy --lib --tests -D warnings: clean.
  • scripts/masking-parity.sh native: PASS on AVX-512 and on config-v3 (AVX2), 19 groups.

The function is a free function in simd_masking_ops.rs and the same body serves every backend; no simd_{arch} file changed.


Generated by Claude Code

Per 64-bit mask word: empty words are skipped, words with at most 16 set
bits keep the set-bit walk, and full words with more than 16 set bits
sum all 64 lanes branch-free, each widened to i64 and masked, so the i64
contract holds and the loop vectorizes. The threshold and the speed-up
(2-5x, never slower than the walk) come from the D-MPF-2b measurement
in lance-graph #1752. A partial final word always takes the walk.

New test masked_sum_i32_both_word_routes_match_reference covers words
at, below and above the threshold plus random densities, and asserts the
fixture takes both routes.

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

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c24784aa-d94a-455f-9eb6-ba499f4fd373
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@AdaWorldAPI
AdaWorldAPI marked this pull request as ready for review October 24, 2026 23:32
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T23:33:29.419268Z 396596f Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AdaWorldAPI
AdaWorldAPI merged commit 195844b into master Oct 24, 2026
29 checks passed
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