|
24 | 24 | import torch |
25 | 25 | import torch.distributed as dist |
26 | 26 |
|
27 | | -from deepmd.dpmodel.common import ( |
28 | | - to_numpy_array, |
29 | | -) |
30 | 27 | from deepmd.dpmodel.train import ( |
31 | 28 | DEFAULT_TASK_KEY, |
32 | 29 | AbstractTrainer, |
|
35 | 32 | TrainingTask, |
36 | 33 | TrainingTaskCollection, |
37 | 34 | TrainStepResult, |
| 35 | + change_model_out_bias, |
| 36 | + change_model_out_bias_by_task, |
38 | 37 | ) |
39 | 38 | from deepmd.dpmodel.utils.batch import ( |
40 | 39 | normalize_batch, |
@@ -1350,6 +1349,9 @@ def __init__( |
1350 | 1349 | self.max_ckpt_keep = int(training_params.get("max_ckpt_keep", 5)) |
1351 | 1350 | self.display_in_training = training_params.get("disp_training", True) |
1352 | 1351 | self.timing_in_training = training_params.get("time_training", True) |
| 1352 | + self.change_bias_after_training = bool( |
| 1353 | + training_params.get("change_bias_after_training", False) |
| 1354 | + ) |
1353 | 1355 |
|
1354 | 1356 | # Model --------------------------------------------------------------- |
1355 | 1357 | self.models: dict[str, torch.nn.Module] = {} |
@@ -2139,8 +2141,25 @@ def run(self) -> None: |
2139 | 2141 | log.info("Start to train %d steps.", self.num_steps) |
2140 | 2142 | wall_start = time.time() |
2141 | 2143 | super().run(self.training_tasks) |
| 2144 | + if self.change_bias_after_training and self.num_steps > self.start_step: |
| 2145 | + self._change_bias_after_training() |
| 2146 | + if self.rank_context.is_chief: |
| 2147 | + self.save_checkpoint(self.num_steps) |
2142 | 2148 | log.info("Training finished. Total wall time: %.2fs", time.time() - wall_start) |
2143 | 2149 |
|
| 2150 | + def _change_bias_after_training(self) -> None: |
| 2151 | + if self.rank == 0: |
| 2152 | + change_model_out_bias_by_task( |
| 2153 | + self.models, |
| 2154 | + self._sample_funcs, |
| 2155 | + self.model_keys, |
| 2156 | + bias_adjust_mode="change-by-statistic", |
| 2157 | + ) |
| 2158 | + if self.is_distributed: |
| 2159 | + for model_key in self.model_keys: |
| 2160 | + self._broadcast_model_stat(self.models[model_key]) |
| 2161 | + self.model = self.models if self.multi_task else self.models[DEFAULT_TASK_KEY] |
| 2162 | + |
2144 | 2163 | def run_full_validation( |
2145 | 2164 | self, |
2146 | 2165 | *, |
@@ -2326,27 +2345,16 @@ def model_change_out_bias( |
2326 | 2345 | ------- |
2327 | 2346 | The model with updated bias. |
2328 | 2347 | """ |
2329 | | - old_bias = deepcopy(_model.get_out_bias()) |
2330 | | - _model.change_out_bias( |
2331 | | - _sample_func, |
2332 | | - bias_adjust_mode=_bias_adjust_mode, |
2333 | | - ) |
2334 | | - new_bias = deepcopy(_model.get_out_bias()) |
2335 | | - |
2336 | 2348 | from deepmd.dpmodel.model.dp_model import ( |
2337 | 2349 | DPModelCommon, |
2338 | 2350 | ) |
2339 | 2351 |
|
2340 | | - if isinstance(_model, DPModelCommon) and _bias_adjust_mode == "set-by-statistic": |
2341 | | - _model.get_fitting_net().compute_input_stats(_sample_func) |
2342 | | - |
2343 | | - model_type_map = _model.get_type_map() |
2344 | | - log.info( |
2345 | | - f"Change output bias of {model_type_map!s} " |
2346 | | - f"from {to_numpy_array(old_bias).reshape(-1)[: len(model_type_map)]!s} " |
2347 | | - f"to {to_numpy_array(new_bias).reshape(-1)[: len(model_type_map)]!s}." |
| 2352 | + return change_model_out_bias( |
| 2353 | + _model, |
| 2354 | + _sample_func, |
| 2355 | + bias_adjust_mode=_bias_adjust_mode, |
| 2356 | + recompute_input_stats=isinstance(_model, DPModelCommon), |
2348 | 2357 | ) |
2349 | | - return _model |
2350 | 2358 |
|
2351 | 2359 |
|
2352 | 2360 | def _get_case_embd_config( |
|
0 commit comments