S-exp GPU
All pages
Docs · Observing a runMarkdown

Writing diagnostics

(diagnostic "name" scalar :reduce :max :key value ...) records one scalar under diagnostics/<name> and returns it. Put it next to the thing it examines; it computes only when a run selects it. How selection and sampling work is diagnostics.

(let ((logits (head (norm-out ...))))
  (diagnostic "logits/max-abs" (max (abs logits)) :reduce :max)
  (diagnostic "logits/rms" (rms logits) :reduce :mean)
  (tap-gradient logits
                (lambda (g) (diagnostic "logits/grad-rms" (rms g) :reduce :mean))))
  • It works where metric does (prepare, the model body, objective, evaluate) and in an optimizer body, so a library model or optimizer carries its own and every run that uses it can turn them on.
  • Name it by what it examines, attention/entropy, not layer3-thing: the name is what a run selects by.
  • Keywords after :reduce are metadata, as for metric: :layer 3 makes one series per layer.
  • :below b and :above a bound it, strictly: a guard of its name that stops the run at a sampled step that crosses the bound. The bound checks each value before the reducer folds it, so :reduce :max with :above is stricter than the max it reports.
  • A diagnostic in the model body reports from the training graph and from each evaluation pass; split tells them apart.

Reducers

:reduce is required. It is how one series folds wherever several values meet: the batches of a pass, the parameters of an update group, the ranks of a data-parallel run.

reducerfolds tofor
:meanthe sum over the graph runs that reported, divided by their numbertypical values
:minthe smallesta lower bound, a margin
:maxthe largesta worst case: constraint error, largest logit
:sumthe totala count

A :min or :max that meets a NaN reports NaN, because that is the worst case. A diagnostic without :reduce, with an unknown one, or one name declared with two reducers is E-CONTRACT-015.

Optimizer diagnostics

In an optimizer body a diagnostic sees everything the update does: the parameter before, grad, every state, the new moments, the proposed step and the new parameter. The built-in optimizers report theirs through update-diagnostics from sexpgpu/optim:

(defoptimizer sgd (param grad ctx &key (lr 0.01) (weight-decay 0.0))
  (let ((next (- param (* lr (+ grad (* weight-decay param))))))
    (update-diagnostics param grad next)
    (optimizer-update next)))
  • The body is traced once per parameter, so each diagnostic has one value per parameter, and the reducer folds them across the update group: :max is the worst parameter, :mean the typical one.
  • Each series carries param, the group's label, beside split train. Metadata the body passes itself, :part "plus" for half of a packed parameter, keeps apart what should not fold together.

A parameter the loss does not depend on, such as an attention key bias (softmax ignores a shift shared by a whole row), gets a gradient of rounding noise, and its update ratio is noise over noise: of order one and different on every device. A group's :max can be that parameter; give such parameters a group of their own, or no bias.

Gradient diagnostics

For the complete optimizer step, defrun :clip-grad-norm provides optimizer/grad-norm, the global L2 norm before clipping, after accumulation and distributed reduction. This has one value for all trainable parameters. optimizer/grad-rms, from the optimizers, reports each group's largest per-parameter RMS; a gradient tap below observes one microbatch.

(tap-gradient x (lambda (g) ...)) returns x unchanged and hands the lambda g, the adjoint of x: the gradient of the objective with respect to it, as autodiff computes it for this microbatch.

  • The adjoint is of the objective as written: an objective that averages has the division in g; one that sums does not.
  • Only the training graph has an objective, so a tap in a model body is traced there and nowhere else; an evaluation pass returns x and never calls the lambda.
  • The lambda's diagnostic calls are its only output; its return value is dropped. A metric inside it is E-CONTRACT-017, because a gradient exists only on the sampled microbatch.
  • An unselected tap is removed with its diagnostics and costs nothing.

Per-term parameter gradients

(objective-term "name" weighted-scalar) returns the scalar unchanged and registers its parameter-gradient diagnostic. Use it in objective around each complete contribution, including loss weights, detached normalization and any microbatch scaling, then sum those returned contributions:

(defun objective (prediction targets)
  (let ((fit (fit-loss prediction targets))
        (aux (aux-loss prediction targets)))
    (+ (objective-term "fit"
                       (/ fit (stop-gradient (maximum fit 1e-6)) microbatches))
       (objective-term "aux" (/ (* 0.2 aux) microbatches)))))

(defrun :steps 10000 :microbatches microbatches
        :diagnostics ["optimizer/grad-rms/*"] :diagnose-every 100)
  • Each selected term reports diagnostics/optimizer/grad-rms/<name>, with param naming its optimizer group. The value is that group's largest per-parameter RMS, including zero for a group the term does not affect. Frozen parameters do not belong to optimizer groups.
  • The gradients are of the registered scalar as written. Multiplication or division outside objective-term does not become part of its diagnostic. Names must be unique within the objective and fit the metric-name limits; unused registrations and non-scalar contributions are E-OBJ-002.
  • Differentiation follows the run's precision policy, including BF16 rounding, as the training backward does. It observes gradients before clipping, optimizer moments, weight decay or updates.
  • Sampling uses the existing diagnostic schedule: rank zero's first microbatch on sampled steps, including the final step. This is not the accumulated or distributed optimizer-step gradient. Norms cannot be added to recover that gradient or reveal cancellation between tasks.
  • Unselected terms add no runtime work. Selected terms add backward work and scalar reductions on sampled microbatches, and increase compilation and peak memory costs. Start with :diagnose-every 100 and measure the overhead for your model. Selected terms require :gradient :reverse (E-CONTRACT-022).

Related: diagnostics, sexpgpu/optim, writing optimizers.