Repository navigation
Split the scatter max/min gradient evenly between tied values - #937
Conversation
|
Equal sharing is a reasonable policy and matches Mooncake's allocating rule. The current implementation has two correctness regressions and makes the common case without ties substantially slower.
🧔♂️🤖 Assisted by GPT-6 |
…em, divide once per destination in Float32 for half precision
|
Thanks for the careful review, @CarloLucibello. I've addressed all three points, now with a test for each regression: Float16 count overflow. The cotangent of each destination is divided by its tie count with Nested Zygote. The tie counting ( One count, one division, atomics only for winners.
Your examples, run locally on CPU: In-place I don't have a GPU, so I couldn't reproduce your RTX 5090 timing or run the CUDA tests. The CUDA scatter suite is skipped in |
|
Both correctness points are fixed on CPU and CUDA: 70,000 tied Float16 (and BFloat16) sources each get
Please also merge master: the Buildkite PR relay check reports that the workflow files differ from master, so no GPU CI has run on this PR yet. 🧔♂️🤖 Assisted by Claude Sonnet 5.5 |
… mask and counts once in the Enzyme rule
|
Thanks, @CarloLucibello, the combined kernel is a nice simplification. Pushed cc182a7 (plus a merge of master for the Buildkite relay):
Pullback without ties, CPU, BenchmarkTools medians (shared box, so noisy; master's first number was 7.8 ms in my last run):
|
Fixes #922.
The pullback of
scatter(max, ...)/scatter(min, ...)was a plain equality mask,(src .== gather(dst, idx)) .* gather(Δ, idx). When several sources tie for the extremum of a destination, each of them got the full cotangent, so withntied sources the gradient of that destination was countedntimes and no longer matched the function.scatter!did the same between the olddstvalue and the sources.This PR splits the cotangent of each destination evenly between the values tied for its extremum, which is what PyTorch's
scatter_reducedoes foramax/amin, and also what NNlib's Mooncake rule forscatter(max/min)already does (_scatter_extremum_grads!inext/NNlibMooncakeExt/rules.jl). So the Zygote/ChainRules path now agrees with it. Without ties nothing changes.Change
In
src/scatter.jl:extremum_ties(dst, src, idx, start_tied)returns the tie masksrc .== gather(dst, idx)and the number of tied values per destination (Int32counts, as in themeangradient, for backends without 64-bit atomics), both from one pass oversrc. On the CPU that's a loop. On GPU arrays it's a KernelAbstractions kernel (_count_ties!) that writes the mask and has only the tied sources issue the atomic add, through_atomic_scatter!, so Metal keeps its special case. It is@non_differentiable, so the mutation stays out of nested AD (e.g. Zygote over Zygote).tie_share, where a count of 0 for a destination no index reaches, or aNaNone, divides by 1). Floats narrower than 32 bits divide inFloat32and round back, so more thanfloatmax(Float16)ties don't overflow. Then∂src = tied .* gather(share, idx).scatter(op, src, idx; init):initcompetes for the extremum, so when it ties with the sources it counts as one more (it has no gradient of its own here, as before). Other ops ignoreinit, as before.scatter!: the olddstvalue is counted in the same group as its sources, and∂dst = kept .* sharewithkept = dst_old .== dst. The new∇scatter!computes the mask, counts and shares once for both cotangents.kept, the tie mask and the counts once before the loop over lanes; each lane divides its seed once per destination.scatterdocstring states the tie rule. The comment above the Mooncake rule said ChainRules gives every tied source the full cotangent; I updated it.Example
gradient(f, Float32[2, 2, 3])Float32[1.0, 1.0, 1.0]Float32[0.5, 0.5, 1.0]gradient(x -> sum(scatter(min, x, [1, 1, 1])), Float32[0, 0, 0])Float32[1.0, 1.0, 1.0]Float32[0.33333334, 0.33333334, 0.33333334](f(x0 .+ h) - f(x0)) / h,sum(gradient)(1.9999981f0, 3.0f0)(1.9999981f0, 2.0f0)scatter!(max, ...), dst[2, 0], src[2, 1], idx[1, 1]: (∂dst, ∂src)(Float32[1.0, 1.0], Float32[1.0, 0.0])(Float32[0.5, 1.0], Float32[0.5, 0.0])scatter(max, x, [1, 1, 2]; init = 2f0)at[2, 2, 3]Float32[1.0, 1.0, 1.0]Float32[0.33333334, 0.33333334, 1.0]x = [1 1 5 1; 2 3 3 3], idx[1, 1, 2, 1]Float32[1.0 1.0 1.0 1.0; 0.0 1.0 1.0 1.0]Float32[0.33333334 0.33333334 1.0 0.33333334; 0.0 0.5 1.0 0.5]3×6input, no ties: same as the old masktruetrueTests
In
test/common_testsuite/scatter.jl:maxandmin: two and three tied sources, a 2-row source where each destination's gradient has to add up to its cotangent, a weighted loss,inittying with the sources, andscatter!with the olddsttied with one or more sources (and not tied). On the CPU it also checks 70,000 tiedFloat16sources (each gets1/70000), and, formax, Zygote over Zygote with and without a tie.test/gpu/cuda/scatter.jl: the 70,000-tie check forFloat16andBFloat16on CUDA (Metal has no atomicmaxforFloat16, so it isn't in the common suite on GPUs).max/minsources are now made distinct. A finite difference that moves one element at a time can't see a shared gradient at a tie (it sees1/2one-sided), so the ties are covered by the testset above instead.On master the new tie tests fail with the values in the master column. With this PR,
common_testsuite/scatter (CPU)(including the Enzyme comparisons) passes, 4272/4272, on Julia 1.13.3. I don't have a GPU, so I ran the GPU code path (the kernel, mask and counts) with JLArrays against the CPU loop for integer, tuple andCartesianIndexindices, but not CUDA or Metal.