Vector Library
A high-performance vector operations library providing distance metrics, data types, and utilities for vector computations in Rust.
Overview
The Vector library provides essential functionality for vector operations, including distance metrics, data types, and utilities used by the DiskANN project. It's designed for high-performance vector computations with support for different numeric types and distance functions.
Features
- Multiple distance metrics - L2, cosine, inner product, and more
- Flexible data types - Support for f32, f16, and other numeric types
- High performance - Optimized vector operations
- Type safety - Strong typing for vector dimensions
- SIMD support - Vectorized operations where available
- Cross-platform - Works on multiple architectures
Quick Start
use ;
// Define a vector type with specific dimension
type Vector128 = ;
// Create vectors
let v1: Vector128 = ;
let v2: Vector128 = ;
// Calculate distance using different metrics
let l2_distance = v1.l2_distance;
let cosine_distance = v1.cosine_distance;
let inner_product = v1.inner_product;
println!;
println!;
println!;
Distance Metrics
L2 Distance (Euclidean)
use Metric;
let v1 = ;
let v2 = ;
let distance = v1.l2_distance;
// Calculates: sqrt((4-1)² + (5-2)² + (6-3)²)
Cosine Distance
let v1 = ;
let v2 = ;
let distance = v1.cosine_distance;
// Calculates: 1 - (v1·v2) / (||v1|| * ||v2||)
Inner Product
let v1 = ;
let v2 = ;
let similarity = v1.inner_product;
// Calculates: v1·v2 = Σ(v1[i] * v2[i])
Data Types
Supported Types
- f32 - 32-bit floating point (most common)
- f16 - 16-bit floating point (memory efficient)
- f64 - 64-bit floating point (high precision)
Type Conversion
use Half;
// Convert between types
let f32_vector: = ;
let f16_vector: = f32_vector.map;
// Convert back
let back_to_f32: = f16_vector.map;
Dimension Support
The library supports fixed-size arrays for different dimensions:
// Common dimensions
type Vector64 = ;
type Vector128 = ;
type Vector256 = ;
type Vector512 = ;
// Custom dimensions
type CustomVector = ;
Performance Optimizations
SIMD Operations
The library automatically uses SIMD instructions when available:
// These operations are automatically vectorized
let v1: = ;
let v2: = ;
let distance = v1.l2_distance; // Uses SIMD if available
Memory Alignment
For optimal performance, ensure vectors are properly aligned:
use ;
// Allocate aligned memory
let layout = from_size_align.unwrap;
let ptr = unsafe ;
Advanced Usage
Custom Distance Metrics
use ;
// Implement custom distance for your type
Batch Operations
use *;
let vectors: = vec!;
let query: = ;
// Parallel distance calculation
let distances: = vectors
.par_iter
.map
.collect;
Integration with DiskANN
The Vector library is designed to work seamlessly with DiskANN:
use ;
use FullPrecisionDistance;
// Create index with vector types
let mut index = new
.with_dimension
.with_metric
.?;
// Insert vectors
let vectors: = vec!;
index.insert_batch?;
Benchmarks
Performance comparison of different distance metrics (Intel i7-8700K):
| Metric | 128-dim | 256-dim | 512-dim | 1024-dim |
|---|---|---|---|---|
| L2 | 0.8μs | 1.2μs | 2.1μs | 4.3μs |
| Cosine | 1.1μs | 1.8μs | 3.2μs | 6.1μs |
| Inner Product | 0.6μs | 1.0μs | 1.8μs | 3.5μs |
Times are per vector pair comparison
Development
Building
Testing
Benchmarks
API Reference
Core Traits
FullPrecisionDistance<T, DIM>- Distance calculation traitMetric- Distance metric enumerationHalf- 16-bit floating point type
Main Functions
l2_distance()- Calculate L2 distancecosine_distance()- Calculate cosine distanceinner_product()- Calculate inner productnormalize()- Normalize vector to unit length
Utility Functions
round_up()- Round up to nearest multipleis_floating_point()- Check if type is floating pointget_distance_function()- Get distance function for metric
Dependencies
- rayon - Parallel processing
- half - 16-bit floating point support
- bytemuck - Memory operations
- serde - Serialization (optional)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
We welcome contributions! Please see the main README for contribution guidelines.