An MoE run can fail loudly (a loss or gradient spike, a NaN) or quietly (routing collapse, a dead or rogue expert) while the loss stays flat. Build the monitor that reads a run's logs and reports every alarm.
spike_steps(values, num_samples=100, factor=10.0)
Scan the steps in order. Non-finite values (NaN, ) are skipped entirely. The first num_samples finite values only set the reference: = the largest absolute value among them, and they are never flagged. Every later finite value with is flagged. Return the list of flagged step indices.
loss_alarm_steps(losses, margin=0.1): flag every finite step whose loss exceeds the lowest finite loss seen so far (up to and including that step) by more than margin. Return the step indices.
maxvio(counts): for a vector of tokens per expert (zero-count experts stay in the mean); None if every count is 0.
expert_norm_ratios(norm_sum, counts): norm_sum[e] is the sum of per-token output norms of expert and counts[e] its token count. Among experts with counts > 0, the mean output norm is norm_sum / counts; let be the median of those means. Return a dict {"max_ratio", "min_ratio", "argmax", "argmin", "empty"}: the largest and smallest mean over (rounded to 4 decimals), the expert ids attaining them (lowest id on ties), and the sorted list of experts with zero tokens. If no expert has tokens or , both ratios and both ids are None.
health_report(loss, grad_norm, counts, norm_sum, maxvio_limit=1.0, rogue_ratio=10.0, dead_ratio=0.1, margin=0.1, num_samples=100, factor=10.0)
loss, grad_norm have length ; counts and norm_sum are (S, L, E) (step, MoE layer, expert). Return a dict with keys in this order:
"nonfinite": steps where the loss or the grad norm is not finite."loss_spikes", "grad_spikes": spike_steps of each series."loss_alarm": loss_alarm_steps(loss, margin)."collapse": [step, layer] pairs with MaxVio strictly above maxvio_limit."rogue": [step, layer, argmax] where max_ratio > rogue_ratio."dead": [step, layer, argmin] where min_ratio < dead_ratio."empty": [step, layer, expert] for every expert with zero tokens.
Lists are ordered by step, then layer, then expert.