Skip to content

Apply new validation to metrics.kl_divergence - #8057

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

Apply new validation to metrics.kl_divergence#8057
rapids-bot[bot] merged 1 commit into
NVIDIA:mainfrom
csadorf:issue-7998-apply-new-validation-to-metrics-kl-divergence

Conversation

@csadorf

@csadorf csadorf commented May 6, 2026

Copy link
Copy Markdown
Contributor

Apply new input validation to cuml.metrics.kl_divergence.

Part of #7998.

Replace input_to_cuml_array in cuml.metrics.kl_divergence with check_array
from cuml.internals.validation, matching the pattern established by
the prior cuml.metrics migrations (xref NVIDIA#7998). Accepts both (N,) and
(N, 1) shapes and rejects 2D inputs with multiple columns explicitly.

The dtype-mismatch error path now raises ValueError (sklearn-aligned)
instead of TypeError; test_kl_divergence updated accordingly.
@csadorf
csadorf requested a review from a team as a code owner May 6, 2026 18:00
@csadorf
csadorf requested a review from betatim May 6, 2026 18:00
@csadorf csadorf added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels May 6, 2026
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label May 6, 2026
@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown
📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced input validation for KL divergence to ensure proper vector handling and dtype compatibility.
    • Improved error messaging for invalid input scenarios.
  • Tests

    • Updated test exception handling to reflect refined dtype validation behavior.

Walkthrough

KL divergence metric implementation in Cython migrated from input_to_cuml_array normalization to a check_array-based validation flow with explicit shape checks, array flattening, and dtype-aware dispatch to float32 or float64 C kernels. Test exception handling updated to reflect new error type.

Changes

KL Divergence Input Validation Refactor

Layer / File(s) Summary
Import Updates
python/cuml/cuml/metrics/kl_divergence.pyx
Replaced input_to_cuml_array with check_array import to enable the new validation approach.
Input Normalization
python/cuml/cuml/metrics/kl_divergence.pyx
P and Q are validated and normalized via check_array, shape verified as 1-D (n,) or (n,1), then flattened; P dtype stored and reused for Q.
Dimension Consistency
python/cuml/cuml/metrics/kl_divergence.pyx
Added explicit check that P and Q lengths match after flattening, with descriptive error if mismatch detected.
dtype-Aware Dispatch
python/cuml/cuml/metrics/kl_divergence.pyx
Conditional branching on P_m.dtype: float32 calls c_kl_divergence with float pointers; otherwise calls with double pointers; both receive n_features_p as length.
Test Exception Update
python/cuml/tests/test_metrics.py
Exception expectation for dtype mismatch in cupy path changed from TypeError to ValueError with "dtype" in message.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • rapidsai/cuml#8043: Migrates input normalization from input_to_cuml_array to check_array-based validation in Cython metric modules.
  • rapidsai/cuml#8024: Applies the same pattern of replacing input_to_cuml_array with check_array and adds dtype-driven conditional dispatch.
  • rapidsai/cuml#8012: Refactors metric input normalization from input_to_* utilities to the new check_array-based validation flow.

Suggested labels

Cython / Python, improvement, non-breaking

Suggested reviewers

  • betatim
  • jcrist
  • divyegala
🚥 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 clearly and specifically describes the main change: applying new validation to the kl_divergence function in metrics, which matches the core objective of the PR.
Description check ✅ Passed The description is directly related to the changeset, explaining the application of new input validation to kl_divergence and referencing the associated issue #7998, which aligns with the implementation changes shown.
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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
python/cuml/cuml/metrics/kl_divergence.pyx (1)

47-50: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Docstring is inaccurate about dtype conversion behavior.

The docstring states that convert_dtype=True will convert both P and Q to float32, but the actual implementation accepts both float32 and float64 (line 64) and converts Q to match P's dtype (line 80). The conversion target depends on P's dtype, not a fixed float32.

📝 Suggested docstring fix
     convert_dtype : bool, optional (default = True)
-        When set to True, the method will, convert P and
-        Q to be the same data type: float32. This
-        will increase memory used for the method.
+        When set to True, the method will convert Q to
+        match the data type of P (float32 or float64). This
+        may increase memory used for the method.
🤖 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/kl_divergence.pyx` around lines 47 - 50, The
docstring for the kl_divergence function incorrectly states convert_dtype
converts both P and Q to float32; update it to say that when convert_dtype=True,
P and Q are converted to the same floating dtype (float32 or float64) supported
by the implementation, with Q being cast to match P.dtype (i.e., the target
dtype follows P's dtype), and mention supported dtypes are float32 and float64;
ensure the docstring references the parameters convert_dtype, P, and Q and
accurately reflects the actual conversion behavior.
🧹 Nitpick comments (1)
python/cuml/cuml/metrics/kl_divergence.pyx (1)

91-91: 💤 Low value

Variable name n_features_p is misleading.

This variable represents the number of elements in the probability distribution (i.e., n_samples), not the number of features. For KL divergence, P and Q are 1D probability vectors, so n_samples or n_elements would be clearer.

♻️ Suggested rename
-    cdef int n_features_p = P_m.shape[0]
-    if Q_m.shape[0] != n_features_p:
+    cdef int n_samples = P_m.shape[0]
+    if Q_m.shape[0] != n_samples:
         raise ValueError(
             "Incompatible dimension for P and Q arrays: "
-            f"P.shape == ({n_features_p},) while Q.shape == ({Q_m.shape[0]},)"
+            f"P.shape == ({n_samples},) while Q.shape == ({Q_m.shape[0]},)"
         )

-    cdef uintptr_t d_P_ptr = P_m.data.ptr
-    cdef uintptr_t d_Q_ptr = Q_m.data.ptr
+    cdef uintptr_t d_P_ptr = P_m.data.ptr
+    cdef uintptr_t d_Q_ptr = Q_m.data.ptr

     if (dtype_p == np.float32):
         res = c_kl_divergence(handle_[0],
                               <float*> d_P_ptr,
                               <float*> d_Q_ptr,
-                              n_features_p)
+                              n_samples)
     else:
         res = c_kl_divergence(handle_[0],
                               <double*> d_P_ptr,
                               <double*> d_Q_ptr,
-                              n_features_p)
+                              n_samples)
🤖 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/kl_divergence.pyx` at line 91, Rename the misleading
variable n_features_p to a clearer name like n_samples (or n_elements) in this
KL divergence implementation: update the declaration cdef int n_features_p =
P_m.shape[0] and replace all subsequent uses of n_features_p throughout the
function (and any helper variables/loops that reference it) to the new
identifier so it reflects that P_m and Q_m are 1D probability vectors; ensure
consistency for related symbols (e.g., if there are n_features_q or similar) and
run tests to confirm no remaining references to n_features_p.
🤖 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.

Outside diff comments:
In `@python/cuml/cuml/metrics/kl_divergence.pyx`:
- Around line 47-50: The docstring for the kl_divergence function incorrectly
states convert_dtype converts both P and Q to float32; update it to say that
when convert_dtype=True, P and Q are converted to the same floating dtype
(float32 or float64) supported by the implementation, with Q being cast to match
P.dtype (i.e., the target dtype follows P's dtype), and mention supported dtypes
are float32 and float64; ensure the docstring references the parameters
convert_dtype, P, and Q and accurately reflects the actual conversion behavior.

---

Nitpick comments:
In `@python/cuml/cuml/metrics/kl_divergence.pyx`:
- Line 91: Rename the misleading variable n_features_p to a clearer name like
n_samples (or n_elements) in this KL divergence implementation: update the
declaration cdef int n_features_p = P_m.shape[0] and replace all subsequent uses
of n_features_p throughout the function (and any helper variables/loops that
reference it) to the new identifier so it reflects that P_m and Q_m are 1D
probability vectors; ensure consistency for related symbols (e.g., if there are
n_features_q or similar) and run tests to confirm no remaining references to
n_features_p.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: de4414b0-2fdd-475c-9105-d4eb8e5967f5

📥 Commits

Reviewing files that changed from the base of the PR and between eae528e and 0590e80.

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

@csadorf csadorf added improvement Improvement / enhancement to an existing function and removed improvement Improvement / enhancement to an existing function labels May 6, 2026

@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 fde6fe4 into NVIDIA:main May 6, 2026
97 of 98 checks passed
@csadorf
csadorf deleted the issue-7998-apply-new-validation-to-metrics-kl-divergence branch May 6, 2026 21:31
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