|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +import logging |
| 3 | +from typing import ( |
| 4 | + Optional, |
| 5 | +) |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from deepmd.utils.path import ( |
| 10 | + DPPath, |
| 11 | +) |
| 12 | + |
| 13 | +log = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def _restore_from_file( |
| 17 | + stat_file_path: DPPath, |
| 18 | + keys: list[str] = ["energy"], |
| 19 | +) -> Optional[tuple[dict, dict]]: |
| 20 | + """Restore bias and std from stat file. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + stat_file_path : DPPath |
| 25 | + Path to the stat file directory/file |
| 26 | + keys : list[str] |
| 27 | + Keys to restore statistics for |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + ret_bias : dict or None |
| 32 | + Bias values for each key |
| 33 | + ret_std : dict or None |
| 34 | + Standard deviation values for each key |
| 35 | + """ |
| 36 | + if stat_file_path is None: |
| 37 | + return None, None |
| 38 | + stat_files = [stat_file_path / f"bias_atom_{kk}" for kk in keys] |
| 39 | + if all(not (ii.is_file()) for ii in stat_files): |
| 40 | + return None, None |
| 41 | + stat_files = [stat_file_path / f"std_atom_{kk}" for kk in keys] |
| 42 | + if all(not (ii.is_file()) for ii in stat_files): |
| 43 | + return None, None |
| 44 | + |
| 45 | + ret_bias = {} |
| 46 | + ret_std = {} |
| 47 | + for kk in keys: |
| 48 | + fp = stat_file_path / f"bias_atom_{kk}" |
| 49 | + # only read the key that exists |
| 50 | + if fp.is_file(): |
| 51 | + ret_bias[kk] = fp.load_numpy() |
| 52 | + for kk in keys: |
| 53 | + fp = stat_file_path / f"std_atom_{kk}" |
| 54 | + # only read the key that exists |
| 55 | + if fp.is_file(): |
| 56 | + ret_std[kk] = fp.load_numpy() |
| 57 | + return ret_bias, ret_std |
| 58 | + |
| 59 | + |
| 60 | +def _save_to_file( |
| 61 | + stat_file_path: DPPath, |
| 62 | + bias_out: dict, |
| 63 | + std_out: dict, |
| 64 | +) -> None: |
| 65 | + """Save bias and std to stat file. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + stat_file_path : DPPath |
| 70 | + Path to the stat file directory/file |
| 71 | + bias_out : dict |
| 72 | + Bias values for each key |
| 73 | + std_out : dict |
| 74 | + Standard deviation values for each key |
| 75 | + """ |
| 76 | + assert stat_file_path is not None |
| 77 | + stat_file_path.mkdir(exist_ok=True, parents=True) |
| 78 | + for kk, vv in bias_out.items(): |
| 79 | + fp = stat_file_path / f"bias_atom_{kk}" |
| 80 | + fp.save_numpy(vv) |
| 81 | + for kk, vv in std_out.items(): |
| 82 | + fp = stat_file_path / f"std_atom_{kk}" |
| 83 | + fp.save_numpy(vv) |
| 84 | + |
| 85 | + |
| 86 | +def compute_output_stats( |
| 87 | + all_stat: dict, |
| 88 | + ntypes: int, |
| 89 | + keys: list[str] = ["energy"], |
| 90 | + stat_file_path: Optional[DPPath] = None, |
| 91 | + rcond: Optional[float] = None, |
| 92 | + mixed_type: bool = False, |
| 93 | +) -> tuple[dict, dict]: |
| 94 | + """Compute output statistics for TensorFlow models. |
| 95 | +
|
| 96 | + This is a simplified version of the PyTorch compute_output_stats function |
| 97 | + adapted for TensorFlow models. |
| 98 | +
|
| 99 | + Parameters |
| 100 | + ---------- |
| 101 | + all_stat : dict |
| 102 | + Dictionary containing statistical data |
| 103 | + ntypes : int |
| 104 | + Number of atom types |
| 105 | + keys : list[str] |
| 106 | + Keys to compute statistics for |
| 107 | + stat_file_path : DPPath, optional |
| 108 | + Path to save/load statistics |
| 109 | + rcond : float, optional |
| 110 | + Condition number for regression |
| 111 | + mixed_type : bool |
| 112 | + Whether mixed type format is used |
| 113 | +
|
| 114 | + Returns |
| 115 | + ------- |
| 116 | + bias_out : dict |
| 117 | + Computed bias values |
| 118 | + std_out : dict |
| 119 | + Computed standard deviation values |
| 120 | + """ |
| 121 | + # Try to restore from file first |
| 122 | + bias_out, std_out = _restore_from_file(stat_file_path, keys) |
| 123 | + |
| 124 | + if bias_out is not None and std_out is not None: |
| 125 | + log.info("Successfully restored statistics from stat file") |
| 126 | + return bias_out, std_out |
| 127 | + |
| 128 | + # If restore failed, compute from data |
| 129 | + log.info("Computing statistics from training data") |
| 130 | + |
| 131 | + from deepmd.utils.out_stat import ( |
| 132 | + compute_stats_from_redu, |
| 133 | + ) |
| 134 | + |
| 135 | + bias_out = {} |
| 136 | + std_out = {} |
| 137 | + |
| 138 | + for key in keys: |
| 139 | + if key in all_stat: |
| 140 | + # Get energy and natoms data |
| 141 | + energy_data = np.concatenate(all_stat[key]) |
| 142 | + natoms_data = np.concatenate(all_stat["natoms_vec"])[ |
| 143 | + :, 2: |
| 144 | + ] # Skip first 2 elements |
| 145 | + |
| 146 | + # Compute statistics using existing utility |
| 147 | + bias, std = compute_stats_from_redu( |
| 148 | + energy_data.reshape(-1, 1), # Reshape to column vector |
| 149 | + natoms_data, |
| 150 | + rcond=rcond, |
| 151 | + ) |
| 152 | + |
| 153 | + bias_out[key] = bias.reshape(-1) # Flatten to 1D |
| 154 | + std_out[key] = std.reshape(-1) # Flatten to 1D |
| 155 | + |
| 156 | + log.info( |
| 157 | + f"Statistics computed for {key}: bias shape {bias_out[key].shape}, std shape {std_out[key].shape}" |
| 158 | + ) |
| 159 | + |
| 160 | + # Save to file if path provided |
| 161 | + if stat_file_path is not None and bias_out: |
| 162 | + _save_to_file(stat_file_path, bias_out, std_out) |
| 163 | + log.info("Statistics saved to stat file") |
| 164 | + |
| 165 | + return bias_out, std_out |
0 commit comments