Skip to content

Apply new validation to metrics.pairwise_distances - #8061

Closed
csadorf wants to merge 1 commit into
NVIDIA:mainfrom
csadorf:issue-7998-apply-new-validation-to-metrics-pairwise-distances
Closed

Apply new validation to metrics.pairwise_distances#8061
csadorf wants to merge 1 commit into
NVIDIA:mainfrom
csadorf:issue-7998-apply-new-validation-to-metrics-pairwise-distances

Conversation

@csadorf

@csadorf csadorf commented May 6, 2026

Copy link
Copy Markdown
Contributor

Convert the dense paths in cuml.metrics.pairwise_distances and nan_euclidean_distances to the new input-validation infrastructure.

Part of #7998.

Migrate the dense pairwise_distances and nan_euclidean_distances
implementations from cuml.common.input_to_cuml_array to check_array
from cuml.internals.validation, matching the pattern established
by the prior cuml.metrics migrations (xref NVIDIA#7998).

The legacy code derived row/column-major-ness from CumlArray.order
('K' propagated from the input). check_array returns a cupy array, so
the orientation is now read directly from the array's contiguity flags.
For degenerate X (1 sample or 1 feature) the layout is ambiguous in X
alone, so Y is allowed to choose the layout (order='A') and we propagate
its choice back into the is_row_major flag.

The dtype-mismatch error path now raises ValueError (sklearn-aligned)
instead of TypeError; test_pairwise_distances_exceptions updated
accordingly.

The sparse pairwise_distances path is unchanged: it never used
input_to_cuml_array.
@csadorf
csadorf requested a review from a team as a code owner May 6, 2026 19:10
@csadorf
csadorf requested a review from viclafargue May 6, 2026 19:10
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label May 6, 2026
@csadorf csadorf added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels May 6, 2026
@csadorf
csadorf marked this pull request as draft May 6, 2026 19:12
@copy-pr-bot

copy-pr-bot Bot commented May 6, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR migrates pairwise_distances.pyx from the legacy input_to_cuml_array preprocessing to the newer check_array validation workflow. It updates X and Y normalization, adjusts pointer extraction, derives memory layout flags, and updates kernel invocation signatures. A test exception type is also corrected.

Changes

Input Validation & Kernel Integration Migration

Layer / File(s) Summary
Import & Validation Setup
python/cuml/cuml/metrics/pairwise_distances.pyx (lines 13–18)
Removed input_to_cuml_array import and added check_array import to establish the new validation pathway.
X and Y Preprocessing
python/cuml/cuml/metrics/pairwise_distances.pyx (lines 180–200, 355–365)
X and Y are now normalized via check_array into X_m and Y_m; shapes (n_samples_x, n_features_x) and dtypes are extracted from the checked arrays.
Memory Layout Derivation
python/cuml/cuml/metrics/pairwise_distances.pyx (lines 366–370, 421–422)
is_row_major flag is derived from X_m layout; destination array dest_m is created with explicit row-major or Fortran-order specification.
Pointer Extraction & Kernel Calls
python/cuml/cuml/metrics/pairwise_distances.pyx (lines 423–424, 433–436, 441–447)
Data pointers d_X_ptr and d_Y_ptr are extracted from X_m.data.ptr and Y_m.data.ptr; pairwise_distance kernel is invoked with explicit scalar dimension parameters (n_samples_x, n_samples_y, n_features_x).
Test Exception Correction
python/cuml/tests/test_metrics.py (lines 1319–1320)
Test expectation for dtype mismatch with convert_dtype=False updated from TypeError to ValueError.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • rapidsai/cuml#8024: Applies the same input_to_cuml_array to check_array migration pattern across the Lars module with equivalent pointer and layout handling updates.
  • rapidsai/cuml#8006: Performs parallel input-validation utility migration from legacy input handling to the new check_array/check_inputs framework.
  • rapidsai/cuml#8050: Applies identical input-validation migration pattern in cuml.metrics modules, replacing legacy preprocessing with check_array-based normalization.

Suggested labels

Cython / Python, improvement, non-breaking, sklearn-api-compat

Suggested reviewers

  • betatim
  • jcrist
  • viclafargue
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Apply new validation to metrics.pairwise_distances' directly describes the main change: applying new validation infrastructure to pairwise_distances.
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.
Description check ✅ Passed The pull request description clearly describes the changes made: replacing input_to_cuml_array with check_array, modifying layout derivation logic, changing dtype-mismatch error type from TypeError to ValueError, and updating corresponding tests. All changes are directly related to the changeset.

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

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

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

🧹 Nitpick comments (1)
python/cuml/tests/test_metrics.py (1)

1320-1323: ⚡ Quick win

Add a regression for the n_features == 1 layout path.

This PR added explicit degenerate-layout handling, but the current order regression at Lines 1230-1295 still repeats the single-sample (1, 4) case twice. Please add a (n, 1) variant here as well so the new y_order="A" / contiguity propagation branch is actually locked down against scikit-learn.

As per coding guidelines, "Test files must validate numerical correctness by comparing with scikit-learn, include edge case coverage (empty datasets, single sample, high-dimensional data)".

🤖 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/tests/test_metrics.py` around lines 1320 - 1323, Add a regression
that covers the degenerate n_features==1 layout by adding a test variant using X
arrays shaped (n, 1) (e.g., 4x1) alongside the existing (1, 4) case so the new
y_order="A" / contiguity propagation branch is exercised; construct X_double
(float64) and X_float (float32) with shape (n,1) and compare
pairwise_distances(...) results against sklearn.metrics.pairwise_distances
(using the same metric and convert_dtype settings) to assert numerical
equality/consistency as the other order tests do.
🤖 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.

Nitpick comments:
In `@python/cuml/tests/test_metrics.py`:
- Around line 1320-1323: Add a regression that covers the degenerate
n_features==1 layout by adding a test variant using X arrays shaped (n, 1)
(e.g., 4x1) alongside the existing (1, 4) case so the new y_order="A" /
contiguity propagation branch is exercised; construct X_double (float64) and
X_float (float32) with shape (n,1) and compare pairwise_distances(...) results
against sklearn.metrics.pairwise_distances (using the same metric and
convert_dtype settings) to assert numerical equality/consistency as the other
order tests do.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c1bf9ce4-247f-4f6e-8a54-4c6bcbcbacbc

📥 Commits

Reviewing files that changed from the base of the PR and between 7ba22e6 and 1303f61.

📒 Files selected for processing (2)
  • python/cuml/cuml/metrics/pairwise_distances.pyx
  • python/cuml/tests/test_metrics.py

@csadorf csadorf closed this May 6, 2026
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.

3 participants