S-exp GPU
All pages
Docs · Models and optimizersMarkdown

Writing optimizers

An optimizer is a function from a parameter, its gradient, its states and ctx to the new parameter and the new states. The body is traced once per parameter into an update graph, and parameters whose graphs come out the same, as a group's parameters of one shape usually do, share one.

(require "sexpgpu/optim" schedule-value update-diagnostics)

(defoptimizer momentum-sgd (param grad ctx &key (lr 0.01) (momentum 0.9))
  (setq lr (schedule-value lr ctx))
  (defstate velocity (zeros-like param))
  (let* ((v (+ (* momentum velocity) grad))
         (next (- param (* lr v))))
    (update-diagnostics param grad next)
    (optimizer-update next :velocity v)))

defoptimizer

(defoptimizer name (param grad ctx &key ...) body...) defines an optimizer. Calling it with keyword values configures it: (momentum-sgd :lr 0.05) is the value the contract's optimizer holds, and what group :optimizer accepts; see optimizer groups.

  • Traced once per parameter, so a shape-dependent choice is a plain compile-time if: Muon transposes a tall matrix by asking (> (dim x 0) (dim x 1)).
  • Each update runs on one trainable parameter and its states, and every trainable parameter has exactly one; there is no cross-parameter state. A shared graph runs once per parameter, each time on that parameter's own values, so its diagnostics and guards count once per parameter. An optimizer that draws random values draws a stream per parameter, so its graphs are never shared. Reading other parameters is below.
  • A body reports with diagnostic, never metric (E-CONTRACT-011); see optimizer diagnostics.

Periodic state updates

Use a scalar if on ctx to run a computation only on selected updates. For example, this binding refreshes a matrix on the first update and then every ten completed steps:

(next-basis (if (= (mod (getf ctx :step) 10) 0)
                (let ((q (qr (matmul factor basis)))) q)
                basis))

Return next-basis with the rest of the update in optimizer-update. :step counts completed optimizer steps and continues from the saved step on resume. Both arms are traced and planned before training. They must agree in structure, shapes, kinds and report declarations. Only the selected arm runs, and its proposal goes through the ordinary optimizer guards before it is accepted.

The condition must be a bool scalar computed from host-known scalar ctx values and constants (E-BRANCH-005). It needs no device readback. Use where for eager selection on tensor data. Declare optimizer state outside the lazy branches.

Reading other parameters

An update may read any other parameter, trainable or frozen, by calling a model that reads it, (panel) for a root whose function returns its table. It reads it at its declared shape and dtype, never precision-lowered (see numerics), and sees the value it had at the start of the step: every update of a step reads, then every proposal is committed together once the guards pass, so an update never sees another update's proposal of the same step. Its own parameter is still param, and a model the body calls reads that same value.

States

(defstate name init) declares one per-parameter state.

  • init evaluates normally and must be a zero constant: zeros, zeros-like or a zero full, including casts, reshapes, transposes and broadcasts of those. Helpers, conditionals and macros may return it.
  • Nonzero or computed values (even ones that happen to be zero), duplicate states and a defstate outside an optimizer are E-OPT-003.
  • Ordinary states are named in the IR and checkpoints by their names; gensym states get stable state.N names in declaration order.

optimizer-update

(optimizer-update new-param :state new-value ...) is the body's result.

  • Every declared state is returned exactly once. A body that does not end in optimizer-update, or drops a state, is E-OPT-004.
  • A state is named by keyword, :velocity, or by its quoted declaration symbol, ',velocity, which is how a macro returns a gensym state. Never turn a gensym into a keyword to name its state.
  • It is an ordinary function, so apply can pass a computed list of updates.

Schedules

A schedule is a function of ctx returning a scalar. Called while the update graph is traced, it builds ordinary nodes in it.

(defun stable-then-decay (peak &key (decay-fraction 0.7))
  (lambda (ctx)
    (let ((progress (getf ctx :progress)))
      (* peak (minimum 1.0 (/ (- 1.0 progress) decay-fraction))))))

To accept a number or a schedule, call (schedule-value lr ctx) from sexpgpu/optim in the body: it calls a function with ctx and returns anything else unchanged. The standard optimizers do this for every declared hyperparameter. Other function arguments, a gradient transform for example, stay callable by the body. The ctx keys are in curriculum and ctx.

Related: sexpgpu/optim, optimizer groups, macros.