S-exp GPU
All pages
Docs · Start hereMarkdown

Quickstart

Install, write an example, check it, read what the compiler made of it, train a few steps on the CPU, and pack it for a GPU box.

curl -fsSL https://sx.041.io/install.sh | sh
sexpgpu new minimal.sx --example minimal
sexpgpu check minimal.sx && sexpgpu run minimal.sx

1. Write the experiment

$ sexpgpu new minimal.sx --example minimal
wrote minimal.sx and data/tiny-train.jsonl.gz from the `minimal` example: 32 parameters, 20 steps

minimal.sx is the smallest file the contract accepts (the run file explains each name):

(require "sexpgpu/nn" cross-entropy gpt)
(require "sexpgpu/optim" sgd)

(defvar train-loader
  (loader :sources [(files ["data/tiny-train.jsonl.gz"] :encoding "jsonl.gz")]
          :fields [(field :tokens :from "input_ids" :dtype :i32 :shape [17])]
          :batch-size 4
          :infinite true))

(defun prepare (batch)
  (let ((tokens (field batch :tokens)))
    (list :inputs (slice tokens 1 0 16) :targets (slice tokens 1 1 17))))

(defvar model (gpt :vocab 32 :layers 2 :dim 32 :heads 2))

(defun objective (prediction targets) (cross-entropy prediction targets))

(defvar optimizer (sgd :lr 0.5))

(defrun :steps 20 :microbatches 2 :precision :f32 :seed 1)

The loader reads data/tiny-train.jsonl.gz next to the file, which new wrote with it: 512 lines, each a JSON object with an input_ids array of 17 token ids below 32, a counting sequence the model can learn. A data file already there with the same bytes is kept; one with other bytes is refused (exit 2) and nothing is written.

Any shape works once the :shape, the slices and :vocab agree; see loaders.

2. Check it

$ sexpgpu check minimal.sx
ok: 32 parameters, 38 graphs, 20 steps
slug minimal

check needs no data and no GPU. Break a shape and it names your line; see check.

3. Read what it compiled to

sexpgpu explain minimal.sx

Parameters with their optimizer groups, every graph, one training step and the peak activation memory. See explain.

4. Train a few steps

$ sexpgpu run minimal.sx --steps 3 --device cpu
...
done: 3 steps, train/loss 3.1645
  counter records 24

--device cpu is the optimized host backend and accepts --cpu-cores N to limit its worker count. Use --device reference for the unoptimized oracle on toy sizes (devices). On a GPU machine, --device cuda --metrics jsonl:minimal.jsonl trains at full speed and writes every number; see run and metrics. On an Apple silicon Mac, use --device metal; see Metal.

5. Run it on a cloud GPU

sexpgpu bundle packs the run, the binary and its data into one directory, and rrun by 041 runs it on the cheapest GPU that fits:

sexpgpu bundle minimal.sx -o ~/.sexpgpu/bundles/minimal
cd ~/.sexpgpu/bundles/minimal
o41-rrun run --gpu A100,H100,L4 --spot --cuda 13.0 \
    --image nvidia/cuda:13.0.3-cudnn-runtime-ubuntu24.04 \
    --env SEXPGPU_DEVICE=cuda -f -- ./run.sh

This packs the binary you run, so on a Mac pack the Linux one with --binary; see run on rrun. On a GPU box of your own, copy the directory and run it there: scp -r ~/.sexpgpu/bundles/minimal gpu-box: && ssh gpu-box 'SEXPGPU_DEVICE=cuda minimal/run.sh'.

Next