graphiti/tests
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
..
cross_encoder Gemini client improvements; Gemini reranker (#645) 2025-06-30 12:55:17 -07:00
driver [REFACTOR][FIX] Move away from DEFAULT_DATABASE environment variable in favour of driver-config support (dc) (#699) 2025-07-10 17:25:39 -04:00
embedder save edge update (#721) 2025-07-14 11:15:38 -04:00
evals add_episode() refactor (#421) 2025-04-30 12:08:52 -04:00
llm_client save edge update (#721) 2025-07-14 11:15:38 -04:00
utils Optimize MMR calculation with vectorized numpy operations 2025-07-18 11:54:15 -07:00
helpers_test.py FalkorDB Integration: Bug Fixes and Unit Tests (#607) 2025-06-30 11:01:44 -04:00
test_entity_exclusion_int.py migrate to pyright (#646) 2025-06-30 12:04:21 -07:00
test_graphiti_falkordb_int.py reformat (#655) 2025-07-01 12:26:15 -04:00
test_graphiti_int.py reformat (#655) 2025-07-01 12:26:15 -04:00
test_node_falkordb_int.py reformat (#655) 2025-07-01 12:26:15 -04:00
test_node_int.py Node episodes list (#381) 2025-04-20 23:20:19 -04:00