DiJiang: Efficient Large Language Models through Compact Kernelization

Hanting Chen, Liuzhicheng, Xutao Wang, Yuchuan Tian, Yunhe Wang

Abstract

The quadratic computational complexity of the self-attention mechanism remains a primary bottleneck for scaling Large Language Models (LLMs) to longer sequences. While linear attention alternatives exist, they typically necessitate expensive training from scratch. This paper introduces DiJiang, a novel frequency-domain kernelization approach that transforms pre-trained Transformer models into linear-complexity models with minimal retraining. By utilizing the Discrete Cosine Transform (DCT) and Weighted Quasi-Monte Carlo (WQMC) sampling, DiJiang achieves performance parity with LLaMA2-7B while requiring only 1/50th of the training cost and providing up to a 10x boost in inference speed.

1. Introduction

The "quadratic bottleneck" is a fundamental limitation of the standard Transformer architecture. In vanilla self-attention, the computational cost grows quadratically ($O(n^2)$) relative to the sequence length $n$, as every token must attend to every other token. This complexity restricts the use of long contexts and increases the environmental and economic costs of model deployment.

Existing solutions, such as linear attention models (e.g., Performer, Linformer), attempt to approximate the attention matrix. However, these models often require training from the ground up. For a model like LLaMA2-7B, which requires over 80,000 GPU-hours for initial training, starting from scratch is often economically infeasible. DiJiang addresses this by providing a pathway to convert existing, high-performing quadratic models into linear ones efficiently through a process of "kernelization."

2. Methodology: Frequency Domain Kernelization

DiJiang replaces the standard Softmax attention with a linear dot-product of feature maps: $\text{Attn}(Q, K, V) = \phi(Q)\phi(K)^T V$. The core of the DiJiang approach lies in its "Frequency Domain Kernelization," which consists of three primary innovations:

2.1 Discrete Cosine Transform (DCT) Mapping

Unlike standard linear attention models that use random orthogonal features, DiJiang maps queries and keys to the frequency domain using the Discrete Cosine Transform (DCT). DCT is highly efficient ($O(m \log m)$ complexity) and hardware-friendly. Its "energy compaction" property allows the model to represent the most significant features of the attention map using fewer coefficients, resulting in a more compact and accurate kernel approximation.

2.2 Weighted Quasi-Monte Carlo (WQMC) Sampling

Traditional linear approximations rely on Monte Carlo sampling, which is prone to high variance and approximation errors. DiJiang employs Weighted Quasi-Monte Carlo sampling, using deterministic sequences that cover the feature space more uniformly. This shift improves the theoretical error bound from $O(1/\sqrt{m})$ to $O(1/m)$. Furthermore, learnable weights are integrated to further "tighten" the approximation to the original Softmax distribution.

2.3 Warm-Start Transformation

A critical advantage of DiJiang is its "warm-start" capability. Instead of random initialization, the model parameters are initialized from a pre-trained Transformer. A brief "kernelization" fine-tuning phase is then performed to adapt the model to the linear attention structure, significantly reducing the data requirements for convergence compared to training from scratch.

3. Experimental Evaluation

The researchers evaluated DiJiang across various architectures, including the Pythia suite (70M to 2.8B parameters), OPT-350M, TinyLLaMA-1.1B, and LLaMA2-7B. Training and fine-tuning were conducted using The Pile, an 825 GiB English text corpus.

3.1 Performance and Efficiency

The flagship DiJiang-7B model achieved an average benchmark score of 0.557 (across PIQA, WinoGrande, WSC, ARC, and LogiQA), closely matching the original LLaMA2-7B score of 0.565. Remarkably, this was achieved using only 40 billion tokens of training—a 50x reduction compared to the 2 trillion tokens used for the original LLaMA2.

3.2 Inference and Memory

DiJiang demonstrates significant improvements in inference throughput. On a Pythia-410M base, throughput increased from 203 tokens/s to 787 tokens/s (nearly 4x). Smaller models saw speedups of up to 10x. Additionally, DiJiang maintains a near-constant GPU memory footprint regardless of sequence length, facilitating long-context applications that would otherwise be memory-constrained in vanilla Transformers.

4. Discussion and Conclusion

The success of DiJiang suggests that the primary limitation of previous linear attention models was the noise introduced by sampling methods rather than the linear structure itself. By leveraging DCT and WQMC, DiJiang provides a high-fidelity approximation of the Softmax kernel.

While DiJiang shows immense promise, some limitations remain. The current implementation utilizes a learned gating mechanism for stability, and experiments have primarily focused on 2048-token sequences. Future research will explore the scalability of this kernelization to ultra-long contexts (100k+ tokens) and investigate whether this method can eventually replace training-from-scratch for entirely new architectures.

Summary updated

BibTeX
@inproceedings{chen2024dijiang,
  author = {Hanting Chen and Liuzhicheng and Xutao Wang and Yuchuan Tian and Yunhe Wang},
  title = {{DiJiang: Efficient Large Language Models through Compact Kernelization}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=0uUHfhXdnH},
}
Figure