An attempt to implement Reverse-Mode AD in terms of delcont primops introduced in GHC 9.6.
That is, it reimplements ad-delcont, which translates Scala implementation of Backpropagation with Continuation Callbacks, in terms of newPromptTag#, prompt#, and control0#.
- In computing multivariate gradients: in most cases, our implementation is at most slightly faster than Edward Kmett's
ad. In some cases, ours is 4x-10x faster. - To differentiate univariate functions, always use
adas it uses Forward-mode. - Our implementation in most cases outperforms
backpropandad-delcont(monad transformer-based impl).
transformers:ad-delcontad:ad, generic functions fromNumeric.ADad/double:ad,Double-specialised functions provided inNumeric.AD.Double.backprop:backpropprimop: our generic implementation.primop/double: our implementation specialised forDouble.
- :checkmark: Explore more fine-grained use of delcont
- See
Numeric.AD.DelCont.MultiPromptfor PoC - We can abolish refs except for the ones for the outermost primitive variables
- perhaps coroutine-like hack can eliminateThis
- This implementation, however, is not as efficient as STRef-based in terms of time
- This is because each continuation allocates different values rather than single mutable variable
- But still in some cases, allocation can be slightly reduced by this approach (need confirmation)
- In particular, as the # of variable increases, the time overhead seems decaying and allocation becomes slightly fewer
- See
- Avoids (indirect) references at any costs!
RemoveRefs from constants- This increases both runtime and allocation by twice (see the benchmark log)
- Branching overhead outweighs
- Marco Zocca: ad-delcont: Reverse-mode automatic differentiation with delimited continuations
- Fei Wang et al.: Backpropagation with Continuation Callbacks: Foundations for Efficient and Expressive Differentiable Programming
- Justin Le: backprop: Heterogeneous automatic differentation
- Edward Kmett: ad: Automatic Differentiation
- The GHC Team: ``Continuations'' section in GHC.Prim document for GHC 9.6
- R. K. Dybvig et al.: A Monadic Framework for Delimited Continuations