LeetDecoding: A PyTorch Library for Exponentially Decaying Causal Linear Attention with CUDA Implementations

Jiaping Wang, Simiao Zhang, Qiao-Chu He, Yifan ChenarXiv 2025 · DOI

Abstract

Standard Transformers face a "quadratic wall" where computational costs scale at $O(N^2)$ with sequence length. LeetDecoding is an open-source PyTorch library that provides high-performance CUDA and Triton implementations for exponentially decaying causal linear attention. By unifying disparate algorithms, it enables scaling large language models (LLMs) to sequence lengths exceeding 100,000 tokens while maintaining linear $O(N)$ complexity.

Introduction

The primary bottleneck in scaling Transformers to long contexts is the softmax attention mechanism. Its quadratic growth in memory and computation makes processing long documents or maintaining long-term state prohibitively expensive. Linear attention offers a theoretical $O(N)$ alternative, but implementing it for causal (autoregressive) models is complex. Existing solutions are often fragmented across research papers or lack the low-level GPU optimizations necessary for production-scale LLMs. LeetDecoding fills this gap by providing a unified, optimized suite of causal linear attention operators.

The LeetDecoding Library

LeetDecoding implements several key algorithms designed to optimize the causal linear attention operation:

  • FleetAttention (FA): A novel method introduced in this work that uses a non-iterative matrix formulation and "cumsum" operations to maximize GPU parallelism.
  • causal-dot-product: A row-by-row computation method optimized with native CUDA kernels.
  • lightningAttention-2: A state-of-the-art "divide and conquer" approach implemented in OpenAI Triton.
  • Baselines: Includes vanilla quadratic attention, block-based, and recursive methods for comparison.

The library is designed for ease of use, offering both native PyTorch implementations and high-performance GPU kernels.

Table 2: Implementation status of various methods in LeetDecoding, showing the availability of PyTorch and optimized CUDA/Triton kernels.

Complexity and Hardware Optimization

A critical contribution of this work is clarifying the complexity of causal linear attention. While some practitioners assume causal masking necessitates $O(N^2)$ complexity, LeetDecoding demonstrates that $O(N)$ is achievable with the right algorithmic approach. Notably, the authors prove that certain recursive methods actually scale at $O(N \log N)$, making them less efficient for extremely long sequences compared to non-recursive $O(N)$ alternatives.

Table 1: Complexity analysis of different causal linear attention methods implemented in the LeetDecoding library.

Beyond theoretical complexity, hardware-aware implementation is vital. Custom CUDA and Triton kernels in the library provide 2x to 20x speedups over standard PyTorch implementations by optimizing memory bandwidth and GPU utilization. The choice of the "best" algorithm depends on the specific use case; for instance, lightningAttention-2 is optimized for batch size 1, whereas causal-dot-product performs better at higher batch sizes.

Empirical Results

Evaluations on benchmarks like LongBench and models such as TransNormerLLM-7B highlight the library's impact:

  • Memory Efficiency: LeetDecoding successfully processed sequences of 100,000 tokens on hardware where standard attention failed due to Out-of-Memory (OOM) errors at 8,192 tokens.
  • Inference Speed: In full-model tests, the Cdotp (causal-dot-product) method achieved a 10% reduction in latency compared to original model implementations.
  • Integration: The library is accessible via pip install leet-decoding, allowing for drop-in replacement of attention layers in existing model architectures.

Conclusion

LeetDecoding provides a robust framework for researchers and practitioners to experiment with and deploy efficient linear attention mechanisms. By combining theoretical rigor with high-performance engineering, it helps bypass the quadratic scaling limits of traditional Transformers. Future work will focus on optimizing training-time performance and expanding integration with the broader HuggingFace ecosystem.

Summary updated

BibTeX
@misc{wang2025leetdecoding,
  author = {Jiaping Wang and Simiao Zhang and Qiao-Chu He and Yifan Chen},
  title = {{LeetDecoding: A PyTorch Library for Exponentially Decaying Causal Linear Attention with CUDA Implementations}},
  year = {2025},
  eprint = {2501.02573},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2501.02573},
}
Figure