MCPcopy Create free account
hub / github.com/pytorch/pytorch / _alloc_storage

Function _alloc_storage

torch/distributed/utils.py:149–165  ·  view source on GitHub ↗

Allocate storage for ``tensor`` with the given size. Returns: bool: ``True`` if this method allocated storage and ``False`` if the storage was already allocated.

(tensor: torch.Tensor, size: torch.Size)

Source from the content-addressed store, hash-verified

147
148
149def _alloc_storage(tensor: torch.Tensor, size: torch.Size) -> None:
150 """
151 Allocate storage for ``tensor`` with the given size.
152
153 Returns:
154 bool: ``True`` if this method allocated storage and ``False`` if the
155 storage was already allocated.
156 """
157 with torch.no_grad():
158 already_allocated = tensor._typed_storage()._size() == size.numel()
159 if not already_allocated:
160 tensor_storage_size = tensor._typed_storage()._size()
161 _p_assert(
162 tensor_storage_size == 0,
163 f"Tensor storage should have been resized to be 0 but got {tensor_storage_size}",
164 )
165 tensor._typed_storage()._resize_(size.numel())
166
167
168

Callers 3

_root_copy_hookMethod · 0.90

Calls 5

_p_assertFunction · 0.85
_sizeMethod · 0.80
_typed_storageMethod · 0.80
_resize_Method · 0.80
numelMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…