All pages
Truth, iteration and values
Truth and staging
nil and false are false; every other ordinary value is true, including zero,
the empty string and an empty vector. if, when, unless, cond, and,
or, not, truthy, filter and assert all use this rule. and and or
return the deciding ordinary value itself, not a boolean. They also check the
last evaluated operand; neither can return a tensor.
Outside an optimizer's periodic update,
a tensor, a parameter or a ctx scalar used as a condition is E-FLOW-001. Use
logical-and, logical-or, logical-not and logical-xor for
elementwise boolean operations. Shapes are static
and both branches must exist in the graph, so select elementwise instead:
(if (> x 0.0) x 0.0) ; E-FLOW-001: x is a tensor
(where (> x 0.0) x (zeros-like x)) ; the graph computes both, picks per element
(if (> (dim x 0) (dim x 1)) ...) ; fine: dim is an ordinary integerShape queries (shape, rank, dim, numel, dtype) return ordinary values,
so compile-time decisions on shapes are plain ifs. That is how Muon transposes
a tall matrix; see sexpgpu/optim.
Inside defoptimizer, a scalar ctx condition the host already knows can
select a lazy if with equal branch schemas. Only its selected branch executes;
see periodic state updates.
Finite training phases
A defphase selects a different training objective inside one run. Each
alternative is compiled before training; ordinary if still makes compile-time
choices and where still computes both values.
(defun objective (prediction targets)
(+ (main-loss prediction targets) (helper-loss prediction targets)))
(defun main-only (prediction targets)
(main-loss prediction targets))
(defphase main :from-step 4000 :objective main-only)The first 4,000 updates use objective; update index 4,000 and later use
main-only. This deliberately removes the helper's loss. A weight reaching zero
does not select a phase automatically.
defphase takes a unique name other than default, and these optional keys:
| key | meaning |
|---|---|
:from-step | first zero-based update index that can use this phase; default 0 |
:stage | require this zero-based training curriculum stage; absent means any stage |
:objective | function with the same arguments as objective; defaults to objective |
:prepare | function with the same contract as prepare; defaults to prepare |
The last declaration whose step and stage match wins. When none matches, the run
uses the default prepare and objective. Stage selection occurs after all
scheduled evaluation passes and their curriculum decisions, before the next
update. Every microbatch of an update uses that one phase. A phase can be
re-entered when curriculum returns to its stage.
All phases use the same model instances, parameters, parameter shapes, loaders,
batch sizes, precision and optimizer definitions. A phase's prepare must
declare the same counters in the same order. It may pass ordinary values into
the model to select compile-time behavior while preserving those contracts.
A parameter with no differentiable connection to the selected objective keeps
its value and every optimizer state unchanged: no momentum update or weight
decay runs for it. Observations alone do not activate its optimizer. When the
parameter becomes active again, its optimizer resumes those saved states using
the global step context. A connected parameter whose numerical gradient is zero
still runs its optimizer. Model-buffer updates continue after every update. With
:gradient :forward-credit, an explicitly declared credit map keeps its
parameter active even when the scalar loss does not read it.
Only the selected training graph emits metrics, guards and diagnostics; absent
metrics have no sample for that step. Training metrics carry phase metadata,
and a transition emits a training phase <name> annotation. Evaluation passes
keep their declared preparation and evaluation functions and share the same
model weights; they do not select the training objective.
Checkpoints keep all parameters and optimizer states, including inactive ones. Resume selects the phase from the saved next-update index and curriculum stage; a checkpoint at an evaluation boundary repeats evaluation and curriculum before selecting that update's phase. Saved lowering choices include each phase's stacking. Source edits are reported under the normal resume rules.
Iteration
| form | behavior |
|---|---|
(dotimes (i count [result]) body...) | evaluates count once, runs the body for i from 0 below it; a negative count runs nothing; result sees i equal to the iteration count |
(dolist (item list [result]) body...) | evaluates the list once, runs the body per element; result sees item bound to nil |
(repeat n f) | calls f with 0..n-1 and collects the results: how a stack of blocks is built |
(map f xs), (filter f xs), (reduce f init xs) | collecting and folding; see builtins |
dotimes and dolist collect nothing: they return result, or nil without
one, and preserve its multiple values. Their loop variable is one binding shared
across iterations. With a tensor accumulator they unroll into the graph:
(let ((result (zeros-like x)))
(dotimes (i degree result)
(setq result (+ result (* (sum (slice coefficients 0 i (+ i 1))) (pow x i))))))Multiple values
(values expr...) returns zero or more values. Ordinary arguments, variable
initializers, assignments, vector elements and training contract components take
the primary value; zero values give nil there. The last form of a body and the
chosen branch of if keep all values.
| form | behavior |
|---|---|
(values-list list) | the list's elements as values |
(multiple-value-list expr) | all returned values as a list |
(multiple-value-bind (names...) expr body...) | binds the values; missing ones are nil, extras discarded |
(multiple-value-call f expr...) | calls f with every value of every expr, concatenated |
(multiple-value-prog1 expr body...) | returns expr's values after running the body |
sexpgpu eval prints several values as #<values ...>.
Errors on purpose
(error message...)stops compilation withE-EVAL-001and the message.(assert test [message])stops withE-EVAL-002whentestis false:(assert (= 2 (rank param)) "muon updates matrices").
Both point at the line that raised them, or at your model application when raised inside a library. See check.
Related: It is Common Lisp, tensor operations.