Repository navigation
Conversation
_scatter_diagonals compared the full (B, H, Lq, Lk) diagonal-id grid once per chosen diagonal (50 to 500 passes). Build a per-head bool table over diagonal ids with scatter_ and read it once with diag_id instead. When diag_id is shared by all heads, index only the last table dimension. The mask is identical. A mixed set of reference calls goes from 104.40 ms to 25.59 ms per run on one CPU thread. Co-Authored-By: Aiden <aiden@weco.ai>
yghstill
approved these changes
Nov 3, 2026
This branch has not been deployed
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.
TL;DR
_scatter_diagonalsin the minference CPU reference (vertical_and_slash_attention).diag_id.pytest tests/sparse/...: same result asmain(72 passed; the 5 failures are the same CUDA/Triton-only tests).What & Why
Before,
_scatter_diagonalslooped over every chosen diagonal. Each pass compared the full(B, H, Lq, Lk)id grid with one id and merged the result into the mask. The budget is at least 50 diagonals, so the code read the whole grid 50 to 500 times.Now the function builds one bool table per head over the diagonal ids. It sets
Trueat the chosen ids withscatter_. Then it reads the table once withdiag_id. Whendiag_idis shared by all heads (the normal call site), it indexes only the last table dimension.The output stays identical. An entry is
Trueexactly when its diagonal id is one of the chosen ids, as before. The table covers the full id range, so negative or large ids also work.Changes
angelslim/compressor/sparsity/algorithms/minference/reference.py: replace the per-diagonal compare loop in_scatter_diagonalswith a table lookup.Falsemask early when there are no chosen diagonals or the grid is empty.Results
Median time per
vertical_and_slash_attentioncall, CPU, one thread. The outputs are bit-identical (torch.equal) in every row.a_shapevariant, L=1024, 4 heads (does not use this code)The gain grows with sequence length and with the number of diagonals, because the old loop did one full pass per diagonal.
Why this is safe and easy to maintain
The change is small: one function, about 15 lines, with plain
scatter_and indexing. The mask has the same values, dtype and shape as before.test_vertical_and_slash_diagonal_alignmentand the othertests/sparsetests cover this path, and they pass.Validation
main(same result on both):python -m pytest -q tests/sparse/test_algorithms_integration.py tests/sparse/test_stem_integration.py tests/sparse/test_guards.py tests/sparse/test_save_load.py->
72 passed, 5 failed, 57 skipped. The 5 failures are the xattention/flexprefill/flashprefill head_dim and block-size checks andtest_sparsity_registered_in_compressor_factory. They also fail onmainon a CPU-only host without CUDA/Triton kernels, and they do not use this code.minference_reference_prefilldispatch). The before and after runs were interleaved on an isolated CPU core withOMP_NUM_THREADS=1, and every output was checked to be exactly equal. 104.40 ms -> 25.59 ms per run, 3 repeats (ratios 0.280, 0.243, 0.243).AI assistance
OpenAI Codex (gpt-6.1-sol) generated candidate versions of this change, and Weco Observe tracked them. Claude Code picked the simplest fast candidate and ran the tests and benchmarks above. The diff is the candidate source, unchanged.
Weco share link: https://dashboard.weco.ai/share/81ec1Qup7kZzZJiWFCcWekBExmbj_ZDz