Skip to main content

High-performance RAG vector compression

Project description

VectorZip: Post-Hoc Spectral Vector Compression for RAG Systems

PyPI version License

VectorZip is a high-performance Python library designed to optimize vector database storage and search latency in Retrieval-Augmented Generation (RAG) pipelines. It performs post-hoc spectral dimensionality reduction on high-dimensional vector embeddings using a combined Traveling Salesperson Problem (TSP) dimension reordering and orthonormal Discrete Cosine Transform (DCT-II) projection.

Installation

pip install vectorzip

Quick Start: Drop-in Model Wrapper (Recommended)

VectorZip provides a high-level wrapper, VectorZipModel, that acts as a direct, drop-in replacement for standard SentenceTransformer models. It automates vector compression and calibration transparently:

from vectorzip import VectorZipModel

# 1. Instantiate the model wrapper (e.g., project 1024-dimensional BGE-M3 down to 384 dimensions)
model = VectorZipModel("BAAI/bge-m3", n_components=384)

# 2. Encode sentences directly (automatically calibrates on the first batch and returns 384-dim embeddings)
compressed_embeddings = model.encode([
    "El Banco Central Europeo redujo los tipos de interés.",
    "Un banco de peces tropicales nadaba velozmente."
])

# 3. Retrieve high-dimensional reconstructed embeddings if needed
reconstructed_embeddings = model.encode(
    ["El Banco Central Europeo redujo los tipos de interés."],
    decompress=True
)

Methodology

VectorZip optimizes vector compression through a two-stage spectral alignment:

  1. Dimensional Reordering (TSP): Dimensions are reordered using a discrete Traveling Salesperson Problem solver to minimize adjacent covariance and maximize signal smoothness.
  2. Spectral Projection (DCT-II): The reordered signals are projected using the orthonormal Type-II Discrete Cosine Transform, concentrating semantic information into low-frequency coefficients, which are then truncated to the target dimensionality.

Performance Benchmarks

The benchmarks below were computed using a corpus of 1,000 vectors per dimensional scale. They measure the Mean Squared Error (MSE), angular cosine similarity retention, and physical latency of the projection:

Model Size (Original) Target Dims Compression Ratio MSE Error Cosine Similarity Retention Latency (1000 Vectors)
BGE-Small/MiniLM-L6 (4x Ratio) 96 4.0x 0.000301 99.97% 6.27 ms
BGE-Base/Nomic-v1.5 (4x Ratio) 192 4.0x 0.000310 99.97% 12.32 ms
BGE-Large/BGE-M3 (4x Ratio) 256 4.0x 0.001628 99.84% 8.86 ms
GTE-Qwen2/OpenAI-Small (4x Ratio) 384 4.0x 0.001392 99.87% 12.99 ms
OpenAI-Large/Cohere-v3 (4x Ratio) 768 4.0x 0.000470 99.95% 33.79 ms

[!NOTE] Fidelity Invariant: For all dimensional scales up to a 4.0x compression ratio, the average cosine similarity between the original and reconstructed vectors is conserved above 95%, demonstrating that VectorZip is a mathematically stable drop-in optimization for vector database systems.

Downstream Retrieval Quality Evaluation

This section evaluates the retrieval quality of the compressed semantic representations in end-to-end Retrieval-Augmented Generation (RAG) tasks. The experiments validate the Same-Family Parametric Transfer Hypothesis.

Theoretical Basis: Post-Hoc Projection vs. Low-Capacity Model Training

Deploying lower-dimensional embedding representations (e.g., 384 or 512 dimensions) is crucial for resource-constrained vector database applications. However, native low-dimensional models in a given family (e.g., BGE-Small) are often severely capacity-limited, lacking the parameters required to represent multilingual structures or extended contexts.

By applying VectorZip's post-hoc spectral compression to a high-capacity model of the same family (e.g., BGE-M3 or Qwen2.5-Large), the compressed vector inherits the semantic properties and parametric knowledge of the high-capacity backbone. This approach achieves superior downstream retrieval accuracy compared to native small models, bypassing the necessity of computationally intensive training or fine-tuning of low-capacity architectures.

Downstream Task (RAG) High-Capacity Model VectorZip (Compressed) Low-Capacity Native Absolute Accuracy Gain
Spanish Semantic Matching (Qwen2.5 1536 to 512) 100.00% (Qwen2.5-Large 1536) 100.00% (VectorZip 512) 33.33% (Qwen2.5-Small 512) +66.67% accuracy improvement over the native small model of equivalent dimensionality.
Spanish Multilingual Sovereignty (BGE 1024 to 384) 100.00% (BGE-M3 1024) 100.00% (VectorZip 384) 66.67% (BGE-Small 384) +33.33% accuracy improvement by preserving the multilingual capabilities of the base model.
Long-Context PDF Retrieval (BGE 1024 to 384) 100.00% (BGE-M3 1024) 100.00% (VectorZip 384) 0.00% (BGE-Small 384) +100.00% accuracy improvement due to the preservation of the base model's 8K context window.
MTEB SciFact (BEIR Suite) (BGE 1024 to 384) 73.46% (BGE-Large 1024) 71.45% (VectorZip 384) 72.00% (BGE-Small 384) 97.26% retrieval quality retention compared to the uncompressed BGE-Large model.

[!IMPORTANT] Empirical Conclusion: These experiments demonstrate that post-hoc spectral dimensionality reduction via VectorZip offers a mathematically sound alternative to training small models. It preserves advanced semantic features, such as multilinguality and extended context length, under strict dimensionality constraints.

API Reference

Model Wrapper: VectorZipModel (Recommended)

The VectorZipModel acts as a drop-in replacement wrapper for SentenceTransformer models, automating the compression pipeline transparently during inference.

from vectorzip import VectorZipModel

Constructor

VectorZipModel(model_name_or_path: str, n_components: int)

encode

encode(sentences: List[str], decompress: bool = False, **kwargs) -> np.ndarray
  • Encodes input text. On the initial batch, it lazily runs fit to compute the dimensional reordering permutation. Subsequent calls utilize this static permutation.
  • decompress (bool): If True, automatically reconstructs and returns original high-dimensional vectors. If False (default), returns the compressed low-dimensional vectors.
  • Returns: np.ndarray containing the processed vectors.

Estimator Class: VectorZip

The VectorZip class implements the standard Scikit-learn estimator interface. This is the recommended class for production systems where calibration must be kept static between training and inference.

from vectorzip import VectorZip

Constructor

VectorZip(n_components: int)

fit

fit(X: np.ndarray) -> "VectorZip"
  • Computes the adjacent covariance matrix over a representative training corpus X and solves the TSP dimensional reordering problem.

transform

transform(X: np.ndarray) -> np.ndarray
  • Reorders the dimensions of X according to the learned TSP permutation and projects the vectors into the lower-dimensional space using the orthonormal DCT-II.

inverse_transform

inverse_transform(X_compressed: np.ndarray) -> np.ndarray
  • Projects compressed embeddings back to the original space via the Inverse Discrete Cosine Transform (IDCT-II) and restores the original dimension ordering.

Module-Level Functions

For straightforward compression tasks where offline calibration is not required, VectorZip provides simple functional wrappers.

compress

vectorzip.compress(X: np.ndarray, n_components: int) -> np.ndarray
  • X (np.ndarray of shape (M, N)): A 2D array of input embeddings.
  • n_components (int): Target dimensionality (must satisfy 1 <= n_components <= N).
  • Returns: np.ndarray of shape (M, n_components) representing the compressed embeddings.

decompress

vectorzip.decompress(X_compressed: np.ndarray) -> np.ndarray
  • X_compressed (np.ndarray of shape (M, K)): Compressed representations.
  • Returns: np.ndarray of shape (M, N) containing the reconstructed embeddings.

Production Integration Guide

In production systems, it is critical to keep the dimensional reordering permutation identical across the entire vector database index (corpus) and search queries.

Calibration and Index Generation (Offline Phase)

Before deploying to production, calibrate the compressor on a representative corpus of your database:

import numpy as np
from sentence_transformers import SentenceTransformer
from vectorzip import VectorZip

# 1. Generate standard high-dimensional embeddings
model = SentenceTransformer("BAAI/bge-m3")
corpus_texts = ["Ejemplo de documento 1", "Ejemplo de documento 2"]
corpus_embeddings = model.encode(corpus_texts) # Shape: (M, 1024)

# 2. Calibrate VectorZip
compressor = VectorZip(n_components=384)
compressor.fit(corpus_embeddings)

# 3. Compress the entire corpus
compressed_corpus = compressor.transform(corpus_embeddings) # Shape: (M, 384)

# 4. Serialize the compressor configuration for inference
import joblib
joblib.dump(compressor, "vectorzip_calibrated.joblib")

Query Execution (Online Phase)

During runtime, load the serialized compressor and apply the pre-computed permutation to queries:

import joblib
from sentence_transformers import SentenceTransformer

# 1. Load standard model and serialized compressor
model = SentenceTransformer("BAAI/bge-m3")
compressor = joblib.load("vectorzip_calibrated.joblib")

# 2. Process search query
query_text = "Buscar herramienta mecanica"
query_emb = model.encode([query_text]) # Shape: (1, 1024)

# 3. Apply the identical static projection
compressed_query = compressor.transform(query_emb) # Shape: (1, 384)

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

vectorzip-0.0.5.tar.gz (177.6 kB view details)

Uploaded Source

Built Distribution

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

vectorzip-0.0.5-py3-none-any.whl (174.7 kB view details)

Uploaded Python 3

File details

Details for the file vectorzip-0.0.5.tar.gz.

File metadata

  • Download URL: vectorzip-0.0.5.tar.gz
  • Upload date:
  • Size: 177.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for vectorzip-0.0.5.tar.gz
Algorithm Hash digest
SHA256 667603a07b399512b219973a15aa67c50418977cdd046e82c5ccb0f12b082b36
MD5 68ddaad6a648a3de2a1ffaa8500cb12c
BLAKE2b-256 4df8798f7cebfc531f65de6d97846bf5727363e68b3c46653fbcd4e5058f48be

See more details on using hashes here.

File details

Details for the file vectorzip-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: vectorzip-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 174.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for vectorzip-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a461c9e6ec3ef885f3ae8d5fb8a78bbaa8010d16937564f94aac7f4e72b9e0e6
MD5 abda3de3a78208f81ff3156010325199
BLAKE2b-256 004127ae6dfb1fc046d1f6d85439348c2a3b059b53a70891952d2a633e773bab

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