Skip to content

Fix KNN neighbor buffer layout bugs - #8393

Open
nethum529 wants to merge 7 commits into
NVIDIA:mainfrom
nethum529:bug-issue-8362-neighbors-fixes
Open

Fix KNN neighbor buffer layout bugs#8393
nethum529 wants to merge 7 commits into
NVIDIA:mainfrom
nethum529:bug-issue-8362-neighbors-fixes

Conversation

@nethum529

Copy link
Copy Markdown
Contributor

Summary

  • Copy callable weights into aligned C-contiguous CuPy storage before the KNN kernel reads them.
  • Convert sparse self-edge indices to contiguous int64 before the raw kernel while keeping explicit sparse query results as int32.
  • Add regressions for strided weights, unaligned weights, and duplicate sparse rows.

Testing

  • 14 passed for the KNN weights test selection.
  • Compute Sanitizer reports ERROR SUMMARY: 0 errors for the unaligned callable path.
  • Changed-file pre-commit checks pass.

Fixes #8362

Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
@nethum529
nethum529 requested a review from a team as a code owner July 18, 2026 04:22
@nethum529
nethum529 requested a review from csadorf July 18, 2026 04:22
@copy-pr-bot

copy-pr-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Cython / Python Cython or Python issue label Jul 18, 2026
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 7a356767-15b0-4c4d-8174-5d525c0183ce

📥 Commits

Reviewing files that changed from the base of the PR and between 6607a3e and 565dcf3.

📒 Files selected for processing (1)
  • python/cuml/cuml/neighbors/nearest_neighbors.pyx
🚧 Files skipped from review as they are similar to previous changes (1)
  • python/cuml/cuml/neighbors/nearest_neighbors.pyx

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved KNN regressor reliability with custom callable weights by ensuring returned GPU weights are consistently float32 and C-order contiguous.
    • Fixed sparse nearest-neighbor self-query behavior by ensuring indices are correctly formatted, excluding self-edges, and returning consistent distance and index data.
  • Tests

    • Added coverage for callable weight functions returning non-contiguous GPU arrays, verifying predictions match scikit-learn.
    • Added a sparse nearest-neighbor regression test validating self-edge exclusion and result equivalence to scikit-learn.

Walkthrough

The changes normalize callable KNN weights to C-contiguous float32 arrays and normalize sparse self-edge indices to contiguous int64 arrays. New regression tests cover non-contiguous callable weights and sparse self-query behavior.

Changes

Neighbors layout fixes

Layer / File(s) Summary
Align callable weights
python/cuml/cuml/neighbors/weights.py, python/cuml/tests/test_kneighbors_regressor.py
Callable weight results are converted to C-contiguous float32 storage, with coverage for non-contiguous CuPy outputs.
Normalize sparse self-edge indices
python/cuml/cuml/neighbors/nearest_neighbors.pyx, python/cuml/tests/test_nearest_neighbors.py
Self-edge removal converts indices to contiguous int64 storage, with sparse self-query coverage against scikit-learn results.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Suggested reviewers: jcrist

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary KNN buffer layout fixes.
Description check ✅ Passed The description accurately covers the KNN buffer fixes, regression tests, testing results, and linked issue.
Linked Issues check ✅ Passed The changes fix callable-weight contiguity and sparse self-edge index-width issues and add relevant regression tests for issue [#8362].
Out of Scope Changes check ✅ Passed The code and regression tests stay within the linked issue scope; copyright header updates are incidental metadata changes.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@viclafargue viclafargue 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.

LGTM for the most part, but I have one comment.

Comment thread python/cuml/cuml/neighbors/weights.py Outdated
Comment thread python/cuml/cuml/neighbors/weights.py Outdated
@csadorf
csadorf removed their request for review July 20, 2026 22:56
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

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

Inline comments:
In `@python/cuml/cuml/neighbors/weights.py`:
- Around line 59-61: Update the callable-weights conversion in weights.py to use
an unconditional allocating CuPy conversion, such as cp.array with copy=True,
while preserving float32 dtype and C order so the resulting pointer is aligned
for kneighbors_regressor.pyx. Add coverage using an unaligned C-contiguous
device-array result from the callable and verify the conversion produces a safe
aligned allocation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: e206c94b-e742-476f-ad69-1d3114bec701

📥 Commits

Reviewing files that changed from the base of the PR and between ee12f2c and 0d98baa.

📒 Files selected for processing (2)
  • python/cuml/cuml/neighbors/weights.py
  • python/cuml/tests/test_kneighbors_regressor.py
💤 Files with no reviewable changes (1)
  • python/cuml/tests/test_kneighbors_regressor.py

Comment thread python/cuml/cuml/neighbors/weights.py
@nethum529

Copy link
Copy Markdown
Contributor Author

Pushed the simplification and merged latest upstream main. The whole alignment-handling block is replaced by the one-liner as suggested, and the regression tests pass unchanged since they only assert behavior.

@viclafargue viclafargue 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.

Thanks! Please update the PR description to remove the claim that unaligned weights are handled. Otherwise, this looks good to me.

It looks like cuPy can successfully convert and reorder unaligned memory. However, if the type and orders are correct the unaligned pointer is preserved throughout.

@nethum529

Copy link
Copy Markdown
Contributor Author

Will do & Happy to help :)

@viclafargue

Copy link
Copy Markdown
Contributor

/ok to test d1c8ef1

@viclafargue

Copy link
Copy Markdown
Contributor

/ok to test 6607a3e

@viclafargue viclafargue added bug Something isn't working non-breaking Non-breaking change labels Jul 31, 2026
@viclafargue
viclafargue requested a review from jcrist August 6, 2026 09:57
@viclafargue

Copy link
Copy Markdown
Contributor

/ok to test 565dcf3

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

Labels

bug Something isn't working Cython / Python Cython or Python issue non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A few small cuml.neighbors bugs

5 participants