Skip to content

Sog devel#5361

Closed
Johnsonjz wants to merge 9 commits intodeepmodeling:masterfrom
Johnsonjz:sog_devel
Closed

Sog devel#5361
Johnsonjz wants to merge 9 commits intodeepmodeling:masterfrom
Johnsonjz:sog_devel

Conversation

@Johnsonjz
Copy link
Copy Markdown

@Johnsonjz Johnsonjz commented Apr 2, 2026

  1. implementation of SOG-Net and LES, based on the local charge setting
  2. direct calculation of force and virial, instead of autograd of energy

todo:

  1. improve of computational efficiency of kernel function(mainly at NUFFT)
  2. more experiments to verify the correctness and effectiveness of the implementation
  3. implementation on the LAMMPS end

Summary by CodeRabbit

  • New Features

    • Added LES (long-range energy separation) atomic model with NUFFT-based frame correction
    • Added LR (long-range) atomic model with trainable correction head
    • Added SOG (sum of Gaussians) atomic model with NUFFT-based frame correction
    • New fitting networks for LES, LR, and SOG models with separate short-range and long-range outputs
    • Extended model registry to support new model types and fitting configurations
  • Tests

    • Added test suites validating LES and SOG models
  • Chores

    • Added pytorch-finufft dependency for NUFFT operations
    • Added example configurations and benchmarking scripts for water datasets

1. implementation of SOG-Net and LES, based on the local charge setting
2. direct calculation of force and virial, instead of autograd of energy

todo:
1. improve of computational efficiency of kernel function(mainly at NUFFT)
2. more experiments to verify the correctness and effectiveness of the implementation
3. implementation on the LAMMPS end
Copilot AI review requested due to automatic review settings April 2, 2026 09:21
@dosubot dosubot bot added the new feature label Apr 2, 2026
@Johnsonjz Johnsonjz closed this Apr 2, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 2, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 54f247ba-247e-4dcf-8454-1ec421016e84

📥 Commits

Reviewing files that changed from the base of the PR and between bf3e483 and 6c3f792.

📒 Files selected for processing (26)
  • deepmd/pt/model/atomic_model/__init__.py
  • deepmd/pt/model/atomic_model/les_atomic_model.py
  • deepmd/pt/model/atomic_model/lr_energy_atomic_model.py
  • deepmd/pt/model/atomic_model/sog_atomic_model.py
  • deepmd/pt/model/model/__init__.py
  • deepmd/pt/model/model/les_model.py
  • deepmd/pt/model/model/sog_model.py
  • deepmd/pt/model/task/__init__.py
  • deepmd/pt/model/task/les_energy_fitting.py
  • deepmd/pt/model/task/lr_fitting.py
  • deepmd/pt/model/task/sog_energy_fitting.py
  • deepmd/pt_expt/train/training.py
  • deepmd/utils/argcheck.py
  • examples/water/dpa3/dpa3.hdf5
  • examples/water/dpa3/input_torch_copy.json
  • examples/water/sog/README.md
  • examples/water/sog/ab_retain_graph.py
  • examples/water/sog/check_sog_consistency_with_cace.py
  • examples/water/sog/compare_sog_dpa3_timing.py
  • examples/water/sog/input_torch.json
  • examples/water/sog/profile_sog_timing.py
  • examples/water/sog/profile_sog_whatif.py
  • examples/water/sog/sog.hdf5
  • pyproject.toml
  • source/tests/pt/model/test_les_working_layer.py
  • source/tests/pt/model/test_sog_working_layer.py

📝 Walkthrough

Walkthrough

Adds three energy model families (LES, LR, SOG): new fitting networks, atomic-model wrappers, top-level PyTorch models with NUFFT-based frame-correction pipelines, argument schema, examples/benchmarks, tests, and packaging/entrypoint updates. Exports and registry entries are wired across model, task, and atomic_model packages.

Changes

Cohort / File(s) Summary
Atomic Models
deepmd/pt/model/atomic_model/__init__.py, deepmd/pt/model/atomic_model/les_atomic_model.py, deepmd/pt/model/atomic_model/lr_energy_atomic_model.py, deepmd/pt/model/atomic_model/sog_atomic_model.py
Added LESEnergyAtomicModel, LREnergyAtomicModel, SOGEnergyAtomicModel; each wraps descriptor+fitting net, implements forward_atomic, output-defs, stat compute/load, per-type output bias application, and serialize/deserialize.
Fitting Nets
deepmd/pt/model/task/__init__.py, deepmd/pt/model/task/lr_fitting.py, deepmd/pt/model/task/les_energy_fitting.py, deepmd/pt/model/task/sog_energy_fitting.py
Introduced LRFittingNet (SR/LR dual-net infra), LESEnergyFittingNet (sigma, LES opts), and SOGEnergyFittingNet (wl/sl, SOG params). Added output definitions, forward flows, serialization/deserialization, and exported these classes.
High-level Models
deepmd/pt/model/model/__init__.py, deepmd/pt/model/model/les_model.py, deepmd/pt/model/model/sog_model.py
Added LESEnergyModel and SOGEnergyModel (registered types), integrated NUFFT-based per-frame correction pipelines, Hessian toggle, translated output definitions, and forward/forward_lower wiring; get_standard_model() recognizes new fitting types.
Argument Schema
deepmd/utils/argcheck.py
Added fitting_sog_energy and fitting_les_energy argument plugins and doc_sog/doc_les for configuring SR/LR and long-range kernel params.
Training infra
deepmd/pt_expt/train/training.py
Trainer now calls set_debug_print_freq() on submodules that expose that API during initialization.
Examples & Benchmarks
examples/water/sog/*, examples/water/dpa3/input_torch_copy.json
New SOG example configs, README, and multiple benchmarking/profiling scripts (timing, profiling, CACE comparison, retain-graph patching).
Tests
source/tests/pt/model/test_les_working_layer.py, source/tests/pt/model/test_sog_working_layer.py
New unit tests (skipped if finufft missing) validating frame-correction application, forward vs forward_lower consistency, and gradient flow for long-range parameters.
Packaging
pyproject.toml
Package renamed to deepmd-kit-dev, CLI script key renamed to dp_dev, added pytorch-finufft and conditional cufinufft to GPU dependency group.
Misc examples
examples/water/sog/*.py, examples/water/sog/input_torch.json, examples/water/sog/README.md
Multiple helper/benchmark scripts and JSON config for SOG training and profiling.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Caller (coord, atype, box)
    participant Extender as Extender / NeighborList
    participant Descriptor as Descriptor
    participant Fitting as FittingNet (SR / LR)
    participant FrameCorr as FrameCorrection (NUFFT)
    participant ModelOut as Model Output

    Client->>Extender: extend_input_and_build_neighbor_list(coord, atype, box)
    Extender->>Descriptor: compute per-atom descriptor features (g2,h2,rot_mat,...)
    Descriptor->>Fitting: per-atom descriptor features (+fparam/aparam)
    Fitting->>Fitting: SR forward -> energy_redu\nLR forward -> latent_charge
    Fitting->>FrameCorr: per-frame coords + latent_charge + box
    Note over FrameCorr: fractional coords -> k-grid -> Gaussian damping\nNUFFT type-1(/type-2) -> corr_redu (+ optional force/virial)
    FrameCorr->>ModelOut: frame correction bundle (corr_redu, force_local, virial_local)
    Fitting->>ModelOut: base energy and derivative tensors
    ModelOut->>Client: combined energy, force, virial (with frame correction applied)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

Suggested reviewers

  • njzjz
  • wanghan-iapcm
  • iProzd
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds PyTorch implementations of SOG-Net and LES long-range corrections driven by learned per-atom “latent charge”, including direct NUFFT-based force/virial computation and accompanying tests/examples.

Changes:

  • Introduces SOG/LES fitting nets, atomic models, and model wrappers that add a NUFFT-based per-frame long-range correction (energy/force/virial).
  • Adds working-layer unit tests for SOG/LES and a water SOG training + profiling example bundle.
  • Updates CLI arg schema and packaging/dependency configuration to expose the new fitting types and finufft requirements.

Reviewed changes

Copilot reviewed 24 out of 26 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
source/tests/pt/model/test_sog_working_layer.py Adds SOG working-layer tests (frame correction + forward/forward_lower consistency).
source/tests/pt/model/test_les_working_layer.py Adds LES working-layer tests (frame correction + consistency + grad path).
pyproject.toml Renames distribution + CLI script; adds finufft deps to a pinned PyTorch GPU group.
examples/water/sog/sog.hdf5 Adds SOG example statistics file.
examples/water/sog/README.md Documents how to run the SOG water example.
examples/water/sog/profile_sog_whatif.py Profiles timing under alternative model hyperparameters.
examples/water/sog/profile_sog_timing.py Provides detailed timing breakdown (incl. NUFFT) via monkeypatching.
examples/water/sog/input_torch.json Adds a full SOG training configuration for the water example.
examples/water/sog/compare_sog_dpa3_timing.py Compares runtime vs DPA3 and isolates frame-correction overhead.
examples/water/sog/check_sog_consistency_with_cace.py Script intended to compare SOG correction against an external reference.
examples/water/sog/ab_retain_graph.py Benchmarks an alternative autograd-based correction application path.
examples/water/dpa3/input_torch_copy.json Adds a copied DPA3 config used for timing comparisons.
examples/water/dpa3/dpa3.hdf5 Adds DPA3 example statistics file.
deepmd/utils/argcheck.py Registers new sog_energy / les_energy fitting args.
deepmd/pt/model/task/sog_energy_fitting.py Implements SOGEnergyFittingNet (SR energy + LR latent charge + kernel params).
deepmd/pt/model/task/lr_fitting.py Adds a generic SR+LR fitting base used by SOG/LES.
deepmd/pt/model/task/les_energy_fitting.py Implements LESEnergyFittingNet (SR energy + LR latent charge + sigma param).
deepmd/pt/model/task/init.py Exposes LR/SOG/LES fitting nets in the task package API.
deepmd/pt/model/model/sog_model.py Adds SOGEnergyModel with NUFFT-based frame correction (energy/force/virial).
deepmd/pt/model/model/les_model.py Adds LESEnergyModel with NUFFT-based frame correction (energy/force/virial).
deepmd/pt/model/model/init.py Wires new fitting types into get_standard_model and imports new models.
deepmd/pt/model/atomic_model/sog_atomic_model.py Adds SOGEnergyAtomicModel to produce SR energy + latent charge.
deepmd/pt/model/atomic_model/lr_energy_atomic_model.py Adds/updates an auxiliary SR+property correction atomic model.
deepmd/pt/model/atomic_model/les_atomic_model.py Adds LESEnergyAtomicModel to produce SR energy + latent charge.
deepmd/pt/model/atomic_model/init.py Exports new atomic models.
deepmd/pt_expt/train/training.py Propagates disp_freq to modules supporting set_debug_print_freq.
Comments suppressed due to low confidence (1)

pyproject.toml:179

  • In the pin_pytorch_gpu dependency group, newly added pytorch-finufft>=... / cufinufft>=... are lower-bounded rather than pinned, which is inconsistent with the intent implied by the group name (and with other pin_* groups that use exact pins). Consider pinning exact versions here (or moving these deps to a non-pinned extra) to keep environments reproducible.
  "cufinufft>=2.5.0; platform_system=='Linux' and platform_machine=='x86_64'",
]
pin_jax = [
  "jax==0.5.0;python_version>='3.10'",
]

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread deepmd/utils/argcheck.py
Comment on lines +2392 to +2396
doc=doc_only_pt_supported + doc_trainable,
),
Argument(
"rcond",
[float, type(None)],
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

les_energy fitting args define shift/amplitude, but LESEnergyFittingNet.__init__ expects sigma (and does not accept shift/amplitude). This will make valid LES configs fail validation or mislead users. Replace these arguments with a sigma argument (plus doc/default) to match the actual LES fitting net API.

Copilot uses AI. Check for mistakes.
Any,
)

import pytorch_finufft
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Importing pytorch_finufft at module import time makes deepmd.pt.model.model.sog_model (and anything that imports it) unusable unless the optional NUFFT dependency is installed. This also breaks the unit tests' skip logic, because the model import will raise before the skip decorator can run. Consider wrapping the import in a try/except ImportError and raising a clear runtime error only when the NUFFT correction path is actually executed (or lazily importing inside _compute_sog_frame_correction_bundle).

Suggested change
import pytorch_finufft
try:
import pytorch_finufft
except ImportError: # Optional dependency; only required for NUFFT-based corrections.
class _MissingPytorchFinufftProxy:
def __getattr__(self, name: str) -> Any:
raise RuntimeError(
"pytorch_finufft is not installed but is required for NUFFT-based "
"corrections in SOGEnergyModel. Please install pytorch_finufft to "
"use this functionality."
)
pytorch_finufft = _MissingPytorchFinufftProxy() # type: ignore[assignment]

Copilot uses AI. Check for mistakes.
Any,
)

import pytorch_finufft
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Importing pytorch_finufft at module import time makes deepmd.pt.model.model.les_model unusable unless the optional NUFFT dependency is installed, and will also break any tests that try to skip when finufft is absent (the import fails before skip logic runs). Wrap this in try/except ImportError and defer the hard failure until the frame-correction code path is invoked (or lazily import inside _compute_les_frame_correction_bundle).

Suggested change
import pytorch_finufft
try:
import pytorch_finufft
except ImportError:
class _MissingPytorchFinufft:
"""Placeholder for optional pytorch_finufft dependency.
Any attempt to access attributes on this object will raise an
ImportError at runtime, deferring the hard failure until the
NUFFT-based frame-correction code path is actually used.
"""
def __getattr__(self, name: str) -> Any: # type: ignore[override]
raise ImportError(
"pytorch_finufft is required for LES frame correction but is "
"not installed. Please install pytorch_finufft to use this "
"functionality."
)
pytorch_finufft = _MissingPytorchFinufft() # type: ignore[assignment]

Copilot uses AI. Check for mistakes.
Comment thread pyproject.toml
Comment on lines 13 to 16
[project]
name = "deepmd-kit"
name = "deepmd-kit-dev"
dynamic = ["version", "optional-dependencies", "scripts", "readme"]
description = "A deep learning package for many-body potential energy representation and molecular dynamics"
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Changing the distributable name to deepmd-kit-dev is a breaking packaging change and doesn't seem related to the PR description (SOG/LES implementation). Unless this is intentional for a separate dev distribution, it will break downstream installs and tooling expecting deepmd-kit. Consider reverting the project name change or splitting it into a dedicated packaging PR.

Copilot uses AI. Check for mistakes.
Comment thread pyproject.toml
@@ -142,7 +142,7 @@ jax = [
]

[tool.deepmd_build_backend.scripts]
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Renaming the CLI entry point from dp to dp_dev is a breaking user-facing change (and examples/docs still use dp). If this is meant to be the same tool, keep the dp script name (or provide both dp and dp_dev aliases) to avoid breaking existing workflows.

Suggested change
[tool.deepmd_build_backend.scripts]
[tool.deepmd_build_backend.scripts]
dp = "deepmd.main:main"

Copilot uses AI. Check for mistakes.
Comment on lines +406 to +410
model_predict_lower = self.forward_common_lower(
extended_coord,
extended_atype,
nlist,
mapping,
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Same issue as in SOGEnergyModel.forward: this else: is paired with if self.do_grad_c('energy'), so forces requested without virials will hit the else branch and overwrite model_predict['force'] with model_ret['dforce']. Adjust conditionals so dforce is only used when do_grad_r is false.

Copilot uses AI. Check for mistakes.
Comment on lines +76 to 80
SOGEnergyModel,
)
from .spin_model import (
SpinEnergyModel,
SpinModel,
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Unconditionally importing the SOG/LES model modules here will also unconditionally import pytorch_finufft (currently imported at module import time inside sog_model.py/les_model.py). That makes import deepmd.pt.model.model fail for users who don't have finufft installed, even if they never use SOG/LES. Consider guarding these imports or lazily importing the models only when needed.

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +17
from deepmd.pt.model.descriptor.se_a import (
DescrptSeA,
)
from deepmd.pt.model.model.sog_model import (
SOGEnergyModel,
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Even though the test class is skipped when finufft isn't installed, SOGEnergyModel is imported at module import time. Since deepmd.pt.model.model.sog_model currently imports pytorch_finufft unconditionally, the test module will still fail to import before the skip decorator can run. Either make sog_model tolerate missing finufft (preferred) or delay importing SOGEnergyModel until after the skip condition is evaluated.

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +17
from deepmd.pt.model.descriptor.se_a import (
DescrptSeA,
)
from deepmd.pt.model.model.les_model import (
LESEnergyModel,
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

Same import-time issue as the SOG test: LESEnergyModel is imported at module import time, but deepmd.pt.model.model.les_model currently imports pytorch_finufft unconditionally. If finufft isn't installed, the test module will fail to import before the skip decorator can take effect. Prefer fixing les_model to handle missing finufft or delay importing LESEnergyModel until after the skip check.

Copilot uses AI. Check for mistakes.
Comment on lines +18 to +22
def main() -> None:
cfg = json.loads(pathlib.Path("examples/water/sog/input_torch.json").read_text())[
"model"
]
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

This script hard-codes a local absolute path (/data/zyjin/.../cace/modules/sog.py), which will fail for other users and in CI. Consider taking the path from a CLI argument / environment variable, or removing this script from the repo if it's only intended for local debugging.

Copilot uses AI. Check for mistakes.
@Johnsonjz Johnsonjz reopened this Apr 6, 2026
@Johnsonjz Johnsonjz closed this Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants