Skip to content

Commit c46c6a4

Browse files
Brennan Vincentfacebook-github-bot
authored andcommitted
Zero slice bug (#20914)
Summary: Bug reported internally at FB: ```python >>> t=torch.from_numpy(np.empty((0,4))) >>> t[:,1::2]*=1 Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: Trying to resize storage that is not resizable at ../aten/src/TH/THStorageFunctions.cpp:76 ``` This happens because the storage offset of `t[:, 1::2]` is 1, and it has 0 elements. We can fix this by avoiding resizing the storage for no-element arrays. (We could *also* have avoided it by not modifying the storage index in this case, but I felt this way was more semantically correct -- in general, we should not be assuming it's okay to do anything to the storage when it has zero elements). Pull Request resolved: #20914 Differential Revision: D15497860 Pulled By: umanwizard fbshipit-source-id: 6af61d73a05edfc5c07ce8be9e530f15bf72e6a9
1 parent 3858e16 commit c46c6a4

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

aten/src/ATen/native/Resize.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ namespace at { namespace native {
1010
// to benchmark than TH; I can't get gbenchmark to call fns from THTensor.cpp
1111

1212
static inline void maybe_resize_storage_cpu(TensorImpl* self, int64_t new_size) {
13-
if (new_size + self->storage_offset() > 0) {
13+
// It does not make sense to try to resize a storage
14+
// to hold 0 elements, and this can break
15+
// if storage_offset is positive but
16+
// new_size is 0, so just bail in that case
17+
// (same comment is in Resize.cuh)
18+
if (new_size > 0) {
1419
if (!THTensor_getStoragePtr(self)) {
1520
THTensor_stealAndSetStoragePtr(self, THStorage_new(self->dtype()));
1621
}

aten/src/ATen/native/cuda/Resize.cuh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ namespace at { namespace native {
1212
// to benchmark than THC; I can't get gbenchmark to call fns from THTensor.cpp
1313

1414
static inline void maybe_resize_storage_cuda(TensorImpl* self, int64_t new_size) {
15-
if (new_size + self->storage_offset() > 0) {
15+
// It does not make sense to try to resize a storage
16+
// to hold 0 elements, and this can break
17+
// if storage_offset is positive but
18+
// new_size is 0, so just bail in that case
19+
// (same comment is in Resize.h)
20+
if (new_size > 0) {
1621
if (!THTensor_getStoragePtr(self)) {
1722
AT_ERROR("Tensor: invalid null storage");
1823
}

test/test_torch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7328,6 +7328,14 @@ def _test_advancedindex_big(self, conv_fn):
73287328
def test_advancedindex_big(self):
73297329
self._test_advancedindex_big(self, lambda x: x)
73307330

7331+
@unittest.skipIf(not TEST_NUMPY, "Numpy not found")
7332+
def test_empty_storage_view(self):
7333+
# we should be able to "modify" slices of a 0-element
7334+
# array without an error being raised due to
7335+
# trying to resize its storage
7336+
t = torch.from_numpy(np.empty((0, 4)))
7337+
t[:, 1::2] *= 1
7338+
73317339
@unittest.skipIf(not TEST_NUMPY, "Numpy not found")
73327340
def test_newaxis_numpy_comparison(self):
73337341
def run_test(tensor, *idx):

0 commit comments

Comments
 (0)