(self, param_state_pairs, optim_state, scaler_state=None)
| 1377 | paddle.save(scaler, path + '.pdscaler') |
| 1378 | |
| 1379 | def load(self, param_state_pairs, optim_state, scaler_state=None): |
| 1380 | # restore parameter states |
| 1381 | for param, state in param_state_pairs: |
| 1382 | param.set_value(state) |
| 1383 | |
| 1384 | if hasattr(self.model, '_scaler') and self.model._scaler is not None: |
| 1385 | if scaler_state: |
| 1386 | self.model._scaler.load_state_dict(scaler_state) |
| 1387 | |
| 1388 | # restore optimizer states |
| 1389 | if not self.model._optimizer or not optim_state: |
| 1390 | return |
| 1391 | |
| 1392 | # If optimizer performs set_state_dict when state vars haven't been created, |
| 1393 | # which would happen when set_state_dict before minimize, the state would be |
| 1394 | # stored in optimizer._accumulators_holder and loaded lazily. |
| 1395 | # To contrive this when loading from static-graph saved states, extend |
| 1396 | # state dict to include keys named according to dygraph naming rules. |
| 1397 | # TODO: if len(self.model._optimizer._accumulators) > 0 |
| 1398 | converted_state = dict(optim_state) |
| 1399 | opt_unq_name = self.model._optimizer._name |
| 1400 | if opt_unq_name is None: |
| 1401 | opt_unq_name = '' |
| 1402 | |
| 1403 | opt_cls_name = self.model._optimizer.__class__.__name__ |
| 1404 | opt_name = opt_unq_name[: opt_unq_name.rfind("_")] # remove suffix idx |
| 1405 | param_names = [param.name for param in self.model.network.parameters()] |
| 1406 | for var_name, state_var in sorted( |
| 1407 | optim_state.items(), key=lambda x: len(x[0]), reverse=True |
| 1408 | ): |
| 1409 | if var_name in ["@LR_DECAY_COUNTER@", "global_step"]: |
| 1410 | # NOTE: dygraph saved global_step is 1 larger than that in |
| 1411 | # static-graph, since the time of global_step to increase is |
| 1412 | # different. |
| 1413 | if var_name == "@LR_DECAY_COUNTER@": |
| 1414 | converted_state["global_step"] = ( |
| 1415 | np.array(converted_state.pop("@LR_DECAY_COUNTER@")) + 1 |
| 1416 | ) |
| 1417 | else: |
| 1418 | # moment and other accumulators |
| 1419 | # extend state dict to include promising dygraph names |
| 1420 | for param_name in param_names: |
| 1421 | if var_name.startswith(param_name + "_" + opt_name): |
| 1422 | # when init optimizer with name |
| 1423 | accum_name = var_name[ |
| 1424 | len(param_name + "_" + opt_name + "_") : |
| 1425 | ] |
| 1426 | elif ( |
| 1427 | var_name.startswith(param_name + "_") |
| 1428 | and opt_name == opt_cls_name |
| 1429 | ): |
| 1430 | # when init optimizer without name |
| 1431 | accum_name = var_name[len(param_name + "_") :] |
| 1432 | else: |
| 1433 | continue |
| 1434 | # remove suffix idx |
| 1435 | accum_name = accum_name[: accum_name.rfind("_")] |
| 1436 | # state names always end with "_0" in dygraph because of the |
nothing calls this directly
no test coverage detected