graphiti/tests/utils
Daniel Chalef 166c67492a Optimize MMR calculation with vectorized numpy operations
This commit implements a comprehensive optimization of the Maximal Marginal Relevance (MMR)
calculation in the search utilities. The key improvements include:

## Algorithm Improvements
- **True MMR Implementation**: Replaced the previous diversity-aware scoring with proper
  iterative MMR algorithm that greedily selects documents one at a time
- **Vectorized Operations**: Leveraged numpy's optimized BLAS operations through matrix
  multiplication instead of individual dot products
- **Adaptive Strategy**: Uses different optimization strategies for small (≤100) and large
  datasets to balance performance and memory usage

## Performance Optimizations
- **Memory Efficiency**: Reduced memory complexity from O(n²) to O(n) for large datasets
- **BLAS Optimization**: Proper use of matrix multiplication leverages optimized BLAS libraries
- **Batch Normalization**: Added `normalize_embeddings_batch()` for efficient L2 normalization
  of multiple embeddings at once
- **Early Termination**: Stops selection when no candidates meet minimum score threshold

## Key Changes
- `maximal_marginal_relevance()`: Complete rewrite with proper iterative MMR algorithm
- `normalize_embeddings_batch()`: New function for efficient batch normalization
- `_mmr_small_dataset()`: Optimized implementation for small datasets using precomputed
  similarity matrices
- Added comprehensive test suite with 9 test cases covering edge cases, correctness,
  and performance scenarios

## Benefits
- **Correctness**: Now implements true MMR algorithm instead of approximate diversity scoring
- **Memory Usage**: O(n) memory complexity vs O(n²) for the original implementation
- **Scalability**: Better performance characteristics for large datasets
- **Maintainability**: Cleaner, more readable code with comprehensive test coverage

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-18 11:54:15 -07:00
..
maintenance Node dedupe efficiency (#490) 2025-05-15 13:56:33 -04:00
search Optimize MMR calculation with vectorized numpy operations 2025-07-18 11:54:15 -07:00