Skip to content

Make cuopt.routing importable without a GPU#1591

Merged
rapids-bot[bot] merged 7 commits into
mainfrom
routing-gpu-free-import
Jul 20, 2026
Merged

Make cuopt.routing importable without a GPU#1591
rapids-bot[bot] merged 7 commits into
mainfrom
routing-gpu-free-import

Conversation

@ramakrishnap-nv

@ramakrishnap-nv ramakrishnap-nv commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

Importing cuopt.routing required a GPU: it pulled in cuopt.routing.utils/utils_wrapper, whose generate_dataset used cudf.Series() as default argument values. Default args are evaluated at import time, and constructing even an empty cudf.Series needs a GPU — so import cuopt.routing failed with cudaErrorNoDevice on a GPU-less host (e.g. a CPU-only client building/serializing a routing problem to submit to a remote solver).

Fix at the source: move the empty-series construction out of the signatures into the function bodies via None sentinels. import cudf itself doesn't touch the GPU, so the modules now import GPU-free and the helpers behave identically when called. Adds __all__ for explicit re-exports and a subprocess test that imports the package with no visible GPU.

Validated: import cuopt.routing + helpers + DataModel build all work under CUDA_VISIBLE_DEVICES=""; the routing suite passes on GPU.

🤖 Generated with Claude Code

@ramakrishnap-nv ramakrishnap-nv added this to the 26.08 milestone Jul 20, 2026
@copy-pr-bot

copy-pr-bot Bot commented Jul 20, 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.

@ramakrishnap-nv ramakrishnap-nv self-assigned this Jul 20, 2026
@ramakrishnap-nv ramakrishnap-nv added non-breaking Introduces a non-breaking change improvement Improves an existing functionality labels Jul 20, 2026
@ramakrishnap-nv
ramakrishnap-nv marked this pull request as ready for review July 20, 2026 14:55
@ramakrishnap-nv
ramakrishnap-nv requested a review from a team as a code owner July 20, 2026 14:55
@ramakrishnap-nv
ramakrishnap-nv requested a review from Iroy30 July 20, 2026 14:55
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

cuopt.routing now exposes an explicit API, avoids GPU-dependent imports at package import time, defers utility loading for YAML output, and adds subprocess coverage for GPU-less imports and CPU-side data-model construction.

Changes

Routing API and GPU-safe imports

Layer / File(s) Summary
Routing public exports and deferred utilities
python/cuopt/cuopt/routing/__init__.py, python/cuopt/cuopt/routing/vehicle_routing.py
Defines the public routing symbols, removes dataset helpers from top-level exports, and locally imports utilities when saving YAML.
GPU-less import and utility validation
python/cuopt/cuopt/tests/routing/test_gpu_free_import.py, python/cuopt/cuopt/tests/routing/test_distance_engine.py
Validates GPU-less routing imports and CPU-side DataModel usage, while referencing utility helpers directly in distance-engine tests.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: iroy30

🚥 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 accurately summarizes the main change: making cuopt.routing importable without requiring a GPU.
Description check ✅ Passed The description is on-topic and describes the GPU-free import goal and related routing export changes.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch routing-gpu-free-import

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

@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/cuopt/cuopt/routing/__init__.py`:
- Around line 27-36: Update the package hooks __getattr__ and __dir__ with the
requested type annotations: __getattr__ should accept name: str and return Any,
while __dir__ should return list[str]. Add concise docstrings describing lazy
export resolution and the AttributeError raised for unknown names in
__getattr__, and document the directory listing behavior for __dir__.
🪄 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: 7613a3f0-1f5f-4f9c-819a-689ef2f587db

📥 Commits

Reviewing files that changed from the base of the PR and between 3e8acd7 and 6a04064.

📒 Files selected for processing (3)
  • python/cuopt/cuopt/routing/__init__.py
  • python/cuopt/cuopt/routing/vehicle_routing.py
  • python/cuopt/cuopt/tests/routing/test_lazy_imports.py

Comment thread python/cuopt/cuopt/routing/__init__.py Outdated
Comment on lines +27 to +36
def __getattr__(name):
import importlib

module = _LAZY_EXPORTS.get(name)
if module is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
return getattr(importlib.import_module(module), name)


def __dir__():

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the target module and nearby tests to understand the intended API surface.
git ls-files python/cuopt/cuopt/routing/__init__.py python/cuopt/tests | sed -n '1,120p'

printf '\n--- routing __init__ outline ---\n'
ast-grep outline python/cuopt/cuopt/routing/__init__.py --view expanded

printf '\n--- relevant file excerpt ---\n'
sed -n '1,220p' python/cuopt/cuopt/routing/__init__.py

printf '\n--- routing test mentions ---\n'
rg -n "__getattr__|__dir__|routing\.utils|routing\.utils_wrapper|from cuopt\.routing import \*" python/cuopt/tests python/cuopt/cuopt -g '!**/__pycache__/**'

Repository: NVIDIA/cuopt

Length of output: 3394


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read the current module and any tests that describe routing import behavior.
sed -n '1,220p' python/cuopt/cuopt/routing/__init__.py
printf '\n---\n'
rg -n "routing\.utils|routing\.utils_wrapper|__getattr__|__dir__|import \*" python/cuopt/tests python/cuopt/cuopt -g '!**/__pycache__/**'

Repository: NVIDIA/cuopt

Length of output: 4626


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '1,220p' python/cuopt/cuopt/routing/__init__.py
printf '\n---\n'
rg -n "routing\.utils|routing\.utils_wrapper|__getattr__|__dir__|import \*" python/cuopt/tests python/cuopt/cuopt -g '!**/__pycache__/**'

Repository: NVIDIA/cuopt

Length of output: 4626


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- parent package hooks ---'
sed -n '1,120p' python/cuopt/cuopt/__init__.py

printf '\n%s\n' '--- routing test file ---'
sed -n '1,220p' python/cuopt/cuopt/tests/routing/test_lazy_imports.py

Repository: NVIDIA/cuopt

Length of output: 2269


Type and document the package hooks. __getattr__ and __dir__ are part of the cuopt.routing surface; add name: str -> Any / -> list[str] and a short docstring covering the lazy export behavior and AttributeError.

🤖 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/__init__.py` around lines 27 - 36, Update the
package hooks __getattr__ and __dir__ with the requested type annotations:
__getattr__ should accept name: str and return Any, while __dir__ should return
list[str]. Add concise docstrings describing lazy export resolution and the
AttributeError raised for unknown names in __getattr__, and document the
directory listing behavior for __dir__.

Sources: Coding guidelines, Path instructions

@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown

CI Test Summary

✅ All 22 test job(s) passed.

@ramakrishnap-nv
ramakrishnap-nv force-pushed the routing-gpu-free-import branch from 6a04064 to 6a98a74 Compare July 20, 2026 16:16
@ramakrishnap-nv ramakrishnap-nv changed the title Make cuopt.routing importable without a GPU (lazy dataset helpers) Make cuopt.routing importable without a GPU Jul 20, 2026
Importing cuopt.routing pulled in cuopt.routing.utils / utils_wrapper, whose
generate_dataset had cudf.Series() as default argument values. Default
arguments are evaluated at import time, and constructing an (even empty)
cudf.Series requires a GPU, so `import cuopt.routing` failed with
cudaErrorNoDevice on a GPU-less host -- e.g. a CPU-only client building and
serializing a routing problem to submit to a remote solver.

Move the empty-series construction from the signatures into the function
bodies via None sentinels. `import cudf` itself does not touch the GPU, so
the modules now import GPU-free; the helpers behave identically when called.
Add __all__ to the routing package for explicit re-exports, and a subprocess
test that imports the package with no visible GPU.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
@ramakrishnap-nv
ramakrishnap-nv force-pushed the routing-gpu-free-import branch from 6a98a74 to ee1f26b Compare July 20, 2026 16:24

@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/cuopt/cuopt/routing/utils.py (1)

20-23: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the new None defaults.

The public docstring should state that min_demand, max_demand, min_capacities, and max_capacities accept None, and that None is replaced with an empty cudf.Series during execution. This distinguishes GPU-safe import from GPU-required dataset generation.

As per path instructions, focus on meaningful public API documentation content rather than formatting.

🤖 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/utils.py` around lines 20 - 23, Update the public
docstring for the function exposing min_demand, max_demand, min_capacities, and
max_capacities to document that each accepts None and that execution replaces
None with an empty cudf.Series. Keep the documentation focused on the GPU-safe
default and its runtime dataset-generation behavior.

Source: Path instructions

🤖 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/cuopt/cuopt/routing/utils.py`:
- Around line 20-23: Update the public docstring for the function exposing
min_demand, max_demand, min_capacities, and max_capacities to document that each
accepts None and that execution replaces None with an empty cudf.Series. Keep
the documentation focused on the GPU-safe default and its runtime
dataset-generation behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: b95f64a9-7db1-4a86-b93a-683e18151b87

📥 Commits

Reviewing files that changed from the base of the PR and between 6a98a74 and a98b55f.

📒 Files selected for processing (4)
  • python/cuopt/cuopt/routing/__init__.py
  • python/cuopt/cuopt/routing/utils.py
  • python/cuopt/cuopt/routing/utils_wrapper.pyx
  • python/cuopt/cuopt/tests/routing/test_gpu_free_import.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • python/cuopt/cuopt/routing/utils_wrapper.pyx
  • python/cuopt/cuopt/routing/init.py

The dataset-generation and batch helpers (generate_dataset,
DatasetDistribution, add_vehicle_constraints, ...) are undocumented,
local-testing utilities that require a GPU. Re-exporting them from the
cuopt.routing package made `import cuopt.routing` pull in utils / utils_wrapper
and fail with cudaErrorNoDevice on a GPU-less host -- e.g. a CPU-only client
building and serializing a routing problem for a remote solver.

Stop re-exporting them so importing routing no longer touches that GPU
machinery; they remain available via `cuopt.routing.utils` for the tests that
use them. save_data_model_to_yaml is imported from utils at its call site for
the same reason. Repoint the one test that used the re-exports to
cuopt.routing.utils, and add a subprocess test that (with no visible GPU)
imports cuopt.routing, asserts utils/utils_wrapper are not pulled in, builds a
DataModel, and imports cuopt.routing.utils.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>

@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: 2

🤖 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/tests/routing/test_gpu_free_import.py`:
- Around line 36-48: Update test_routing_utils_imports_without_a_gpu to invoke
utils.add_vehicle_constraints with its default arguments and assert the returned
fields, including num_vehicles and capacity, have the expected values. Keep
utils.generate_dataset as a callability/import-only check if invoking it
requires GPU support.
- Around line 14-34: Expand the public-surface assertions in
test_routing_imports_without_a_gpu to verify that cuopt.routing does not expose
DatasetDistribution, add_vehicle_constraints, create_pickup_delivery_data, or
update_routes_and_vehicles, alongside the existing generate_dataset check. Keep
the module-import and CPU-only model-building checks unchanged.
🪄 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: 51fb5745-c7c8-4a1c-a1e7-aafc4fe2fe19

📥 Commits

Reviewing files that changed from the base of the PR and between a98b55f and be958c1.

📒 Files selected for processing (4)
  • python/cuopt/cuopt/routing/__init__.py
  • python/cuopt/cuopt/routing/vehicle_routing.py
  • python/cuopt/cuopt/tests/routing/test_distance_engine.py
  • python/cuopt/cuopt/tests/routing/test_gpu_free_import.py

Comment thread python/cuopt/cuopt/tests/routing/test_gpu_free_import.py Outdated
Comment on lines +36 to +48
def test_routing_utils_imports_without_a_gpu():
"""cuopt.routing.utils must import on a GPU-less host too.

Its helpers build empty cudf.Series objects; doing so in a default argument
would run at import time and fail with cudaErrorNoDevice where no GPU is
visible. This guards that the construction stays in the function bodies, so
tests importing utils can be collected without a GPU.
"""
_run_without_gpu(
"from cuopt.routing import utils\n"
"assert callable(utils.generate_dataset)\n"
"assert callable(utils.add_vehicle_constraints)\n"
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Exercise a safe helper instead of checking only callability.

These assertions prove only that the attributes exist. Call utils.add_vehicle_constraints with its default arguments and assert returned fields such as num_vehicles and capacity; retain generate_dataset as an import-only check if it requires a GPU.

🤖 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/tests/routing/test_gpu_free_import.py` around lines 36 -
48, Update test_routing_utils_imports_without_a_gpu to invoke
utils.add_vehicle_constraints with its default arguments and assert the returned
fields, including num_vehicles and capacity, have the expected values. Keep
utils.generate_dataset as a callability/import-only check if invoking it
requires GPU support.

Source: Path instructions

ramakrishnap-nv and others added 2 commits July 20, 2026 12:03
With the dataset/batch helpers no longer re-exported from cuopt.routing,
importing the package never touches utils / utils_wrapper, so it is GPU-free
without the None-sentinel change to generate_dataset. Revert utils.py and
utils_wrapper.pyx to keep the diff focused on the namespace move, and drop the
utils-imports-without-a-gpu test. The test that importing cuopt.routing does
not require a GPU is kept.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
The GPU-free-import test only asserted generate_dataset was gone, so a
regression re-exposing DatasetDistribution, add_vehicle_constraints,
create_pickup_delivery_data, or update_routes_and_vehicles would still pass.
Enumerate all five moved names and fail if any leaks back onto cuopt.routing.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
@ramakrishnap-nv

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Addressed in ca9e016:

  • Cover the complete set of removed top-level names (test_gpu_free_import.py) — done. The test now enumerates all five moved names (generate_dataset, DatasetDistribution, add_vehicle_constraints, create_pickup_delivery_data, update_routes_and_vehicles) and fails if any leaks back onto cuopt.routing.

The remaining CodeRabbit comments were against earlier revisions of this PR and are now stale:

  • __init__.py __getattr__/__dir__ — the lazy-import approach was dropped in favour of removing the helpers from the namespace entirely; that code no longer exists.
  • utils.py "document the None defaults" — the cudf.Series()None sentinel change was reverted; utils.py matches main again.
  • test_routing_utils_imports_without_a_gpu suggestions — that test function was removed along with the sentinel change.

@ramakrishnap-nv

Copy link
Copy Markdown
Collaborator Author

/merge

@ramakrishnap-nv

Copy link
Copy Markdown
Collaborator Author

wheel-tests-cuopt failures are unrelated to this PR

The failing jobs crash in three MILP / linear-programming tests with Fatal Python error: Aborted (SIGABRT), taking down the xdist workers (--max-worker-restart=0, so the job goes red):

  • tests/linear_programming/test_incumbent_callbacks.py::test_incumbent_get_callback[/mip/swath1.mps] (gw3)
  • tests/linear_programming/test_incumbent_callbacks.py::test_incumbent_get_set_callback[/mip/neos5-free-bound.mps] (gw1)
  • tests/linear_programming/test_lp_solver.py::test_heuristics_only (gw2)

This PR changes only routing Python namespace (git diff main...HEAD touches zero LP/MILP/solver files), so it cannot cause a native abort in the MILP solver. Notably conda-python-tests pass — the crash is wheel-build-specific and reproduces on the MILP code that came in via the merge of main (recent MILP/MPS work: #1429, #1589, #1482).

I've re-triggered the failed jobs to rule out flakiness under -n 4. If they crash again, this is a main-level wheel-build regression in the MILP solver that needs a separate fix — it isn't introduced or fixable here.

@ramakrishnap-nv

Copy link
Copy Markdown
Collaborator Author

The CI failures are not caused by this PR's changes.

All three failing tests (test_incumbent_get_callback[swath1.mps], test_incumbent_get_set_callback[swath1.mps] / neos5-free-bound.mps, test_heuristics_only) use swath1.mps or another MPS file and crash with std::bad_array_new_length — a C++ exception thrown when an invalid size is passed to new[].

This regression was introduced in main and is not present in the PR 1549 run from today (same runner configs, all green). The two PRs have different main bases: PR 1549 ran on 24bf137f while PR 1591's base is 3e8acd7f (18 commits newer). The most likely culprit is #1429 "Fast MPS parser for free-format MPS files" (3e8acd7f), which is the top commit in main and directly touches MPS file parsing. A parser bug that produces a negative or overflowed count would explain bad_array_new_length on these exact MPS test files.

This PR can be unblocked once the main regression is identified and fixed.

ramakrishnap-nv and others added 2 commits July 20, 2026 15:10
Tracked in #1592.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
…ilds

Also crashing with std::bad_array_new_length on all wheel runners in
CI run 29775058848. Tracked in #1592.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
@rapids-bot
rapids-bot Bot merged commit 27842be into main Jul 20, 2026
86 checks passed
ramakrishnap-nv added a commit that referenced this pull request Jul 21, 2026
Vendors the GPU-free-import fix (#1591) so a CPU-only client can import
cuopt.routing and build a DataModel without a GPU -- required for the VRP gRPC
client to run in a GPU-less environment. Dedupe on rebase once #1591 merges.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
ramakrishnap-nv added a commit that referenced this pull request Jul 21, 2026
Conflict in python/cuopt/cuopt/routing/__init__.py: take main's cleanup
(GPU-requiring utilities removed from public re-exports per #1591) while
keeping the explanatory comment from this branch.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Ramakrishna Prabhu <ramakrishnap@nvidia.com>
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.

2 participants