TorchNTK: A Library for Calculation of Neural Tangent Kernels of PyTorch Models

Andrew Engel, Zhichao Wang, Anand D. Sarwate, Sutanay Choudhury, Tony ChiangarXiv 2022 · DOI

Abstract

The Neural Tangent Kernel (NTK) is a cornerstone of modern deep learning theory, yet computing the empirical NTK for finite-width models remains a significant computational challenge. We introduce torchNTK, an open-source PyTorch library that provides flexible autograd-based methods and highly optimized explicit differentiation for Multilayer Perceptrons (MLPs). TorchNTK enables researchers to compute the NTK with significantly lower memory overhead and up to 1000x speedups compared to standard Jacobian-based implementations, facilitating the study of neural network evolution in practical regimes.

Introduction

The Neural Tangent Kernel (NTK) describes how neural networks behave like kernel machines in the infinite-width limit. However, studying the empirical NTK of real-world, finite-width models is notoriously difficult due to memory constraints. For a dataset of size $N$ and a model with $P$ parameters, the required Jacobian matrix is of size $P \times N$. Even for modest models (e.g., 100k parameters and 60k images), this matrix requires approximately 24GB of VRAM, often exceeding the capacity of consumer-grade GPUs. While JAX-based libraries exist, the PyTorch ecosystem has lacked a dedicated tool for these calculations, often leading to "Out of Memory" (OOM) errors.

Methods

TorchNTK offers two primary approaches to calculating the NTK:

  1. Autograd-based Methods: These are flexible and work "out-of-the-box" for any PyTorch architecture, including CNNs. The library includes implementations using torch.autograd.functional.jacobian, iterative layer-wise autograd, and torch.vmap for parallelization.
  2. Explicit Differentiation: The authors derived recursive formulas specifically for Multilayer Perceptrons (MLPs). By bypassing the general-purpose autograd engine and calculating gradients directly, this method is significantly faster and more memory-efficient.

A key feature of the library is its layer-wise decomposition. Instead of calculating one monolithic NTK, the library exposes the additive components contributed by each layer. This allows for spectral analysis of individual layers and enables "deep and narrow" models to be processed by holding several smaller $N \times N$ matrices in memory rather than one massive Jacobian.

Evaluation

The library was evaluated using synthetic data for performance benchmarking and a binary classification subset of MNIST (digits 6 and 9) to demonstrate NTK evolution during training.

Performance Benchmarks

Benchmarks conducted on an NVIDIA A-100 GPU highlight the library's efficiency. For a high-parameter MLP (MLP-h) with 1,000 datapoints, the explicit differentiation method was over 1000x faster than the nearest autograd technique.

Table 1: MLP Benchmark Time showing explicit differentiation's speed advantage.

In terms of memory, standard autograd methods hit OOM limits at just 1,000 datapoints in the MLP-h regime. In contrast, the explicit differentiation method successfully processed 20,000 datapoints while staying within GPU memory limits.

Table 4: MLP-h Benchmark Memory showing how explicit differentiation scales where others fail.

Practical Application

The library's accuracy was verified by training a kernel machine using the calculated NTK on the MNIST binary task, achieving ~98.9% accuracy, which closely matches the performance of the original neural network.

Discussion and Key Insights

  • Explicit Differentiation: Hand-derived formulas for MLPs avoid the overhead of the computational graph, leading to massive performance gains.
  • Layer-wise Efficiency: Computing the NTK layer-by-layer is far more memory-efficient for deep networks than computing the full Jacobian at once.
  • Learning Diagnostics: Visualizing the NTK over training reveals that it evolves to represent the similarity between datapoints, with diagonal blocks (same-class samples) becoming more distinct as training progresses.
  • Fisher Information: Layer-wise NTK calculation provides a gateway to studying the layer-wise Fisher Information Matrix (FIM), useful for analyzing loss landscape geometry.

Limitations and Future Work

Currently, TorchNTK's high-performance explicit differentiation is limited to MLPs and single-output models. Future work will focus on extending these optimized methods to CNNs and multi-class classification. Additionally, multi-GPU support is planned to further scale the library for larger models and datasets.

Summary updated

BibTeX
@misc{engel2022torchntk,
  author = {Andrew Engel and Zhichao Wang and Anand D. Sarwate and Sutanay Choudhury and Tony Chiang},
  title = {{TorchNTK: A Library for Calculation of Neural Tangent Kernels of PyTorch Models}},
  year = {2022},
  eprint = {2205.12372},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2205.12372},
}
Figure