Skip to content

Cap RAPTOR UMAP n_neighbors to prevent OOM on large datasets - #16627

Merged
yingfeng merged 1 commit into
infiniflow:mainfrom
afrozenpeach:fix/raptor-umap-n-neighbors-oom
Jul 4, 2026
Merged

yingfeng merged 1 commit into
infiniflow:mainfrom
afrozenpeach:fix/raptor-umap-n-neighbors-oom

Conversation

@afrozenpeach

Copy link
Copy Markdown
Contributor

Problem

raptor.py computes n_neighbors = int((len(embeddings) - 1) ** 0.8) and
passes it to umap.UMAP(...). In a dataset-scope RAPTOR build the first
layer's embeddings is the entire KB's chunk set, so this is effectively
unbounded: ~93k chunks → n_neighbors ≈ 9,446.

UMAP's k-NN graph is N × n_neighbors; at these values the raw neighbor
arrays alone are ~14 GB (93k × 9446 × 16 B), and the symmetrized fuzzy
simplicial set + spectral init push peak well past 30 GB. The task
executor is OOM-killed inside fit_transform before any clustering runs —
the log shows "Task has been received" with no "Cluster one layer" line —
after which the unacked task re-queues and OOMs again in a loop.

The line above already flags this: # Degrade too much ??.

Fix

Cap n_neighbors at 100. UMAP's neighborhood size has strongly
diminishing returns well below this (default 15; a few dozen already
captures global structure), so the ceiling preserves — likely improves —
cluster quality while bounding memory to O(N). Mirrors the existing
n_components=min(12, len(embeddings) - 2) clamp two lines down.

​```diff

  •    n_neighbors = int((len(embeddings) - 1) ** 0.8)
    
  •    n_neighbors = min(int((len(embeddings) - 1) ** 0.8), 100)
    

​```

Repro

Dataset-scope RAPTOR over a KB with ~90k+ chunks on a box with <~64 GB
available: executor OOM-killed in the first-layer UMAP fit_transform.
With the cap, first-layer UMAP peaks in low single-digit GB and the build
proceeds to completion.

Scope

Only affects large dataset-scope builds; file-scope RAPTOR already had
n_neighbors well under 100. No behavior change beyond the ceiling.

n_neighbors = (N-1)**0.8 is unbounded; for dataset-scope RAPTOR over a
large KB the first layer sees the full chunk count (e.g. ~93k -> ~9446),
so UMAP's kNN graph alone runs to tens of GB and the task executor is
OOM-killed inside fit_transform before any clustering runs, then
re-queues and loops. Cap at 100 (UMAP's useful range; its own default is
15), matching the existing n_components=min(12, ...) clamp below and the
'Degrade too much ??' note above.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. labels Jul 4, 2026
@coderabbitai

coderabbitai Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The UMAP n_neighbors parameter in the RAPTOR clustering method is now capped at a maximum of 100, replacing the previously uncapped power-based calculation used for dimensionality reduction before clustering.

Changes

RAPTOR Clustering Parameter Cap

Layer / File(s) Summary
Cap n_neighbors value
rag/advanced_rag/knowlege_compile/raptor.py
n_neighbors for UMAP fit_transform is now capped at 100 using min(int((len(embeddings) - 1) ** 0.8), 100) instead of the uncapped expression.

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

Poem

A single line, a tiny hop,
n_neighbors now knows where to stop.
One hundred's the limit, no more, no less,
This bunny approves this small tidy fix! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title accurately summarizes the main change: capping RAPTOR UMAP n_neighbors to avoid OOM on large datasets.
Description check ✅ Passed The description includes the problem, fix, reproduction, and scope, providing the required summary context despite differing headings from the template.

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.

@yingfeng yingfeng added the ci Continue Integration label Jul 4, 2026
@yingfeng
yingfeng marked this pull request as draft July 4, 2026 09:28
@yingfeng
yingfeng marked this pull request as ready for review July 4, 2026 09:28
@yingfeng
yingfeng merged commit 0fcfb38 into infiniflow:main Jul 4, 2026
12 of 15 checks passed
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16627      +/-   ##
==========================================
- Coverage   94.56%   93.16%   -1.40%     
==========================================
  Files          10       10              
  Lines         717      717              
  Branches      118      118              
==========================================
- Hits          678      668      -10     
- Misses         25       29       +4     
- Partials       14       20       +6     

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. 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