SonicBoom

Forward-mode device graphs (TTS)

Status: implemented and tested. This page describes the inference mode of the device graph, the weight store it binds from, and the Guile surface (sonicboom compile). The GPU proof (a small MLP and a Qwen2-shaped block run against PyTorch reference vectors) lives in the private test suite; the public suite exercises the same API on the CPU interpreter.

What it is

A forward-mode device graph is one traced Scheme function compiled into an acyclic inference graph and executed node by node on the GPU. It is the same machinery as the training device graph (DeviceGraph), in the other mode:

Nothing in this path changes the training mode: a training graph compiles and runs exactly as before (its tests and goldens are the guard).

The weight store

A weight store maps one file once and supplies its named tensors to compiled graphs:

The Guile surface

(use-modules (sonicboom compile)
             (sonicboom trace symbolic))

(define model (load-model "flow.safetensors"))

(define (dit-step x w)
  (let* ((h (matmul x (w "blocks.0.attn.to_q.weight" 'float32 '(64 64))))
         (h (matmul h (w "blocks.0.ff.down.weight" 'float32 '(128 64))))
         (b (w "blocks.0.ff.down.bias" 'float32 '(1 64))))
    (relu (add h b))))

(define fwd (compile-forward model
                             '((x float32 (2 64)))
                             dit-step
                             #:backend 'auto))
(fwd (make-ctensor 'float32 '(2 64) (make-list 128 0.5)))

compile-forward calls dit-step once, on symbolic tensors:

The result is an ordinary procedure taking one ctensor per input and returning the output tensor, or the list of outputs in return order. Runs of one compiled forward are serialized; different compiled forwards may run concurrently.

#:backend selects 'device (requires a CUDA build), 'cpu (the interpreter, always available) or 'auto (device when the loaded library has CUDA). Both backends bind the same weights from the same file: the device uploads them, the CPU interpreter copies them from the store’s host bytes.

compiled-forward-memory-stats reports the device memory a compiled forward holds — activation slots (the buffer-reuse assignment), bound weights (owned by the store), scratch, and their sum — as an alist. A CPU forward reports zero.

The compiled forward owns its native executable: a GC guardian releases it when the procedure is collected, free-compiled-forward! releases it explicitly (idempotent; later runs raise), and reap-compiled-forwards! releases collected ones. load-model has the matching free-weights! and reap-weights!.

Shapes and buckets

Shapes are static per executable. A variable-length input is handled by buckets — the length rounded up to a compiled size — with the caller passing the valid length explicitly, usually as a mask input the graph applies (its meaning is the model’s). Compiling one graph per bucket against one store is the supported pattern; the mask is what makes the padding harmless, and the test of a bucket is that the same input at two buckets gives bit-identical valid regions, with capture on and off. If a model cannot express that mask (its attention would read the padded keys), the bucket sizes are a correctness choice, not a packing trick: compile only the sizes the model supports.

Errors

A compile reports, with the names involved:

C ABI (append-only)

The layer-2 API stays free of CUDA and native-torch types; the additions are new entries in capi/include/sonicboom/capi.h:

Weights are bound through the same boom_run_multi entry as any other executable; an inference graph is bound by name through boom_input_name_at / boom_output_name_at, exactly as a training graph is.