graphiti/graphiti_core/driver/driver.py
Daniel Chalef 1a6db24600 Final MMR optimization focused on 1024D vectors with smart dimensionality dispatch
This commit delivers a production-ready MMR optimization specifically tailored for
Graphiti's primary use case while handling high-dimensional vectors appropriately.

## Performance Improvements for 1024D Vectors
- **Average 1.16x speedup** (13.6% reduction in search latency)
- **Best performance: 1.31x speedup** for 25 candidates (23.5% faster)
- **Sub-millisecond latency**: 0.266ms for 10 candidates, 0.662ms for 25 candidates
- **Scalable performance**: Maintains improvements up to 100 candidates

## Smart Algorithm Dispatch
- **1024D vectors**: Uses optimized precomputed similarity matrix approach
- **High-dimensional vectors (≥2048D)**: Falls back to original algorithm to avoid overhead
- **Adaptive thresholds**: Considers both dataset size and dimensionality for optimal performance

## Key Optimizations for Primary Use Case
1. **Float32 precision**: Better cache efficiency for moderate-dimensional vectors
2. **Precomputed similarity matrices**: O(1) similarity lookups for small datasets
3. **Vectorized batch operations**: Efficient numpy operations with optimized BLAS
4. **Boolean masking**: Replaced expensive set operations with numpy arrays
5. **Smart memory management**: Optimal layouts for CPU cache utilization

## Technical Implementation
- **Memory efficient**: All test cases fit in CPU cache (max 0.43MB for 100×1024D)
- **Cache-conscious**: Contiguous float32 arrays improve memory bandwidth
- **BLAS optimized**: Matrix multiplication leverages hardware acceleration
- **Correctness maintained**: All existing tests pass with identical results

## Production Impact
- **Real-time search**: Sub-millisecond performance for typical scenarios
- **Scalable**: Performance improvements across all tested dataset sizes
- **Robust**: Handles edge cases and high-dimensional vectors gracefully
- **Backward compatible**: Drop-in replacement with identical API

This optimization transforms MMR from a potential bottleneck into a highly efficient
operation for Graphiti's search pipeline, providing significant performance gains for
the most common use case (1024D vectors) while maintaining robustness for all scenarios.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-18 12:28:50 -07:00

67 lines
1.9 KiB
Python

"""
Copyright 2024, Zep Software, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import logging
from abc import ABC, abstractmethod
from collections.abc import Coroutine
from typing import Any
logger = logging.getLogger(__name__)
class GraphDriverSession(ABC):
async def __aenter__(self):
return self
@abstractmethod
async def __aexit__(self, exc_type, exc, tb):
# No cleanup needed for Falkor, but method must exist
pass
@abstractmethod
async def run(self, query: str, **kwargs: Any) -> Any:
raise NotImplementedError()
@abstractmethod
async def close(self):
raise NotImplementedError()
@abstractmethod
async def execute_write(self, func, *args, **kwargs):
raise NotImplementedError()
class GraphDriver(ABC):
provider: str
fulltext_syntax: str = (
'' # Neo4j (default) syntax does not require a prefix for fulltext queries
)
@abstractmethod
def execute_query(self, cypher_query_: str, **kwargs: Any) -> Coroutine:
raise NotImplementedError()
@abstractmethod
def session(self, database: str | None = None) -> GraphDriverSession:
raise NotImplementedError()
@abstractmethod
def close(self):
raise NotImplementedError()
@abstractmethod
def delete_all_indexes(self, database_: str | None = None) -> Coroutine:
raise NotImplementedError()