Skip to content

Commit

Permalink
add docstrings for accumulate, accumulate! and store! (#16)
Browse files Browse the repository at this point in the history
* add docstrings for accumulate, accumulate! and store!

* Fix "pseudo" typo
  • Loading branch information
jrevels authored and ararslan committed Apr 15, 2019
1 parent 6cfd4b9 commit b747029
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,52 @@ Base.iterate(rule::AbstractRule) = (rule, nothing)
Base.iterate(::AbstractRule, ::Any) = nothing

"""
TODO
accumulate(Δ, rule::AbstractRule, args...)
Return `Δ + rule(args...)` evaluated in a manner that supports ChainRules'
various `AbstractDifferential` types.
This method intended to be customizable for specific rules/input types. For
example, here is pseudocode to overload `accumulate` w.r.t. a specific forward
differentiation rule for a given function `f`:
```
df(x) = # forward differentiation primitive implementation
frule(::typeof(f), x) = (f(x), Rule(df))
accumulate(Δ, rule::Rule{typeof(df)}, x) = # customized `accumulate` implementation
```
See also: [`accumulate!`](@ref), [`store!`](@ref), [`AbstractRule`](@ref)
"""
accumulate(Δ, rule::AbstractRule, args...) = add(Δ, rule(args...))

"""
TODO
accumulate!(Δ, rule::AbstractRule, args...)
Similar to [`accumulate`](@ref), but compute `Δ + rule(args...)` in-place,
storing the result in `Δ`.
Note that this function internally calls `Base.Broadcast.materialize!(Δ, ...)`.
See also: [`accumulate`](@ref), [`store!`](@ref), [`AbstractRule`](@ref)
"""
accumulate!(Δ, rule::AbstractRule, args...) = materialize!(Δ, broadcastable(add(cast(Δ), rule(args...))))

"""
TODO
store!(Δ, rule::AbstractRule, args...)
Compute `rule(args...)` and store the result in `Δ`, potentially avoiding
intermediate temporary allocations that might be necessary for alternative
approaches (e.g. `copyto!(Δ, extern(rule(args...)))`)
Note that this function internally calls `Base.Broadcast.materialize!(Δ, ...)`.
Like [`accumulate`](@ref) and [`accumulate!`](@ref), this function is intended
to be customizable for specific rules/input types.
See also: [`accumulate`](@ref), [`accumulate!`](@ref), [`AbstractRule`](@ref)
"""
store!(Δ, rule::AbstractRule, args...) = materialize!(Δ, broadcastable(rule(args...)))

Expand Down

0 comments on commit b747029

Please sign in to comment.