JuliaAttic / ReverseDiffSource.jl

Reverse automated differentiation from source
MIT License
47 stars 12 forks source link

Simplifications for zero vectors needed? #26

Open baggepinnen opened 8 years ago

baggepinnen commented 8 years ago

The following small example seems to produce very strange code, especially all the call to zeros() seem unnecessary?

x = randn(3)
g = randn(3)
test = quote
    d  = sum((x-g).^2)
    cs = d + log(d)
end
rdiff(ex,x=x)

julia> rdiff(ex,x=x) quote _tmp1 = xp - x _tmp2 = x - goal _tmp3 = _tmp1 .^ 2 _tmp4 = size(_tmp3) _tmp5 = _tmp2 .^ 2 _tmp6 = sum(_tmp5) _tmp7 = size(_tmp5) _tmp8 = _tmp6 + c3 (((c1 * _tmp6 + c2 * log(_tmp8)) + sum(_tmp3)) + sum(u .^ 2),(zeros(size(x)) + -((zeros(size(_tmp1)) + (2_tmp1) .* (zeros(_tmp4) + ones(_tmp4))))) + (zeros(size(_tmp2)) + (2_tmp2) .* (zeros(_tmp7) + ones(_tmp7) .* (c2 / _tmp8 + c1)))) end

fredo-dedup commented 8 years ago

This is a needed enhancement. The problem is that the simplification pass on the code cannot reduce zeros(..) + x to x without knowing the type of x (it simplifies if x is an Array, but not if x is a scalar); and the type of the variables are not always known at the simplification stage.

The change needed is not too complicated but it may make rdiff() significantly slower on more complex functions. I am trying to find a better solution involving larger changes in the code.