Skip to content

🌲 Ahead-of-time Static Macro-gen Automatic Differentiation. A little bit like Jax. Now in Beta

License

Notifications You must be signed in to change notification settings

MDResearch/Mady

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MADY (Macro-gen Automatic Differentiation)

Rust Test Project Crates.io docs.rs Crates.io

🚚 Mady have new home! 👉MadyLab

MADY is open source tool for ahead-of-time automatic differentiation. In addition to ahead-of-time differentiation, MADY also provide some basic math structures, functions and operations with differentiation support.

Feature

  • Ahead-of-time gen

    generate by proc-macro

  • Static

    no tree or graph struct in runtime, just normal function call

  • Multithreading

    because it's just a normal function, no hacky code or unsafe

  • Fast

    just static code, llvm can optimize all the code

Supported Differentiation

  • functions: min, max
  • operations: add, sub, mul, div

Get Started

First, set up project and add mady as dependency in your Cargo.yml

[dependencies]
mady = "version here"

or

cargo add mady

Write a simple fn (only differentiation support operation/function can be used)

fn simple(a:isize, b:isize)-> isize{
  a + b
}

Finally, add #[derive_grad()] (attribute macro) to your function.

// isize here, because the input grad type of simple is isize
#[derive_grad(isize, isize)]
fn simple(a:isize, b:isize)-> isize{
  a + b
}

expect output

fn simple(a:isize, b:isize)-> isize{
  a + b
}
fn grad_simple(a:isize, b:isize)-> (isize,(isize,isize)){
  (a + b, (1, 1))
}

$$ grad\_simple(a,b)=(simple(a,b),({d simple \over d a},{d simple \over d b})) $$

To use unsupported function like sin, add fn named grad_{{fn name}}.

impl GradSin for f64 {
    fn grad_sin(self) -> self {
        self.cos()
    }
}

example