Adding a UserWarning when pickling / deepcopying tensor views
- Dominant language
- Python
- Stars
- 103k
- Forks
- 29.5k
- PR merge metrics
- PR metrics pending
Description
### 🚀 The feature, motivation and pitch
Serializing (via pickle, copy.deepcopy, or torch.save) a small tensor view can result in unexpectedly large memory usage because the entire underlying storage is serialized rather than just the visible elements. While this is the expected behavior and it is documented ([here](https://docs.pytorch.org/docs/2.11/notes/serialization.html#preserve-storage-sharing) and [there](https://docs.pytorch.org/docs/2.11/generated/torch.save.html)) it can be surprising and when users are unaware of it. Moreover this is not numpy default behavior which might be confusing for people transitioning from numpy.
This behavior can:
- Lead to unintentionally large serialized objects
- Cause memory/performance issues in data pipelines
- Be difficult to diagnose without knowledge of PyTorch’s storage model
It personnaly took a couple hour to understand why my RAM was blowing up when moving tensor views between ray processes. Other pytorch users have also reported similar confusion #1995.
I think the simplest and most user friendly fix would be to simply emit a UserWarning when serializing a view.
### Alternatives
Expose a flag to the user to mimic numpy's behavior.
### Additional context
_No response_
cc @mruberry @mikaylagawarecki
Contributor guide
Assessment
This issue has not been assessed yet.