Skip to content

Repository files navigation

Replication Package: Hessian-Based Insights for Fault Diagnosis in Attention-Based Models

Code for the study "Can Hessian-Based Insights Support Fault Diagnosis in Attention-Based Models?" (FSE 2025, IVR track).

Study overview

Schematic diagram of the study

The study runs in six stages. We select three attention-based models (HAN, 3D-CNN, DistilBERT), measure first-order gradients as a baseline, compute the Hessian (curvature and off-diagonal parameter interactions), isolate the attention parameters of each layer, apply controlled parameter perturbations, and read the results as fault-diagnosis insights about high-risk components.

What the code computes

  • RQ1, curvature analysis. Per-layer Hessian trace (Hutchinson estimator) and top eigenvalues (Lanczos algorithm) via PyHessian, scoped to one attention layer at a time. A negative trace marks a fragile (negative-trace, indefinite) region. A positive trace marks a stable region.
  • RQ2, parameter interaction. Off-diagonal Hessian blocks between parameter groups, computed as mixed second partial derivatives through nested autograd, reduced to a mean interaction value. The HAN word-vs-sentence interaction is the headline measurement.
  • Gradient baseline. Per-layer first-order gradient norms, the first-order measurement the paper compares curvature against.
  • Intervention study. Real prediction-variability and off-diagonal Hessian measured before perturbation, after perturbation, and after a reduced learning-rate intervention on the coupled word parameters.
  • Loss-perturbation curves. Loss as a function of perturbation magnitude alpha for each attention layer, the data behind the loss-sensitivity figure.

Structure

  • models/han.py, models/cnn3d.py -- HAN and 3D-CNN with their attention layers.
  • models/distilbert.py -- DistilBERT implemented from scratch (embeddings, multi-head self-attention, FFN, the full 6-layer stack), in the same explicit style as the other two models. Its module names match the official layout, so from_pretrained loads the real distilbert-base-uncased weights into it by name. The forward pass is verified to produce bit-identical logits to the official HuggingFace model on the same weights.
  • models/distilbert_wrapper.py -- thin adapter (DistilBertClassifier) over the from-scratch model, giving the single-tensor-in, logits-out interface the diagnostic scripts and PyHessian use.
  • scripts/diagnostic_tools.py -- HessianAnalyzer, GradientAnalyzer, Perturbator, LayerIsolator. The real computation lives here.
  • scripts/data.py -- real dataset loaders (auto-download for text, manual for video).
  • scripts/reproduce_results.py -- RQ1 and RQ2 driver.
  • scripts/intervention_study.py -- RQ2 validation on HAN.
  • scripts/loss_perturbation.py -- loss-sensitivity sweep and figure.
  • results/ -- generated CSV outputs.

Datasets

Model Task Dataset Source
DistilBERT Sentiment analysis SST-2 stanfordnlp/sst2, auto-downloaded
HAN Document classification AG News fancyzhx/ag_news, auto-downloaded
3D-CNN Video classification HMDB51 manual one-time download (see below)

The text datasets download on first run through the HuggingFace datasets library. HMDB51 is not auto-downloadable under its license. Download it from the HMDB51 page, then point the loader at it:

export HMDB51_DATA_ROOT=/path/to/hmdb51/videos
export HMDB51_ANNOTATION_ROOT=/path/to/hmdb51/splits

Without these, run with --skip-video and the 3D-CNN rows are omitted.

Requirements

pip install -r requirements.txt

Research questions

  • RQ1 (fragile regions). Curvature analysis surfaces negative-trace, indefinite regions in specific attention layers that gradients alone do not distinguish from stable regions.
  • RQ2 (fault propagation). Off-diagonal Hessian elements quantify cross-component coupling, showing how a perturbation in one attention component can cascade into another.

About

For FSE 2025 work on Hessian Analysis in Attention Models!

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages