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(tf): set visible_device_list for TF C++ #4172

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Sep 30, 2024

Fix #4171.

Summary by CodeRabbit

  • New Features

    • Enhanced GPU selection logic for improved resource management.
    • Added support for single-frame and multi-frame computations with new parameters for atom energy and virial calculations.
    • Extended functionality for mixed-type computations in the model.
  • Bug Fixes

    • Improved error handling during initialization and model execution.
    • Added output tensor dimension validations to ensure expected structures are maintained.
  • Documentation

    • Clarified output tensor validation to ensure expected dimensions are maintained.

Copy link
Contributor

coderabbitai bot commented Sep 30, 2024

📝 Walkthrough

Walkthrough

The changes primarily involve modifications to the GPU configuration and model execution logic across several files. The DipoleChargeModifierTF class now consistently uses the first GPU, while the DeepPotTF and DeepTensorTF classes have introduced new overloads for the run_model function to support additional parameters for atom energy and virial calculations. Improvements in error handling and output validation have also been implemented to enhance overall robustness.

Changes

Files Change Summary
source/api_cc/src/DataModifierTF.cc Simplified GPU device selection to always use "/gpu:0" and adjusted visible device list logic.
source/api_cc/src/DeepPotTF.cc Added new overloads for run_model to support atom energy and virial calculations; modified existing signatures.
source/api_cc/src/DeepTensorTF.cc Enhanced initialization and model execution logic; updated run_model and compute methods for better tensor output handling.

Assessment against linked issues

Objective Addressed Explanation
VRAM is wasted when running Lammps with multiple GPUs (#[4171])
Ensure GPU device selection does not create devices for all GPUs (#[4171])

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.

@njzjz njzjz linked an issue Sep 30, 2024 that may be closed by this pull request
@github-actions github-actions bot added the C++ label Sep 30, 2024
@njzjz
Copy link
Member Author

njzjz commented Sep 30, 2024

Need to test manually before megering.

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: 5

🧹 Outside diff range and nitpick comments (1)
source/api_cc/src/DataModifierTF.cc (1)

53-54: Use stable references in comments

The comment links to a specific commit in the TensorFlow repository. Linking to a specific commit may become outdated as the codebase evolves. Consider linking to a stable reference or the latest documentation to ensure the link remains valid over time.

Suggestion

Update the link to point to the master branch or official documentation:

- // https://github.com/tensorflow/tensorflow/blame/8fac27b486939f40bc8e362b94a16a4a8bb51869/tensorflow/core/protobuf/config.proto#L80
+ // https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto#L80

Alternatively, reference the TensorFlow GPU configuration guide available in the official documentation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ad8bebe and d3944bb.

📒 Files selected for processing (3)
  • source/api_cc/src/DataModifierTF.cc (1 hunks)
  • source/api_cc/src/DeepPotTF.cc (1 hunks)
  • source/api_cc/src/DeepTensorTF.cc (1 hunks)

source/api_cc/src/DataModifierTF.cc Outdated Show resolved Hide resolved
source/api_cc/src/DataModifierTF.cc Outdated Show resolved Hide resolved
source/api_cc/src/DeepTensorTF.cc Outdated Show resolved Hide resolved
source/api_cc/src/DeepPotTF.cc Show resolved Hide resolved
source/api_cc/src/DeepPotTF.cc Outdated Show resolved Hide resolved
Signed-off-by: Jinzhe Zeng <[email protected]>
Signed-off-by: Jinzhe Zeng <[email protected]>
Copy link

codecov bot commented Oct 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.46%. Comparing base (ad8bebe) to head (cf5db50).

Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #4172   +/-   ##
=======================================
  Coverage   83.46%   83.46%           
=======================================
  Files         537      537           
  Lines       52168    52168           
  Branches     3046     3046           
=======================================
  Hits        43543    43543           
  Misses       7678     7678           
  Partials      947      947           

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

@Entropy-Enthalpy
Copy link

Should I assist with the test?

@njzjz
Copy link
Member Author

njzjz commented Oct 1, 2024

Should I assist with the test?

Yes, please.

@Entropy-Enthalpy
Copy link

Should I assist with the test?

Yes, please.

I've just done the testing, the issue has been resolved! Now I'm looking forward to merging!

@njzjz njzjz marked this pull request as ready for review October 2, 2024 01:38
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: 0

🧹 Outside diff range and nitpick comments (2)
source/api_cc/src/DeepTensorTF.cc (2)

Line range hint 639-680: Approved: Enhanced computew methods with optimization potential

The new computew method overloads are a valuable addition:

  1. They provide support for ghost atoms and neighbor lists, increasing the flexibility of the framework.
  2. The conditional execution based on request_deriv can improve performance by avoiding unnecessary computations.

These changes align well with the goal of improving the framework's efficiency and flexibility.

Consider extracting the common logic in these methods to reduce code duplication. For example:

template <typename VALUETYPE>
void DeepTensorTF::computew(std::vector<VALUETYPE> &global_tensor,
                            std::vector<VALUETYPE> &force,
                            std::vector<VALUETYPE> &virial,
                            std::vector<VALUETYPE> &atom_tensor,
                            std::vector<VALUETYPE> &atom_virial,
                            const std::vector<VALUETYPE> &coord,
                            const std::vector<int> &atype,
                            const std::vector<VALUETYPE> &box,
                            const int nghost,
                            const InputNlist &inlist,
                            const bool request_deriv) {
  if (request_deriv) {
    compute(global_tensor, force, virial, atom_tensor, atom_virial, coord,
            atype, box, nghost, inlist);
  } else {
    compute(global_tensor, coord, atype, box, nghost, inlist);
    force.clear();
    virial.clear();
    atom_tensor.clear();
    atom_virial.clear();
  }
}

Then, specialize this template for both double and float types, and use it for both the new and existing computew methods.


Line range hint 1-680: Approved: Robust structure with room for documentation improvement

The overall structure of the DeepTensorTF class is well-organized and consistent:

  1. Template specializations for various compute methods provide type-specific optimizations.
  2. Error handling improvements, such as in the constructor, enhance the code's robustness.
  3. The consistent structure across methods makes the code easier to maintain and understand.

Consider adding more inline documentation, especially for complex methods like compute_inner and run_model. This would improve code readability and make it easier for other developers to understand and maintain the code. For example:

/**
 * @brief Computes the inner tensor operations.
 * @param dtensor_ Output tensor
 * @param dcoord_ Input coordinates
 * @param datype_ Atom types
 * @param dbox Box dimensions
 * @param nghost Number of ghost atoms
 * @param nlist_ Neighbor list
 */
template <typename VALUETYPE>
void DeepTensorTF::compute_inner(std::vector<VALUETYPE> &dtensor_,
                                 const std::vector<VALUETYPE> &dcoord_,
                                 const std::vector<int> &datype_,
                                 const std::vector<VALUETYPE> &dbox,
                                 const int nghost,
                                 const InputNlist &nlist_) {
    // ... existing implementation ...
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between da2e7ec and cf5db50.

📒 Files selected for processing (3)
  • source/api_cc/src/DataModifierTF.cc (1 hunks)
  • source/api_cc/src/DeepPotTF.cc (1 hunks)
  • source/api_cc/src/DeepTensorTF.cc (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • source/api_cc/src/DataModifierTF.cc
🔇 Additional comments (3)
source/api_cc/src/DeepTensorTF.cc (1)

49-53: Improved GPU device selection logic

The changes in the GPU device selection are a significant improvement:

  1. Replacing the hardcoded "/gpu:0" with a dynamic selection based on gpu_rank % gpu_num allows for better utilization of multiple GPUs.
  2. Setting the visible_device_list ensures that each process only sees and uses its assigned GPU.

These modifications directly address the VRAM waste issue described in #4171 by preventing each MPI rank from consuming VRAM on all GPUs.

source/api_cc/src/DeepPotTF.cc (2)

451-452: Clarify the comment for better readability

The existing comment lacks context and may not provide sufficient information without following the link. Consider adding a brief description to enhance clarity and maintainability.


450-454: Ensure consistency between default device and visible_device_list

The variable str is set to "/gpu:0", while visible_device_list uses gpu_rank % gpu_num. This mismatch could lead to inconsistencies where the default device does not match the visible devices, potentially causing unexpected behavior.

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] VRAM is wasted when running Lammps with multiple GPUs
3 participants