Commit 82dd693
Split nn.Module._save_to_state_dict to make it overridable (#21933)
Summary:
# Motivation
We allow to override JIT module serialization with `__getstate__/__setstate__` in order to cover cases where parameters are not serializable. Use cases include: MKLDNN integration: https://github.com/pytorch/pytorch/blob/a388c783505987363717bd4da0b166e8d1d7ccb9/torch/utils/mkldnn.py#L18-L26
and also fbgemm prepacked format integration for quantized tensors.
However many Eager scripts use `torch.save(module.state_dict())` form of serialization. There are several ways to make it work:
* make packed_weight itself pickleable (e.g. by binding `__getstate__/__setstate__` on C++ UDT level)
* change: we’d need to allow module buffers to be of arbitrary, non-Tensor types
* pro: no change to state_dict behavior
* cons: might not be directly inspectable by user calling .state_dict(), especially if packed weights represent several tensors fused together
* make packed_weight being proper Tensor layout
* pro: no change to state_dict or buffers behavior
* cons: adding new tensor layouts is pretty costly today
* cons: doesn’t work if multiple tensors are packed in one interleaved representation
* *[this approach]* allow Modules to override state_dict and return regular tensors
* pro: most flexible and hackable
* pro: maintains semantic meaning of statedict as all data necessary to represent module’s state
* cons: complicates state_dict logic
* cons: potential code duplication between `__getstate__/__setstate__`
Based on discussions with zdevito and gchanan we decided to pick latter approach. Rationale: this behavior is fully opt-in and will impact only modules that need it. For those modules the requirement listed above won't be true. But we do preserve requirement that all elements of state_dict are tensors. (https://fburl.com/qgybrug4 for internal discussion)
In the future we might also implement one of the approaches above but those are more involved.
Pull Request resolved: #21933
Differential Revision: D15937678
Pulled By: dzhulgakov
fbshipit-source-id: 3cb5d1a8304d04def7aabc0969d0a2e7be1823671 parent b2197ef commit 82dd693
3 files changed
+61
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4669 | 4669 | | |
4670 | 4670 | | |
4671 | 4671 | | |
| 4672 | + | |
| 4673 | + | |
| 4674 | + | |
| 4675 | + | |
| 4676 | + | |
| 4677 | + | |
| 4678 | + | |
| 4679 | + | |
| 4680 | + | |
| 4681 | + | |
| 4682 | + | |
| 4683 | + | |
| 4684 | + | |
| 4685 | + | |
| 4686 | + | |
| 4687 | + | |
| 4688 | + | |
| 4689 | + | |
| 4690 | + | |
| 4691 | + | |
| 4692 | + | |
| 4693 | + | |
| 4694 | + | |
| 4695 | + | |
| 4696 | + | |
| 4697 | + | |
| 4698 | + | |
| 4699 | + | |
| 4700 | + | |
| 4701 | + | |
| 4702 | + | |
| 4703 | + | |
4672 | 4704 | | |
4673 | 4705 | | |
4674 | 4706 | | |
| |||
5985 | 6017 | | |
5986 | 6018 | | |
5987 | 6019 | | |
5988 | | - | |
| 6020 | + | |
5989 | 6021 | | |
5990 | 6022 | | |
5991 | 6023 | | |
5992 | | - | |
| 6024 | + | |
5993 | 6025 | | |
5994 | 6026 | | |
5995 | 6027 | | |
| |||
6008 | 6040 | | |
6009 | 6041 | | |
6010 | 6042 | | |
6011 | | - | |
| 6043 | + | |
6012 | 6044 | | |
6013 | 6045 | | |
6014 | 6046 | | |
| |||
6043 | 6075 | | |
6044 | 6076 | | |
6045 | 6077 | | |
6046 | | - | |
| 6078 | + | |
6047 | 6079 | | |
6048 | 6080 | | |
6049 | 6081 | | |
| |||
6062 | 6094 | | |
6063 | 6095 | | |
6064 | 6096 | | |
6065 | | - | |
6066 | | - | |
| 6097 | + | |
| 6098 | + | |
6067 | 6099 | | |
6068 | 6100 | | |
6069 | 6101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1808 | 1808 | | |
1809 | 1809 | | |
1810 | 1810 | | |
1811 | | - | |
1812 | | - | |
| 1811 | + | |
| 1812 | + | |
1813 | 1813 | | |
1814 | 1814 | | |
1815 | 1815 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
635 | 635 | | |
636 | 636 | | |
637 | 637 | | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
638 | 658 | | |
639 | 659 | | |
640 | 660 | | |
| |||
655 | 675 | | |
656 | 676 | | |
657 | 677 | | |
658 | | - | |
659 | | - | |
660 | | - | |
661 | | - | |
662 | | - | |
663 | | - | |
| 678 | + | |
664 | 679 | | |
665 | 680 | | |
666 | 681 | | |
| |||
0 commit comments