5 releases (breaking)

0.4.0 Feb 20, 2025
0.3.0 Jan 21, 2025
0.2.0 Nov 22, 2024
0.1.0 Nov 19, 2024
0.0.0 Nov 19, 2024

#407 in WebAssembly

Download history 6/week @ 2024-12-01 11/week @ 2024-12-08 1/week @ 2024-12-15 98/week @ 2025-01-19 6/week @ 2025-01-26 23/week @ 2025-02-02 320/week @ 2025-02-09 452/week @ 2025-02-16 340/week @ 2025-02-23 433/week @ 2025-03-02 302/week @ 2025-03-09 219/week @ 2025-03-16

1,333 downloads per month
Used in floretta-cli

MIT license

87KB
2K SLoC

Rust 2K SLoC // 0.0% comments WebAssembly 319 SLoC

Floretta

Floretta is an automatic differentiation tool for WebAssembly. This crate is the Rust library; for the command line interface, see the floretta-cli crate.

To install:

cargo add floretta

The typical workflow is to create an empty config via Autodiff::new, use Autodiff::export to specify one or more functions to export the backward pass, and then use Autodiff::transform to process a Wasm module.

For example, if you have wat and Wasmtime installed:

use wasmtime::{Engine, Instance, Module, Store};

let input = wat::parse_str(r#"
(module
  (func (export "square") (param f64) (result f64)
    (f64.mul (local.get 0) (local.get 0))))
"#).unwrap();

let mut ad = floretta::Autodiff::new();
ad.export("square", "backprop");
let output = ad.transform(&input).unwrap();

let engine = Engine::default();
let mut store = Store::new(&engine, ());
let module = Module::new(&engine, &output).unwrap();
let instance = Instance::new(&mut store, &module, &[]).unwrap();
let square = instance.get_typed_func::<f64, f64>(&mut store, "square").unwrap();
let backprop = instance.get_typed_func::<f64, f64>(&mut store, "backprop").unwrap();

assert_eq!(square.call(&mut store, 3.).unwrap(), 9.);
assert_eq!(backprop.call(&mut store, 1.).unwrap(), 6.);

Dependencies

~4.5–6MB
~112K SLoC