Skip to main content

HNSW based output embeddings for LLM's

Project description

Accelerating LLM Inference via Vector Index Based Output Embeddings

Paper

Arxiv Link (online soon)

Abstract

Large output embedding matrices create a significant memory bandwidth bottleneck during autoregressive decoding, especially for compact LLMs with large multilingual vocabularies. We reformulate the output projection followed by top-k token selection as a maximum inner product search over token embeddings and replace the dense vocabulary projection with an HNSW-based vector index. The resulting output head retrieves only a small candidate set of high-scoring tokens and can be integrated into existing decoding pipelines by scattering retrieved logits into a sparse full-vocabulary tensor. On CPU inference with Gemma 3, Llama 3.2, and Qwen 3 models, our method substantially accelerates the output projection and improves end-to-end batch-size-one decoding throughput by up to 82% for Gemma 3 270M, while preserving generation quality under AlpacaEval evaluation. These results suggest approximate retrieval is a practical alternative to dense output projections in latency-sensitive small-batch decoding.

Getting Started

For a full walkthrough, check out the quickstart notebook.

Using the vector index embedding is straightforward and integrates seamlessly with Hugging Face transformers by simply replacing the model's standard lm_head.

For immediate use, we have provided prebuilt vector indices for the following models:

  • google/gemma-3-270m-it / google/gemma-3-1b-it
  • Qwen/Qwen3-0.6B / Qwen/Qwen3-1.7B
  • meta-llama/Llama-3.2-1B-Instruct /meta-llama/Llama-3.2-3B-Instruct

Install

pip install vector-index-embedding transformers

Run

from transformers import pipeline
from vectorindex import VectorIndexEmbedding

model_id = "Qwen/Qwen3-0.6B"
pipe = pipeline("text-generation", model=model_id, device="cpu")
pipe.model.lm_head = VectorIndexEmbedding.from_pretrained(model_id, ef=200)

prompt = [{"role": "user", "content": "Who was Alan Turing?"}]
out = pipe(prompt)
print(out[0]["generated_text"][-1]["content"])

Faster hnswlib

Our faster hnswlib implementation can be found here: github.com/martinloretzzz/hnswlib

Warning: This implementation might not work on all systems as it was only tested on the one where we're running the benchmarks and the SIMD implementation was only adapted for that architecture.

This fork has 2 improvements for fast inner products on high dimensional data:

  • We calculate all the inner products in parallel, that way reducing memory accesses in half (we load one element of the query and compare it to N other vectors at the same time)
  • We removed a heuristic that restricted multi-threading, as our data is extremely high dimensional and always benefits from using all cores.

Citation

If you use this work, please cite:

TBD

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vector_index_embedding-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vector_index_embedding-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file vector_index_embedding-0.1.1.tar.gz.

File metadata

  • Download URL: vector_index_embedding-0.1.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for vector_index_embedding-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5a1c3be51d892cc7c7c96332fbe86476350d7ee95ba2db7976d4b151cb033b58
MD5 5c3755b3fafd6e3f30d30c9ef328277e
BLAKE2b-256 2ed1709fa19de6b5fbcdc83d617a2a337576558be8d8c6cf3e649017d4eec299

See more details on using hashes here.

File details

Details for the file vector_index_embedding-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for vector_index_embedding-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9aabf397d87ae484871622310a7c107616600af12fd3d03ffbd4cfa1719b2402
MD5 48ed3c6dce1ba4cdd8145a73e30a2294
BLAKE2b-256 2c814b93695857697faaa95229b5f0eeceddbb5ee96a721cb49e8ba025b4d3e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page