Add hoist_invariants() directive for semiring reductions - #9190
Add hoist_invariants() directive for semiring reductions#9190alexreinking wants to merge 6 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## alexreinking/change-type #9190 +/- ##
===========================================================
Coverage ? 69.56%
===========================================================
Files ? 259
Lines ? 78797
Branches ? 19218
===========================================================
Hits ? 54816
Misses ? 18223
Partials ? 5758 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
9e244ff to
328fd0a
Compare
f2cbdc3 to
f271649
Compare
f271649 to
9660010
Compare
9660010 to
12834ac
Compare
12834ac to
d049c0c
Compare
d049c0c to
460d780
Compare
8f48f71 to
25be6cb
Compare
a26996d to
9787945
Compare
7d6e34b to
c8dc9c6
Compare
c8dc9c6 to
7e94a45
Compare
| Buffer<float> kernel(diameter); | ||
| kernel.set_min(-radius); | ||
| constexpr std::array<int, diameter> binomial_weights = { | ||
| 1, 14, 91, 364, 1001, 2002, 3003, 3432, |
There was a problem hiding this comment.
Better to just use arbitrary or random numbers here, or the lack of high freqs in the output might disguise output-is-shifted-over errors.
| } | ||
|
|
||
| constexpr int stride = 2; | ||
| constexpr int radius = 7; |
There was a problem hiding this comment.
Make radius much larger - 50 or 100. Make the output size smaller if that makes it take too long to run.
7e94a45 to
7a912b0
Compare
519d92d to
bd73073
Compare
bd73073 to
0c7be4f
Compare
0c7be4f to
1a19df0
Compare
abadams
left a comment
There was a problem hiding this comment.
Sadly need to delete the separable downsample test, because it doesn't do what we hoped, but LGTM for the rest of it.
e80fe7d to
3cbcddf
Compare
Introduce Stage::hoist_invariants(), which applies a semiring distributive law to hoist loop-invariant factors out of associative reductions. The returned intermediate accumulates the factor-free reduction over the original RDom, while the write-back applies the factor once. Support +/*, min/+, max/+, or/&&, and and/|| laws, including factors nested within associative chains. Add reusable binary-operator helpers, the Python binding, and correctness tests.
Add a quantized (int8 x int8 -> f32) mat-vec performance test targeting ARM's SDOT instruction, scheduled by composing eager_inline(), hoist_invariants(), rfactor(), and change_type(). eager_inline() folds the Wt and VecDq producers into Acc's update so their scale factors -- WtScale(i) and VecScale -- surface as explicit leaves that hoist_invariants() can then lift out of the reduction as the invariant product WtScale(i) * VecScale. rfactor() splits the now scale-free reduction by block, and change_type(Int(32)) retypes the innermost per-block dot product so CodeGen_ARM matches it to SDOT. The generated assembly contains sdot instructions, the Hoisted/PlainRfactor numerical cross-check passes, and the composed schedule is several times faster than the non-hoisting variant. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
3cbcddf to
fb002b3
Compare
…elf_reference A prior rebase silently merged this call site as a 3-argument Call::make() after the Function overload gained a required follow_global_wrappers parameter. This is a Func's self-reference to the rfactor intermediate, so it must not follow global wrappers, matching the other self-reference call sites. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
fb002b3 to
e0bc603
Compare
This PR adds a scheduling directive similar to
rfactorthat hoists invariant factors according to a detected distributive (semiring) law into the write-back part of the accumulation step. This enables writing straightforward quantized kernels in the algorithm language and factoring them into efficient kernels.This is meant to work towards optimizing quantized mat-mul and mat-vec kernels (especially those from GGML).
Checklist