Foundations to Frontier
Tests
M. Robust, Verifiable and Onboard AI › M2 Verification and Assurance

Runtime Monitoring

Verification proves properties for regions chosen before deployment; in operation a network meets inputs nobody anticipated. A runtime monitor watches each input, or the network's internal state, and raises an alarm when the prediction should not be trusted, so that a fallback can take over. This page builds six common monitors (softmax confidence, energy, Mahalanobis distance of features, activation patterns, nearest-neighbour distance and an input range check), calibrates their alarm thresholds with conformal guarantees, and tests them on four kinds of shift. It also builds the Simplex architecture, in which a verified safety controller and a simple decision module keep a learned controller safe.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A watchdog for the network

A network trained on some kinds of inputs gives an answer for anything you show it, even things it has never seen, and it often sounds sure of itself. A runtime monitor is a watchdog that looks at each input, or at what the network is doing inside, and barks when something looks unfamiliar. The system can then slow down, ask a human or switch to a simpler controller that is known to be safe. A good watchdog barks rarely when all is normal and reliably when it is not, and different watchdogs notice different kinds of trouble.

🎮 Verification lab

Detection at 5% false alarms (new class, M2-4 study, seed 0)

softmax (msp)
22.6% (AUROC 0.791)
energy
38.4% (AUROC 0.862)
Mahalanobis
36.7% (AUROC 0.845)
activation pattern
30.3% (AUROC 0.813)
kNN distance
58.7% (AUROC 0.910)
input range
5.8% (AUROC 0.524)

AUROC below 0.5 means the monitor finds these inputs more normal than real data: softmax confidence grows far from the training data.

Position over 30 s (red line: wall at 10 m; dashed: target 9.5 m). No crash. The safety controller was used in 0 of 300 steps.

The decision module accepts a command only if, one step later, the vehicle could still stop before the wall at full braking. Simulated in your browser.

Why engineers care

Networks with ReLU activations become more confident, not less, as inputs move far from the training data (Hein and colleagues, 2019); the exercises reproduce this.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A runtime monitor decides…
  2. 2. After an alarm, a system typically…
  3. 3. A good monitor…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Out-of-distribution (OOD) detection: deciding whether an input comes from the distribution the model was trained on. Scores: maximum softmax probability (Hendrycks and Gimpel, 2017), energy (Liu and colleagues, 2020), Mahalanobis distance of features (Lee and colleagues, 2018), distance to the k-th nearest training feature (Sun and colleagues, 2022).
  • Activation-pattern monitor: store the on/off patterns of a hidden layer seen in training and flag inputs whose pattern is far from all of them (Cheng, Nührenberg and Yasuoka, 2019); box abstractions of features are a variant (Henzinger and colleagues, 2020).
  • False-alarm rate and detection rate: shares of in-distribution and shifted inputs above the threshold; AUROC summarises all thresholds as the probability that a shifted input scores higher than a normal one.
  • Conformal threshold: the ⌈(n + 1)(1 − α)⌉-th smallest of n validation scores bounds the expected false-alarm rate by α.
  • Selective prediction: abstain on the inputs with the worst scores and report the accuracy on the rest (risk against coverage).
  • Simplex architecture / runtime assurance: an advanced controller, a verified safety controller and a decision module that switches to the safety controller before the state can leave a recoverable set (Sha, 2001); ASTM F3269 describes it for aircraft.
📝 Quick check (Level 1)
0/3 answered
  1. 1. AUROC is the probability that…
  2. 2. A conformal threshold bounds…
  3. 3. In the Simplex architecture, safety relies on…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Monitors for a classifier

Exercise 1.

  • Gaussian clusters in 10 dimensions; a ReLU network with two hidden layers of 32 units trained on three of four clusters (the fourth is held back as an unseen class).
  • Six monitor scores: 1 − max softmax, energy, class-conditional Mahalanobis distance of the last hidden layer, Hamming distance of its on/off pattern to the nearest training pattern of the predicted class, distance of normalised features to the 10th nearest training feature, and how far the raw inputs lie outside their training range.
  • AUROC by ranks (ties count half) and conformal thresholds.
  • Four shifts: the unseen class, inputs amplified 4 times, three sensors stuck at 6, and uniform noise.

Practice.

  • Run several monitors: each kind of shift has its own best detector, and a cheap input range check catches sensor faults that feature-based scores miss.
  • Calibrate thresholds on held-out in-distribution data with a conformal rank, and split the false-alarm budget between monitors.
  • Do not rely on softmax confidence alone for safety: it predicts in-distribution errors but can be high far from the data.
  • Define the fallback before deployment: what happens on an alarm (slow down, hand over, safe stop) matters as much as the detector.
  • Watch alarm rates over time, not only single inputs, to catch slow drift (topic N4-1).
class Monitors:
    def fit(self, net, X, y):
        H = features(net, X)                            # last hidden layer
        self.mu = class means of H; self.P = inverse(shared covariance of H)
        self.patterns = set of on/off patterns of H per class
        self.F = H / |H|; self.lo, self.hi = X.min(0), X.max(0)
    def scores(self, X):
        Z, H = net(X)
        msp = 1 - max softmax(Z); energy = -logsumexp(Z)
        maha = min over classes of (H - mu_c)^T P (H - mu_c)
        pattern = Hamming distance to nearest pattern of predicted class
        knn = 10th smallest distance between H/|H| and self.F
        range = total amount by which X lies outside [lo, hi]
🐍 Exercise 1: six monitors, AUROC and conformal thresholds
Implement six runtime monitors on a ReLU classifier, AUROC by ranks and conformal alarm thresholds.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why run several monitors?
  2. 2. Softmax confidence alone is risky because it…
  3. 3. Thresholds should be set on…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the conformal rank works. Let the n validation scores and the score of a new in-distribution input be exchangeable, with no ties. The new score is equally likely to take any of the n + 1 ranks. It exceeds the k-th smallest validation score only if its rank is above k, which happens with probability (n + 1 − k)/(n + 1). Choosing k = ⌈(n + 1)(1 − α)⌉ makes this at most α. The guarantee holds on average over validation sets, not for every one (Practice 3).

Why softmax confidence fails far from the data. A ReLU network is piecewise linear, so along a ray x = t·u its logits are eventually linear in t, z(t) ≈ t·a + c. Unless all components of a are equal, the gap between the largest logit and the others grows with t, and the softmax tends to 1 for one class (Hein and colleagues, 2019). Amplified inputs therefore look more normal to the softmax and energy scores than real data, which is what AUROC below 0.5 means. Distances in feature or input space grow with t instead, so they detect such inputs; normalising features, as the kNN monitor does, removes exactly this information.

Why Simplex is safe. Let the recoverable set be the states from which full braking stops before the wall, p + v²/(2a_max) ≤ wall for v ≥ 0. Under full braking the stopping point p + v²/(2a_max) does not change, so the safety controller keeps the state recoverable; the decision module only accepts an advanced command if the next state is recoverable. By induction the state is always recoverable and the vehicle never hits the wall, whatever the advanced controller does. The proof needs only the model of the vehicle and the safety controller, never the learned one.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The conformal guarantee holds…
  2. 2. Far from the data, a ReLU network's softmax tends to…
  3. 3. Simplex safety is proved using…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Which monitors detect which kinds of shift, how well do they flag the classifier’s own errors, and do the conformal thresholds keep false alarms at 5%?

History.

  • L. Sha (2001) introduced the Simplex architecture for using complex controllers safely; runtime assurance was later standardised for aircraft in ASTM F3269 (2017).
  • D. Hendrycks and K. Gimpel (2017) proposed the maximum softmax probability baseline; K. Lee and colleagues (2018) the Mahalanobis detector; W. Liu and colleagues (2020) energy scores; Y. Sun and colleagues (2022) nearest-neighbour distances.
  • C.-H. Cheng, G. Nührenberg and H. Yasuoka (2019) monitored neuron activation patterns; T. Henzinger, A. Lukina and C. Schilling (2020) used box abstractions (‘outside the box’).
  • M. Hein, M. Andriushchenko and J. Bitterwolf (2019) showed that ReLU networks are overconfident far from the training data.
  • V. Vovk and colleagues (2005) developed conformal prediction; A. Angelopoulos and S. Bates (2021) popularised it for machine learning.

Exercise 2.

  • Training: 1,500 points from three clusters; thresholds for 5% false alarms from 1,000 validation inputs.
  • Test: 3,000 in-distribution inputs and 1,000 inputs of each shift. For each monitor: AUROC and detection rate per shift, false alarms on the test inputs, and AUROC for telling the classifier’s errors from its correct answers.

Seed 0 (test accuracy 0.969), AUROC and detection rate:

monitor new class amplified stuck sensors noise errors (AUROC) false alarms
1 − max softmax 0.791, 0.226 0.204, 0.006 0.484, 0.091 0.479, 0.092 0.926 0.053
energy 0.862, 0.384 0.023, 0.002 0.403, 0.117 0.299, 0.052 0.893 0.057
Mahalanobis 0.845, 0.367 1.000, 0.999 1.000, 1.000 0.999, 0.995 0.688 0.037
activation pattern 0.813, 0.303 0.514, 0.038 0.869, 0.140 0.803, 0.305 0.872 0.026
kNN distance 0.910, 0.587 0.506, 0.052 0.849, 0.290 0.868, 0.510 0.932 0.054
input range 0.524, 0.058 0.999, 0.999 1.000, 1.000 0.990, 0.981 0.506 0.010

Seeds 1 and 2 (test accuracy 0.930 and 0.968): AUROC of 1 − max softmax on amplified inputs 0.243 and 0.253, of energy 0.042 and 0.042; Mahalanobis and range at least 0.99 on amplified, stuck sensors and noise; kNN on the new class 0.826 and 0.934, range 0.521 and 0.517; error AUROC of 1 − max softmax 0.874 and 0.954, of kNN 0.781 and 0.873, of range 0.496 and 0.501; false alarms at most 0.058.

Findings.

  • Softmax confidence and energy were fooled by amplified inputs (AUROC 0.20 to 0.25 and 0.02 to 0.04): the network was more confident on them than on real data.
  • Feature distance (Mahalanobis) and the input range check caught amplified inputs, stuck sensors and noise almost perfectly in every seed, but the range check was blind to the unseen class (AUROC 0.52 in every seed), whose inputs lie inside the normal ranges.
  • The kNN distance was best on the unseen class in every seed (AUROC 0.83 to 0.93) but missed amplified inputs (about 0.51 to 0.53), because normalising the features removes their scale.
  • Telling errors apart was a different task: 1 − max softmax did well (AUROC 0.87 to 0.95) and the range check not at all (about 0.5); in-distribution errors lie near the decision boundary, not in unusual places.
  • The conformal thresholds kept false alarms near or below 5% (at most 5.8% over all monitors and seeds); the discrete pattern and range scores fired less often because of ties.
🐍 Exercise 2 (research): which monitors detect which kinds of shift, how well do they flag the classifier's own errors, and do the conformal thresholds keep false alarms at 5%?
Compare six runtime monitors on four kinds of shift and on the classifier's own errors, with thresholds set for 5% false alarms.

Monitoring in practice

  • Libraries: PyTorch-OOD and OpenOOD implement dozens of detectors and benchmarks; conformal tools such as MAPIE and crepes calibrate thresholds and prediction sets.
  • Safety-critical systems combine input checks (ranges, rates of change, consistency between redundant sensors), model-level monitors and a runtime-assurance switch to a simple certified controller, as in ASTM F3269 for aircraft and in the safety concepts of automated vehicles.
  • Open problems: monitors are themselves learned and can be fooled; near shifts are hard to detect; and the right fallback depends on the system. Monitoring evidence feeds into the safety case (topic M2-5).

References

  1. L. Sha (2001). “Using simplicity to control complexity.” IEEE Software.
  2. D. Hendrycks and K. Gimpel (2017). “A baseline for detecting misclassified and out-of-distribution examples in neural networks.” ICLR.
  3. K. Lee, K. Lee, H. Lee and J. Shin (2018). “A simple unified framework for detecting out-of-distribution samples and adversarial attacks.” NeurIPS.
  4. C.-H. Cheng, G. Nührenberg and H. Yasuoka (2019). “Runtime monitoring neuron activation patterns.” DATE.
  5. M. Hein, M. Andriushchenko and J. Bitterwolf (2019). “Why ReLU networks yield high-confidence predictions far away from the training data and how to mitigate the problem.” CVPR.
  6. Y. Sun, Y. Ming, X. Zhu and Y. Li (2022). “Out-of-distribution detection with deep nearest neighbors.” ICML.