The Differentiable Curry

The Differentiable Curry

The Differentiable Curry Martin Abadi Richard Wei Gordon Plotkin Dimitrios Vytiniotis Dan Belov Google Brain DeepMind 1 Introduction plications in a compiler, such as extra inlining and loop unrolling or early defunctionalization, and to Differentiable programming allows programmers to allow for separate compilation, to name a few. calculate program gradients and unlocks experimen- This is the challenge we address. We focus on tation with new optimizers and neural network archi- (i) statically-typed, (ii) compile-time, (ii) reverse-mode tectures. This is why modern deep learning frame- AD, a scenario exemplified by Swift AD. (http://bit. works [1, 23, 20, 7] introduce derivative APIs (e.g. ly/swift-autodiff) Our contributions are: tf.gradients in TensorFlow). Programmers ask for the gradient of an objective function with respect to • Following recent work [11] we introduce combina- its parameters; which is then used to optimize these tors for rep-functions that also return pullback lin- parameters, e.g. through stochastic gradient descent. ear maps (back-propagators), and show how they Recent projects, as Swift for TensorFlow (S4TF) can be used for AD. Generalizing to higher-order (www.tensorflow.org/swift) and Julia Zygote [15], functions boils down to introducing differentiable in the spirit of the seminal \Lambda the Ultimate curry and eval combinators. Backpropagator" (LTUB) [21], advocate AD as a first-class construct in a general-purpose programming • Higher-order functions (like curry) accept or re- language, and aim to take advantage of traditional turn closures. For a function from tensors to compiler optimizations for efficient code generation. tensors, its pullback at some input is also a linear The idea is to produce statically, for every differ- map from tensors to tensors. We use the term entiable function of type a ! b, another function re- tangent space, denoted as Tant , for the space of turning the result of the original function and a linear permutations of values of a type t. Hence the back-propagator map, of type a ! (b, Tanb −◦ Tana ). tangent space of a tensor is just a tensor. But We call these compiler-generated functions the rep- what should be the tangent space of a function resentation functions (rep-functions for short) of dif- type? Perhaps surprisingly, a function type itself ferentiable functions, and will use a b as a type is not the right answer. We provide two possi- abbreviation for a ! (b, Tanb −◦ Tana ). To achieve ble implementations for function tangents and this, the AD pass merely composes rep-functions out differentiable currying, and explain the tradeoffs. of primitive rep-functions like those for (+) and (−), by systematically lifting these primitives through the • The first implementation { novel to our knowl- constructs of the programming language. edge { is typeable in a simply-typed discipline An important challenge in this setting is the dif- and bears similarities to tracing but may involve ferentiation of functions that accept or return other re-computation of the function we differentiate functions, perhaps capturing (differentiable or non- during the execution of its backwards derivative. differentiable) variables. Partial applications must not \forget" to back-propagate to captured variables, and • The second is a dependently typed version of an more generally we need AD that provably preserves idea behind \Lambda the Ultimate Backpropaga- equational reasoning { needed to justify inlining, com- tor" [21], itself inspired by the view of functions mon sub-expression elimination etc. As we will see as closures whose tangents are the tangents of (Section 2), higher-order functions are ubiquitous in their environments. We put that idea in the modern statically-typed languages, even inside the im- combinatory setting of Elliott. Our work implies plementation of end-to-end first-order programs. They that an \erased" version that relies on reinterpret have to be tackled heads-on to avoid additional com- casts in a weaker type system is in fact safe. 1 2 Higher-order functions and AD J [[∆ ` e : τ]] = b Higher-order functions may not seem essential for differentiation but in a general-purpose programming b = J [[∆; (x:τ) ` e : σ]] BDLam language (e.g. Swift) are actually ubiquitous, even J [[∆ ` diffλx:τ:e : τ σ]] = curry b inside the implementation of a first-order program. Consider, for instance, the case of a recurrent neural b1 = J [[∆ ` e1 : τ σ]] network (RNN) model. An RNN, in its simplest form, b2 = J [[∆ ` e2 : τ]] BDApp folds a state transformer (called the RNN cell { e.g. a J [[∆ ` e1 e2 : σ]] = prod b1 b2 ◦ eval Long Short-Term Memory [14])) through a sequence of inputs and produces a final state. The state is often x is i-th variable in ∆ BDVar called the \hidden state" of the RNN: J [[∆ ` x : τ]] = proji b1 = J [[∆ ` e1 : τ1]] b2 = J [[∆ ` e2 : τ2]] BDProd rnnCell :: (Params, Tensor, Tensor) ! Tensor J [[∆ ` (e1; e2):(τ1; τ2)]] = prod b1 b2 rnnCell (params, hidden_state, input) = ... // return new hidden_state Figure 1: Translating to combinators runRNN :: Params Tensor [Tensor] Tensor runRNN params init_state xs = letg :: Tensor Tensor Tensor 3 Combinatory-style AD g hid input = lstmCell (params, hid, input) in foldg init_state xs We first illustrate how AD can be implemented using the aforementioned set of combinators. Here function g is the partial application of rnnCell 3.1 Step 1: Translate to combinators on params, passed to the recursive function fold. This We first use the combinator language in the previous example shows many features required for a general section as the target of conversion from a (conven- purpose language; (i) partial applications, (ii) recur- tional) call-by-value higher-order lambda calculus of sive functions (such as fold), (iii) recursive datatypes differentiable functions λ . The translation, in Fig- (such as [ Tensor] above). In particular function g, the @ ure 1, has (yet) nothing to do with AD; rather it partial application of rnnCell captures the parameters is reminiscent of the well-understood translation of and { unlike our simple example above { needs to back- λ-calculus into cartesian closed categories (CCCs) [8]. propagate to these parameters. Moreover, we cannot J [[∆ ` e : τ]] defines such a type-directed trans- eliminate higher-order functions by inlining, unless lation. The rules ensure that if ∆ ` e : τ then we unroll fold. And even if we could unroll fold, ` J [[∆ ` e : τ]] : ∆ τ, where we abuse notation rnnCell might be imported from a different module and refer to ∆ as the tuple of all types of environment whose source is not available for inlining. variables. The conversion also assumes (not shown, for In this extended abstract we will only focus on lack of space) that all differentiable primitives, such as higher-order functions. We will show how we can (∗):(Float; Float) Float come with rep-function express differentiation through higher-order programs implementations. following Elliott's recipe by providing implementations of a small set of combinators, given below: 3.2 Step 2: Implement combinators Next, we implement our combinators, accepting id :: τ τ and returning a b values, i.e. functions of type curry :: ((τa,τb) τc) ! (τa (τb τc)) a ! (b, Tanb ! Tana ). We also need to define a tan- eval :: (τ σ, τ) σ gent space Tant for every type t. Figure 2 presents prod :: (τy τa) ! (τy τb) ! (τy (τa,τb)) such a small library. Figure 6 in the Appendix gives (◦) :: (τa τb) ! (τb τc) ! (τa τc) constτ :: σ ! (τ σ) the definition of T [τ] for all the types of λ@ . The proji :: (τ1,:::,τn) τi cases for floats, products, and tensors of floats are standard; also the rules for \discrete" types all return Unfolding types, we see that curry/eval require a Unit. Some combinators (e.g. prod) rely on having definition for function tangents, Tan(τ σ). What 0 and (+) defined for every tangent type T [τ], also these should be (and why) is answered in this work. found in Figure 6. In the next sections we proceed to 2 tion forwards and then backwards for every element, vjp :(τ σ) ! τ !T [σ] !T [τ] arriving at the implementation of curry in Figure 3. vjpfx gb = snd (fx ) gb The eval combinator merely records the primal value (x) and the output tangent (g) in a singleton list. mult :: (Float,Float) Float mult (x1,x2) = (x1∗x2, λg.(x2∗g, x1∗g)) 4.1 Properties and metatheory Is our construction correct? We answer by showing proj_left :: (τ,σ) τ proj_left (a,b) = (a, λg.(g, 0)) that it respects equational reasoning principles. For example, when given f :(τ1; τ2) τ3 which we can curry and repeatedly evaluate with an argument of prod :: (τ σ1) ! (τ σ2) ! (τ (σ1, σ2)) prodfgy = let (a, pbf) = fy type τ1 and another of type τ2, we will get a func- (b, pbg) = gy tion that is not only forward-equivalent, but also has in ((a, b), λ(ga, gb). pbf ga + pbg gb) an equivalent back-propagator. An example are the (◦) :: (τa τb) ! (τb τc) ! (τa τc) forward-equivalent functions foo1 and foo2 in the (◦) fga = let (b, pbf) = fa \Partial Application" column of Figure 4. (c, pbg) = gb Consider foo1 and foo2 in the \Forgetting Results" in (c, λgc. pbf (pbg gc)) column of Figure 4. The two functions should be Figure 2: Library of combinators (excerpt) equivalent in forward and reverse mode, but for foo1 we will back-propagate a tangent value of [(x; 0)] for the use of g. In foo2, since g is not used at all, we will back-propagate []. We want to therefore treat [(x; 0)] :: ((τ ,τ ) τ ) ! (τ (τ τ )) curry a b c a b c and [] as equivalent, even if they are different lists. curryf = new_f Finally, foo1 and foo2 in the \Summing Results" where new_f :: τa ! (τb τc, T [τb τc] !T [τa]) new_ft = column are forward-equivalent, hence we expect equiv- let new_g :: τb ! (τc, T [τc] !T [τb]) alent back-propagators.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us