Skip to content

Fix sample_weight handling in KernelRidge - #8040

Merged
rockhowse merged 2 commits into
NVIDIA:mainfrom
jcrist:fixup-kernel-ridge-sample-weight
May 1, 2026
Merged

Fix sample_weight handling in KernelRidge#8040
rockhowse merged 2 commits into
NVIDIA:mainfrom
jcrist:fixup-kernel-ridge-sample-weight

Conversation

@jcrist

@jcrist jcrist commented May 1, 2026

Copy link
Copy Markdown
Member

This fixes a bug introduced in the validation port to KernelRidge. This solver needs to special case scalar sample weights; the naive application of check_inputs here resulted in them being treated differently than before.

This PR fixes the validation flow to treat scalar weights as they were before. It also adjusts the hypothesis test to hit this case in an example.

This fixes a bug introduced in the validation port to `KernelRidge`.
This solver needs to special case scalar sample weights; the naive
application of `check_inputs` here resulted in them being treated
differently than before.

This PR fixes the validation flow to treat scalar weights as they were
before. It also adjusts the hypothesis test to hit this case in an
example.
@jcrist jcrist self-assigned this May 1, 2026
@jcrist
jcrist requested a review from a team as a code owner May 1, 2026 04:49
@jcrist
jcrist requested a review from dantegd May 1, 2026 04:49
@jcrist jcrist added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels May 1, 2026
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label May 1, 2026
@coderabbitai

coderabbitai Bot commented May 1, 2026

Copy link
Copy Markdown

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: b3d99446-99b3-4744-bca0-3931d0e8bcc5

📥 Commits

Reviewing files that changed from the base of the PR and between f1a1092 and d72d569.

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

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved sample-weight handling to correctly accept scalar finite weights and normalize/validate non-scalar or non-finite weights.
    • Increased robustness of kernel computations to improve reliability across kernel types.
  • Tests

    • Expanded tests for column-vector targets, scalar sample weights, and target reshaping; updated precision coverage for certain cases.

Walkthrough

KernelRidge.fit now conditionally validates sample_weight (allowing finite scalars unchanged, normalizing others via check_sample_weight and check_consistent_length), and _get_kernel is wrapped with run_in_internal_context. Tests update gradient handling for 1D targets and add/adjust parametrizations.

Changes

Cohort / File(s) Summary
KernelRidge Implementation
python/cuml/cuml/kernel_ridge/kernel_ridge.py
fit: sample_weight is no longer passed through check_inputs; finite scalar weights are allowed unchanged, non-scalar/non-finite weights are normalized with check_sample_weight (using estimator dtype and convert_dtype) and validated with check_consistent_length. _get_kernel decorated with run_in_internal_context. Minor comment removed in _solve_cholesky_kernel.
KernelRidge Tests
python/cuml/tests/test_kernel_ridge.py
gradient_norm now treats 1D y as 2D by reshaping before CuPy conversion and per-target gradient computation. Test parametrization: one dtype changed from np.float32 to np.float64, and an explicit example added for linear kernel with column-vector y and scalar sample_weight. File header year updated.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

Cython / Python, sklearn-api-compat

Suggested reviewers

  • dantegd
  • csadorf
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 'Fix sample_weight handling in KernelRidge' directly and specifically describes the main change—fixing a bug in sample_weight validation introduced during a validation port.
Description check ✅ Passed The description explains the bug context (validation port issue), the specific problem (scalar sample weights handled incorrectly), the fix applied (special-casing scalar weights), and testing improvements, all directly related to the changeset.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Review rate limit: 9/10 reviews remaining, refill in 6 minutes.

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

@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 the current code and only fix it if needed.

Inline comments:
In `@python/cuml/cuml/kernel_ridge/kernel_ridge.py`:
- Around line 307-314: The scalar fast-path for sample_weight bypasses
validation and allows invalid scalars (0, NaN, Inf) into _solve_cholesky_kernel;
change the cp.isscalar(sample_weight) branch in kernel_ridge.fit (around the
sample_weight handling) to validate scalars before skipping array checks: either
call check_sample_weight on a 1-element array-wrapped scalar or perform the same
checks manually (finite, non-negative, non-zero if required) and raise the same
errors as check_sample_weight, so that invalid scalar values are rejected
consistently with the non-scalar path and with scikit-learn behavior.
🪄 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: 93b3bf5d-e721-4ba5-aca5-5d0c0195630b

📥 Commits

Reviewing files that changed from the base of the PR and between 016840c and f1a1092.

📒 Files selected for processing (2)
  • python/cuml/cuml/kernel_ridge/kernel_ridge.py
  • python/cuml/tests/test_kernel_ridge.py

Comment thread python/cuml/cuml/kernel_ridge/kernel_ridge.py
@rockhowse
rockhowse merged commit c840432 into NVIDIA:main May 1, 2026
168 of 174 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Cython / Python Cython or Python issue improvement Improvement / enhancement to an existing function non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants