Non-Contradiction / autodiffr

Automatic Differentiation for R
https://non-contradiction.github.io/autodiffr/
Other
23 stars 2 forks source link

Progress of Wrapping APIs of ReverseDiff #3

Closed Non-Contradiction closed 6 years ago

Non-Contradiction commented 6 years ago

The ReverseDiff api is at http://www.juliadiff.org/ReverseDiff.jl/api/

Follow a similar discussion as to #2, we omit everything with ! because ! is julia's notation for mutating the content of input, which common native R function will not do.

Gradients of f(x::AbstractArray{<:Real}...)::Real

Jacobians of f(x::AbstractArray{<:Real}...)::AbstractArray{<:Real}

Hessians of f(x::AbstractArray{<:Real})::Real

The AbstractTape API

ReverseDiff works by recording the target function's execution trace to a "tape", then running the tape forwards and backwards to propagate new input values and derivative information.

In many cases, it is the recording phase of this process that consumes the most time and memory, while the forward and reverse execution passes are often fast and non-allocating. Luckily, ReverseDiff provides the AbstractTape family of types, which enable the user to pre-record a reusable tape for a given function and differentiation operation.

Note that pre-recording a tape can only capture the the execution trace of the target function with the given input values. Therefore, re-running the tape (even with new input values) will only execute the paths that were recorded using the original input values. In other words, the tape cannot any re-enact branching behavior that depends on the input values. You can guarantee your own safety in this regard by never using the AbstractTape API with functions that contain control flow based on the input values.

Similarly to the branching issue, a tape is not guaranteed to capture any side-effects caused or depended on by the target function.

The AbstractConfig API

For the sake of convenience and performance, all "extra" information used by ReverseDiff's API methods is bundled up in the ReverseDiff.AbstractConfig family of types. These types allow the user to easily feed several different parameters to ReverseDiff's API methods, such as work buffers and tape configurations. ReverseDiff's basic API methods will allocate these types automatically by default, but you can reduce memory usage and improve performance if you preallocate them yourself.

Optimization Annotations

Update

Since we have finished all the basic implementations. The next step will be:

Non-Contradiction commented 6 years ago

It seems that if we are not allowing mutating functions, then the tape api is useless. So they will not be wrapped temporarily.

Non-Contradiction commented 6 years ago

For the abstract config api, it seems that currently we don't have to use ::Type{D} because in R we usually only deal with Float64 and for most R users (I guess ?), it will seem weird to use functions like that.

Non-Contradiction commented 6 years ago

Finish exportation and documentation of wrapper functions. Add some basic tests for wrapper functions. Need to adopt tests at https://github.com/JuliaDiff/ReverseDiff.jl/tree/master/test https://github.com/JuliaDiff/DiffTests.jl/blob/master/src/DiffTests.jl to close this issue.

Non-Contradiction commented 6 years ago

Close. To be continued at #17.