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(pt): fix not used sys_probs #4353

Merged
merged 1 commit into from
Nov 13, 2024
Merged

Conversation

iProzd
Copy link
Collaborator

@iProzd iProzd commented Nov 13, 2024

sys_probs was not used in pt, because its priority was lower than that of auto_prob, while auto_prob always has its default values.
See #4346 (reply in thread) .

Summary by CodeRabbit

  • New Features
    • Introduced a new sampler selection function for improved data loading flexibility.
  • Bug Fixes
    • Streamlined logic for obtaining data samplers, enhancing maintainability.
  • Tests
    • Added end-to-end tests for sampler functionality, ensuring accuracy with system probabilities and automatic styles.

Copy link
Contributor

coderabbitai bot commented Nov 13, 2024

📝 Walkthrough

Walkthrough

The changes involve refactoring the data sampling logic in the training.py and dataloader.py files within the deepmd module. The function get_weighted_sampler has been renamed to get_sampler_from_params, which simplifies how samplers are created based on training parameters. The get_data_loader function now directly utilizes this new function. Additionally, two new test methods have been added to the TestSampler class to validate the functionality of the sampler under different parameter conditions.

Changes

File Path Change Summary
deepmd/pt/train/training.py Renamed get_weighted_sampler to get_sampler_from_params; simplified sampler logic in get_data_loader; adjusted parameters for get_dataloader_and_buffer.
deepmd/pt/utils/dataloader.py Added get_sampler_from_params function to determine sampler based on _params; maintains existing functionality.
source/tests/pt/test_sampler.py Added test_sys_probs_end2end and test_auto_prob_sys_size_ext_end2end methods to TestSampler class; imported get_sampler_from_params.

Sequence Diagram(s)

sequenceDiagram
    participant Trainer
    participant DataLoader
    participant Sampler

    Trainer->>DataLoader: get_data_loader()
    DataLoader->>Sampler: get_sampler_from_params(training_params)
    Sampler-->>DataLoader: return sampler
    DataLoader-->>Trainer: return dataloader
Loading

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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
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: 2

🧹 Outside diff range and nitpick comments (4)
source/tests/pt/test_sampler.py (2)

17-17: Consider documenting deprecation of get_weighted_sampler

Since both get_sampler_from_params and get_weighted_sampler are imported, and the new tests use the former, it appears that get_weighted_sampler might be deprecated. Consider adding a deprecation notice if that's the case.


109-119: LGTM! Consider enhancing the test documentation

The test effectively verifies that sys_probs takes precedence when both sys_probs and auto_prob are provided, which aligns with fixing the "not used sys_probs" issue mentioned in the PR title.

Consider adding a docstring to better document the test's purpose:

 def test_sys_probs_end2end(self):
+    """
+    Verifies that sys_probs takes precedence over auto_prob when both are provided,
+    ensuring that manually specified system probabilities are not ignored.
+    """
     sys_probs = [0.1, 0.4, 0.5]
deepmd/pt/utils/dataloader.py (2)

311-324: Add documentation and type hints for better code clarity.

The function logic looks good, but could benefit from improved documentation and type safety:

Consider applying these improvements:

-def get_sampler_from_params(_data, _params):
+def get_sampler_from_params(_data: DpLoaderSet, _params: dict) -> WeightedRandomSampler:
+    """Create a weighted sampler based on provided parameters.
+
+    Parameters
+    ----------
+    _data : DpLoaderSet
+        The dataset for which to create the sampler
+    _params : dict
+        Dictionary containing sampling parameters:
+        - sys_probs: Optional[str] - System probabilities string
+        - auto_prob: Optional[str] - Automatic probability configuration
+
+    Returns
+    -------
+    WeightedRandomSampler
+        The configured sampler instance
+    """
     if (
         "sys_probs" in _params and _params["sys_probs"] is not None
     ):  # use sys_probs first

309-310: Remove extra blank line.

Keep only one blank line between function definitions for consistent spacing.

 def get_weighted_sampler(training_data, prob_style, sys_prob=False):
     # ... existing implementation ...
-

 def get_sampler_from_params(_data, _params):
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 85e5e20 and 302cdf2.

📒 Files selected for processing (3)
  • deepmd/pt/train/training.py (3 hunks)
  • deepmd/pt/utils/dataloader.py (1 hunks)
  • source/tests/pt/test_sampler.py (2 hunks)
🔇 Additional comments (3)
deepmd/pt/train/training.py (3)

50-50: LGTM: Import statement updated correctly.

The import statement has been updated to use the renamed function get_sampler_from_params, which aligns with the refactoring changes.


163-163: LGTM: Simplified sampler creation logic.

The code now uses the new get_sampler_from_params function with appropriate warning for missing sampler cases.


184-184: Verify parameter structure assumptions.

The code now accesses specific data subsets using "training_data" and "validation_data" keys. While this improves clarity, we should verify that these keys are guaranteed to exist in the parameters.

Also applies to: 191-193

✅ Verification successful

Parameter structure validation is confirmed.

The codebase has robust validation for both parameters:

  • "training_data" is a required parameter with strict validation of its structure
  • "validation_data" is an optional parameter (can be None) with equivalent validation when present

The parameter structure assumptions are safe and guaranteed by the built-in argument validation system.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the parameter structure in training configuration files
# Look for training parameter definitions to confirm the expected structure

# Search for training parameter definitions
rg -A 10 "training_data.*=|training_data.*:" 

# Search for validation parameter definitions
rg -A 10 "validation_data.*=|validation_data.*:"

Length of output: 403111

source/tests/pt/test_sampler.py Show resolved Hide resolved
deepmd/pt/utils/dataloader.py Show resolved Hide resolved
@njzjz njzjz added this pull request to the merge queue Nov 13, 2024
Merged via the queue into deepmodeling:devel with commit a83c98d Nov 13, 2024
51 checks passed
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.

4 participants