style: extend device lint check to Array API#5261
style: extend device lint check to Array API#5261wanghan-iapcm merged 3 commits intodeepmodeling:masterfrom
device lint check to Array API#5261Conversation
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@ustc.edu.cn>
There was a problem hiding this comment.
Pull request overview
Extends the repository’s “explicit device” linting and updates Array API-based tensor creation sites to consistently place newly created arrays on the intended device (avoiding accidental CPU allocations when running with torch-backed Array API).
Changes:
- Extend
no-explicit-devicelint rule to coverxp.*creation calls in addition totorch.*. - Propagate device explicitly when creating
xp.eye(...)in the polar atomic model output-stat path. - Add
device=...to Array API helper allocations (xp.arange,xp.zeros) to match the new lint requirement.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| source/checker/deepmd_checker.py | Lint rule now flags missing device= for xp.* creation ops (in addition to torch.*). |
| deepmd/dpmodel/atomic_model/polar_atomic_model.py | Ensures xp.eye is created on the same device as the model’s bias tensors. |
| deepmd/dpmodel/array_api.py | Adds explicit device= when creating intermediate arrays in array-api helper utilities. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Repository UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe PR updates device placement specifications across array API operations and adds device inference logic in model code. It also expands device-check validation in the linter to include both PyTorch and XP backends, ensuring tensors are created on consistent devices. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@deepmd/dpmodel/array_api.py`:
- Around line 81-83: The call to array_api_compat.device(...) is passing
input.device which will raise AttributeError for NumPy arrays; change the
argument to pass the array itself (input) instead. Locate the xp.arange call
that constructs idx (the line using idx = xp.arange(...
device=array_api_compat.device(...))) and replace
array_api_compat.device(input.device) with array_api_compat.device(input) so it
matches the other usages across the codebase and works for both NumPy and
device-backed arrays.
In `@source/checker/deepmd_checker.py`:
- Around line 63-65: Update the misleading comment and lint text that claim
"only PT needs device": since the check now triggers when node.func.expr.name is
in {"torch", "xp"}, change the inline comment near the no_device check to
mention both PyTorch and the xp backend, and update the lint message/description
associated with the "no-explicit-device" rule (or its registration) so its
wording clearly covers both torch and xp backends rather than implying
PyTorch-only; look for uses of no_device, node.func.expr.name and the
self.add_message("no-explicit-device", node=node) call and adjust the comment
and rule message/description accordingly.
ℹ️ Review info
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
deepmd/dpmodel/array_api.pydeepmd/dpmodel/atomic_model/polar_atomic_model.pysource/checker/deepmd_checker.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5261 +/- ##
=======================================
Coverage 82.00% 82.00%
=======================================
Files 750 750
Lines 75082 75083 +1
Branches 3615 3616 +1
=======================================
+ Hits 61571 61572 +1
Misses 12347 12347
Partials 1164 1164 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary by CodeRabbit