Skip to content

Apply new validation to metrics.pairwise_kernels - #8050

Merged
rapids-bot[bot] merged 1 commit into
NVIDIA:mainfrom
csadorf:issue-7998-apply-new-validation-to-metrics-pairwise-kernels
May 6, 2026
Merged

Apply new validation to metrics.pairwise_kernels#8050
rapids-bot[bot] merged 1 commit into
NVIDIA:mainfrom
csadorf:issue-7998-apply-new-validation-to-metrics-pairwise-kernels

Conversation

@csadorf

@csadorf csadorf commented May 5, 2026

Copy link
Copy Markdown
Contributor

Apply new input validation to cuml.metrics.pairwise_kernels.

Part of #7998

Replace the legacy input_to_cupy_array calls in cuml.metrics.pairwise_kernels
with check_array from cuml.internals.validation, matching the pattern
established by the regression, _classification, _ranking, confusion_matrix,
and hinge_loss migrations (xref NVIDIA#7998).

Matches sklearn behavior: non-finite inputs (NaN, inf) now raise ValueError,
consistent with sklearn.metrics.pairwise.pairwise_kernels defaults.
@csadorf
csadorf requested a review from a team as a code owner May 5, 2026 21:11
@csadorf
csadorf requested a review from betatim May 5, 2026 21:11
@csadorf csadorf added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels May 5, 2026
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label May 5, 2026
@coderabbitai

coderabbitai Bot commented May 5, 2026

Copy link
Copy Markdown
📝 Walkthrough

Summary by CodeRabbit

  • Chores

    • Updated copyright year.
  • Refactor

    • Enhanced input validation handling for kernel metric functions. Full backward compatibility maintained with no API changes.

Walkthrough

The pairwise_kernels.py file updates its copyright year to 2026 and replaces the input_to_cupy_array import with check_array from cuml.internals.validation. The pairwise_kernels function refactors input validation to use check_array for both X and Y parameters, with Y set to X when not provided. No public API signatures or core kernel logic changed.

Changes

Input Validation Refactoring

Layer / File(s) Summary
Imports
python/cuml/cuml/metrics/pairwise_kernels.py
Import changed from input_to_cupy_array to check_array from cuml.internals.validation.
Core Implementation
python/cuml/cuml/metrics/pairwise_kernels.py
pairwise_kernels function refactored: X is validated via check_array; Y is validated if provided, otherwise set to X. Previous input_to_cupy_array calls replaced.
Metadata
python/cuml/cuml/metrics/pairwise_kernels.py
Copyright year updated in file header from 2022-2025 to 2022-2026.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Apply new validation to metrics.pairwise_kernels' directly and specifically describes the main change in the PR, which is applying new input validation to the pairwise_kernels function.
Description check ✅ Passed The description 'Apply new input validation to cuml.metrics.pairwise_kernels. Part of #7998' is directly related to the changeset, which replaces legacy input_to_cupy_array with check_array validation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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

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 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/metrics/pairwise_kernels.py`:
- Around line 278-283: The validation path drops the previous dtype coercion so
Y is no longer converted to X.dtype and the convert_dtype behavior is ignored;
when validating Y inside pairwise_kernels, call check_array with the dtype
matching X (e.g., check_array(Y, input_name="Y", dtype=X.dtype)) or pass the
existing convert_dtype flag into check_array so Y is coerced to X.dtype,
ensuring X and Y keep the same dtype and preserving the original mixed-dtype
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: dd6e5f51-3ddc-4556-88d9-e9c28681600d

📥 Commits

Reviewing files that changed from the base of the PR and between f39dde1 and 4e86dd3.

📒 Files selected for processing (1)
  • python/cuml/cuml/metrics/pairwise_kernels.py

Comment on lines +278 to 283
X = check_array(X, input_name="X")
if Y is None:
Y = X
else:
Y = input_to_cupy_array(Y).array
Y = check_array(Y, input_name="Y")
if X.shape[1] != Y.shape[1]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

convert_dtype behavior was dropped in the new validation path.

At Line 282, Y is validated but never coerced to X.dtype, so convert_dtype (Line 187) is effectively ignored. That changes prior mixed-dtype behavior and breaks the function’s documented contract.

Proposed fix
     X = check_array(X, input_name="X")
     if Y is None:
         Y = X
     else:
         Y = check_array(Y, input_name="Y")
+        if convert_dtype and Y.dtype != X.dtype:
+            Y = Y.astype(X.dtype, copy=False)

As per coding guidelines, “API breaking changes to Python estimator interfaces … or breaking backward compatibility require at least one release cycle for deprecations.”

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
X = check_array(X, input_name="X")
if Y is None:
Y = X
else:
Y = input_to_cupy_array(Y).array
Y = check_array(Y, input_name="Y")
if X.shape[1] != Y.shape[1]:
X = check_array(X, input_name="X")
if Y is None:
Y = X
else:
Y = check_array(Y, input_name="Y")
if convert_dtype and Y.dtype != X.dtype:
Y = Y.astype(X.dtype, copy=False)
if X.shape[1] != Y.shape[1]:
🤖 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 `@python/cuml/cuml/metrics/pairwise_kernels.py` around lines 278 - 283, The
validation path drops the previous dtype coercion so Y is no longer converted to
X.dtype and the convert_dtype behavior is ignored; when validating Y inside
pairwise_kernels, call check_array with the dtype matching X (e.g.,
check_array(Y, input_name="Y", dtype=X.dtype)) or pass the existing
convert_dtype flag into check_array so Y is coerced to X.dtype, ensuring X and Y
keep the same dtype and preserving the original mixed-dtype behavior.

@jcrist jcrist left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

:shipit:

@jcrist

jcrist commented May 6, 2026

Copy link
Copy Markdown
Member

/merge

@rapids-bot
rapids-bot Bot merged commit 6daff92 into NVIDIA:main May 6, 2026
96 checks passed
@csadorf
csadorf deleted the issue-7998-apply-new-validation-to-metrics-pairwise-kernels branch May 6, 2026 21:35
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.

4 participants