Repository navigation
Commit 91a918e
authored
Save a lowered program whose named data is file-backed (pytorch#23693)
## Problem
A backend can give ExecuTorch its named data as `FileBackedData` instead
of `bytes`. The data then waits in a file on disk until the `.pte` or
`.ptd` is written, instead of sitting in memory a second time. The CUDA
backend stores its weights this way, and `NamedDataStore`,
`to_executorch()` and `write_tensor_data_to_file()` all handle it.
Saving such a lowered program with `exir.save()` fails, though:
```
TypeError: Object of type FileBackedData is not JSON serializable
```
The serializer writes the lowered module's named data as JSON, and its
encoder only knows `bytes`.
## Change
The encoder for named data now reads a file-backed buffer into `bytes`
when it reaches it, then encodes it like any other buffer. The lowered
module keeps its file-backed buffers, and the saved file is the same as
if the backend had given `bytes`. Loading did not need a change: it
already decodes every buffer back to `bytes`.
What this costs: like any buffer in this JSON format, the data is held
in memory while it is encoded, about four times the size of all named
data, the same as for `bytes`. The named data of a lowered module is
written once for each call to it, so a store shared by several lowered
modules is written several times, and each one loads back as its own
copy. That was already so for `bytes`, and changing it needs a format
change.
## Test plan
A new test in `exir/tests/test_serde.py` lowers a small graph with the
named-data test backend, which gives it two buffers, and makes one of
them a `FileBackedData` that owns its file, as the CUDA backend's are.
It checks that the saved program is the same as when both buffers are
`bytes`, that the module and its file still hold the data after saving,
and that the program loads back with both buffers in order. It fails
without this change with the error above, and passes with it.
Ran `exir/tests/test_serde.py` on Linux aarch64 with Python 3.12, using
this branch's `exir` package and schema over an installed nightly: 16
passed with the change, and 15 passed on `main` (all tests except the
new one). The new test also fails on versions of the change that close
the file after reading it, convert only the first buffer, write the
converted copy back into the module, or use another base64 alphabet.1 parent 69eeed2 commit 91a918e
3 files changed
Lines changed: 55 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
51 | 60 | | |
52 | 61 | | |
53 | 62 | | |
| |||
291 | 300 | | |
292 | 301 | | |
293 | 302 | | |
294 | | - | |
| 303 | + | |
295 | 304 | | |
296 | 305 | | |
297 | 306 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
26 | 31 | | |
27 | 32 | | |
| 33 | + | |
28 | 34 | | |
29 | 35 | | |
30 | 36 | | |
| |||
246 | 252 | | |
247 | 253 | | |
248 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
249 | 293 | | |
250 | 294 | | |
251 | 295 | | |
| |||
0 commit comments