Document torch.from_file and fix UntypedStorage.from_file docs (#111688)

Fixes https://github.com/pytorch/pytorch/issues/37439

Also threads through filename so it is accessible via `t.storage().filename`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/111688
Approved by: https://github.com/albanD
This commit is contained in:
Mikayla Gawarecki
2023-10-25 09:35:09 -07:00
committed by PyTorch MergeBot
parent f3b42ab5b9
commit b54ab57522
7 changed files with 119 additions and 18 deletions

View File

@ -84,6 +84,7 @@ class _StorageBase:
@classmethod
def _expired(cls, *args, **kwargs) -> T: ... # type: ignore[empty-body, misc, type-var] # noqa: E704
def _byteswap(self, *args, **kwargs): ... # noqa: E704
def _get_filename(self, *args, **kwargs) -> _Optional[str]: ... # type: ignore[empty-body, misc] # noqa: E704
def __str__(self):
info_str = (
@ -329,6 +330,12 @@ class UntypedStorage(torch._C.StorageBase, _StorageBase):
def is_hpu(self):
return self.device.type == 'hpu'
@property
def filename(self) -> _Optional[str]:
"""Returns the file name associated with this storage if the storage was memory mapped from a file.
or ``None`` if the storage was not created by memory mapping a file."""
return self._get_filename()
@_share_memory_lock_protected
def share_memory_(self, *args, **kwargs):
return super().share_memory_(*args, **kwargs)
@ -458,6 +465,12 @@ class TypedStorage:
def _dtype(self):
return self.dtype
@property
def filename(self) -> _Optional[str]:
"""Returns the file name associated with this storage if the storage was memory mapped from a file.
or ``None`` if the storage was not created by memory mapping a file."""
return self._untyped_storage.filename
def fill_(self, value):
_warn_typed_storage_removal()
self._setitem(slice(0, self._size()), value)
@ -1047,21 +1060,22 @@ class TypedStorage:
@classmethod
def from_file(cls, filename, shared, size):
"""
from_file(filename, shared=False, size=0) -> Storage
"""from_file(filename, shared=False, size=0) -> Storage
If `shared` is `True`, then memory is shared between all processes.
All changes are written to the file. If `shared` is `False`, then the changes on
Creates a CPU storage backed by a memory-mapped file.
If ``shared`` is ``True``, then memory is shared between all processes.
All changes are written to the file. If ``shared`` is ``False``, then the changes on
the storage do not affect the file.
`size` is the number of elements in the storage. If `shared` is `False`,
then the file must contain at least `size * sizeof(Type)` bytes
(`Type` is the type of storage). If `shared` is `True` the file will be
created if needed.
``size`` is the number of elements in the storage. If ``shared`` is ``False``,
then the file must contain at least ``size * sizeof(Type)`` bytes
(``Type`` is the type of storage). If ``shared`` is ``True`` the file will be created if needed.
Args:
filename (str): file name to map
shared (bool): whether to share memory
shared (bool): whether to share memory (whether ``MAP_SHARED`` or ``MAP_PRIVATE`` is passed to the
underlying `mmap(2) call <https://man7.org/linux/man-pages/man2/mmap.2.html>`_)
size (int): number of elements in the storage
"""
_warn_typed_storage_removal()