Skip to content

Make RAPTOR GMM robust on small reduced clusters - #16632

Merged
wangq8 merged 4 commits into
infiniflow:mainfrom
afrozenpeach:fix/raptor-gmm-diag-regcovar
Jul 6, 2026
Merged

wangq8 merged 4 commits into
infiniflow:mainfrom
afrozenpeach:fix/raptor-gmm-diag-regcovar

Conversation

@afrozenpeach

Copy link
Copy Markdown
Contributor

Problem

RAPTOR's GMM clustering — both the BIC sweep in _get_optimal_clusters and
the final assignment fit — uses a full-covariance GaussianMixture. On large
knowledge bases with a higher max_cluster, components end up owning very few
points, 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 GaussianMixture call sites:

  • covariance_type="diag" — the input embeddings are already UMAP-reduced
    to ≤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 that
    owns a singleton or near-identical points can't collapse to zero variance.

Result

The same build that crashed on full covariance at max_cluster=96 completes
cleanly with diag + reg_covar, and lower cluster counts (48, 64) build
without 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 diag is
the appropriate default here rather than a lossy compromise.

…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.
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 4, 2026
@coderabbitai

coderabbitai Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ad11f345-2397-4a6f-a04f-97470a97738a

📥 Commits

Reviewing files that changed from the base of the PR and between 72a823e and f3b103f.

📒 Files selected for processing (2)
  • rag/advanced_rag/knowlege_compile/raptor.py
  • test/unit_test/rag/test_raptor_psi_tree_builder.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • rag/advanced_rag/knowlege_compile/raptor.py

📝 Walkthrough

Walkthrough

This 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.

Changes

RAPTOR GMM Configuration

Layer / File(s) Summary
GMM covariance and test stub update
rag/advanced_rag/knowlege_compile/raptor.py, test/unit_test/rag/test_raptor_psi_tree_builder.py
_get_optimal_clusters and the clustering branch now pass covariance_type="diag" and reg_covar=1e-4 to GaussianMixture, and the test stub accepts extra keyword arguments.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Possibly related PRs

Suggested labels: 🐞 bug

Poem

A rabbit hopped through clusters neat,
With diagonal steps and stable beat,
A tiny reg_covar in place,
Keeps mixtures snug in math’s embrace. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main change: stabilizing RAPTOR GMM clustering on small reduced clusters.
Description check ✅ Passed The PR description is thorough and explains the problem, fix, result, and context needed for reviewers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 win

Update the GaussianMixture mock signature. Both call sites now pass covariance_type="diag" and reg_covar=1e-4, but RecordingGaussianMixture in test/unit_test/rag/test_raptor_psi_tree_builder.py only accepts n_components and random_state, so this test will raise TypeError unless 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 value

Duplicated reg_covar magic value.

reg_covar=1e-4 is 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

📥 Commits

Reviewing files that changed from the base of the PR and between 0fcfb38 and 72a823e.

📒 Files selected for processing (1)
  • rag/advanced_rag/knowlege_compile/raptor.py

@wangq8 wangq8 added the ci Continue Integration label Jul 6, 2026
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (3a247db) to head (9d4fa1e).
⚠️ Report is 9 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wangq8
wangq8 merged commit 8a19c6a into infiniflow:main Jul 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants