denizyuret/AutoGrad.jl

StackOverflowError in grad on broadcasted function

baggepinnen opened this issue · 3 comments

Minimal NWE

using AutoGrad
f(x) = x.^2
jf = AutoGrad.grad(f)
jf(1)

Note that f is a scalar function for scalar inputs

Somewhat related to #67

This is the current behaviour on julia 0.7 for the MWE

julia> using AutoGrad

julia> f(x) = x.^2
f (generic function with 1 method)

julia> jf = AutoGrad.grad(f)
(::getfield(AutoGrad, Symbol("#gradfun#3")){getfield(AutoGrad, Symbol("##gradfun#1#2")){typeof(f),Int64}}) (generic function with 1 method)

julia> jf(1)
┌ Warning: broadcast will default to iterating over its arguments in the future. Wrap arguments of
│ type `x::Rec{Int64}` with `Ref(x)` to ensure they broadcast as "scalar" elements.
│   caller = ip:0x0
└ @ Core :-1
2

While the result is correct, the warning is hinting at some pitfall in our current implementation of broadcast for recorded scalars.

This example works smoothly though:

julia> f(x) = sum(x.*[1,2,3])
f (generic function with 1 method)

julia> grad(f)(1)
6

This should be fixed in latest master, please test and close.

Works, thanks!