| Crates.io | floretta |
| lib.rs | floretta |
| version | 0.5.0 |
| created_at | 2024-11-19 14:35:44.592448+00 |
| updated_at | 2025-04-22 18:30:15.636054+00 |
| description | Automatic differentiation for WebAssembly. |
| homepage | |
| repository | https://github.com/samestep/floretta |
| max_upload_size | |
| id | 1453333 |
| size | 247,752 |
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
Here are some usage examples, assuming you have wat and Wasmtime installed.
Use Autodiff::new to create an empty config, then use Autodiff::forward to transform a Wasm module to compute derivatives in forward mode.
use floretta::Autodiff;
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 output = Autodiff::new().forward(&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), (f64, f64)>(&mut store, "square").unwrap();
assert_eq!(square.call(&mut store, (3., 1.)).unwrap(), (9., 6.));
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::reverse to transform a Wasm module to compute derivatives in reverse mode.
use floretta::Autodiff;
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 = Autodiff::new();
ad.export("square", "backprop");
let output = ad.reverse(&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.);