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)
| 147 | |
| 148 | |
| 149 | def _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 |
no test coverage detected
searching dependent graphs…