deepmodeling / deepmd-kit

A deep learning package for many-body potential energy representation and molecular dynamics
https://docs.deepmodeling.com/projects/deepmd/
GNU Lesser General Public License v3.0
1.46k stars 504 forks source link

fix(test): make unit tests deterministic #3856

Closed njzjz closed 3 months ago

njzjz commented 4 months ago

Fix #3847. This PR makes all unit tests deterministic. We have found non-deterministic unit tests have more disadvantages than advantages. Read more: https://softwareengineering.stackexchange.com/questions/221632/testing-deterministic-or-non-deterministic

Summary by CodeRabbit

coderabbitai[bot] commented 4 months ago
Walkthrough ## Walkthrough The recent changes in the test files primarily involve introducing a global seed for `torch.Generator` to ensure consistent and reproducible random number generation across various test cases. This modification addresses occasional unit test failures by standardizing the initialization of random tensors. ## Changes | File Path | Change Summary | |---------------------------------------------------|-------------------------------------------------------------------------------| | `source/tests/pt/model/test_null_input.py` | Introduced global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_permutation.py` | Added global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_permutation_denoise.py`| Added global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_region.py` | Introduced global seed for `torch.Generator` in specific test methods. | | `source/tests/pt/model/test_rot.py` | Added global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_rot_denoise.py` | Added global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_smooth.py` | Introduced global seed for `torch.Generator`, adjusted epsilon values. | | `source/tests/pt/model/test_smooth_denoise.py` | Added global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_trans.py` | Introduced global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_trans_denoise.py` | Added global seed for `torch.Generator` and updated `torch.rand` calls. | | `source/tests/pt/model/test_unused_params.py` | Introduced global seed for `torch.Generator` and updated `torch.rand` calls. | ## Assessment against linked issues | Objective | Addressed | Explanation | |---------------------------------------------------------------------------|-----------|------------------------------------------------------| | Ensure consistent random number generation across tests (#3847) | ✅ | | | Address occasional unit test failures due to random number inconsistencies (#3847) | ✅ | |

Recent review details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between 30b6f0f718335916e5eb7303434f17ec6e632bce and c03ec57349792c427d445165974311a861264b47.
Files selected for processing (16) * source/tests/pt/model/test_dipole_fitting.py (5 hunks) * source/tests/pt/model/test_ener_spin_model.py (2 hunks) * source/tests/pt/model/test_forward_lower.py (2 hunks) * source/tests/pt/model/test_linear_atomic_model.py (2 hunks) * source/tests/pt/model/test_make_hessian_model.py (3 hunks) * source/tests/pt/model/test_null_input.py (3 hunks) * source/tests/pt/model/test_permutation.py (2 hunks) * source/tests/pt/model/test_permutation_denoise.py (2 hunks) * source/tests/pt/model/test_region.py (3 hunks) * source/tests/pt/model/test_rot.py (3 hunks) * source/tests/pt/model/test_rot_denoise.py (3 hunks) * source/tests/pt/model/test_smooth.py (4 hunks) * source/tests/pt/model/test_smooth_denoise.py (3 hunks) * source/tests/pt/model/test_trans.py (2 hunks) * source/tests/pt/model/test_trans_denoise.py (2 hunks) * source/tests/pt/model/test_unused_params.py (2 hunks)
Files skipped from review as they are similar to previous changes (15) * source/tests/pt/model/test_dipole_fitting.py * source/tests/pt/model/test_ener_spin_model.py * source/tests/pt/model/test_forward_lower.py * source/tests/pt/model/test_make_hessian_model.py * source/tests/pt/model/test_null_input.py * source/tests/pt/model/test_permutation.py * source/tests/pt/model/test_permutation_denoise.py * source/tests/pt/model/test_region.py * source/tests/pt/model/test_rot.py * source/tests/pt/model/test_rot_denoise.py * source/tests/pt/model/test_smooth.py * source/tests/pt/model/test_smooth_denoise.py * source/tests/pt/model/test_trans.py * source/tests/pt/model/test_trans_denoise.py * source/tests/pt/model/test_unused_params.py
Additional comments not posted (2)
source/tests/pt/model/test_linear_atomic_model.py (2)
`35-37`: Introduce a global seed for deterministic tests. This change is crucial for achieving deterministic behavior in tests, which aligns with the PR's objective to address non-deterministic unit tests. --- `197-198`: Use of global seed in random number generation. Proper use of the `GLOBAL_SEED` to ensure that the random number generation is consistent across test runs. This is a good practice for maintaining the reproducibility of tests.
--- 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?
Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` 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 as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 82.66%. Comparing base (eb474d4) to head (c03ec57). Report is 120 commits behind head on devel.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## devel #3856 +/- ## ========================================== - Coverage 82.67% 82.66% -0.01% ========================================== Files 515 517 +2 Lines 49549 49724 +175 Branches 2989 2989 ========================================== + Hits 40965 41105 +140 - Misses 7673 7709 +36 + Partials 911 910 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

njzjz commented 4 months ago

Several tests in test_smooth failed using today as the seed...

FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA1::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 2 / 30 (6.7%)
Greatest absolute difference: 4.697115381646633e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 7.770803885056612e-05 at index (2, 2) (up to 0 allowed)

FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA1Excl12::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 2 / 30 (6.7%)
Greatest absolute difference: 3.200459405494627e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 5.073069526107276e-05 at index (4, 2) (up to 0 allowed)
FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA2_2::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 4 / 30 (13.3%)
Greatest absolute difference: 5.451339182582782e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 5.638892512663301e-05 at index (4, 2) (up to 0 allowed)

FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA1Excl1::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 2 / 30 (6.7%)
Greatest absolute difference: 7.867066788103116e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 0.00022819964422492295 at index (2, 2) (up to 0 allowed)
njzjz commented 4 months ago

fyi @iProzd

wanghan-iapcm commented 4 months ago

Several tests in test_smooth failed using today as the seed...

FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA1::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 2 / 30 (6.7%)
Greatest absolute difference: 4.697115381646633e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 7.770803885056612e-05 at index (2, 2) (up to 0 allowed)

FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA1Excl12::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 2 / 30 (6.7%)
Greatest absolute difference: 3.200459405494627e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 5.073069526107276e-05 at index (4, 2) (up to 0 allowed)
FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA2_2::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 4 / 30 (13.3%)
Greatest absolute difference: 5.451339182582782e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 5.638892512663301e-05 at index (4, 2) (up to 0 allowed)

FAILED source/tests/pt/model/test_smooth.py::TestEnergyModelDPA1Excl1::test - AssertionError: Tensor-likes are not close!

Mismatched elements: 2 / 30 (6.7%)
Greatest absolute difference: 7.867066788103116e-05 at index (4, 2) (up to 1e-05 allowed)
Greatest relative difference: 0.00022819964422492295 at index (2, 2) (up to 0 allowed)

we may decrease the epsilon to reduce the difference..

anyangml commented 4 months ago

shall we provide a global rand seed for all the tests?

so if one wants to change the seed he/she needs to change only one line, rather than replace all the 20240604 strings across the code.

we should parameterize the seed, also make it dynamic by pulling the current date if possible. That way, it's reproducible and covers more test cases.

njzjz commented 4 months ago

make it dynamic by pulling the current date if possible

This will make the tests fail on random days, which should not be used in the automatic CI environment.

njzjz commented 3 months ago

we may decrease the epsilon to reduce the difference..

I changed from 1e-5 to 1e-6 but seems not working.

njzjz commented 3 months ago

Several tests in test_smooth failed using today as the seed...

I just realized that I forgot to set generator for torch.randint.