Hessian of Perplexity for Large Language Models by PyTorch autograd (Open Source)
Ivan Ilin
TL;DR
This work addresses the infeasibility of computing the full Hessian for large language models by providing a practical workflow to obtain exact Hessians on small parameter subsets using PyTorch autograd and to estimate the Hessian diagonal via Hessian-vector products and Hutchinson's trick. It introduces a formal parameterization of transformer-like blocks, defines the perplexity-based objective $\phi(\mathbf w)=\mathrm{PPL}(\mathcal{R}^{-1}(\mathbf w))$, and demonstrates subset-focused Hessian computations across single layers, whole blocks, and across all layers. The study shows that the Hessian is approximately diagonal in empirical observations and details both the methodology and computational considerations (including memory constraints and additive batch properties) needed to perform Hessian analysis on large models. The results provide a concrete, open-source toolkit for curvature analysis, enabling researchers to study second-order structure, verify diagonal-approximation assumptions, and support quantization and optimization research in transformer-based LLMs.
Abstract
Computing the full Hessian matrix -- the matrix of second-order derivatives for an entire Large Language Model (LLM) is infeasible due to its sheer size. In this technical report, we aim to provide a comprehensive guide on how to accurately compute at least a small portion of the Hessian for LLMs using PyTorch autograd library. We also demonstrate how to compute the full diagonal of the Hessian matrix using multiple samples of vector-Hessian Products (HVPs). We hope that both this guide and the accompanying GitHub code will be valuable resources for practitioners and researchers interested in better understanding the behavior and structure of the Hessian in LLMs.
