Transformer tricks: Precomputing the first layer

Nils Graef
2/20/2024
cs.LG

Official code: OpenMachine-ai/transformer-tricks (218 stars, MIT, last commit 2026-05-02).

Abstract

This micro-paper describes a trick to speed up inference of transformers with RoPE (such as LLaMA, Mistral, PaLM, and Gemma). For these models, a large portion of the first transformer layer can be precomputed, which results in slightly lower latency and lower cost-per-token. Because this trick optimizes only one layer, the relative savings depend on the total number of layers. For example, the maximum savings for a model with only 4 layers (such as Whisper tiny) is limited to 25%, while a 32-layer model is limited to 3% savings. See https://github.com/OpenMachine-ai/transformer-tricks for code and more transformer tricks.

View on arXivView PDF

Code Implementations(1)

MIT

A collection of tricks and tools to speed up transformer models

21814TeX, ShellMar 11, 20245 months agoMIT
aiarxivarxiv-papersllmllm-inference+6 more

Cite this paper

@article{graef2024transformer,
  title  = {Transformer tricks: Precomputing the first layer},
  author = {Nils Graef},
  year   = {2024},
  eprint = {2402.13388},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2402.13388v3}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2402.13388.svg)](https://codeofpaper.com/paper/2402.13388)

Discussion