-
Notifications
You must be signed in to change notification settings - Fork 526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat (pt): Expose Linear Ener Model #4194
Feat (pt): Expose Linear Ener Model #4194
Conversation
📝 Walkthrough📝 WalkthroughWalkthroughThe changes include the addition of new JSON configuration files for linear energy models and modifications to several Python classes and methods to support these models. Specifically, new configuration files define model parameters, learning rates, loss functions, and training setups. Additionally, a new function Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Model
User->>Model: Submit input data
Model->>Model: Process input with descriptor type "linear_ener"
Model-->>User: Return processed output
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🔇 Additional comments (5)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (1)
examples/water/zbl/input.json (1)
Line range hint
1-95
: Overall impact of descriptor type changeThe change from "se_e2_a" to "se_atten" descriptor type is the only modification in this configuration file. However, this alteration can have a significant impact on the model's behavior, performance, and potentially its results.
Consider the following recommendations:
- Document the rationale behind this change in the PR description or commit message.
- Ensure comprehensive testing of the model with this new configuration, comparing results with the previous version to understand the impact.
- Update any relevant documentation or user guides that might reference the descriptor type.
- If this change is part of a larger update or feature addition, make sure all related components and configurations are consistently updated across the project.
Would you like assistance in generating a test plan or documentation updates related to this change?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- examples/water/zbl/input.json (1 hunks)
🧰 Additional context used
🔇 Additional comments (1)
examples/water/zbl/input.json (1)
Line range hint
13-26
: Verify descriptor configuration compatibilityWith the change of descriptor type to "se_atten", please verify that the other parameters in the descriptor configuration are still appropriate and optimal. Specifically, check the following:
- The
sel
parameter: [46, 92]- The
neuron
configuration: [25, 50, 100]- Other parameters like
rcut
,rcut_smth
,axis_neuron
, etc.To assist in this verification, let's search for other configurations using the "se_atten" descriptor type:
This will help us compare the current configuration with other instances of "se_atten" usage in the project, if any exist.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## devel #4194 +/- ##
==========================================
- Coverage 83.50% 83.45% -0.06%
==========================================
Files 539 540 +1
Lines 52339 52455 +116
Branches 3047 3047
==========================================
+ Hits 43708 43777 +69
- Misses 7685 7730 +45
- Partials 946 948 +2 ☔ View full report in Codecov by Sentry. |
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 8
🧹 Outside diff range and nitpick comments (1)
deepmd/pt/model/model/dp_linear_model.py (1)
143-143
: Correct the typographical error in the docstringThere's a typographical error in the docstring for the
update_sel
method. The word "statictics" should be corrected to "statistics".Apply this diff to fix the typo:
- data used to do neighbor statictics + Data used to perform neighbor statistics
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
- deepmd/pt/model/atomic_model/linear_atomic_model.py (3 hunks)
- deepmd/pt/model/model/init.py (3 hunks)
- deepmd/pt/model/model/dp_linear_model.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (5)
deepmd/pt/model/model/__init__.py (2)
113-113
:⚠️ Potential issueValidate the
weights
parameter to prevent unexpected behaviorThe
weights
parameter defaults to"mean"
if not specified. To enhance robustness, consider validating theweights
value to ensure it meets expected criteria and handling any invalid inputs appropriately.
39-41
: Verify the existence and correct import ofdp_linear_model
andDPLinearModel
The import statement requires the
dp_linear_model
module and theDPLinearModel
class to be present in the package. Ensure thatdp_linear_model.py
exists in the same directory and thatDPLinearModel
is defined within it.Run the following script to confirm:
✅ Verification successful
DPLinearModel import verified successfully
dp_linear_model.py
exists indeepmd/pt/model/model/
.DPLinearModel
class is defined withindp_linear_model.py
.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that dp_linear_model.py exists and contains the DPLinearModel class. # Test: Find dp_linear_model.py. Expect: Path to dp_linear_model.py fd 'dp_linear_model.py' deepmd/pt/model/model/ # Test: Search for the DPLinearModel class definition. Expect: Definition of DPLinearModel class rg --type py --context 2 'class DPLinearModel' $(fd 'dp_linear_model.py' deepmd/pt/model/model/)Length of output: 323
deepmd/pt/model/atomic_model/linear_atomic_model.py (3)
51-52
: Docstring accurately reflects the newweights
parameterThe update to the docstring correctly documents the
weights
parameter, including its expected types and usage.
59-59
: Addition ofweights
parameter in__init__
method is appropriateThe inclusion of the
weights
parameter with a default value of"mean"
in the constructor enhances the flexibility of the model.
352-373
:_compute_weight
method correctly computes model weightsThe logic in the
_compute_weight
method effectively handles different types ofweights
inputs ("sum"
,"mean"
, or a list of floats) and computes the weights appropriately for each case.
for more information, see https://pre-commit.ci
This PR should resolve #4186 |
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (2)
deepmd/pt/model/model/__init__.py (2)
111-164
: LGTM: Implementation of get_linear_model functionThe
get_linear_model
function is well-structured and correctly handles the creation of linear models based on the input parameters. It appropriately manages both descriptor-based models and pair table models.Consider improving error handling for missing keys
When accessing nested keys in
sub_model_params
, there's a risk ofKeyError
if the keys are missing. Consider implementing checks or usingdict.get()
with default values to prevent potential crashes and provide informative error messages.Replace assert with explicit exception handling
The assertion for pair table models (lines 144-146) could be replaced with explicit exception handling for improved robustness, especially when Python is run with optimizations.
Consider replacing the assertion with a
ValueError
:if "type" not in sub_model_params or sub_model_params["type"] != "pairtab": raise ValueError("Sub-models in LinearEnergyModel must be a DPModel or a PairTable Model")
309-310
: LGTM: Addition of linear_ener model typeThe addition of the "linear_ener" model type and the call to
get_linear_model
is consistent with the new functionality.Consider explicit error handling for unknown model types
The current implementation falls back to
BaseModel.get_class_by_type(model_type)
for unknown types, which may not handle all cases gracefully. Consider adding explicit error handling for unknownmodel_type
values to improve clarity and debuggability.You could modify the code as follows:
elif model_type == "linear_ener": return get_linear_model(model_params) else: raise ValueError(f"Unknown model type: {model_type}")If
BaseModel.get_class_by_type
is intended to handle custom model types dynamically, ensure it includes proper error handling and documentation.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
- deepmd/pt/model/model/init.py (3 hunks)
- deepmd/pt/model/model/dp_linear_model.py (1 hunks)
- examples/water/d3/dftd3.txt (1 hunks)
- examples/water/d3/input_pt.json (1 hunks)
✅ Files skipped from review due to trivial changes (1)
- examples/water/d3/dftd3.txt
🧰 Additional context used
🔇 Additional comments (9)
examples/water/d3/input_pt.json (5)
1-52
: LGTM: Model parameters are well-defined and consistent with PR objectives.The model configuration is well-structured and includes detailed parameters for both the "se_atten" descriptor and the "pairtab" model. This aligns with the PR title "Feat (pt): Expose Linear Ener Model" and the changes mentioned in the summary.
53-59
: LGTM: Learning rate configuration is appropriate.The learning rate configuration uses exponential decay, which is a good practice for adjusting the learning rate during training. The parameters (decay_steps, start_lr, stop_lr) are well-defined.
60-69
: LGTM: Loss function configuration is well-defined.The loss function configuration is appropriate for an energy model, with preferences for energy and force components.
Please verify if setting both
start_pref_v
andlimit_pref_v
to 0 for the volume component is intentional. If volume is not relevant for this model, this is correct; otherwise, you might want to adjust these values.
70-96
: LGTM: Training configuration is comprehensive.The training configuration is well-structured, with separate systems for training and validation, which is a good practice. The number of training steps, display frequency, and save frequency are well-defined.
Could you please clarify what the "auto" batch size means for the training data? It might be helpful to specify how this automatic batch size is determined or provide a default value if the automatic determination fails.
1-96
: Overall, the configuration file is well-structured and comprehensive.This new configuration file for the linear energy model is well-defined and aligns with the PR objectives. It includes all necessary sections (model parameters, learning rate, loss function, and training configuration) with appropriate details. The use of comments throughout the file enhances readability.
A few minor points to consider:
- Verify if the volume preference in the loss function (both start and limit set to 0) is intentional.
- Clarify what the "auto" batch size means for the training data.
These clarifications will ensure the configuration is fully optimized for your use case.
deepmd/pt/model/model/__init__.py (1)
39-41
: LGTM: New import for DPLinearModelThe import statement for
DPLinearModel
is correctly added and appropriately placed with other model imports.deepmd/pt/model/model/dp_linear_model.py (3)
35-41
: Initialization method is correctly implemented.The
__init__
method properly initializes the superclass with the provided arguments.
60-92
: Theforward
method processes outputs correctly.The method correctly constructs the
model_predict
dictionary based on the model outputs and gradient requirements.
93-129
: Theforward_lower
method handles extended inputs appropriately.The method processes extended coordinates and neighbor lists, constructing the
model_predict
dictionary accordingly.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
There was a problem hiding this 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
🧹 Outside diff range and nitpick comments (1)
source/tests/universal/pt/model/test_model.py (1)
877-882
: Remove unused variablesdp_model1
anddp_model2
The variables
dp_model1
anddp_model2
are assigned but never used. This can lead to unnecessary code clutter and potential confusion. Consider removing these variables if they are not needed.Apply this diff to remove the unused variables:
- dp_model1 = DPAtomicModel( - ds1, - ft1, - type_map=cls.expected_type_map, - ) - dp_model2 = DPAtomicModel( - ds2, - ft2, - type_map=cls.expected_type_map, - )🧰 Tools
🪛 Ruff
877-877: Local variable
dp_model1
is assigned to but never usedRemove assignment to unused variable
dp_model1
(F841)
882-882: Local variable
dp_model2
is assigned to but never usedRemove assignment to unused variable
dp_model2
(F841)
🪛 GitHub Check: CodeQL
[notice] 877-877: Unused local variable
Variable dp_model1 is not used.
[notice] 882-882: Unused local variable
Variable dp_model2 is not used.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
- source/tests/universal/common/cases/model/model.py (1 hunks)
- source/tests/universal/pt/model/test_model.py (3 hunks)
🧰 Additional context used
🪛 Ruff
source/tests/universal/pt/model/test_model.py
877-877: Local variable
dp_model1
is assigned to but never usedRemove assignment to unused variable
dp_model1
(F841)
882-882: Local variable
dp_model2
is assigned to but never usedRemove assignment to unused variable
dp_model2
(F841)
888-888: Undefined name
dp_model
(F821)
889-889: Undefined name
pt_model
(F821)
🪛 GitHub Check: CodeQL
source/tests/universal/pt/model/test_model.py
[notice] 877-877: Unused local variable
Variable dp_model1 is not used.
[notice] 882-882: Unused local variable
Variable dp_model2 is not used.
🔇 Additional comments (2)
source/tests/universal/common/cases/model/model.py (2)
Line range hint
1-48
: Summary: New test class added for LinearEnerModelThe addition of
LinearEnerModelTest
class expands the test coverage to include the new linear energy model. The class structure and attribute initialization are consistent with other test classes in the file, maintaining the overall design pattern.To further improve this addition:
- Consider adding explanatory comments to clarify the purpose and uniqueness of this test class.
- Evaluate if additional test methods specific to linear energy models are needed.
- Review if any methods from
ModelTestCase
need to be overridden for specific linear model testing.These enhancements will ensure comprehensive testing of the new linear energy model while maintaining code clarity and consistency.
31-47
: LGTM! New test class added for LinearEnerModel.The addition of
LinearEnerModelTest
class is a good step towards testing the new linear energy model. The class structure and attribute initialization are consistent with other test classes in the file.Consider adding explanatory comments.
To improve code readability and maintainability, consider adding comments explaining the purpose of this new test class and how it differs from
EnerModelTest
.Evaluate the need for additional tests.
While the current setup mirrors
EnerModelTest
, consider if there are any specific characteristics or edge cases of the linear energy model that might require additional test methods.Review inherited methods for potential overrides.
Verify if any methods from
ModelTestCase
need to be overridden to accommodate specific testing requirements for the linear energy model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
source/tests/universal/pt/model/test_model.py (1)
850-850
: Consider adjusting the precision for smooth testingThe
aprec_dict["test_smooth"]
is set to1e-5
, which might be too strict for some cases. Consider adjusting this value if you encounter frequent test failures due to small numerical differences.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- source/tests/universal/pt/model/test_model.py (3 hunks)
🧰 Additional context used
🔇 Additional comments (6)
source/tests/universal/pt/model/test_model.py (6)
22-22
: LGTM: New import added for DPLinearModelThe import of
DPLinearModel
has been correctly added to the list of imports fromdeepmd.pt.model.model
.
47-47
: LGTM: New import added for LinearEnerModelTestThe import of
LinearEnerModelTest
has been correctly added to the list of imports from the common test cases.
810-829
: LGTM: Parameterized decorator for TestLinearEnergyModelPTThe parameterized decorator for
TestLinearEnergyModelPT
is correctly implemented, using appropriate descriptor and fitting parameters for testing.
830-842
: LGTM: TestLinearEnergyModelPT class definition and modules_to_test propertyThe
TestLinearEnergyModelPT
class is correctly defined, inheriting fromunittest.TestCase
,LinearEnerModelTest
, andPTTestCase
. Themodules_to_test
property is implemented correctly to include script module API testing when appropriate.
877-877
: Remove unused variablesThe variables
dp_model1
anddp_model2
are defined but not used in the current implementation. Consider removing them to improve code clarity.Also applies to: 882-882
844-903
:⚠️ Potential issuePotential issue in setUpClass method
The
setUpClass
method has a potential issue with undefined variables. The variablesdp_model1
anddp_model2
are created but not used in theDPLinearModel
initialization. Instead, undefined variablesdp_model
andpt_model
are used.Consider updating the code as follows:
- cls.module = DPLinearModel( - dp_model, - pt_model, - type_map=cls.expected_type_map, - ) + cls.module = DPLinearModel( + [dp_model1, dp_model2], + type_map=cls.expected_type_map, + )Also, note that there are unused variables
dp_model1
anddp_model2
(lines 877 and 882). Consider removing them if they are not needed elsewhere in the class.To verify the usage of these variables, run the following script:
There was a problem hiding this 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
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- deepmd/pt/model/atomic_model/linear_atomic_model.py (4 hunks)
🧰 Additional context used
🔇 Additional comments (5)
deepmd/pt/model/atomic_model/linear_atomic_model.py (5)
51-52
: Documentation of 'weights' parameter is clearThe addition of the
weights
parameter in the docstring enhances flexibility by allowing users to specify model weighting. The explanation is clear and concise.
59-59
: Parameter 'weights' added with appropriate defaultThe
weights
parameter is added to the constructor with a default value of"mean"
, providing sensible default behavior while allowing customization.
95-102
: Replaceassert
statements with explicit exceptions (duplicate comment)Using
assert
statements for input validation can be problematic because assertions may be ignored in optimized Python runs (when using the-O
flag). It's safer to use explicit exception handling to ensure validations are always enforced. Consider raising exceptions likeValueError
instead.
347-350
: Type annotations enhance code clarityAdding type annotations to the
_compute_weight
method improves code readability and maintainability. This helps with static type checking and better understanding of expected parameter types.
355-382
: Weight computation logic is soundThe implementation of
_compute_weight
correctly handles different types ofweights
inputs (str
andlist
) and computes the weights accordingly. The use of comprehensions and built-in functions is appropriate.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
source/tests/universal/pt/model/test_model.py (1)
Line range hint
812-869
: Consider diversifying model configurations insetUpClass
.The
setUpClass
method correctly sets up the testing environment forDPLinearModel
. However, consider the following suggestions to enhance the test coverage:
- The descriptors
ds1
andds2
are currently identical. Consider using different parameters for each to test howDPLinearModel
handles diverse descriptors.- Similarly,
ft1
andft2
are identical. Varying their parameters could provide more comprehensive testing.- As a result,
dp_model1
anddp_model2
are currently identical. Creating them with different configurations would better test the capabilities ofDPLinearModel
in handling diverse atomic models.Implementing these changes could provide a more robust test suite for
DPLinearModel
.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- source/tests/universal/pt/model/test_model.py (6 hunks)
🧰 Additional context used
🔇 Additional comments (4)
source/tests/universal/pt/model/test_model.py (4)
Line range hint
774-796
: LGTM: Class definition and parameterization are well-structured.The
TestLinearEnergyModelPT
class is properly defined with comprehensive parameterization, covering various descriptor and fitting parameter combinations. This approach ensures thorough testing of theDPLinearModel
with different configurations.
Line range hint
797-810
: LGTM:modules_to_test
property is correctly implemented.The
modules_to_test
property is well-implemented, consistently following the pattern used in other test classes. It appropriately includes script modules for testing when available, which enhances the test coverage.
Line range hint
856-870
: LGTM: JIT compilation and expected value setup are correct.The latter part of the
setUpClass
method correctly sets up JIT compilation for the module and defines the expected values for message passing, dimension parameters, and selection type. This implementation is consistent with other test classes and provides a solid foundation for the tests.
843-854
: Resolved: Previously unused variables are now utilized.The variables
dp_model1
anddp_model2
, which were flagged as unused in past reviews, are now correctly utilized in creating theDPLinearModel
. This resolves the previous issue and improves the overall code quality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (1)
source/tests/pt/model/test_permutation.py (1)
Line range hint
247-251
: Consider Adding Explicit Spin-Related Assertions in TestsWhile the spin parameters are included in the test setup, consider adding specific test methods or assertions that validate spin-related outputs. This will ensure comprehensive coverage of spin functionalities and confirm that spin interactions are correctly handled by the model.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- source/tests/pt/model/test_permutation.py (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
source/tests/pt/model/test_permutation.py (2)
Line range hint
101-137
: 'model_spin' Configuration Added CorrectlyThe
model_spin
configuration is appropriately defined, extending the existing model configurations to include spin parameters. This addition enhances the testing capabilities for spin functionalities and aligns with the codebase's structure.
Line range hint
247-251
: Test ClassTestEnergyModelSpinSeA
Properly Implements Spin TestingThe test class
TestEnergyModelSpinSeA
correctly initializes the model with spin parameters and setsself.test_spin = True
to enable spin-specific testing within thePermutationTest
framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add a linear model example using two dp atomic model?
Summary by CodeRabbit
New Features
LinearEnergyModel
class for advanced energy and force calculations.Bug Fixes