Skip to main content

Crate rlx_web

Crate rlx_web 

Source
Expand description

WebAssembly entry point for RLX.

Compiles to wasm32-unknown-unknown and exposes a small JS-callable surface via wasm-bindgen. Both forward (inference) and backward (gradients via rlx_autodiff) passes are available.

§Compute paths

  • CPU backend (default, works today): mlp_forward, mlp_loss, mlp_grads, mlp_train_step build an IR graph and run it through rlx_runtime::Session on the CPU backend. Fully synchronous — runs on the browser main thread (rlx-cpu is single-threaded on wasm).

  • WebGPU (webgpu feature): async forward/backward via rlx_wgpu::WgpuExecutable::run_async (see [webgpu]); GPU→CPU readback cannot block the browser event loop, so those entry points are async.

The forward and backward graphs are identical across backends — only the executor differs. The backward graph comes from rlx_autodiff::grad_with_loss; its outputs are [loss, ∂loss/∂p…] and it is seeded with a d_output input (the cotangent, [1.0] for a scalar loss).

Structs§

VisionBench
Vision benchmark handle — forward, backward, and SGD on CPU / WebGPU / WebGL.
VisionModelInfo
Metadata for a vision model (shapes, param layout, WebGL train support).

Functions§

backend
Label of the compute backend used by the synchronous entry points.
list_vision_models
List vision model slugs available in the browser bundle.
mlp_forward
Run a two-layer MLP — relu(x·W1 + b1)·W2 + b2 — on the CPU backend and return the output row.
mlp_grads
Forward + backward in one call on the CPU backend. Returns a flat vector: [loss, ∂/∂w1 …, ∂/∂b1 …, ∂/∂w2 …, ∂/∂b2 …] (gradients in row-major parameter order, same lengths as the inputs).
mlp_loss
Mean-of-squares loss Σ (y − target)² for the MLP, on the CPU backend. (Used for inference + as the scalar the backward pass differentiates.)
mlp_train_step
One SGD step: compute gradients of the loss and return the updated parameters as [w1 …, b1 …, w2 …, b2 …] (same layout as the inputs).
parse_backend
Parse backend name: auto, cpu, webgpu, webgl.
preferred_backend
Highest-priority available browser backend label (cpu, webgpu, webgl, or none).
start
Module entry point — installs a panic hook that routes Rust panics to the browser console. Runs automatically when the wasm module is instantiated.
transformer_next_logits
Run a real decoder-only transformer (RMSNorm + RoPE + causal multi-head attention + SwiGLU) on the CPU backend with deterministic synthesized weights, and return the next-token logits (last position), length vocab. tokens are token ids (as f32); seq = tokens.len(). dim must equal n_heads * head_dim.
vision_model_info
Look up model metadata by slug (mnist-cnn, mnist-mlp, cifar-cnn, resnet).