Rediscovering Hashed Random Projections for Efficient Quantization of Contextualized Sentence Embeddings

Ulf A. Hamster, Ji-Ung Lee, Alexander Geyken, Iryna GurevycharXiv 2023

Abstract

Training and inference on edge devices often requires an efficient setup due to computational limitations. While pre-computing data representations and caching them on a server can mitigate extensive edge device computation, this leads to two challenges. First, the amount of storage required on the server that scales linearly with the number of instances. Second, the bandwidth required to send extensively large amounts of data to an edge device. To reduce the memory footprint of pre-computed data representations, we propose a simple, yet effective approach that uses randomly initialized hyperplane projections. To further reduce their size by up to 98.96%, we quantize the resulting floating-point representations into binary vectors. Despite the greatly reduced size, we show that the embeddings remain effective for training models across various English and German sentence classification tasks that retain 94%--99% of their floating-point.

Introduction

Modern Natural Language Processing (NLP) relies heavily on Large Language Models (LLMs) like BERT and GPT, which generate "contextualized embeddings"—dense mathematical representations of text. While these embeddings are powerful, their high dimensionality (often 768 or more) creates a significant bottleneck for edge computing. Storing these representations on a server and transmitting them to mobile or IoT devices consumes substantial storage and network bandwidth.

Existing compression methods often require expensive re-training or focus on static word lookups. This paper explores a "rediscovered" technique: Hashed Random Projections (HRP). By projecting dynamic, per-sentence embeddings into a lower-dimensional space and binarizing them, the authors achieve massive compression with minimal loss in semantic utility.

Methodology: Hashed Random Projections

The proposed approach is elegant in its simplicity and requires zero training. It leverages the mathematical properties of random projections to preserve the relative distances between data points in a high-dimensional space.

The HRP-based quantization pipeline, showing the transformation from input sentence to binary vector.

As illustrated in the pipeline above, the process consists of two primary steps:

  1. Random Projection: A high-dimensional sentence embedding ($x_{SBert} \in \mathbb{R}^{d_s}$) is multiplied by a fixed, randomly initialized matrix ($W_{hrp} \in \mathbb{R}^{d_s \times d_t}$) drawn from a normal distribution. This projects the data into a target dimension $d_t$.
  2. Binarization: A Heaviside step function is applied to the resulting vector. If a value is $\ge 0$, it becomes a 1; otherwise, it becomes a 0.

The final output is a compact bitstring. Because the projection matrix is fixed and random, the system can be deployed instantly without a heavy optimization phase for every new model version.

Experimental Setup

The researchers stress-tested this method across a wide variety of multilingual tasks and models:

  • English Classification (SentEval): Including sentiment analysis (MR, SST5), paraphrase detection (MRPC), and question classification (TREC).
  • Semantic Textual Similarity (STS): Benchmarks from 2012 through 2016 to evaluate meaning preservation.
  • German Classification: A set of four tasks including sentiment and dialect classification.
  • Models: Six embedding models were tested, including SBert (MPNet, MiniLM, DistilUSE), LaBSE, m-USE, and LASER.

Key Results

The experiments revealed that sentence embeddings are remarkably redundant. The HRP method reduced memory footprints by up to 98.96% while retaining 94%–99% of the performance of the original 32-bit floating-point embeddings.

Accuracy vs. memory consumption for English classification tasks, illustrating the trade-off between compression and performance.

Performance scales predictably with the target dimension. While 256 bits are sufficient for many tasks, increasing the dimension to 2048 bits almost entirely closes the performance gap with the uncompressed model. For instance, with the SBert MPNet v2 model, shrinking the representation to just 256 bits (roughly 1% of its original size) resulted in only a minor drop in Spearman’s correlation for similarity tasks.

Conclusion

Hashed Random Projections offer a promising and simple tool for the NLP community. By enabling massive compression without the need for learned quantization, HRP facilitates the deployment of state-of-the-art models on resource-constrained edge hardware, reducing both the environmental and financial costs of data transmission and storage.

Summary updated

BibTeX
@misc{hamster2023rediscovering,
  author = {Ulf A. Hamster and Ji-Ung Lee and Alexander Geyken and Iryna Gurevych},
  title = {{Rediscovering Hashed Random Projections for Efficient Quantization of Contextualized Sentence Embeddings}},
  year = {2023},
  eprint = {2304.02481},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2304.02481},
}
Figure