Kotlingrad
π§© Shape-Safe Symbolic Differentiation with Algebraic Data Types
Install / Use
/learn @breandan/KotlingradREADME
Kotlinβ: Type-safe Symbolic Differentiation for the JVM
Kotlinβ is a type-safe automatic differentiation framework written in Kotlin. It allows users to express differentiable programs with higher-dimensional data structures and operators. We attempt to restrict syntactically valid constructions to those which are algebraically valid and can be checked at compile-time. By enforcing these constraints in the type system, it eliminates certain classes of runtime errors that may occur during the execution of a differentiable program. Due to type-inference, most type declarations may be safely omitted by the end-user. Kotlinβ strives to be expressive, safe, and notationally similar to mathematics.
Table of contents
- Introduction
- Supported features
- Usage
- Visualization
- Testing and gradient checking
- How does it work?
- Experimental ideas
- Formal grammar
- UML diagram
- Comparison to other frameworks
- References
- Acknowledgements
Introduction
Inspired by Stalinβ, Autograd, DiffSharp, Myia, Nexus, Tangent, Lantern et al., Kotlinβ attempts to port recent advancements in automatic differentiation (AD) to the Kotlin language. AD is useful for gradient descent and has a variety of applications in numerical optimization and machine learning. Our implementation adds a number of experimental ideas, including compile-time shape-safety, algebraic simplification and numerical stability checking with property-based testing. We aim to provide an algebraically-grounded implementation of AD for shape-safe tensor operations. Tensors in Kotlinβ are represented as multidimensional arrays.
Features
Kotlinβ currently supports the following features:
- Arithmetical operations on scalars, vectors and matrices
- Shape-safe vector and matrix algebra
- Partial and higher-order differentiation on scalars
- Property-based testing for numerical gradient checking
- Recovery of symbolic derivatives from AD
Additionally, it aims to support:
- PyTorch-style define-by-run semantics
- N-dimensional tensors and higher-order tensor operators
- Fully-general AD over control flow, variable reassignment (via delegation), and array programming, possibly using a typed IR such as Myia
All of these features are implemented without access to bytecode or special compiler tricks - just using higher-order functions and lambdas as shown in Lambda the Ultimate Backpropogator, embedded DSLs a la Lightweight Modular Staging, and ordinary generics. Please see below for a more detailed feature comparison.
Usage
Installation
Kotlinβ is hosted on Maven Central. An example project is provided here.
Gradle
dependencies {
implementation("ai.hypergraph:kotlingrad:0.4.7")
}
Maven
<dependency>
<groupId>ai.hypergraph</groupId>
<artifactId>kotlingrad</artifactId>
<version>0.4.7</version>
</dependency>
Jupyter Notebook
To access Kotlinβ's notebook support, use the following line magic:
@file:DependsOn("ai.hypergraph:kotlingrad:0.4.7")
For more information, explore the tutorial.
Notation
Kotlinβ operators are higher-order functions, which take at most two inputs and return a single output, all of which are functions with the same numerical type, and whose shape is denoted using superscript in the rightmost column below.
| Math | Infix <sup>β </sup> | Prefix | Postfix<sup>β‘</sup> | Operator Type Signature |
|:------------------------------------------------------------------:|:-------------------------------:|:--------------------------------:|:-----------------------------------:|:-------------------------------------------------------------------------------:|
| $$\mathbf{A}(\mathbf{B})$$<br>$$\mathbf{A}\circ\mathbf{B}$$ | a(b)<br>a of b | | | $$(\texttt{a}: β^{Ο}ββ^{Ο}, \texttt{b}: β^{Ξ»} β β^{Ο}) β (β^{Ξ»}ββ^{Ο})$$ |
| $$\mathbf{A}\pm\mathbf{B}$$ | a + b<br>a - b | plus(a, b)<br>minus(a, b) | | $$(\texttt{a}: β^{Ο}ββ^{Ο}, \texttt{b}: β^{Ξ»} β β^{Ο}) β (β^{?}ββ^{Ο})$$ |
| $$\mathbf{A}\mathbf{B}$$ | a * b<br>a.times(b) | times(a, b) | | $$(\texttt{a}: β^{Ο}ββ^{mΓn}, \texttt{b}: β^{Ξ»}ββ^{nΓp}) β (β^{?}ββ^{mΓp})$$ |
| $$\frac{\mathbf{A}}{\mathbf{B}}$$<br>$$\mathbf{A}\mathbf{B}^{-1}$$ | a / b<br>a.div(b) | div(a, b) | | $$(\texttt{a}: β^{Ο}ββ^{mΓn}, \texttt{b}: β^{Ξ»}ββ^{pΓn}) β (β^{?}ββ^{mΓp})$$ |
| $$\pm\mathbf{A}$$ | | -a<br>+a | a.neg()<br>a.pos() | $$(\texttt{a}: β^{Ο}ββ^{Ο}) β (β^{Ο}ββ^{Ο})$$ |
| $$\sin{a}$$<br>$$\cos{a}$$<br>$$\tan{a}$$ | | sin(a)<br>cos(a)<br>tan(a) | a.sin()<br>a.cos()<br>a.tan() | $$(\texttt{a}: βββ) β (βββ)$$ |
| $$\ln{a}$$ | | ln(a)<br>log(a) | a.ln()<br>a.log() | $$(\texttt{a}: β^{Ο}ββ^{mΓm}) β (β^{Ο}ββ^{mΓm})$$ |
| $$\log_{b}a$$ | a.log(b) | log(a, b) | | $$(\texttt{a}: β^{Ο}ββ^{mΓm}, \texttt{b}: β^{Ξ»}ββ^{mΓm}) β (β^{?}ββ)$$ |
| $$\mathbf{A}^b$$ | a.pow(b) | pow(a, b) | | $$(\texttt{a}: β^{Ο}ββ^{mΓm}, \texttt{b}: β^{Ξ»}ββ) β (β^{?}ββ^{mΓm})$$ |
| $$\sqrt{A}$$<br>$$\sqrt[3]{A}$$ | a.pow(1.0/2)<br>a.root(3) | sqrt(a)<br>cbrt(a) | a.sqrt()<br>a.cbrt() | $$(\texttt{a}: β^{Ο}ββ^{mΓm}) β (β^{Ο}ββ^{mΓm})$$ |
| $$\frac{da}{db},\frac{\partial{a}}{\partial{b}}$$ <br> $$D_b{a}$$ | a.d(b)<br>d(a) / d(b) | grad(a)[b] | | $$(\texttt{a}: C(β^{Ο}ββ)^{*}, \texttt{b}: C(β^{Ξ»}ββ)) β (β^{?}ββ)$$ |
| $$\nabla{a}$$ | | grad(a) | `a.
Related Skills
node-connect
338.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.4kCommit, push, and open a PR
