Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Add an internal helper function to do more in-place updating
Browse files Browse the repository at this point in the history
Currently the `∇(x̄, f, Arg{N}, args...)` method, which updates `x̄` with
the result of `∇(f, Arg{N}, args...)`. This is done in-place for some
functions `f` but not all. In the case of the fallback method, we can
use dispatch to determine whether it's safe to do this in-place.
  • Loading branch information
ararslan committed Mar 27, 2019
1 parent 7ee3613 commit a22bdb1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,14 @@ output and `ȳ` the reverse-mode sensitivity of `y`.
(y::Node, ȳ) = propagate(tape(y), reverse_tape(y, ȳ))
@inline (y::Node{<:∇Scalar}) = (y, one(unbox(y)))

@inline (x̄, f, ::Type{Arg{N}}, args...) where N =+ (f, Arg{N}, args...)
# This is a fallback method where we don't necessarily know what we'll be adding and whether
# we can update the value in-place, so we'll try to be clever and dispatch.
@inline (x̄, f, ::Type{Arg{N}}, args...) where {N} = update!(x̄, (f, Arg{N}, args...))
# Use broadcast for mixed array/scalar operations
@inline update!(x̄::∇Scalar, y::∇Array) =.+ y
@inline update!(x̄::∇Array, y::∇ArrayOrScalar) = (x̄ .=.+ y)
# Otherwise use regular addition
@inline update!(x̄, y) =+ y

"""
∇(f; get_output::Bool=false)
Expand Down

0 comments on commit a22bdb1

Please sign in to comment.