[Paper Review] N-Grammer: Augmenting Transformers with latent n-grams
N-Grammer enhances Transformers with learnable, discrete latent n-grams via product quantization of attention head outputs, enabling faster inference without sacrificing performance. It matches larger Transformers and Primer on C4 and SuperGLUE while reducing reliance on expensive dense matrix operations.
Transformer models have recently emerged as one of the foundational models in natural language processing, and as a byproduct, there is significant recent interest and investment in scaling these models. However, the training and inference costs of these large Transformer language models are prohibitive, thus necessitating more research in identifying more efficient variants. In this work, we propose a simple yet effective modification to the Transformer architecture inspired by the literature in statistical language modeling, by augmenting the model with n-grams that are constructed from a discrete latent representation of the text sequence. We evaluate our model, the N-Grammer on language modeling on the C4 data-set as well as text classification on the SuperGLUE data-set, and find that it outperforms several strong baselines such as the Transformer and the Primer. We open-source our model for reproducibility purposes in Jax.
Motivation & Objective
- To reduce the inference cost of large Transformer models without compromising performance.
- To explore efficient architectural modifications inspired by statistical language modeling and discrete latent representations.
- To enable faster inference by replacing dense operations with sparse embedding lookups via learned n-gram structures.
- To evaluate the effectiveness of latent n-grams on standard NLP benchmarks like C4 and SuperGLUE.
Proposed method
- The N-Grammer layer applies product quantization (PQ) to the output of each attention head to create discrete latent codes.
- Bi-grams are formed from consecutive latent codes and mapped to trainable n-gram embeddings via a lookup table.
- The final representation is a residual connection between the original input embedding and the n-gram embedding.
- The model is trained end-to-end with standard objectives, and the n-gram table is cached for inference.
- The architecture uses sparse embedding lookups instead of dense matrix multiplications, reducing computational cost.
- The method supports efficient deployment by caching token-to-cluster mappings, enabling constant-time inference overhead.
Experimental results
Research questions
- RQ1Can latent n-grams learned from discrete representations improve Transformer efficiency without performance degradation?
- RQ2How does the N-Grammer layer compare to larger Transformers and the Primer model in terms of accuracy and inference speed?
- RQ3To what extent do learned latent clusters capture meaningful semantic groupings (e.g., sports, arts, places)?
- RQ4Can sparse embedding lookups replace dense operations in attention mechanisms to reduce inference latency?
- RQ5Does the use of product quantization on attention heads lead to meaningful, semantically coherent n-gram representations?
Key findings
- The N-Grammer model achieves a C4 perplexity of 64.98, outperforming the standard Transformer (66.74) and Primer (62.20).
- On SuperGLUE, the N-Grammer model achieves a 68.27 average score, surpassing the Transformer (65.38) and Primer (63.46).
- The model with 8K latent clusters and 196K n-gram embeddings matches the performance of larger models while being significantly faster at inference.
- Cluster analysis reveals that latent codes group semantically related tokens—e.g., sports, arts, and places—demonstrating meaningful representation learning.
- The N-Grammer layer reduces reliance on expensive dense matrix multiplications by replacing them with fast sparse embedding lookups.
- Caching the token-to-cluster mapping enables constant overhead during inference, making the model highly deployable on hardware favoring sparse operations.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.