Skip to content
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

fix(test): make unit tests deterministic #3856

Merged
merged 8 commits into from
Jun 7, 2024

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Jun 4, 2024

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

  • Tests
    • Introduced a global seed for consistent random number generation across various test files.
    • Updated the initialization of random tensors to include a generator parameter seeded with the global seed.
    • Adjusted epsilon values and setup methods in specific test cases for accuracy and consistency.

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

Signed-off-by: Jinzhe Zeng <[email protected]>
@njzjz njzjz linked an issue Jun 4, 2024 that may be closed by this pull request
@github-actions github-actions bot added the Python label Jun 4, 2024
Copy link
Contributor

coderabbitai bot commented Jun 4, 2024

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 30b6f0f and c03ec57.

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
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Jun 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@njzjz
Copy link
Member Author

njzjz commented Jun 4, 2024

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
Copy link
Member Author

njzjz commented Jun 4, 2024

fyi @iProzd

Copy link
Collaborator

@wanghan-iapcm wanghan-iapcm left a comment

Choose a reason for hiding this comment

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

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.

@wanghan-iapcm
Copy link
Collaborator

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
Copy link
Collaborator

anyangml commented Jun 5, 2024

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
Copy link
Member Author

njzjz commented Jun 5, 2024

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 added 3 commits June 6, 2024 02:46
Signed-off-by: Jinzhe Zeng <[email protected]>
Signed-off-by: Jinzhe Zeng <[email protected]>
Signed-off-by: Jinzhe Zeng <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 6

source/tests/pt/model/test_smooth_denoise.py Outdated Show resolved Hide resolved
source/tests/pt/model/test_trans_denoise.py Outdated Show resolved Hide resolved
source/tests/common/dpmodel/test_env_mat.py Show resolved Hide resolved
source/tests/common/dpmodel/test_region.py Show resolved Hide resolved
source/tests/consistent/test_activation.py Show resolved Hide resolved
@njzjz
Copy link
Member Author

njzjz commented Jun 6, 2024

we may decrease the epsilon to reduce the difference..

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

njzjz and others added 4 commits June 6, 2024 03:46
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Jinzhe Zeng <[email protected]>
Signed-off-by: Jinzhe Zeng <[email protected]>
Signed-off-by: Jinzhe Zeng <[email protected]>
This reverts commit d25a290.
@njzjz
Copy link
Member Author

njzjz commented Jun 6, 2024

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

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

@njzjz njzjz requested a review from wanghan-iapcm June 6, 2024 21:36
@wanghan-iapcm wanghan-iapcm added this pull request to the merge queue Jun 7, 2024
Merged via the queue into deepmodeling:devel with commit 674bad7 Jun 7, 2024
60 checks passed
mtaillefumier pushed a commit to mtaillefumier/deepmd-kit that referenced this pull request Sep 18, 2024
Fix deepmodeling#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

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Tests**
- Introduced a global seed for consistent random number generation
across various test files.
- Updated the initialization of random tensors to include a generator
parameter seeded with the global seed.
- Adjusted epsilon values and setup methods in specific test cases for
accuracy and consistency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jinzhe Zeng <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] recent occasional UT failure bug
3 participants