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): prevent fitting_attr variable scope from becoming fitting_attr_1 #3930

Merged
merged 5 commits into from
Jul 2, 2024

Conversation

njzjz
Copy link
Member

@njzjz njzjz commented Jun 29, 2024

Fix #3928. Prevent fitting_attr from becoming fitting_attr_1.

Summary by CodeRabbit

  • Refactor
    • Improved TensorFlow variable scope management by switching to tf.AUTO_REUSE to streamline code and reduce the likelihood of variable reuse conflicts.

Fix deepmodeling#3928. Prevent `fitting_attr` from becoming `fitting_attr_1`.

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
@njzjz njzjz added the bug label Jun 29, 2024
@njzjz njzjz added this to the v2.2.11 milestone Jun 29, 2024
@njzjz njzjz requested a review from wanghan-iapcm June 29, 2024 05:11
@njzjz njzjz linked an issue Jun 29, 2024 that may be closed by this pull request
Copy link
Contributor

coderabbitai bot commented Jun 29, 2024

Walkthrough

The recent changes focus on modifying the reuse parameter in TensorFlow's tf.variable_scope function calls throughout multiple files in the deepmd module. Specifically, the parameter is set from a variable to tf.AUTO_REUSE for consistency and to potentially avoid errors related to variable reuse.

Changes

Files Modified Change Summary
deepmd/tf/descriptor/se_a_mask.py Changed reuse parameter in tf.variable_scope to tf.AUTO_REUSE.
deepmd/tf/fit/dos.py Changed reuse parameter in tf.variable_scope to tf.AUTO_REUSE.
deepmd/tf/fit/ener.py Changed reuse parameter in tf.variable_scope from a variable to tf.AUTO_REUSE.

Sequence Diagram(s)

[Not applicable: Changes involve simple substitutions in parameter values for existing function calls and do not modify control flow or functionalities substantially.]

Assessment against linked issues

Objective Addressed Explanation
Fix error when loading a model with the se_a_mask descriptor (#3928)

Tip

Early access features
  • OpenAI gpt-4o model for reviews and chat.

Note:

  • You can disable early access features from the CodeRabbit UI or by setting early_access: false in the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues.
  • OSS projects are always opted into early access features.

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.

@njzjz njzjz marked this pull request as draft June 29, 2024 05:14
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: 3

Outside diff range and nitpick comments (10)
deepmd/tf/descriptor/se_a_mask.py (5)

Line range hint 124-124: Avoid using mutable data structures for argument defaults.

Replace the default value of exclude_types with None and initialize it within the function to avoid potential issues with mutable default arguments.

exclude_types: Optional[List[List[int]]] = None,

And initialize within the function:

if exclude_types is None:
    exclude_types = []
Tools
Ruff

315-315: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 129-129: Avoid using mutable data structures for argument defaults.

Replace the default value of layer_name with None and initialize it within the function to avoid potential issues with mutable default arguments.

layer_name: Optional[List[Optional[str]]] = None,

And initialize within the function:

if layer_name is None:
    layer_name = []
Tools
Ruff

315-315: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 185-188: Remove unused local variables.

The variables avg_zero and std_ones are assigned but never used.

- avg_zero = np.zeros([self.ntypes, self.ndescrpt]).astype(GLOBAL_NP_FLOAT_PRECISION)
- std_ones = np.ones([self.ntypes, self.ndescrpt]).astype(GLOBAL_NP_FLOAT_PRECISION)
Tools
Ruff

315-315: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 225-225: Add explicit stacklevel keyword argument to warnings.warn.

To ensure the warning points to the correct location in the code, add the stacklevel argument.

warnings.warn("The cutoff radius is not used for this descriptor", stacklevel=2)
Tools
Ruff

315-315: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 324-331: Remove unused local variables.

The variables t_rcut, t_ntypes, t_ndescrpt, and t_sel are assigned but never used.

- t_rcut = tf.constant(self.rcut, name="rcut", dtype=GLOBAL_TF_FLOAT_PRECISION)
- t_ntypes = tf.constant(self.ntypes, name="ntypes", dtype=tf.int32)
- t_ndescrpt = tf.constant(self.ndescrpt, name="ndescrpt", dtype=tf.int32)
- t_sel = tf.constant(self.sel_a, name="sel", dtype=tf.int32)
Tools
Ruff

315-315: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)

deepmd/tf/fit/dos.py (4)

Line range hint 111-111: Avoid using mutable data structures for argument defaults.

Replace the default value of neuron with None and initialize it within the function to avoid potential issues with mutable default arguments.

neuron: Optional[List[int]] = None,

And initialize within the function:

if neuron is None:
    neuron = [120, 120, 120]
Tools
Ruff

443-443: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


444-444: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


445-445: Local variable t_numb_dos is assigned to but never used

Remove assignment to unused variable t_numb_dos

(F841)


Line range hint 325-328: Use ternary operator instead of if-else-block.

Replace the if-else block with a ternary operator for conciseness.

one_layer = one_layer_nvnmd if nvnmd_cfg.enable else one_layer_deepmd
Tools
Ruff

443-443: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


444-444: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


445-445: Local variable t_numb_dos is assigned to but never used

Remove assignment to unused variable t_numb_dos

(F841)


Line range hint 497-498: Combine nested if statements using and.

Combine the if statements for conciseness.

if self.layer_name is not None and self.layer_name[ii] is not None:
Tools
Ruff

443-443: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


444-444: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


445-445: Local variable t_numb_dos is assigned to but never used

Remove assignment to unused variable t_numb_dos

(F841)


Line range hint 623-629: Use contextlib.suppress instead of try-except-pass.

Use contextlib.suppress for cleaner exception handling.

from contextlib import suppress

with suppress(GraphWithoutTensorError):
    self.bias_dos = get_tensor_by_name_from_graph(
        graph, f"fitting_attr{suffix}/t_bias_dos"
    )
Tools
Ruff

443-443: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


444-444: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


445-445: Local variable t_numb_dos is assigned to but never used

Remove assignment to unused variable t_numb_dos

(F841)

deepmd/tf/fit/ener.py (1)

Line range hint 160-160: Avoid mutable data structures for argument defaults.

Replace mutable data structures with None and initialize within the function.

-        neuron: List[int] = [120, 120, 120],
-        atom_ener: List[float] = [],
+        neuron: Optional[List[int]] = None,
+        atom_ener: Optional[List[float]] = None,
        self.n_neuron = neuron if neuron is not None else [120, 120, 120]
        self.atom_ener_v = atom_ener if atom_ener is not None else []

Also applies to: 168-168

Tools
Ruff

537-537: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


541-541: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


542-542: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)

deepmd/tf/fit/ener.py Outdated Show resolved Hide resolved
@njzjz njzjz changed the title fix(pt): change fitting_attr variable scope reuse to AUTO_REUSE fix(pt): prevent fitting_attr variable scope from becoming fitting_attr_1 Jun 29, 2024
@njzjz njzjz marked this pull request as ready for review June 29, 2024 05:21
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: 3

Outside diff range and nitpick comments (5)
deepmd/tf/descriptor/se_a_mask.py (1)

Line range hint 325-332: Remove unused local variables.

The variables t_rcut, t_ntypes, t_ndescrpt, and t_sel are assigned but never used.

- t_rcut = tf.constant(
-     self.rcut,
-     name="rcut",
-     dtype=GLOBAL_TF_FLOAT_PRECISION,
- )
- t_ntypes = tf.constant(self.ntypes, name="ntypes", dtype=tf.int32)
- t_ndescrpt = tf.constant(self.ndescrpt, name="ndescrpt", dtype=tf.int32)
- t_sel = tf.constant(self.sel_a, name="sel", dtype=tf.int32)
Tools
Ruff

316-316: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)

deepmd/tf/fit/ener.py (4)

Line range hint 535-537: Simplify nested if statements.

Combine the nested if statements into a single if statement.

-            if "t_bias_atom_e" in nvnmd_cfg.weight.keys():
+            if "t_bias_atom_e" in nvnmd_cfg.weight:
Tools
Ruff

537-537: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


542-542: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


543-543: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


Line range hint 607-608: Simplify nested if statements.

Combine the nested if statements using and.

-        if (
-            nvnmd_cfg.enable
-            and nvnmd_cfg.quantize_descriptor
-            and nvnmd_cfg.restore_descriptor
-            and (nvnmd_cfg.version == 1)
-        ):
+        if nvnmd_cfg.enable and nvnmd_cfg.quantize_descriptor and nvnmd_cfg.restore_descriptor and (nvnmd_cfg.version == 1):
Tools
Ruff

537-537: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


542-542: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


543-543: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


Line range hint 398-401: Use ternary operator.

Replace the if-else block with a ternary operator.

-        if nvnmd_cfg.enable:
-            one_layer = one_layer_nvnmd
-        else:
-            one_layer = one_layer_deepmd
+        one_layer = one_layer_nvnmd if nvnmd_cfg.enable else one_layer_deepmd
Tools
Ruff

537-537: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


542-542: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


543-543: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)


Line range hint 1034-1037: Use ternary operator.

Replace the if-else block with a ternary operator.

-            if sys.pbc:
-                box = test_data["box"][:numb_test]
-            else:
-                box = None
+            box = test_data["box"][:numb_test] if sys.pbc else None
Tools
Ruff

537-537: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


542-542: Local variable t_dfparam is assigned to but never used

Remove assignment to unused variable t_dfparam

(F841)


543-543: Local variable t_daparam is assigned to but never used

Remove assignment to unused variable t_daparam

(F841)

deepmd/tf/fit/ener.py Outdated Show resolved Hide resolved
deepmd/tf/descriptor/se_a_mask.py Outdated Show resolved Hide resolved
deepmd/tf/fit/dos.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Jun 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.87%. Comparing base (20aeaf8) to head (70d4d4e).
Report is 113 commits behind head on devel.

Additional details and impacted files
@@            Coverage Diff             @@
##            devel    #3930      +/-   ##
==========================================
- Coverage   82.88%   82.87%   -0.01%     
==========================================
  Files         520      520              
  Lines       50679    50678       -1     
  Branches     3015     3015              
==========================================
- Hits        42003    42001       -2     
+ Misses       7740     7738       -2     
- Partials      936      939       +3     

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

@njzjz njzjz requested review from wanghan-iapcm and removed request for wanghan-iapcm June 29, 2024 05:37
@njzjz
Copy link
Member Author

njzjz commented Jun 29, 2024

It looks something goes wrong in the UT...

@njzjz njzjz marked this pull request as draft June 29, 2024 07:22
Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
@njzjz njzjz marked this pull request as ready for review June 29, 2024 08:12
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)
deepmd/tf/descriptor/se_a_mask.py (4)

Line range hint 325-325: Remove unused local variable.

The variable t_rcut is assigned but never used.

-            t_rcut = tf.constant(
-                self.rcut,
-                name="rcut",
-                dtype=GLOBAL_TF_FLOAT_PRECISION,
-            )
Tools
Ruff

314-314: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 330-330: Remove unused local variable.

The variable t_ntypes is assigned but never used.

-            t_ntypes = tf.constant(self.ntypes, name="ntypes", dtype=tf.int32)
Tools
Ruff

314-314: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 331-331: Remove unused local variable.

The variable t_ndescrpt is assigned but never used.

-            t_ndescrpt = tf.constant(self.ndescrpt, name="ndescrpt", dtype=tf.int32)
Tools
Ruff

314-314: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)


Line range hint 332-332: Remove unused local variable.

The variable t_sel is assigned but never used.

-            t_sel = tf.constant(self.sel_a, name="sel", dtype=tf.int32)
Tools
Ruff

314-314: Local variable t_aparam_nall is assigned to but never used

Remove assignment to unused variable t_aparam_nall

(F841)

deepmd/tf/descriptor/se_a_mask.py Show resolved Hide resolved
@@ -311,8 +311,9 @@
aparam[:, :] is the real/virtual sign for each atom.
"""
aparam = input_dict["aparam"]
with tf.variable_scope("fitting_attr" + suffix, reuse=reuse):
t_aparam_nall = tf.constant(True, name="aparam_nall", dtype=tf.bool)
t_aparam_nall = tf.constant(

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable t_aparam_nall is not used.
@njzjz njzjz changed the title fix(pt): prevent fitting_attr variable scope from becoming fitting_attr_1 fix(tf): prevent fitting_attr variable scope from becoming fitting_attr_1 Jul 1, 2024
@wanghan-iapcm wanghan-iapcm requested a review from iProzd July 1, 2024 01:35
@iProzd iProzd added this pull request to the merge queue Jul 2, 2024
Merged via the queue into deepmodeling:devel with commit e809e64 Jul 2, 2024
60 checks passed
njzjz added a commit to njzjz/deepmd-kit that referenced this pull request Jul 2, 2024
…_attr_1` (deepmodeling#3930)

Fix deepmodeling#3928. Prevent `fitting_attr` from becoming `fitting_attr_1`.

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

- **Refactor**
- Improved TensorFlow variable scope management by switching to
`tf.AUTO_REUSE` to streamline code and reduce the likelihood of variable
reuse conflicts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
(cherry picked from commit e809e64)
@njzjz njzjz mentioned this pull request Jul 2, 2024
njzjz added a commit that referenced this pull request Jul 3, 2024
…_attr_1` (#3930)

Fix #3928. Prevent `fitting_attr` from becoming `fitting_attr_1`.

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

- **Refactor**
- Improved TensorFlow variable scope management by switching to
`tf.AUTO_REUSE` to streamline code and reduce the likelihood of variable
reuse conflicts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
(cherry picked from commit e809e64)
mtaillefumier pushed a commit to mtaillefumier/deepmd-kit that referenced this pull request Sep 18, 2024
…_attr_1` (deepmodeling#3930)

Fix deepmodeling#3928. Prevent `fitting_attr` from becoming `fitting_attr_1`.

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

- **Refactor**
- Improved TensorFlow variable scope management by switching to
`tf.AUTO_REUSE` to streamline code and reduce the likelihood of variable
reuse conflicts.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Error when loading a model with the se_a_mask descriptor
3 participants