perf: GPU memory optimization, torch_scatter compat, NaN handling, and robustness fixes - #99
Open
liu-687 wants to merge 3 commits into
Open
perf: GPU memory optimization, torch_scatter compat, NaN handling, and robustness fixes#99liu-687 wants to merge 3 commits into
liu-687 wants to merge 3 commits into
Conversation
added 2 commits
July 21, 2026 23:28
…d device fixes - kernel.py: explicit GPU memory management in make_mask to prevent OOM on large datasets; keep mask/label on CPU, clean up after each sample - kernel.py, model.py, graph_norm.py: fallback to native PyTorch scatter via compat_scatter.py when torch_scatter is unavailable - graph.py: handle single-sample inference without subgraph_dict gracefully - pred_ham.py: replace NaN entries with zeros for uncovered orbital pairs instead of crashing with assertion error - evaluate.py: fix device mismatch by reshaping before moving to label device - add .gitignore for __pycache__, egg-info, build artifacts
There was a problem hiding this comment.
Pull request overview
This PR improves DeepH-pack’s robustness and portability by reducing GPU memory pressure during mask/label preparation, adding a native PyTorch fallback for torch_scatter, and hardening inference utilities against missing/invalid values.
Changes:
- Added
torch_scatterimport fallbacks to a newcompat_scatter.pyimplementation for broader platform support. - Reduced GPU memory usage in
DeepHKernel.make_maskvia targeted tensor moves and per-sample cleanup. - Improved inference robustness (NaN filling in
pred_ham.py, relaxed LCMP subgraph assumptions ingraph.py) and fixed a device handling issue inevaluate.py.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
deeph/scripts/evaluate.py |
Fixes output reshaping/device placement to avoid device mismatch. |
deeph/model.py |
Adds torch_scatter fallback imports to compat_scatter. |
deeph/kernel.py |
Adjusts mask/label creation workflow for lower GPU memory usage and adds scatter fallback import. |
deeph/inference/pred_ham.py |
Replaces NaN assertion with NaN→0 filling + logging during block restoration. |
deeph/graph.py |
Changes LCMP collation behavior when subgraph data is missing. |
deeph/from_PyG_future/graph_norm.py |
Adds scatter_mean fallback import. |
deeph/data.py |
Saves un-collated data_list and collates on load; adds GC and dataset slicing logic. |
deeph/compat_scatter.py |
New: native PyTorch scatter/scatter_add/scatter_mean compatibility layer. |
.gitignore |
New: ignores common Python build/cache artifacts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…MP error - kernel.py: wrap torch.cuda.empty_cache() with torch.cuda.is_available() check to avoid RuntimeError on CPU-only PyTorch builds - graph.py: replace silent (batch, None) return with descriptive RuntimeError when subgraph_dict is missing, preventing cryptic downstream crashes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR includes a series of optimizations and robustness improvements to the DeepH-pack codebase, mainly focused on memory efficiency, portability, and inference reliability.
Changes
🚀 GPU Memory Optimization (
kernel.py)make_masknow moves only necessary tensors (index_to_Z,x,edge_index) to GPU individually instead of the entire data objectmaskandlabeltensors are kept on CPU after computation, dramatically reducing GPU memory usagedel+torch.cuda.empty_cache()after each sample to prevent memory fragmentation🔧 Portability — Native PyTorch Scatter Fallback (
kernel.py,model.py,graph_norm.py)torch_scatterimports now usetry/except ImportErrorwith fallback to native PyTorch via newcompat_scatter.pycompat_scatter.pyprovides drop-in replacements forscatter_add,scatter,scatter_meanusing built-inscatter_add_torch_scatterC++ extension on platforms where compilation is problematic🛡️ Robustness Improvements
pred_ham.py: Replaces NaN entries with zeros for orbital pairs not covered by the trained model, instead of crashing with assertion error. Logs count of replaced entries for visibilitygraph.py: Gracefully handles single-sample inference without DFT subgraph data (returnsNonefor subgraph instead of asserting)🐛 Bug Fixes
evaluate.py: Fixed device mismatch — reshapes output before moving to label's device instead of hardcoding.cpu()data.py: Cleaned up redundant imports, added explicit GC calls to recover pool worker memory after multiprocessingFiles Changed
deeph/kernel.pydeeph/data.pydeeph/model.pydeeph/graph.pydeeph/inference/pred_ham.pydeeph/scripts/evaluate.pydeeph/from_PyG_future/graph_norm.pydeeph/compat_scatter.py.gitignore__pycache__/,*.egg-info/Backward Compatibility
subgraph_dictare handled gracefully