Skip to main content

Ultra-fast similarity search with Hilbert curve quantization and MPEG-AI compression

Project description

Hilbert Quantization

PyPI version Python 3.8+ License: MIT Tests

Ultra-fast similarity search with 6x compression and competitive performance

Hilbert Quantization is a high-performance similarity search library that combines Hilbert curve mapping with MPEG-AI compression to deliver both speed and storage efficiency. It's designed for applications where both search performance and storage costs matter.

🆕 New in v1.1.0: Streaming Optimization

  • Memory-Efficient Processing: Constant O(1) memory usage regardless of dataset size
  • Streaming Index Generation: Build hierarchical indices incrementally during processing
  • Integrated Mapping: Single-pass Hilbert curve mapping with index generation
  • Automatic Optimization: Smart approach selection based on dataset characteristics
  • Scalable Architecture: Handle datasets with millions of parameters

🚀 Key Features

  • ⚡ Ultra-fast search: Sub-millisecond to few-millisecond search times
  • 💾 6x compression: Massive storage savings compared to traditional methods
  • 🏆 Competitive performance: Matches industry leaders like Pinecone and FAISS
  • 📈 Scalable: Better performance on larger datasets
  • 🔧 Easy to use: Simple API with sensible defaults
  • 🐍 Pure Python: No external dependencies beyond NumPy

📊 Performance Comparison

Method Search Time Storage Size Compression Use Case
Hilbert Quantization 4.6ms 0.02GB 6x Best overall
Pinecone (Managed) 2.1ms 0.19GB 1x Speed-first
FAISS (GPT-4 style) 4.8ms 0.16GB 1x Accuracy-first
Brute Force 5.9ms 0.14GB 1x Simple baseline

Benchmark on 25K embeddings (1536D, GPT-4 style)

🛠️ Installation

pip install hilbert-quantization

Optional Dependencies

# For benchmarking and visualization
pip install hilbert-quantization[benchmark]

# For GPU acceleration (experimental)
pip install hilbert-quantization[gpu]

# For development
pip install hilbert-quantization[dev]

🚀 Quick Start

Basic Usage

import numpy as np
from hilbert_quantization import HilbertQuantizer

# Initialize quantizer
quantizer = HilbertQuantizer()

# Create some example embeddings
embeddings = [
    np.random.normal(0, 1, 1024).astype(np.float32) 
    for _ in range(10000)
]

# Quantize embeddings (one-time setup)
quantized_models = []
for i, embedding in enumerate(embeddings):
    quantized = quantizer.quantize(embedding, model_id=f"doc_{i}")
    quantized_models.append(quantized)

# Search for similar embeddings
query = np.random.normal(0, 1, 1024).astype(np.float32)
results = quantizer.search(query, quantized_models, max_results=5)

# Print results
for result in results:
    print(f"Model: {result.model.metadata.model_name}")
    print(f"Similarity: {result.similarity_score:.3f}")

🆕 Streaming Optimization (v1.1.0)

For large datasets or memory-constrained environments:

from hilbert_quantization import QuantizationConfig
from hilbert_quantization.core.index_generator import HierarchicalIndexGeneratorImpl
import numpy as np

# Create large dataset
image = np.random.randn(512, 512)  # 262k parameters

# Configure streaming optimization
config = QuantizationConfig(
    use_streaming_optimization=True,    # Enable streaming
    enable_integrated_mapping=True,     # Single-pass processing
    memory_efficient_mode=True          # Optimize for memory
)

# Create generator with streaming enabled
generator = HierarchicalIndexGeneratorImpl(config)

# Generate hierarchical indices with constant memory usage
indices = generator.generate_optimized_indices(image, 1000)

print(f"Processed {image.size:,} parameters with constant memory usage")
print(f"Generated {len(indices)} hierarchical indices")

Cache-Optimized Search (Recommended for Production)

from hilbert_quantization import HilbertQuantizer
from hilbert_quantization.optimized import CacheOptimizedDatabase, CacheOptimizedSearch

# Setup
quantizer = HilbertQuantizer()
search_engine = CacheOptimizedSearch()

# Quantize your embeddings
quantized_models = [quantizer.quantize(emb, f"id_{i}") for i, emb in enumerate(embeddings)]

# Build cache-optimized database (one-time setup)
database = CacheOptimizedDatabase(quantized_models)

# Pre-quantize your query (for multiple searches)
query_quantized = quantizer.quantize(query_embedding, "query")

# Ultra-fast search
results = search_engine.cache_optimized_search(
    query_quantized.hierarchical_indices,
    database,
    max_results=10
)

🎯 Use Cases

✅ Ideal For:

  • Large-scale RAG systems (>100K documents)
  • Edge AI deployments (storage-constrained environments)
  • Cost-optimized cloud applications (minimize storage costs)
  • Archival/backup embedding systems
  • Bandwidth-limited distributed systems

⚠️ Consider Alternatives For:

  • Real-time chat applications (need <1ms latency)
  • Small datasets (<10K embeddings)
  • Latency-critical applications where every millisecond counts

📊 Performance Comparison

Streaming vs Traditional Approach

Dataset Size Traditional Streaming Memory Usage Recommendation
1k params 0.002s 0.003s Standard Traditional
50k params 0.18s 0.23s Standard Traditional
100k params 0.79s 0.98s Constant Streaming
500k params 3.5s 4.4s Constant Streaming
2M+ params Memory Error 19s Constant Streaming

When to Use Streaming Optimization

✅ Use Streaming For:

  • Large datasets (>100k parameters)
  • Memory-constrained environments
  • Processing datasets larger than available RAM
  • Integrated workflows requiring single-pass processing

⚡ Use Traditional For:

  • Small datasets (<50k parameters)
  • Maximum speed requirements
  • Simple two-step processing workflows

📈 Benchmarks

Run the included benchmarks to see performance on your hardware:

# Quick benchmark
hilbert-benchmark --quick

# Full industry comparison
hilbert-benchmark --industry-comparison

# Large-scale test
hilbert-benchmark --large-scale --size 1GB

🔧 Advanced Configuration

from hilbert_quantization import HilbertQuantizer, CompressionConfig

# Custom configuration
config = CompressionConfig(
    quality=0.8,  # Higher quality = better accuracy, larger size
    preserve_index_row=True,  # Preserve important structural information
)

quantizer = HilbertQuantizer(config=config)

# Performance tuning
quantizer.update_configuration(
    similarity_threshold=0.1,  # Lower = more results
    max_results=20,  # Maximum results to return
)

🧪 How It Works

Hilbert Quantization uses a novel approach combining:

  1. Hilbert Curve Mapping: Maps high-dimensional embeddings to 1D space while preserving locality
  2. Hierarchical Indexing: Creates multi-level indices for progressive filtering
  3. MPEG-AI Compression: Applies advanced compression while maintaining searchability
  4. Cache-Optimized Layout: Structure of Arrays (SoA) for optimal memory access patterns

Architecture Overview

Input Embedding (1024D)
        ↓
Hilbert Curve Mapping
        ↓
Hierarchical Index Generation
        ↓
MPEG-AI Compression (6x smaller)
        ↓
Cache-Optimized Storage
        ↓
Progressive Search (Level 0 → Level 1 → Level 2 → Final)
        ↓
Results (4.6ms average)

📚 Documentation

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

git clone https://github.com/yourusername/hilbert-quantization.git
cd hilbert-quantization
pip install -e ".[dev]"
pre-commit install

Running Tests

pytest                    # Run all tests
pytest -m "not slow"     # Skip slow tests
pytest --cov             # Run with coverage

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Inspired by research in Hilbert curves and space-filling curves
  • MPEG-AI compression techniques
  • Performance optimization techniques from the vector database community

📞 Support


Made with ❤️ for the AI/ML community

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

hilbert_quantization-1.1.0.tar.gz (154.0 kB view details)

Uploaded Source

Built Distribution

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

hilbert_quantization-1.1.0-py3-none-any.whl (79.9 kB view details)

Uploaded Python 3

File details

Details for the file hilbert_quantization-1.1.0.tar.gz.

File metadata

  • Download URL: hilbert_quantization-1.1.0.tar.gz
  • Upload date:
  • Size: 154.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for hilbert_quantization-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ae2141cbe7bace8412c5878c48b5998c7ffa18d9cea5cbb689857ddc69bfdc6e
MD5 b07e6aaa658fc16186ddee3bc8839709
BLAKE2b-256 808fba347b2507dc8903a743674a72e8a59d4674a06b234b4d6fd139dac86b68

See more details on using hashes here.

File details

Details for the file hilbert_quantization-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hilbert_quantization-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4fb1017c8a376c0e8e82f4292bcc62ceff05fb07b1eabb8a0887348deb5b9712
MD5 ac92a397d1d020ce8864c55880ea5ba7
BLAKE2b-256 85d8ec5cef621c049657b50e058b3ff9fc694deeb95bb5dcce994debed73c4ff

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