Skip to content

Skip optionally the cost matrix validation check#1217

Merged
rapids-bot[bot] merged 3 commits into
NVIDIA:mainfrom
hlinsen:matrix-validation
May 15, 2026
Merged

Skip optionally the cost matrix validation check#1217
rapids-bot[bot] merged 3 commits into
NVIDIA:mainfrom
hlinsen:matrix-validation

Conversation

@hlinsen
Copy link
Copy Markdown
Contributor

@hlinsen hlinsen commented May 14, 2026

Boosts performance when many problems are scheduled in a loop with large matrices

@hlinsen hlinsen added the non-breaking Introduces a non-breaking change label May 14, 2026
@hlinsen hlinsen requested a review from a team as a code owner May 14, 2026 06:43
@hlinsen hlinsen added the improvement Improves an existing functionality label May 14, 2026
@hlinsen hlinsen requested a review from rgsl888prabhu May 14, 2026 06:43
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The DataModel.add_cost_matrix method now accepts an optional keyword-only skip_validation parameter (default False). When True, the Python validate_matrix call is skipped; signature, docstring, and conditional validation are updated.

Changes

Skip validation parameter for cost matrix

Layer / File(s) Summary
Add skip_validation parameter and conditional validation logic
python/cuopt/cuopt/routing/vehicle_routing.py
Method signature adds keyword-only skip_validation parameter; docstring documents the parameter and validation responsibility; validate_matrix is called only when skip_validation=False.

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.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 main change: adding an optional parameter to skip cost matrix validation, which matches the primary modification to the DataModel.add_cost_matrix method signature.
Description check ✅ Passed The description is related to the changeset, explaining the performance benefit of skipping validation when scheduling many problems with large matrices, which is the intended use case for the skip_validation parameter.
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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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/cuopt/cuopt/routing/vehicle_routing.py`:
- Around line 132-133: When skip_validation is True you still need a lightweight
shape guard to avoid malformed matrices crossing into the wrapper: add a cheap
check before returning that cost_mat is a 2D sequence with outer length == n and
every row length == n (where n = self.get_num_locations()), but do not perform
the heavier NULL/non-negative content scans that validate_matrix does; update
the branch handling skip_validation around the validate_matrix call to run this
simple shape guard (using cost_mat and self.get_num_locations() as identifiers)
and only call validate_matrix when skip_validation is False.
🪄 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: c0284935-6266-45f0-97fc-8eb2c739dc8b

📥 Commits

Reviewing files that changed from the base of the PR and between 20e15cd and 8cac3fb.

📒 Files selected for processing (1)
  • python/cuopt/cuopt/routing/vehicle_routing.py

Comment on lines +132 to +133
if not skip_validation:
validate_matrix(cost_mat, "cost matrix", self.get_num_locations())
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

Retain minimal boundary validation when skip_validation=True.

Line 132 currently skips all checks, so malformed matrix dimensions can cross the Python boundary and fail deeper in the wrapper with less actionable errors. Keep a cheap shape guard in the skip path and reserve full scans (NULL/non-negative checks) for the non-skip path.

Suggested patch
-        if not skip_validation:
-            validate_matrix(cost_mat, "cost matrix", self.get_num_locations())
+        n_locations = self.get_num_locations()
+        if skip_validation:
+            if (
+                len(cost_mat) != n_locations
+                or len(cost_mat.columns) != n_locations
+            ):
+                raise ValueError(
+                    "cost matrix must be square with size equal to "
+                    "number of locations"
+                )
+        else:
+            validate_matrix(cost_mat, "cost matrix", n_locations)

As per coding guidelines, "Flag missing input validation at library and server boundaries."

🤖 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/cuopt/cuopt/routing/vehicle_routing.py` around lines 132 - 133, When
skip_validation is True you still need a lightweight shape guard to avoid
malformed matrices crossing into the wrapper: add a cheap check before returning
that cost_mat is a 2D sequence with outer length == n and every row length == n
(where n = self.get_num_locations()), but do not perform the heavier
NULL/non-negative content scans that validate_matrix does; update the branch
handling skip_validation around the validate_matrix call to run this simple
shape guard (using cost_mat and self.get_num_locations() as identifiers) and
only call validate_matrix when skip_validation is False.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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/cuopt/cuopt/routing/vehicle_routing.py`:
- Around line 134-137: Add pytest unit tests covering both branches introduced
around skip_validation: write one test that calls
vehicle_routing.add_cost_matrix(...) with skip_validation=False (or default) to
assert validate_matrix is invoked and that a bad cost_mat raises the expected
validation exception, and a second test that calls add_cost_matrix(...,
skip_validation=True) to ensure validate_matrix is not called but duplicate
vehicle_type handling still triggers the same error/behavior; reference the
functions/methods validate_matrix, add_cost_matrix, get_num_locations and the
vehicle_type parameter to locate the logic and include assertions for duplicate
vehicle_type behavior when adding the same vehicle_type twice.
🪄 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: a273be76-3bcf-436a-afc8-8cd30cfd5ef0

📥 Commits

Reviewing files that changed from the base of the PR and between 8cac3fb and 333ffd2.

📒 Files selected for processing (1)
  • python/cuopt/cuopt/routing/vehicle_routing.py

Comment on lines +134 to 137
if not skip_validation:
validate_matrix(cost_mat, "cost matrix", self.get_num_locations())

super().add_cost_matrix(cost_mat, vehicle_type)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Add pytest coverage for both skip_validation paths.

This change adds a new behavior branch but no tests in this PR context. Please add tests for default validation behavior and explicit skip behavior (while still validating duplicate vehicle_type handling).

As per coding guidelines, "python/cuopt/cuopt/**/*.py: Add unit tests for Python code using pytest, referencing examples in python/cuopt/cuopt/tests".

🤖 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/cuopt/cuopt/routing/vehicle_routing.py` around lines 134 - 137, Add
pytest unit tests covering both branches introduced around skip_validation:
write one test that calls vehicle_routing.add_cost_matrix(...) with
skip_validation=False (or default) to assert validate_matrix is invoked and that
a bad cost_mat raises the expected validation exception, and a second test that
calls add_cost_matrix(..., skip_validation=True) to ensure validate_matrix is
not called but duplicate vehicle_type handling still triggers the same
error/behavior; reference the functions/methods validate_matrix,
add_cost_matrix, get_num_locations and the vehicle_type parameter to locate the
logic and include assertions for duplicate vehicle_type behavior when adding the
same vehicle_type twice.

@hlinsen
Copy link
Copy Markdown
Contributor Author

hlinsen commented May 15, 2026

/merge

@rapids-bot rapids-bot Bot merged commit 32af987 into NVIDIA:main May 15, 2026
98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improves an existing functionality non-breaking Introduces a non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants