pytorch/docs/source/notes
Mikayla Gawarecki 94db935749 Add torch.serialization.skip_data context manager (#134504)
## Semantic

The semantic is
(1) By default `torch.serialization.skip_data(materialize_fake_tensors=False)` will make `torch.save` skip writing storages (but reserve space for them in the checkpoint).

```python
import torch
import torch.nn as nn

sd = nn.Linear(3, 5).state_dict()
with torch.serialization.skip_data():
    torch.save(sd, 'foo.pt')
print(torch.load('foo.pt', weights_only=True))
```

(2)  With `torch.serialization.skip_data(materialize_fake_tensors=True)`If FakeTensor is passed to `torch.save` the pickler will treat these FakeTensors as being "materialized" space will be reserved in the checkpoint for the associated storage bytes, and when loading the type will be Tensor instead of FakeTensor)

```python
import torch
import torch.nn as nn
from torch._subclasses.fake_tensor import FakeTensorMode

with FakeTensorMode():
    m = nn.Linear(3, 5, dtype=torch.float16, device='cuda')

sd = m.state_dict()
with torch.serialization.skip_data(materialize_fake_tensors=True):
    torch.save(sd, 'bla.pt')
print(torch.load('bla.pt', weights_only=True))
# OrderedDict([('weight', tensor([[0., 0., 0.],
#        [0., 0., 0.],
#        [0., 0., 0.],
#        [0., 0., 0.],
#        [0., 0., 0.]], device='cuda:0', dtype=torch.float16)), ('bias', tensor([0., 0., 0., 0., 0.], device='cuda:0', dtype=torch.float16))])

```

## Follow Ups

- [ ] `torch.load` semantic for skip_data context manager
- [ ] Mechanism for getting offsets of storages saved via this method (for writing in a separate pass)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/134504
Approved by: https://github.com/albanD
2024-08-29 04:52:52 +00:00
..
amp_examples.rst update amp example to device-agnostic (#127278) 2024-06-13 02:01:16 +00:00
autograd.rst Fix unexpected inference_mode interaction with torch.autograd.functional.jacobian (#130307) 2024-08-25 22:14:02 +00:00
broadcasting.rst
cpu_threading_runtimes.svg
cpu_threading_torchscript_inference.rst
cpu_threading_torchscript_inference.svg
cuda.rst Graph-Safe RNG State Exchange for Tensor Parallelism (#114068) 2024-03-27 01:14:38 +00:00
custom_operators.rst [docs] Redirect custom ops landing page to the correct place (#129177) 2024-06-21 13:31:32 +00:00
ddp.rst Update DDP dynamo debug docs (#118295) 2024-01-29 14:58:26 +00:00
extending.func.rst Fix the example in the extending.func.rst (#109279) 2023-09-14 17:29:39 +00:00
extending.rst [Doc] fix some typos (found by codespell and typos) (#132544) 2024-08-05 17:21:56 +00:00
faq.rst
fsdp.rst [docs] start a new FSDP notes doc (#117323) 2024-01-22 15:46:35 +00:00
get_start_xpu.rst Adding a note for Getting Started with PyTorch on Intel GPUs (#127872) 2024-06-14 14:24:28 +00:00
gradcheck.rst
hip.rst Tweak formatting of note on macros (#89598) 2022-11-28 20:42:30 +00:00
large_scale_deployments.rst
modules.rst Replace master with main in links and docs/conf.py (#100176) 2023-05-02 18:20:32 +00:00
mps.rst
multiprocessing.rst [draft] Update Multiprocessing best practices with CPU device (#103229) 2023-06-25 06:26:40 +00:00
numerical_accuracy.rst [Doc] fix some typos (found by codespell and typos) (#132544) 2024-08-05 17:21:56 +00:00
randomness.rst Changed cublas repdocubility URL (#121534) 2024-03-08 23:46:21 +00:00
serialization.rst Add torch.serialization.skip_data context manager (#134504) 2024-08-29 04:52:52 +00:00
windows.rst