Kalman Bayesian Transformer

Haoming Jing, Oren Wright, José M. F. Moura, Yorie Nakahira

TL;DR

The Kalman Bayesian Transformer enables highly efficient, sequential fine-tuning of transformer models in memory- and latency-constrained edge environments. By replacing the final linear layer with a Bayesian Neural Network and utilizing closed-form Kalman filtering/smoothing, the method eliminates the need for expensive sampling-based optimization while providing explicit uncertainty quantification and robust protection against catastrophic forgetting.

What Problem Does This Paper Address?

Deploying pre-trained transformers in real-world, streaming environments (like robotics or edge computing) presents a major challenge: data arrives sequentially, and the underlying data distribution often shifts over time. Standard batch retraining is prohibitively expensive and requires massive onboard memory to store past data. Conversely, simple sequential fine-tuning typically suffers from catastrophic forgetting, where the model overwrites previously learned knowledge to accommodate new samples.

While Bayesian deep learning offers a principled solution by treating pre-trained models as priors and quantifying prediction uncertainty, traditional methods like Variational Inference (VI) or Markov Chain Monte Carlo (MCMC) sampling are too computationally heavy for latency-critical applications. This paper addresses the need for a lightweight, data-efficient, and real-time sequential fine-tuning mechanism for transformers.

Methods

The authors introduce the Kalman Bayesian Transformer. Instead of updating the entire network, they freeze the early transformer layers ($T_1$) and replace the final linear projection layer ($T_2$) with an $L$-layer Bayesian Neural Network (BNN).

Fig. 1: Proposed architecture. The original transformer archi- tecture is shown on the left, and the Bayesian neural network that replaces the linear layer is shown on the right.

To make fine-tuning computationally tractable without iterative sampling, the authors combine several novel techniques:

  1. Closed-Form Moment Propagation: The first and second moments (mean and variance) of the network's random variables are propagated analytically layer-by-layer through both linear and ReLU activation functions.
  2. Taylor Approximation for Softmax: Because a softmax function cannot be easily integrated in closed form, the authors apply a first-order Taylor expansion to approximate the mean and covariance of the output probabilities.
  3. Rauch-Tung-Striebel (RTS) Smoother: Fine-tuning is framed as a recursive posterior inference problem. The model executes a single forward pass (Bayesian filtering) followed by a single backward pass (Kalman smoothing) to update the weight distributions analytically.
  4. Autoregressive Data Preprocessing: To handle the non-i.i.d. nature of autoregressive sequence data, a preprocessing step (Algorithm 1) restructures sequential dependencies into independent batched representations suitable for recursive Kalman updates.

Datasets

The method was evaluated using a simulated control environment featuring a Decision Transformer tasked with stabilizing an inverted pendulum.

  • Pre-training Data: Generated using an optimal Linear Quadratic Regulator (LQR) controller designed for a pendulum with a cart mass ($mc$) of 1, pendulum mass ($mp$) of 0.1, and length ($lp$) of 0.5.
  • Fine-tuning Data: Generated from an LQR controller where the system parameters were drastically shifted ($mc=1, mp=1, lp=5$). The model had to adapt to this new physical distribution using streaming, i.i.d. samples.

Key Insights and Ideas

  • Uncertainty-Driven Balancing: By explicitly tracking parameter variances, the Kalman framework adaptively balances prior knowledge against new information. When the model is highly certain about its prior, it updates conservatively; when uncertainty is high, it learns rapidly from new data.
  • Zero-Replay Anti-Forgetting: Traditional networks require a replay buffer of past data to avoid catastrophic forgetting. The Bayesian framework naturally resists forgetting because the prior distribution acts as a mathematical anchor for past knowledge, allowing stable learning with a memory capacity of just one sample.
  • Single-Pass Optimization: Bypassing gradient descent entirely, the RTS smoother updates the network weights in a single backward pass, turning optimization into a direct analytical calculation.

Key Results

  • Superior Stability & Success Rate: With a memory capacity of only 1 sample, the proposed method achieved a 100% success rate in stabilizing the inverted pendulum after processing roughly 100 samples. It significantly outperformed standard warm-started retraining methods, even those equipped with memory capacities of up to 50 samples, which frequently suffered from catastrophic performance drops due to forgetting.
Fig. 3: The rate of success in stabilizing the inverted pen- dulum. The blue line shows the proposed method, and the other lines show the warm-started retraining with different memory capacities.
  • Extreme Computational Efficiency: The average processing time per sample remained virtually flat and near-zero for the proposed method. In contrast, standard retraining time scaled linearly and steeply with the number of samples processed.
Fig. 4: The average computation time. The blue line shows the proposed method, and the other lines show the warm- started retraining with different memory capacities.
  • Accurate Uncertainty Quantification: When evaluated against stochastic training data with varying injected noise ($\Sigma_{\text{data}} = 0, 10, 20, 50$), the model's predicted variance accurately tracked and converged to match the true data variance over training iterations.
Fig. 5: The uncertainty predicted by the proposed method versus training iterations (number of samples processed). To ensure a clear view of the data in the presence of uncertainties, we apply a moving average filter of size 500 and plot the training iterations 30000 to 70000, during which the variances stop to decrease.

Limitations and Future Work

The primary limitation identified by the authors involves covariance propagation. While the forward pass successfully accounts for off-diagonal terms (cross-covariances) in the covariance matrix, a closed-form mathematical solution does not yet exist for propagating these off-diagonal terms during the backward pass through non-linear activation layers (like ReLU). Currently, the backward pass simplifies the computation by only keeping diagonal terms. Developing a closed-form propagation method for these off-diagonal terms in the backward pass remains an open direction for future work.

Summary updated

BibTeX
@inproceedings{jing2026kalman,
  author = {Haoming Jing and Oren Wright and José M. F. Moura and Yorie Nakahira},
  title = {{Kalman Bayesian Transformer}},
  booktitle = {CDC},
  year = {2026},
  url = {https://openreview.net/forum?id=6FwdHIc5K7},
}
Figure