Repository navigation
Make RAPTOR GMM robust on small reduced clusters - #16632
Conversation
…eg_covar) Full-covariance GaussianMixture fails with 'ill-defined empirical covariance' when RAPTOR clusters small groups of UMAP-reduced points at higher max_cluster values -- components collapse onto singleton/near- identical samples. Use covariance_type='diag' (embeddings are already UMAP-reduced to <=12 decorrelated dims, so off-diagonal terms add little) and reg_covar=1e-4 to floor the variance. Applies to both the BIC sweep and the final assignment fit.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR updates RAPTOR’s GaussianMixture setup to use diagonal covariance with regularization in both cluster selection and clustering, and adjusts the related unit test stub to accept the expanded constructor arguments. ChangesRAPTOR GMM Configuration
Estimated code review effort: 1 (Trivial) | ~3 minutes Possibly related PRs
Suggested labels: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
rag/advanced_rag/knowlege_compile/raptor.py (1)
240-259: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winUpdate the GaussianMixture mock signature. Both call sites now pass
covariance_type="diag"andreg_covar=1e-4, butRecordingGaussianMixtureintest/unit_test/rag/test_raptor_psi_tree_builder.pyonly acceptsn_componentsandrandom_state, so this test will raiseTypeErrorunless the stub accepts the extra kwargs.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@rag/advanced_rag/knowlege_compile/raptor.py` around lines 240 - 259, The GaussianMixture stub used by the RAPTOR tests no longer matches the production call in _get_optimal_clusters, because GaussianMixture is now instantiated with covariance_type and reg_covar. Update RecordingGaussianMixture in test/unit_test/rag/test_raptor_psi_tree_builder.py to accept and ignore the extra keyword arguments, so the tests continue to work with the new GaussianMixture call signature.
🧹 Nitpick comments (1)
rag/advanced_rag/knowlege_compile/raptor.py (1)
255-255: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueDuplicated
reg_covarmagic value.
reg_covar=1e-4is hardcoded identically at both call sites. Consider a module-level constant (e.g.,_GMM_REG_COVAR = 1e-4) to keep the two instantiations in sync if this value is tuned later.Also applies to: 345-345
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@rag/advanced_rag/knowlege_compile/raptor.py` at line 255, The GaussianMixture setup uses the same hardcoded reg_covar value in both call sites, so pull that magic number into a single module-level constant and reuse it from the relevant GaussianMixture instantiations in raptor.py. Update the code around the GaussianMixture construction in the clustering logic so both locations reference the shared constant (for example, a private _GMM_REG_COVAR), keeping the value synchronized if it changes later.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@rag/advanced_rag/knowlege_compile/raptor.py`:
- Around line 240-259: The GaussianMixture stub used by the RAPTOR tests no
longer matches the production call in _get_optimal_clusters, because
GaussianMixture is now instantiated with covariance_type and reg_covar. Update
RecordingGaussianMixture in test/unit_test/rag/test_raptor_psi_tree_builder.py
to accept and ignore the extra keyword arguments, so the tests continue to work
with the new GaussianMixture call signature.
---
Nitpick comments:
In `@rag/advanced_rag/knowlege_compile/raptor.py`:
- Line 255: The GaussianMixture setup uses the same hardcoded reg_covar value in
both call sites, so pull that magic number into a single module-level constant
and reuse it from the relevant GaussianMixture instantiations in raptor.py.
Update the code around the GaussianMixture construction in the clustering logic
so both locations reference the shared constant (for example, a private
_GMM_REG_COVAR), keeping the value synchronized if it changes later.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: db39412c-f17f-4836-b495-af1460406f36
📒 Files selected for processing (1)
rag/advanced_rag/knowlege_compile/raptor.py
…tra GMM kwargs in test stub
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16632 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Problem
RAPTOR's GMM clustering — both the BIC sweep in
_get_optimal_clustersandthe final assignment fit — uses a full-covariance
GaussianMixture. On largeknowledge bases with a higher
max_cluster, components end up owning very fewpoints, and the fit raises:
ValueError: Fitting the mixture model failed because some components have
ill-defined empirical covariance (for instance caused by singleton or
collapsed samples).
This aborts the entire RAPTOR task. Reproduced on a ~97k-chunk dataset-scope
build at
max_cluster=96: the first-layer clustering fails outright.Fix
Two changes, applied to both
GaussianMixturecall sites:covariance_type="diag"— the input embeddings are already UMAP-reducedto ≤12 dimensions, where the axes are largely decorrelated by construction,
so a diagonal covariance loses little modeling power while being far more
numerically stable (and cheaper to fit).
reg_covar=1e-4— floors the covariance diagonal so a component thatowns a singleton or near-identical points can't collapse to zero variance.
Result
The same build that crashed on full covariance at
max_cluster=96completescleanly with
diag+reg_covar, and lower cluster counts (48, 64) buildwithout the convergence-failure flood.
Notes
Full covariance would only add value if the reduced dimensions carried
significant off-diagonal correlation; after UMAP that's minimal, so
diagisthe appropriate default here rather than a lossy compromise.