ML Inference (TinyGo)
Run ML inference in WASM using a linear regression model
This example demonstrates a simple ML inference workload compiled to WASM. A linear regression model is trained in Python using scikit-learn, and the coefficients are extracted into a pure Go implementation that runs entirely inside the WASM module — no Python runtime needed at inference time.
The model approximates: y = 2*x0 + 3*x1
Before building, a Python training script generates the model and the Go model code is extracted:
cd examples/ml-wasm
python3 train_model.py
go run model_gen.go > /dev/null # validates coefficients
cd ../..This creates mymodel.pkl and embeds the coefficients in model_gen.go. The weights are:
intercept = 0.0weight[0] = 2.0(for input x0)weight[1] = 3.0(for input x1)
Source Code
The source code is available in the examples/ml-wasm directory.
Loading...
Training Script
Loading...
Generated Model
Loading...
Create Task
Build the WASM binary:
cd propeller
GOOS=wasip2 GOARCH=wasm go build -o build/ml-wasm.wasm examples/ml-wasm/main.go examples/ml-wasm/model_gen.goNow we can create a task:
curl -X POST "http://localhost:7070/tasks" \
-H "Content-Type: application/json" \
-d '{"name": "predict", "inputs": ["200", "300"]}'Inputs are scaled by 100 (e.g. 200 = 2.0) — the module divides by 100, runs inference, and multiplies the result by 100.
Upload Wasm
curl -X PUT "http://localhost:7070/tasks/<task-id>/upload" \
-F "file=@$(pwd)/build/ml-wasm.wasm"Start Task
curl -X POST "http://localhost:7070/tasks/<task-id>/start"The result for inputs [2.0, 3.0] should be approximately 1300 (i.e. (2*2.0 + 3*3.0) * 100 = 1300).
Invoking Using WasmTime
wasmtime --invoke predict ./build/ml-wasm.wasm 200 300