ChainRules.jl icon indicating copy to clipboard operation
ChainRules.jl copied to clipboard

Chainrule for CUDA reduction

Open renatobellotti opened this issue 3 years ago • 4 comments

Hi,

I'd like to suggest including a rule for GPU reductions.

using Zygote

function my_loss(v)
    # This works:
    # l = sum(v)
    # This does not work:
    l = reduce(+, v)
    return l
end

v = cu([1., 2.])
Zygote.gradient(my_loss, v)

See also: https://github.com/FluxML/Zygote.jl/issues/730#issuecomment-1221146525

renatobellotti avatar Aug 20 '22 05:08 renatobellotti

rrule(reduce, +, x; kw...) can just call rrule(sum, x; kw...) right?

mcabbott avatar Aug 20 '22 10:08 mcabbott

Isn't the reduction implemented on the GPU? I don't know the details, but reducing on the GPU and then copying the result is certainly more efficient than copying the entire vector and reducing on the CPU.

renatobellotti avatar Aug 20 '22 11:08 renatobellotti

Sure. The rrule for sum just calls sum again on what it's given, for the forward pass, and thus uses the same GPU code as without AD. (And the reverse pass is written using broadcasting, which also works on the GPU.)

mcabbott avatar Aug 20 '22 17:08 mcabbott

Nice!

renatobellotti avatar Aug 22 '22 11:08 renatobellotti