-
Notifications
You must be signed in to change notification settings - Fork 575
feat(tf): add support for stat_file parameter #4926
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
Draft
Copilot
wants to merge
17
commits into
devel
Choose a base branch
from
copilot/fix-4017
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
610c6fa
Initial plan
Copilot 932223d
Changes before error encountered
Copilot 8e731c8
fix: address PR feedback - remove test files and move h5py import
Copilot 2cb3163
fix: remove unnecessary try/except around h5py import
Copilot a878838
fix: move imports to top-level and remove try/except from tests
Copilot 69dbf52
fix: move data_stat_nbatch to model section in integration test
Copilot c60793c
Potential fix for code scanning alert no. 9893: Unused local variable
njzjz 995a1d6
feat: support stat_file parameter in TensorFlow training
Copilot ee06a1c
fix: remove temp files and revert third-party changes
Copilot 03a4754
fix: correct natoms_vec handling in TensorFlow stat computation
Copilot 17b7a9a
fix: ensure TensorFlow and PyTorch stat file consistency
Copilot 1e4deb2
fix: move imports to top-level and add cross-backend stat file consis…
Copilot 5864cee
refactor: simplify stat file test to basic consistency check only
Copilot 249367c
fix: resolve CI test failure for stat file consistency test
Copilot e8fd06a
fix: revert unintended formatting changes to 3rdparty file
Copilot 7efbdf9
Changes before error encountered
Copilot c51189a
fix: remove temporary checkpoint and training files
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| import logging | ||
| from typing import ( | ||
| Optional, | ||
| ) | ||
|
|
||
| import numpy as np | ||
|
|
||
| from deepmd.utils.path import ( | ||
| DPPath, | ||
| ) | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _restore_from_file( | ||
| stat_file_path: DPPath, | ||
| keys: list[str] = ["energy"], | ||
| ) -> Optional[tuple[dict, dict]]: | ||
| """Restore bias and std from stat file. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| stat_file_path : DPPath | ||
| Path to the stat file directory/file | ||
| keys : list[str] | ||
| Keys to restore statistics for | ||
|
|
||
| Returns | ||
| ------- | ||
| ret_bias : dict or None | ||
| Bias values for each key | ||
| ret_std : dict or None | ||
| Standard deviation values for each key | ||
| """ | ||
| if stat_file_path is None: | ||
| return None, None | ||
| stat_files = [stat_file_path / f"bias_atom_{kk}" for kk in keys] | ||
| if all(not (ii.is_file()) for ii in stat_files): | ||
| return None, None | ||
| stat_files = [stat_file_path / f"std_atom_{kk}" for kk in keys] | ||
| if all(not (ii.is_file()) for ii in stat_files): | ||
| return None, None | ||
|
|
||
| ret_bias = {} | ||
| ret_std = {} | ||
| for kk in keys: | ||
| fp = stat_file_path / f"bias_atom_{kk}" | ||
| # only read the key that exists | ||
| if fp.is_file(): | ||
| ret_bias[kk] = fp.load_numpy() | ||
| for kk in keys: | ||
| fp = stat_file_path / f"std_atom_{kk}" | ||
| # only read the key that exists | ||
| if fp.is_file(): | ||
| ret_std[kk] = fp.load_numpy() | ||
| return ret_bias, ret_std | ||
|
|
||
|
|
||
| def _save_to_file( | ||
| stat_file_path: DPPath, | ||
| bias_out: dict, | ||
| std_out: dict, | ||
| ) -> None: | ||
| """Save bias and std to stat file. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| stat_file_path : DPPath | ||
| Path to the stat file directory/file | ||
| bias_out : dict | ||
| Bias values for each key | ||
| std_out : dict | ||
| Standard deviation values for each key | ||
| """ | ||
| assert stat_file_path is not None | ||
| stat_file_path.mkdir(exist_ok=True, parents=True) | ||
| for kk, vv in bias_out.items(): | ||
| fp = stat_file_path / f"bias_atom_{kk}" | ||
| fp.save_numpy(vv) | ||
| for kk, vv in std_out.items(): | ||
| fp = stat_file_path / f"std_atom_{kk}" | ||
| fp.save_numpy(vv) | ||
|
|
||
|
|
||
| def compute_output_stats( | ||
| all_stat: dict, | ||
| ntypes: int, | ||
| keys: list[str] = ["energy"], | ||
| stat_file_path: Optional[DPPath] = None, | ||
| rcond: Optional[float] = None, | ||
| mixed_type: bool = False, | ||
| ) -> tuple[dict, dict]: | ||
| """Compute output statistics for TensorFlow models. | ||
|
|
||
| This is a simplified version of the PyTorch compute_output_stats function | ||
| adapted for TensorFlow models. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| all_stat : dict | ||
| Dictionary containing statistical data | ||
| ntypes : int | ||
| Number of atom types | ||
| keys : list[str] | ||
| Keys to compute statistics for | ||
| stat_file_path : DPPath, optional | ||
| Path to save/load statistics | ||
| rcond : float, optional | ||
| Condition number for regression | ||
| mixed_type : bool | ||
| Whether mixed type format is used | ||
|
|
||
| Returns | ||
| ------- | ||
| bias_out : dict | ||
| Computed bias values | ||
| std_out : dict | ||
| Computed standard deviation values | ||
| """ | ||
| # Try to restore from file first | ||
| bias_out, std_out = _restore_from_file(stat_file_path, keys) | ||
|
|
||
| if bias_out is not None and std_out is not None: | ||
| log.info("Successfully restored statistics from stat file") | ||
| return bias_out, std_out | ||
|
|
||
| # If restore failed, compute from data | ||
| log.info("Computing statistics from training data") | ||
|
|
||
| from deepmd.utils.out_stat import ( | ||
| compute_stats_from_redu, | ||
| ) | ||
|
|
||
| bias_out = {} | ||
| std_out = {} | ||
|
|
||
| for key in keys: | ||
| if key in all_stat: | ||
| # Get energy and natoms data | ||
| energy_data = np.concatenate(all_stat[key]) | ||
| natoms_data = np.concatenate(all_stat["natoms_vec"])[ | ||
| :, 2: | ||
| ] # Skip first 2 elements | ||
|
|
||
| # Compute statistics using existing utility | ||
| bias, std = compute_stats_from_redu( | ||
| energy_data.reshape(-1, 1), # Reshape to column vector | ||
| natoms_data, | ||
| rcond=rcond, | ||
| ) | ||
|
|
||
| bias_out[key] = bias.reshape(-1) # Flatten to 1D | ||
| std_out[key] = std.reshape(-1) # Flatten to 1D | ||
|
|
||
| log.info( | ||
| f"Statistics computed for {key}: bias shape {bias_out[key].shape}, std shape {std_out[key].shape}" | ||
| ) | ||
|
|
||
| # Save to file if path provided | ||
| if stat_file_path is not None and bias_out: | ||
| _save_to_file(stat_file_path, bias_out, std_out) | ||
| log.info("Statistics saved to stat file") | ||
|
|
||
| return bias_out, std_out | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot imports should be put on the top of the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the import to the top-level imports section. Fixed in 1e4deb2.