Kotlingrad: A shape-safe DSL for differentiable programming
github.com
github.com
https://github.com/breandan/kotlingrad/tree/master/src/main/...
Please feel free to reach out if you have any questions or comments. Thanks so much for your feedback!
Re: Shape Inference. To properly understand how this works, the best place to start is by looking at ToyVectorExample.kt [3]. Once you’re comfortable with that, ToyMatrixExample.kt is a fairly direct generalization. The best way to explain our type system is that we stick to equality checking and perform manual type conversion for tensor contractions and expansions. Haven’t figures out a good way to do concatenation, slicing, or convolution which require type-level arithmetic. This may be feasible for a fixed number set of predefined shapes, but seems to be generally intractable in the Java/Kotlin type system without a significant overhead. But if someone has an idea, I would be very happy to be proven wrong about this!
[1]: https://kotlinlang.org/docs/reference/grammar.html
[2]: https://github.com/Kotlin/kotlin-spec/blob/master/grammar/sr...
[3]: https://github.com/breandan/kotlingrad/blob/master/src/main/...
[4]: https://github.com/breandan/kotlingrad/blob/master/src/main/...
I'd suggest just spelling out what is what instead of relying on names that by themselves are meaningless. So e.g. Dimension1, Dimension2x2, etc. make it clear that 1) it's a class 2) it's intended as syntactic sugar for something else. Could type aliases work here? In any case, there are only so many vector and matrix dimensions you can predefine. Your approach kind of breaks down for larger vectors/matrices and you lose compile time checks for those.
Nevertheless, kudos for pushing the type system this far.
Indeed you are correct about the finite dimension constraint, a problem we discussed with a recent reviewer [1] in further depth. As you correctly noted, since we have a fixed upper bound for type-level integers, when checking large tensors (i.e. whose width exceeds 1000 in any dimension), we fall back to dynamic shape checking, prior to numerical evaluation. Smaller tensors can be shape checked at compile time.
I too, am wary of the "type voodoo" associated with type-level programming, but would argue that the added complexity from our approach (sans type level arithmetic) comes at very little cost to the end user due to type inference. In most cases we can check arrays at compile time. In the worst case, or if the user chooses, we use runtime shape checking or unsafe execution, which is the same as ordinary array programming.
Thanks again for your feedback. Please don't hesitate to reach out if you have any further questions or comments!
[1]: https://openreview.net/forum?id=SkluMSZ08H¬eId=BJeZ3FkuOr
I guess realistically it would have to be functional programming, where each function is itself differentiable, and then you'd have to be very careful with your function composition.
I'm incredibly intrigued, but also pretty skeptical. Like it seems like it would be so hard to ensure that you don't leave the domain where your program is differentiable.
The result is generally only piecewise differentiable because, e.g. you obviously cannot differentiate across the hard branch of an if-statement.
Usually for optimization you want reverse-mode differentiation, where you learn how the function output-- some optimization objective-- depends on the input variables. The automatic differentiation libraries records all the operations you perform on the input variables and then composes them using the chain rule, working backwards.
For some kinds of codebases it's actually pretty natural... e.g. I've taken a big body of C digital signal processing code and in a couple hours gotten gradients for it just by replacing all the floating point variables with special differentiable types from an AD library. These libraries also include collections of standard math lib functions which it knows the derivative for.
For the purpose of optimization the fact that your program isn't differentiable everywhere isn't necessarily a problem. You can use gradient free methods for searches across those boundaries, and gradient methods to refine your results within them.
If you're optimizing over problems that have thousands of input variables having derivatives is critical to making machine optimization tractable-- unless your search space is particularly trivial and can be well approximated by the simple differentiable models that gradient free local optimization tools use internally.
Of course, you could also attempt to directly program your derivatives-- it almost would certainly be much faster to evaluate than transcript based reverse mode AD. But it may be impossible to construct analytical derivatives that cover your whole problem domain, it's often unreasonable hard even when possible, and even if you have them keeping the functions in-sync is difficult and a source of devious bugs.
(1) We can perform gradient descent on integer functions! [1] There are rigorous assumptions for differentiability (e.g. Lipschitz-continuity, β-smoothness), but most often, these don't apply in the real world. Although many discrete optimization problems can relaxed to semi-differentiable ones, even if a function is not strictly differentiable, we can still derive an approximately correct expression for the gradient.
(2) It is possible to symbolically derive functions in discrete spaces! There are many, many examples of this in various settings (e.g. Brzonski's derivative [2], Lambda calculus [3], Boolean algebra [4], et al. [5]). A number of researchers are studying generalized differentiation, or "change propagation" in other topological spaces [6]. Although still very abstract and hard to interpret, these ideas have important applications for incremental computation and together with probabilistic programming [7], may one day lead to an analogous mechanism to gradient descent on a much broader class of functions.
With this in mind, Kotlin𝛁 implements symbolic differentiation. Whenever the user writes "+" or "*" in their program and later asks for the derivative, we apply the sum and product rules, and do not consider the question of domain validity. For floating point numbers, we try to perform some consistency checks using finite differences, but ultimately, leave this up to the user to verify whether their their domain is a valid one.
Shameless plug, but if you find this interesting, you can learn more about this in Chapter 3 of my M.S. thesis [8]. Please don't hesitate to get in touch if you have any questions or would like to chat!
[1]: https://openreview.net/pdf?id=HJGXzmspb [2]: http://maveric.uwaterloo.ca/reports/1964_JACM_Brzozowski.pdf [3]: https://core.ac.uk/download/pdf/82396223.pdf [4]: https://pdfs.semanticscholar.org/a9c8/7568463a5ce8c2cd15ec17... [5]: https://en.wikipedia.org/wiki/Generalizations_of_the_derivat... [6]: https://arxiv.org/pdf/1902.05465.pdf [7]: https://www.cs.ubc.ca/~fwood/CS532W-539W/ [8]: https://github.com/breandan/kotlingrad/blob/master/latex/the...
> Differentiable programming, or ∂P, is a programming paradigm in which the programs can be differentiated throughout, usually via automatic differentiation.[1][2][3] This allows for gradient based optimization of parameters in the program, often via gradient descent. Differentiable programming has found use in a wide variety of areas, particularly scientific computing and artificial intelligence.
[0] https://en.m.wikipedia.org/wiki/Differentiable_programming
[1] Yes, I know the repo name uses "grad" - that spelling actually makes sense.
Re: Trademark. Might be an issue. I’ve checked with the Kotlin team and they are looking into it.
https://github.com/JetBrains/kotlin-native/tree/master/sampl...
If you’re more interested in the type-safety, you might also check out Hasktorch: