All pages
The run file
An experiment file is recognized by top-level bindings with conventional names. Anything else in the file is an ordinary definition and is ignored by the contract. The file is read once and evaluated once, in order.
Extend a run
Start a file with (extends "./base.sx") to keep one base and state only the
differences. For example, a full copy of a training file used only to change
(grid false) to (grid true) becomes:
;; grid-read.sx
(extends "./base.sx")
(defknobs (grid true))The base keeps the model, loaders, optimizer and evaluation passes. You can
override individual knob defaults, literal top-level named definitions, and
defrun settings:
(extends "./base.sx")
(defknobs (lr 0.0003))
(defun evaluate (prediction targets)
(metric "eval/loss" (cross-entropy prediction targets)))
(defrun :steps 20000
:evals (list (eval-pass "test" :loader test-loader :batches 8))
:drop-evals ["old-dev"])defknobs changes defaults; a selected variant, --set, and restored
checkpoint knobs keep their usual precedence. Use defvariant for named knob
combinations inside one program and defsweep for grids. Use extends when a
separate run file changes declarations as well as defaults. It produces one
static program; it adds no model or parameter-name prefix.
Overrides support literal top-level defvar, defun, defmacro, defmodel,
defoptimizer, defeval, defvariant and defsweep. Other declarations,
including defphase, can be inherited or added but are not override slots. A
literal definition replaces the inherited definition at its original position.
New functions and variants come before the inherited body, new value definitions
before its first defrun, and other statements after the body. Literal imports
are loaded first so new functions can use them. Inherited imports remain
available when an override removes their last use; imports written in the child
retain the normal unused-import check. Normal evaluation order still applies to
value initializers: a new value is not available to an earlier inherited
initializer. Move such a constant into the base's defknobs, or replace the
whole dependent definition. Definitions made by macros and unnamed expressions
cannot be patched by spelling a name in the child. A defrun created inside an
expression or macro cannot be overlaid; move it to the top level.
defrun inherits settings you omit. When overlaying settings, both files must
use literal keyword option names (E-CONTRACT-003); their values can be
computed expressions. :evals adds passes in list order; :drop-evals adds
names to remove. Both accumulate across layers. All selected and legacy pass
names must be unique before removal; dropping an absent name is an error. A
legacy top-level defeval can also be overridden by name.
The base path and its require paths resolve from the file containing them.
Local paths in files, manifest, defparam :load and with-loaded declared
in inherited run files keep their paths relative to those files. Data
constructors inside imported libraries keep their usual paths relative to the
entry run. Bundles carry the base files too. explain and diff show
declaration origins alongside effective values.
E-RUN-001 diagnoses a malformed, misplaced or unreadable base declaration;
E-RUN-002 a cycle or more than 64 base links; E-RUN-003 conflicting
overrides in one file. Keep one override per name and one defrun per extending
layer; the diagnostic points to both conflicting declarations.
The order of a file
(require ...) standard modules and library files the file uses
(defvariant ...) named sets of knob overrides, before defknobs
(defknobs ...) everything this run may vary
(defsweep ...) named cross products over knob values
(defvar ...) ordinary constants
train-loader required: where the training records come from, one loader or a vector
(defeval ...) optional, any number: named evaluation passes
prepare required: batch -> (list :inputs ... :targets ...)
eval-prepare optional: defaults to prepare
model required: a model value
objective required: prediction, targets [metadata [ctx]] -> one float scalar
evaluate required when a pass has no :evaluate of its own: emits metrics
optimizer required: a configured optimizer
curriculum optional: stage, ctx, observations -> new stage
(defgenerate ...) optional: records made on the device each step, read as fields
(defphase ...) optional, any number: training objectives selected by step or stage
(defrun ...) required: steps, microbatches, cadence, precision, seed, and
:evaluate :inline or :offline, where the passes run| name | page |
|---|---|
require | modules and require |
defvariant, defknobs, defsweep | knobs, variants and sweeps |
train-loader, prepare, eval-prepare | loaders and prepare |
defeval, eval-pass | evaluation passes |
model | models and parameters |
objective, evaluate | objective and evaluate |
optimizer | optimizer groups, writing optimizers |
curriculum | curriculum and ctx |
defgenerate | generation inside the step |
defphase | finite training phases |
defrun | defrun |
Missing a required name is E-CONTRACT-001; binding one to the wrong kind of
value is E-CONTRACT-002. The components are checked independently, so a
mistake in objective never hides a missing optimizer. The names are looked
up in the run file itself: a library that defines model gives the run none.
A complete minimal run
The smallest file the contract accepts has the six required names, no variation
and no evaluation: minimal.sx in the
quickstart.
A complete realistic run
A realistic run adds knobs and variants (including a smoke variant small
enough for --device cpu), an evaluation pass, a counter, a reported per-token
loss beside the summed objective, and optimizer groups with a schedule each. Its
selection and its optimizer:
(require "sexpgpu/nn" cross-entropy gpt)
(require "sexpgpu/optim" adamw warmup-stable-decay)
(defvariant small (layers 2) (width 128))
(defvariant smoke (layers 2) (width 128) (seq 64))
(defknobs
(lr 0.0015 "peak learning rate of the block matrices, the default group")
(layers 4 "transformer blocks")
(width 256 "model width")
(seq 1024 "sequence length in tokens; a window holds seq + 1")
(seed 1 "the run seed; every random stream folds it in"))
(defsweep seeds (seed [1 2 3]))
(defvar warmup 25)
(defun schedule (peak)
(warmup-stable-decay peak :warmup-steps warmup :decay-fraction 0.7))
(defvar optimizer
(adamw :lr (schedule lr)
:betas [0.9 0.95]
:eps 1e-10
:weight-decay 0.10
:groups [(group (select "model.embed.*")
:lr (schedule 0.3)
:betas [0.8 0.95]
:weight-decay 0.0)
(group (select "model.head.weight")
:lr (schedule (/ 1.0 320.0))
:betas [0.8 0.95]
:weight-decay 0.0)
(group (select :rank-below 2)
:lr (schedule 0.01)
:betas [0.8 0.95]
:weight-decay 0.0)]))explain --variant smoke reads it back in those terms: the
parameters and their element count, the four update groups (the two named ones,
:rank-below 2, and default with the remaining block matrices) and which
parameters each holds, each group's m and v states and the constants its
schedule folded to, and the microbatches of one step.
Move reusable definitions into a library to keep
the run file short, and format it with sexpgpu fmt.
Training may declare a finite set of phases
with defphase, sharing the same parameters and loaders.