Skip to content

Commit 905c4bc

Browse files
committed
Let save move file-backed data into the output instead of copying it
`FileBackedData` keeps a large blob, such as a delegate's compiled engine, in a file until the program is written. Writing it copies that file into the `.pte` or `.ptd`, so until the caller deletes the file afterwards, the blob is on disk twice. For an engine of tens of gigabytes, that copy alone can make an export run out of disk. This adds a keyword-only `consume=True` to `ExecutorchProgramManager.save` and `write_tensor_data_to_file`. With it, data that owns its file (`cleanup=True`, as from `move_from`) is moved into the output instead of copied. The output is first written to `<path>.partial` with holes where the moved data goes, and everything else is written and synced before any source changes. Then each buffer is copied from its end back to its start, one chunk at a time, and its source file is truncated after each chunk is synced. Peak disk use is the data once plus one chunk, instead of twice. At the end the temporary file is renamed onto `path`. The cost is time: the sync per chunk makes the save several times slower than a copy. Data is copied instead, with one warning that gives the count and the reasons, when it can't move safely: it does not own its file, the file has another name or is a link, it is on another file system, the same buffer is used more than once, something is already at `path`, the file system has no sparse files (checked once, before anything moves), or on Windows. If a save fails after data moved, a note on the error names the file that holds it. Other behavior changes: - Reading a `FileBackedData` whose file changed size, or moved away, raises instead of returning short data. - `copy.copy` and `copy.deepcopy` of a `FileBackedData` return the same object, so a copied program shares its data, and closing it through any copy deletes the file. Before, a deep copy could not be read once the original was collected. - New: `FileBackedData.check()`, `FileBackedData.path`, `Cord.file_backed_buffers()`. `write_to_file`, which takes a file the caller opened, always copies, as before. Test plan: `exir/_serialize/test/test_cord.py` and `exir/program/test/test_program.py` on Linux x86_64, including a real save that moves data through `to_edge_transform_and_lower`, a check that peak disk use stays under 1.5 times the data, saves that fail at each step, and every copy case. The move tests are skipped on Windows.
1 parent e3ee8fa commit 905c4bc

5 files changed

Lines changed: 2009 additions & 32 deletions

File tree

‎exir/_serialize/_cord.py‎

Lines changed: 253 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,46 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
import errno
78
import hashlib
89
import io
910
import os
10-
import shutil
11+
import stat
12+
import sys
1113
import tempfile
1214
import weakref
13-
from typing import List, Optional, Union
15+
from typing import Collection, Dict, IO, Iterator, List, Optional, Union
16+
17+
18+
def _sync(fd: int) -> None:
19+
"""Flush ``fd`` to the disk itself."""
20+
if sys.platform == "darwin":
21+
# On macOS, fsync leaves the data in the drive's cache.
22+
import fcntl
23+
24+
try:
25+
fcntl.fcntl(fd, fcntl.F_FULLFSYNC)
26+
return
27+
except OSError:
28+
pass # Not supported by this file system, such as some network shares.
29+
os.fsync(fd)
1430

1531

1632
class FileBackedData:
17-
"""A byte buffer that stays on disk until explicitly closed."""
33+
"""A byte buffer that stays on disk until explicitly closed or moved.
34+
35+
``ExecutorchProgramManager.save(path, consume=True)`` moves data that owns its file
36+
(``cleanup=True``, as from ``move_from``) into the new file it writes instead of
37+
copying it. Chunks are copied from the end back to the start, and the file is
38+
truncated after each chunk is written and synced to the output, so the data is on
39+
disk once, plus one chunk. That needs a regular file with no other name, on the
40+
output's file system. After a move the file is empty, and reading this or any other
41+
``FileBackedData`` on it raises.
42+
43+
A move that fails before its first truncation leaves the data intact. One that fails
44+
later leaves the file in place and says, in a note on the error, which bytes are
45+
still in it and where the rest is in the output.
46+
"""
1847

1948
_COPY_CHUNK_SIZE = 8 * 1024 * 1024
2049

@@ -56,26 +85,186 @@ def move_from(cls, path: str, sha256: Optional[bytes] = None) -> "FileBackedData
5685
def __len__(self) -> int:
5786
return self._size
5887

88+
def __copy__(self) -> "FileBackedData":
89+
return self
90+
91+
def __deepcopy__(self, memo: Dict[int, object]) -> "FileBackedData":
92+
# One object per file: of two that owned it, the first collected would delete the
93+
# file under the other, and one that doesn't own it can't move it.
94+
return self
95+
96+
@property
97+
def path(self) -> str:
98+
return self._path
99+
100+
def _check_size(self, size: int) -> None:
101+
# The size, not a flag, shows a moved file, so every object on it sees it.
102+
if size != self._size:
103+
raise RuntimeError(
104+
f"{self._path} has {size} bytes, expected {self._size}; it changed after "
105+
"it was wrapped, for example because a consuming save moved its data"
106+
)
107+
108+
def check(self) -> None:
109+
"""Raise if the file no longer holds the data, as after a move."""
110+
self._check_size(os.stat(self._path).st_size)
111+
112+
def _open(self) -> IO[bytes]:
113+
f = open(self._path, "rb")
114+
try:
115+
self._check_size(os.fstat(f.fileno()).st_size)
116+
except BaseException:
117+
f.close()
118+
raise
119+
return f
120+
121+
def _check_read(self, read: int, wanted: int) -> None:
122+
# A consuming save can shrink the file while it is read.
123+
if read != wanted:
124+
raise RuntimeError(
125+
f"read {read} of {wanted} bytes of {self._path}; it shrank while it was "
126+
"read, for example because a consuming save moved its data"
127+
)
128+
129+
def _chunks(self) -> Iterator[bytes]:
130+
with self._open() as f:
131+
read = 0
132+
while chunk := f.read(self._COPY_CHUNK_SIZE):
133+
read += len(chunk)
134+
yield chunk
135+
self._check_read(read, self._size)
136+
59137
def prefix(self, size: int) -> bytes:
60-
with open(self._path, "rb") as f:
61-
return f.read(size)
138+
with self._open() as f:
139+
data = f.read(size)
140+
self._check_read(len(data), min(size, self._size))
141+
return data
62142

63143
def sha256(self) -> bytes:
64144
if self._sha256 is None:
65145
digest = hashlib.sha256()
66-
with open(self._path, "rb") as f:
67-
while chunk := f.read(self._COPY_CHUNK_SIZE):
68-
digest.update(chunk)
146+
for chunk in self._chunks():
147+
digest.update(chunk)
69148
self._sha256 = digest.digest()
149+
else:
150+
self.check()
70151
return self._sha256
71152

72153
def to_bytes(self) -> bytes:
73-
with open(self._path, "rb") as f:
74-
return f.read()
154+
with self._open() as f:
155+
data = f.read()
156+
self._check_read(len(data), self._size)
157+
return data
75158

76159
def write_to_file(self, outfile: io.BufferedIOBase) -> None:
77-
with open(self._path, "rb") as f:
78-
shutil.copyfileobj(f, outfile, length=self._COPY_CHUNK_SIZE)
160+
for chunk in self._chunks():
161+
outfile.write(chunk)
162+
163+
def _open_to_move(self, output_device: int) -> Union[int, str]:
164+
"""Open the file to move the data into a file on ``output_device`` (see the class
165+
docstring). Returns the open file descriptor, or why the data has to be copied
166+
instead."""
167+
if self._finalizer is None or not self._finalizer.alive:
168+
return "not owned"
169+
if sys.platform == "win32":
170+
return "on Windows"
171+
try:
172+
# O_NOFOLLOW refuses a link, O_NONBLOCK keeps a pipe from blocking the open, and
173+
# the checks below look at the file opened.
174+
source = os.open(self._path, os.O_RDWR | os.O_NOFOLLOW | os.O_NONBLOCK)
175+
except OSError as e:
176+
return "a link" if e.errno == errno.ELOOP else "read-only or unreadable"
177+
opened = os.fstat(source)
178+
# Truncating changes every name of the file, and on another file system it frees
179+
# nothing for the output.
180+
checks = (
181+
(not stat.S_ISREG(opened.st_mode), "not a regular file"),
182+
(opened.st_nlink != 1, "with another hard link"),
183+
(opened.st_dev != output_device, "on another file system"),
184+
(opened.st_size != self._size, "changed in size"),
185+
)
186+
reason = next((text for failed, text in checks if failed), None)
187+
if reason is None:
188+
return source
189+
os.close(source)
190+
return reason
191+
192+
def _move_reason(self, output_device: int) -> Optional[str]:
193+
"""Why the data can't move into a file on ``output_device``, or None if it can."""
194+
opened = self._open_to_move(output_device)
195+
if isinstance(opened, str):
196+
return opened
197+
os.close(opened)
198+
return None
199+
200+
def _move_to(self, outfile: io.BufferedIOBase, offset: int) -> None:
201+
"""Move the data into ``outfile`` at ``offset``. ``outfile`` is a regular file its
202+
caller created for this write, and ``_move_reason`` found the data movable."""
203+
source = self._open_to_move(os.fstat(outfile.fileno()).st_dev)
204+
if isinstance(source, str):
205+
raise RuntimeError(f"{self._path} can't be moved any more ({source})")
206+
try:
207+
import fcntl
208+
209+
try:
210+
# Two moves of one file would each copy what the other already cut off.
211+
fcntl.flock(source, fcntl.LOCK_EX | fcntl.LOCK_NB)
212+
except BlockingIOError:
213+
raise RuntimeError(
214+
f"{self._path} is being moved by another save"
215+
) from None
216+
self._move_chunks(outfile, source, offset)
217+
finally:
218+
os.close(source)
219+
220+
def _own_file(self) -> None:
221+
# A move detaches the finalizer while it shrinks the file; this takes the file back.
222+
if self._finalizer is not None and not self._finalizer.alive:
223+
self._finalizer = weakref.finalize(self, self._remove, self._path)
224+
225+
def _move_chunks(self, outfile: io.BufferedIOBase, source: int, start: int) -> None:
226+
end = self._size
227+
try:
228+
while end > 0:
229+
begin = max(end - self._COPY_CHUNK_SIZE, 0)
230+
chunk = os.pread(source, end - begin, begin)
231+
if len(chunk) != end - begin:
232+
raise OSError(
233+
f"read {len(chunk)} of {end - begin} bytes at offset {begin} "
234+
f"of {self._path}"
235+
)
236+
outfile.seek(start + begin)
237+
written = outfile.write(chunk)
238+
if written != len(chunk):
239+
raise OSError(
240+
f"wrote {written} of {len(chunk)} bytes at offset "
241+
f"{start + begin}"
242+
)
243+
# The chunk has to be on the output's disk before the source gives it up.
244+
outfile.flush()
245+
_sync(outfile.fileno())
246+
if end == self._size and self._finalizer is not None:
247+
# From the first truncation, part of the data is found only through
248+
# the error note, so no interrupt may let the finalizer delete it.
249+
self._finalizer.detach()
250+
end = begin
251+
os.ftruncate(source, begin)
252+
self._own_file()
253+
except BaseException as e:
254+
try:
255+
# The truncation itself may have failed and left more bytes in the file.
256+
end = os.fstat(source).st_size
257+
except OSError:
258+
pass
259+
if end == self._size:
260+
# Nothing left the file, so this object still owns it.
261+
self._own_file()
262+
else:
263+
e.add_note(
264+
f"Bytes [0, {end}) are still in {self._path}; bytes "
265+
f"[{end}, {self._size}) are at offset {start + end} of the output."
266+
)
267+
raise
79268

80269
def close(self) -> None:
81270
if self._finalizer is not None:
@@ -131,10 +320,58 @@ def append(self, data: Union[CordBuffer, "Cord"]) -> None:
131320
f"Can only append bytes, FileBackedData, or Cords, received {type(data)}"
132321
)
133322

323+
def file_backed_buffers(self) -> List[FileBackedData]:
324+
"""The file-backed buffers of the Cord, in order, with repeats."""
325+
return [item for item in self._buffers if isinstance(item, FileBackedData)]
326+
134327
def write_to_file(self, outfile: io.BufferedIOBase) -> None:
135328
"""Write the Cord to a file."""
329+
self._write_to_file(outfile)
330+
331+
def _write_to_file(
332+
self, outfile: io.BufferedIOBase, move: Collection[FileBackedData] = ()
333+
) -> None:
334+
"""Write the Cord to ``outfile``, moving the buffers in ``move`` into it (see
335+
``FileBackedData``). Each of them must appear once, and ``_move_reason`` must have
336+
found it movable into ``outfile``, a regular file created for this write.
337+
Everything else is written first, around holes left for them, and the sync
338+
before the first truncation reports any error from it, so running out of space
339+
shows before any source shrinks. If the write fails, a note
340+
on the error lists the buffers already moved, whose only copy is now in
341+
``outfile``, and where they are."""
342+
if not move:
343+
for item in self._buffers:
344+
_write_buffer(item, outfile)
345+
return
346+
offsets, end = [], outfile.tell()
136347
for item in self._buffers:
137-
if isinstance(item, bytes):
138-
outfile.write(item)
139-
else:
140-
item.write_to_file(outfile)
348+
offsets.append(end)
349+
end += len(item)
350+
moving = [
351+
isinstance(item, FileBackedData) and item in move for item in self._buffers
352+
]
353+
moved: List[str] = []
354+
try:
355+
for item, offset, skip in zip(self._buffers, offsets, moving):
356+
if not skip:
357+
outfile.seek(offset)
358+
_write_buffer(item, outfile)
359+
for item, offset, skip in zip(self._buffers, offsets, moving):
360+
if skip:
361+
assert isinstance(item, FileBackedData)
362+
item._move_to(outfile, offset)
363+
moved.append(f"{item.path} ({len(item)} bytes) at offset {offset}")
364+
outfile.seek(end)
365+
except BaseException as e:
366+
if moved:
367+
e.add_note(
368+
"Moved into the output before the failure: " + "; ".join(moved)
369+
)
370+
raise
371+
372+
373+
def _write_buffer(item: CordBuffer, outfile: io.BufferedIOBase) -> None:
374+
if isinstance(item, bytes):
375+
outfile.write(item)
376+
else:
377+
item.write_to_file(outfile)

0 commit comments

Comments
 (0)