Fix misuse of offset param in seek (#140633)

Fixes #115630.

The size of BufferAdapter has been calculated wrongly due to misuse of python method seek. Causes miniz reader initialized with wrong size.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/140633
Approved by: https://github.com/ezyang

Co-authored-by: Edward Z. Yang <ezyang@fb.com>
This commit is contained in:
cz2h
2024-11-15 19:07:52 +00:00
committed by PyTorch MergeBot
parent 500ce29e4c
commit 9602f56979
2 changed files with 10 additions and 1 deletions

View File

@ -4394,6 +4394,15 @@ class TestSerialization(TestCase, SerializationMixin):
with zipfile.ZipFile(f) as zip_file:
zip_file.extractall(path=temp_dir)
def test_serialization_with_header(self):
orig = torch.randn(3, 3)
with BytesIOContext() as f:
f.write(b'header')
torch.save(orig, f)
f.seek(6)
loaded = torch.load(f)
self.assertEqual(orig, loaded)
def test_get_unsafe_globals_in_checkpoint(self):
t = torch.randn(2, 3)
tt = TwoTensor(t, t)

View File

@ -1514,7 +1514,7 @@ void initJITBindings(PyObject* module) {
// Jump to the end of the buffer to get its size
auto current = buffer.attr("tell")();
start_offset_ = py::cast<size_t>(current);
buffer.attr("seek")(current, py::module::import("os").attr("SEEK_END"));
buffer.attr("seek")(0, py::module::import("os").attr("SEEK_END"));
size_ = py::cast<size_t>(buffer.attr("tell")()) - start_offset_;
buffer.attr("seek")(current);
// If we can read directly into a buffer, do that instead of an extra copy