Save the state dict of input source data with PyTorch `save`. It can save `nn.Module`, `state_dict`, a dictionary of `nn.Module` or `state_dict`. And automatically convert the data parallel module to regular module. For example:: save_state(net, path) save_state(net
(src: torch.nn.Module | dict, path: PathLike, **kwargs)
| 625 | |
| 626 | |
| 627 | def save_state(src: torch.nn.Module | dict, path: PathLike, **kwargs): |
| 628 | """ |
| 629 | Save the state dict of input source data with PyTorch `save`. |
| 630 | It can save `nn.Module`, `state_dict`, a dictionary of `nn.Module` or `state_dict`. |
| 631 | And automatically convert the data parallel module to regular module. |
| 632 | For example:: |
| 633 | |
| 634 | save_state(net, path) |
| 635 | save_state(net.state_dict(), path) |
| 636 | save_state({"net": net, "opt": opt}, path) |
| 637 | net_dp = torch.nn.DataParallel(net) |
| 638 | save_state(net_dp, path) |
| 639 | |
| 640 | Refer to: https://pytorch.org/ignite/v0.4.8/generated/ignite.handlers.DiskSaver.html. |
| 641 | |
| 642 | Args: |
| 643 | src: input data to save, can be `nn.Module`, `state_dict`, a dictionary of `nn.Module` or `state_dict`. |
| 644 | path: target file path to save the input object. |
| 645 | kwargs: other args for the `save_obj` except for the `obj` and `path`. |
| 646 | default `func` is `torch.save()`, details of the args: |
| 647 | https://pytorch.org/docs/stable/generated/torch.save.html. |
| 648 | |
| 649 | """ |
| 650 | |
| 651 | ckpt: dict = {} |
| 652 | if isinstance(src, dict): |
| 653 | for k, v in src.items(): |
| 654 | ckpt[k] = get_state_dict(v) |
| 655 | else: |
| 656 | ckpt = get_state_dict(src) |
| 657 | |
| 658 | save_obj(obj=ckpt, path=path, **kwargs) |
| 659 | |
| 660 | |
| 661 | def convert_to_onnx( |
searching dependent graphs…