| Crates.io | floretta-cli |
| lib.rs | floretta-cli |
| version | 0.5.0 |
| created_at | 2024-11-19 19:09:25.004383+00 |
| updated_at | 2025-04-22 18:30:37.643095+00 |
| description | Automatic differentiation for WebAssembly. |
| homepage | |
| repository | https://github.com/samestep/floretta |
| max_upload_size | |
| id | 1453747 |
| size | 21,263 |
Floretta is an automatic differentiation tool for WebAssembly. This crate is the command line interface; for the Rust library, see the floretta crate.
To install:
cargo install --locked floretta-cli
Use the --help flag to see all available CLI arguments:
floretta --help
Here are some usage examples, assuming you have Node.js installed. First, create a file called square.wat with these contents:
(module
(func (export "square") (param f64) (result f64)
(f64.mul (local.get 0) (local.get 0))))
You can use Floretta to replace the "square" function approximating real numbers with one approximating the dual numbers:
floretta --forward square.wat --output dual.wasm
Then if you have a Wasm engine, you can use it to compute the der with the emitted Wasm binary by running the augmented function with a value of 1 for the dual part. For instance, you can create a file called dual.mjs with these contents:
import fs from "node:fs/promises";
const wasm = await fs.readFile("dual.wasm");
const module = await WebAssembly.instantiate(wasm);
const { square } = module.instance.exports;
console.log(square(3, 1));
And run it like this:
node dual.mjs
Expected output:
[ 9, 6 ]
You can use Floretta to take the backward pass of the "square" function and name it "backprop":
floretta --reverse square.wat --export square backprop --output gradient.wasm
Then if you have a Wasm engine, you can use it on the emitted Wasm binary to compute a gradient by running the forward pass followed by the backward pass. For instance, you can create a file called gradient.mjs with these contents:
import fs from "node:fs/promises";
const wasm = await fs.readFile("gradient.wasm");
const module = await WebAssembly.instantiate(wasm);
const { square, backprop } = module.instance.exports;
console.log(square(3));
console.log(backprop(1));
And run it like this:
node gradient.mjs
Expected output:
9
6