Repository navigation
Conversation
Replaces `_mapreduce_kernel_expr` and the `@generated _mapreduce_kernel!` by `@inline` functions that recurse over the dimensions, similar to `_stridedloop` in TensorKit.jl. The loops and their variants are the same: unit strides in the innermost loop, accumulation in a register for reductions, `initop` on the first visit of each output block, and blocking. The loop bodies are passed as `@inline` closures, so that the kernel still compiles to a single function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Benchmarks of the generated kernel (#104 at eee0c43) against the recursive kernel (this PR at a41e609). Apple M4 Max, Julia 1.13.1, StridedViews 0.5.8. Minimum times. Each version ran twice, interleaved, in separate processes; the cases marked † are bimodal across processes for both versions (for example Run time, single-threaded
The small broadcast is the only case that is consistently slower, by 0.5–1 ns. For Run time, 4 threads (no reruns)
Small inputs do not use threads and give the same results as single-threaded. Compilation: time of the first call in a fresh process, median of three
The local test suite also finishes faster: the CPU tests with 1 and 5 threads take 4m49s with the recursive kernel and 5m35s with the generated one, mostly because of compilation. 🤖 Generated with Claude Code |
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
lkdvos
left a comment
There was a problem hiding this comment.
From the benchmarks I am concluding that performance is more or less unchanged (and probably depends on architecture/compiler version), so given that I think this is way easier to follow and fewer lines of code, I am overall very happy with this change!
I'm somewhat surprised that the compiler also seems to prefer this version, and compilation is a bit faster as well 🎉
One thing that I had wanted to play around with additionally is to see if we could further reduce the compilation time for large values of N by e.g. only inlining the loops up to N = 4, and accepting the function call overhead beyond that. This obviously requires some more measurements to see if that is a viable strategy, but there must be some cutoff where the compiler can't really do anything more with the nested loops and where the function call overhead far outweighs the compilation cost?
Otherwise the same comments as with the previous PR, it would be nice to tell Claude to reduce some of the comment prose, either writing some proper docstrings or just getting rid of them seems better than explaining things that are really just clear from reading the code.
| # applies `initop` to the entries of `A1` in a block of size `dims`, visiting every entry | ||
| # once: dimensions along which `A1` has stride zero are not looped over | ||
| @inline function _initloops!(initop::IOP, A1, dims, s1, i1::Int) where {IOP} | ||
| dims′ = map((d, s) -> ifelse(iszero(s), 1, d), dims, s1) |
There was a problem hiding this comment.
| dims′ = map((d, s) -> ifelse(iszero(s), 1, d), dims, s1) | |
| isnothing(initop) && return nothing | |
| dims′ = map((d, s) -> ifelse(iszero(s), 1, d), dims, s1) |
I think we can capture the initop === nothing case here, which avoids the if branches in the callers, making them slightly easier to read
| _loops!(@inline((n, s, I) -> _innerinit!(initop, A1, n, s[1], I[1])), dims′, map(tuple, s1), (i1,)) | ||
| return nothing | ||
| end | ||
| @inline function _innerinit!(initop::IOP, A1, n::Int, s::Int, i::Int) where {IOP} |
There was a problem hiding this comment.
I think this function is only used above, and since everything is inlined anyways, can we not just manually inline this one? Claude does sometimes have a tendency to create helper functions for everything, and if there are no performance benefits here I'd say it is easier to just read the code without the helper
| A1, As = arrays[1], tail(arrays) | ||
| @simd for _ in Base.OneTo(n) | ||
| @inbounds A1[ParentIndex(I[1])] = f(_args(As, tail(I))...) | ||
| I = _step(I, s) |
There was a problem hiding this comment.
Do we also want/need an explicit stride-1 version here, or does this auto-vectorize?
There was a problem hiding this comment.
never mind, just saw that this is handled through dispatch. Can we do the same for the _innerreduce!? I think I got confused because there the stride-1 version is explicit, while here it is implicit.
| end | ||
| @inline _step(I, s) = map(+, I, s) | ||
| @inline _step(I, ::Val{1}) = map(i -> i + 1, I) | ||
| @inline _args(arrays, I) = map((a, i) -> @inbounds(a[ParentIndex(i)]), arrays, I) |
There was a problem hiding this comment.
This is always used in combination with the function call, it might make sense to just have
@inline apply_f(f, arrays, I) = f(_args(arrays, I)...)
Builds on #104, and targets its branch
strided-planning; once #104 is merged, this PR can be retargeted tomain.This replaces
_mapreduce_kernel_exprand the@generated _mapreduce_kernel!by@inlinefunctions that recurse over the dimensions, following the idea of_stridedloopin QuantumKitHub/TensorKit.jl#626. Each level of the recursion loops over the last dimension and calls itself for the remaining ones; the base case runs the innermost loop.The kernel keeps its name, signature and the variants of the generated kernel:
initopon the first visit of each output block, along dimensions with a nonzero output stride;The loop bodies are passed to the recursion as
@inlineclosures. Without the@inline, every iteration of the outer loops calls the closure, with a GC frame for the captured arrays, which made small reductions 50% slower; with it, the kernel compiles to a single function without calls, as before.src/mapreduce.jl: 139 lines added, 269 removed. Run times are on par with the generated kernel, compilation is 15–30% faster; see the comment below for benchmarks.🤖 Generated with Claude Code