Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AD in-place instead of broadcast #645

Closed
b-fg opened this issue Apr 28, 2023 · 1 comment
Closed

AD in-place instead of broadcast #645

b-fg opened this issue Apr 28, 2023 · 1 comment

Comments

@b-fg
Copy link

b-fg commented Apr 28, 2023

I am sure this is covered somewhere and #136 (comment) is very much related, but I am struggling to understand why does this MWE work

addX!(a, x) = sum(a .+ x)
a = ones(10)
∂addX(a, x) = ForwardDiff.derivative(x -> addX!(a, x), x)
∂addX(a, 1.0)

but not this one:

function addX!(a, x)
    for i ∈ axes(a)
        a[i] += x
    end
    sum(a)
end

a = ones(10)
∂addX(a, x) = ForwardDiff.derivative(x -> addX!(a, x), x)
∂addX(a, 1.0)

With error: ERROR: MethodError: no method matching +(::Vector{Float64}, ::ForwardDiff.Dual{ForwardDiff.Tag{var"#5#6"{Vector{Float64}}, Float64}, Float64, 1})

What is special about broadcasting instead of explicitly writing the loop?
Thanks!

@mcabbott
Copy link
Member

The broadcasting function does not mutate a. To be equivalent to the loop it would be addX!(a, x) = sum(a .= a .+ x), which gives the same error.

In general you will need to make arrays of the right type to store Dual numbers, e.g. using similar(x).

@b-fg b-fg closed this as completed May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants