Skip to content

Reduce a single best match with max rather than topk - #45

Merged
mcencini merged 1 commit into
mainfrom
perf/top1-max
Oct 13, 2026
Merged

mcencini merged 1 commit into
mainfrom
perf/top1-max

Conversation

@mcencini

Copy link
Copy Markdown
Contributor

torch.topk selects by radix passes whatever k is. For k = 1, which is what dictionary matching normally asks for, it took 77 % of the card time of an exhaustive match (profiled on a laptop RTX 4060).

best_of(scores, k) in _grouped.py reduces with max when k == 1 and keeps topk otherwise; DictionaryMatcher._match_here and match_in_groups use it for both the per-chunk selection and the merge.

Measured from MRI2Qmap (mri2qmap-bartorch): 1,360 atoms of 7 real coefficients against the 16.7M voxels of a 256³ volume, exhaustive search:

whole volume
topk 6.0 s
max 1.9 s

Every voxel matched to the same atom (100.000 %). What remains is the abs of the correlation (49 %), the reduction (26 %) and the GEMM (22 %), all passes over the voxels × atoms score matrix.

Tests: test_one_best_candidate_is_the_first_of_topk; tests/estimators gives 131 passed, 2 skipped, 3 failed, where the 3 (test_the_training_set_is_the_same_size_however_it_is_chunked and the two [cpu] PERK-kernel tests that look for the fused path) fail identically on the released 0.0.13 wheel without this change.

🤖 Generated with Claude Code

torch.topk selects by radix passes whatever k is. With k = 1, which is
what dictionary matching asks for, it was three quarters of the card time
of an exhaustive match: 1,360 seven-coefficient atoms against 16.7M voxels
went from 6.0 s to 1.9 s on a laptop RTX 4060, every voxel matched to the
same atom. best_of keeps topk for k > 1, and both the exhaustive and the
grouped search use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mcencini
mcencini merged commit bff2394 into main Oct 13, 2026
22 checks passed
@mcencini
mcencini deleted the perf/top1-max branch October 13, 2026 15:02
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