Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7763,6 +7763,35 @@ def test_serialization_filelike_uses_readinto(self):
b = torch.load(data)
self.assertTrue(data.was_called('readinto'))

def test_serialization_storage_slice(self):
# Generated using:
#
# t = torch.zeros(2);
# s1 = t.storage()[:1]
# s2 = t.storage()[1:]
# torch.save((s1, s2), 'foo.ser')
#
# with PyTorch 0.3.1
serialized = (b'\x80\x02\x8a\nl\xfc\x9cF\xf9 j\xa8P\x19.\x80\x02M\xe9\x03'
b'.\x80\x02}q\x00(X\n\x00\x00\x00type_sizesq\x01}q\x02(X\x03'
b'\x00\x00\x00intq\x03K\x04X\x05\x00\x00\x00shortq\x04K\x02X'
b'\x04\x00\x00\x00longq\x05K\x04uX\x10\x00\x00\x00protocol_versionq'
b'\x06M\xe9\x03X\r\x00\x00\x00little_endianq\x07\x88u.\x80\x02'
b'(X\x07\x00\x00\x00storageq\x00ctorch\nFloatStorage\nq\x01X\x0e'
b'\x00\x00\x0094279043900432q\x02X\x03\x00\x00\x00cpuq\x03K\x02'
b'X\x0e\x00\x00\x0094279029750368q\x04K\x00K\x01\x87q\x05tq\x06'
b'Q(h\x00h\x01X\x0e\x00\x00\x0094279043900432q\x07h\x03K\x02X'
b'\x0e\x00\x00\x0094279029750432q\x08K\x01K\x01\x87q\ttq\nQ'
b'\x86q\x0b.\x80\x02]q\x00X\x0e\x00\x00\x0094279043900432q'
b'\x01a.\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00')

buf = io.BytesIO(serialized)
(s1, s2) = torch.load(buf)
self.assertEqual(s1[0], 0)
self.assertEqual(s2[0], 0)
self.assertEqual(s1.data_ptr() + 4, s2.data_ptr())

def test_load_error_msg(self):
expected_err_msg = (".*You can only torch.load from a file that is seekable. " +
"Please pre-load the data into a buffer like io.BytesIO and " +
Expand Down
28 changes: 26 additions & 2 deletions torch/csrc/generic/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,32 @@ static PyObject * THPStorage_(get)(THPStorage *self, PyObject *index)
return THPUtils_(newReal)(value);
/* Slice index */
} else if (PySlice_Check(index)) {
THPUtils_setError("storages don't support slicing");
return nullptr;
Py_ssize_t start, stop, slicelength, step;
int64_t len = THWStorage_(size)(LIBRARY_STATE self->cdata);
if (!THPUtils_parseSlice(index, len, &start, &stop, &step, &slicelength))
return NULL;
if (step != 1) {
THPUtils_setError("Trying to slice with a step of %" PRId64 ", but only a step of "
"1 is supported", (int64_t)step);
return NULL;
}

scalar_t *data = THWStorage_(data)(LIBRARY_STATE self->cdata);

at::StorageImpl* old_storage = self->cdata;
c10::raw::intrusive_ptr::incref(old_storage);
at::Storage new_storage(c10::make_intrusive<at::StorageImpl>(
old_storage->dtype(),
slicelength,
at::DataPtr(static_cast<void*>(data + start),
old_storage,
[](void* s) { c10::raw::intrusive_ptr::decref(static_cast<at::StorageImpl*>(s)); },
old_storage->device()),
old_storage->allocator(),
/* resizable */ false));

PyObject *_ret = THPStorage_(New)(new_storage.unsafeReleaseStorageImpl());
return _ret;
}
PyErr_Format(PyExc_TypeError, "can't index a " THPStorageStr " with %s",
THPUtils_typename(index));
Expand Down
15 changes: 1 addition & 14 deletions torch/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,20 +451,7 @@ def persistent_load(saved_id):
storage_views = pickle_module.load(f)
for target_cdata, root_cdata, offset, size in storage_views:
root = deserialized_objects[root_cdata]
if offset != 0 or size != root.size():
warnings.warn("Detected storage view in legacy serialized data: "
"storage views are no longer natively supported, so we are making "
"a copy of the data instead. THIS IS A SEMANTIC CHANGE! "
"If you need aliasing, reserialize your model using "
"tensors that share storage.")

tensor = torch._utils._rebuild_tensor(root, offset, (size,), (1,))
obj = tensor.clone().storage()
else:
# NB: This line does not appear to be exercised by the
# test suite.
obj = root
deserialized_objects[target_cdata] = obj
deserialized_objects[target_cdata] = root[offset:offset + size]

tar.extract('tensors', path=tmpdir)
with open(os.path.join(tmpdir, 'tensors'), 'rb', 0) as f:
Expand Down