S-exp GPU
All pages
Docs · The run fileMarkdown

defrun

defrun is the run configuration the file owns: how many steps, how a step is split, the precision, the seed, and how often to checkpoint and sample diagnostics. It is required, once.

(defrun :steps 300 :microbatches 4 :checkpoint-every 100 :precision :f32 :seed 1)
keyworddefaultmeaning
:steps1target number of completed optimizer updates; nonnegative. 0 skips training and runs the final inline evaluation passes
:microbatches1microbatches per optimizer step; 1 to 4294967295. Gradients accumulate over them. Under several GPUs the count must divide by the device count; see devices
:checkpoint-everynonesteps between checkpoints, at least 1; without a checkpoint location nothing is written. See checkpoints
:precision:f32:f32 or :bf16; anything else is E-CONTRACT-003. See numerics
:gradient:reverse:reverse differentiates the objective; :forward-query estimates it from two evaluations; :forward-credit uses forward-computed local scores; see forward-query training and forward-credit training
:query-radius1e-3with :gradient :forward-query, how far each evaluation moves every parameter element; positive, and large enough for f32 to hold
:seed0nonnegative; mixed into every random stream when the run executes the graphs; see determinism
:diagnosticsnoneglobs over diagnostic names, ["attention/*" "logits/*"]; only these ever compute
:diagnose-every1a sampled step is a multiple of this; at least 1
:diagnose-first0nonnegative; the first this many steps are sampled too, and so is the last
:guard-finitefalsestop, committing nothing, when a proposed parameter or state is not finite; see the finite guard
:clip-grad-normnonepositive finite number; limit the global L2 norm of the step's gradients before optimizer updates; see below
:evaluate:inlinewhere the passes run: :inline in the loop, :offline from the checkpoints with sexpgpu evaluate; see evaluation
:emanonea decay, at least 0 and below 1: the passes read an exponential moving average of the trained weights; see below
:ema-debiasfalsewith :ema, true averages only the trained weights after each step, leaving out the initial weights; see below

Overrides

file keyreplaced byrecorded as
:steps--steps Nknob steps, source override
every pass's :every--eval-every Nknob eval-every, source override
:diagnostics--diagnostics, then SEXPGPU_DIAGNOSTICSknob diagnostics, source override
:evaluate--evaluate inline|offlineknob evaluate, source override
:seed--set seed=N, when the file declares no seed knobknob seed, source set

Everything else, :microbatches, :checkpoint-every, :precision, is the file's alone. There is no defrun key for the device, the metrics target, the checkpoint location or the resume; those are the environment's. See environment.

Values can be knobs: (defrun :steps steps :microbatches microbatches). --steps binds a knob named steps too, so the file reads the length it runs; see the knob steps.

:microbatches, :precision, :gradient, :query-radius, :diagnostics, :guard-finite, :clip-grad-norm, :ema and :ema-debias change the compiled graphs. The other keys are recorded in the setup beside them (how it works), so another seed or another length of run compiles to the same IR.

Global gradient clipping

:clip-grad-norm c limits the L2 norm over all trainable parameters' gradients to c. Omit it to disable clipping.

(defrun :steps 50000
        :clip-grad-norm 1.0
        :diagnostics ["optimizer/grad-norm"]
        :diagnose-every 200)

The norm is computed once per optimizer step, after microbatch accumulation and distributed gradient reduction, before any optimizer reads the gradients. Every gradient is multiplied by min(1, c / norm); zero gradients stay zero. Tied parameters count once, frozen parameters do not contribute, and all optimizer groups share one limit. The optimizer's moments receive the clipped gradients. Weight decay and the optimizer's final update are not norm-limited.

While clipping is enabled, select optimizer/grad-norm to report the norm before clipping as diagnostics/optimizer/grad-norm on sampled steps. It is one global value, tagged split=train, without an optimizer-group param tag. Changing diagnostic selection or sampling does not change clipping.

The norm sums squares in f32, or f64 when a gradient is declared f64; the scaled gradients retain their declared dtypes. Clipping does not repair non-finite gradients. Use :guard-finite true to stop on non-finite proposals.

Weight averaging

:ema decay keeps an exponential moving average of every trainable parameter, the ones a library model declares included, and evaluates it in place of the trained weights:

(defeval test :loader test-loader :every 500)
(defeval test-raw :loader test-loader :every 500 :ema false)

(defrun :steps 50000 :ema 0.999)
  • Each average is a model buffer at ema. followed by its parameter's path, so model.head.weight is averaged at ema.model.head.weight. It starts at the parameter's initial value, loaded or not, and after every optimizer step becomes decay * average + (1 - decay) * parameter. The average of a parameter declared bf16 is computed in f32 and stored as bf16, so with a decay near 1 it moves only when the parameter is far from it; declare a parameter f32 to average it finely. Under :precision :bf16 trained parameters stay f32.
  • Frozen parameters and defbuffer buffers are not averaged. Training, optimizers and buffer updates read the trained weights; only evaluation and the model run alone read the averages.
  • Every pass reads the averages, inline and with sexpgpu evaluate. A pass declared with :ema false reads the trained weights, so one file can report both. A generator reads the weights of the call it runs for: the trained weights before a training call, and before a pass the weights that pass reads.
  • A checkpoint's model, which export writes and serve runs, reads the averages, so it is the model the passes scored. Both take --trained for the trained weights instead.
  • The averages take as much memory as the trainable parameters. A checkpoint stores them with the parameters, and a resume restores them. A resume that turns :ema on or off is refused, since the averages would be new or gone; a resume with another decay continues from the stored averages.
  • :ema can be a knob. A decay of 1 or more, or below 0, is E-CONTRACT-003, and so is a top-level model bound to ema, whose paths the averages would take, and :ema-debias without :ema.

Early steps

After n steps the initial weights still make up decay^n of each average, so early passes read mostly the initialization: with :ema 0.999, 61% at step 500, 37% at step 1,000 and 5% at step 3,000. A pass reading the averages can then score far below an :ema false pass of the same step without anything being wrong with training. As a rule, the averages say little before about 3 / (1 - decay) steps.

:ema-debias true leaves the initial weights out:

(defrun :steps 50000 :ema 0.999 :ema-debias true)
  • After step n each average is the decay-weighted mean of the parameter after steps 1 to n, sum((1 - decay) * decay^(n - k) * parameter_k) / (1 - decay^n). The update at step n is keep * average + take * parameter, with take = (1 - decay) / (1 - decay^n) and keep = 1 - take, so after step 1 the average is the trained parameter, and from about 3 / (1 - decay) steps on it updates as plain :ema does. The initial weights, zero or not, have no share after the first step; before it, as at step-zero evaluation, the averages are the initial weights.
  • n is the run's optimizer step, which a resume and a fork from a checkpoint continue together with the stored averages. A run that loads its weights starts again from step 0, and averages what it trains from them.
  • The averages are still buffers holding weights: passes, export, serve and --trained read them as before, and a bf16 parameter's average is stored as bf16 as above.
  • A resume may turn :ema-debias on or off, or change the decay; it continues from the stored averages with the new update. Late in a run the two updates are the same.

Zero steps

:steps 0 and --steps 0 perform no optimizer update but still need the full run contract: the training graphs, the optimizer states and the training loader are prepared. On a fresh run each inline pass runs once at step 0, subject to signals and the time budget; the curriculum does not run, training counters stay at zero, and a configured checkpoint location receives step-00000000. Offline runs evaluate nothing, as usual. To evaluate an existing checkpoint without the training setup, use sexpgpu evaluate.

The step target counts updates already completed in a resumed checkpoint, so a zero-step resume keeps the checkpoint's step and counters, runs no update, and evaluates at that step. Negative :steps are E-CONTRACT-003.

Step numbering

A training step's number is the update being computed, so the first training step is step 0, and an evaluation or a checkpoint at step n has n completed updates behind it. A loop that numbers training metrics by completed updates would call step 0 update 1.

Related: diagnostics sampling, run.

Select evaluation values

:evals is a list or vector of eval-pass values, evaluated after all top-level definitions. Nil items are omitted. Selected passes follow legacy defeval declarations. :drop-evals ["name"] removes declared passes after selection. Names must be unique before removal. Both lists accumulate when a run extends a base; other defrun settings replace the inherited setting only when written.