Skip to content

Commit e3ee8fa

Browse files
committed
Make save and save_pte_program raise when a write fails
`ExecutorchProgramManager.save` caught any error from the write, logged it and returned normally. `save_pte_program` did the same for the `.pte` and the `.ptd` files, and then returned the path of a file that might be empty or cut short. A caller had no way to notice, short of reading the log. Now both let the error reach the caller. `save` still logs the path when it succeeds. `save_pte_program` now writes the `.pte` through `save` instead of repeating its code, so both paths behave the same way. Behavior change for callers: - `save` and `save_pte_program` raise on a failed write, where they used to log and return. - `save_pte_program` needs an object with `save`, as its type hint already says (`ExecutorchProgramManager`). The legacy `ExecutorchProgram` that the deprecated `exir.capture(...).to_edge().to_executorch()` path returns has no `save`, so passing it now raises `AttributeError` instead of writing the file. Such code can call the legacy object's own `write_to_file` instead. Test plan: `exir/program/test/test_program.py`. New tests check that `save` raises when the write fails, and that `save_pte_program` writes the `.pte` and `.ptd` files, returns the path, and raises when either write fails. Both fail on the code before this change.
1 parent 3d17551 commit e3ee8fa

4 files changed

Lines changed: 45 additions & 15 deletions

File tree

‎exir/program/_program.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,9 +2070,6 @@ def save(self, path: str) -> None:
20702070
if path[-4:] != ".pte":
20712071
logging.error(f"Path {path} does not end with .pte")
20722072
raise ValueError(f"Path {path} does not end with .pte")
2073-
try:
2074-
with open(path, "wb") as file:
2075-
self.write_to_file(file)
2076-
logging.info(f"Saved exported program to {path}")
2077-
except Exception as e:
2078-
logging.error(f"Error while saving to {path}: {e}")
2073+
with open(path, "wb") as file:
2074+
self.write_to_file(file)
2075+
logging.info(f"Saved exported program to {path}")

‎exir/program/test/BUCK‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fbcode_target(_kind = runtime.python_test,
2424
"//executorch/exir/program:lib",
2525
"//executorch/exir/tests:lib",
2626
"//executorch/exir/tests:models",
27+
"//executorch/extension/export_util:export_util",
2728
"//executorch/extension/pybindings:portable_lib", # @manual
2829
],
2930
)

‎exir/program/test/test_program.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
# pyre-unsafe
88

99
import copy
10+
import os
11+
import tempfile
1012
import unittest
1113
from typing import Any, Dict
14+
from unittest import mock
1215

1316
import torch
1417
from executorch.exir import EdgeCompileConfig, ExecutorchBackendConfig
18+
from executorch.exir._serialize._cord import Cord
1519
from executorch.exir.backend.test.op_partitioner_demo import (
1620
AddMulPartitionerDemo,
1721
NonDecompTestPartitioner,
@@ -31,6 +35,7 @@
3135
from executorch.exir.tracer import _default_decomposition_table
3236
from executorch.exir.verification.verifier import EXIREdgeDialectVerifier
3337

38+
from executorch.extension.export_util.utils import save_pte_program
3439
from executorch.extension.pybindings.portable_lib import (
3540
_load_for_executorch_from_buffer,
3641
)
@@ -1226,6 +1231,40 @@ def test_save_fails(self):
12261231
with self.assertRaises(ValueError):
12271232
_ = et.save("/tmp/test_save.pt")
12281233

1234+
def test_save_raises_when_the_write_fails(self):
1235+
model = TestLinear()
1236+
et = to_edge(
1237+
torch.export.export(model, model._get_random_inputs(), strict=True)
1238+
).to_executorch()
1239+
with tempfile.TemporaryDirectory() as directory:
1240+
with mock.patch.object(
1241+
Cord, "write_to_file", side_effect=OSError("disk full")
1242+
):
1243+
with self.assertRaises(OSError):
1244+
et.save(os.path.join(directory, "model.pte"))
1245+
1246+
def test_save_pte_program_writes_the_data_files_and_raises_on_failure(self):
1247+
model = TestLinear()
1248+
et = to_edge(
1249+
torch.export.export(model, model._get_random_inputs(), strict=True)
1250+
).to_executorch()
1251+
et._tensor_data = {"data": Cord(b"data")}
1252+
with tempfile.TemporaryDirectory() as directory:
1253+
path = os.path.join(directory, "model.pte")
1254+
self.assertEqual(path, save_pte_program(et, path, directory))
1255+
with open(os.path.join(directory, "data.ptd"), "rb") as f:
1256+
self.assertEqual(b"data", f.read())
1257+
failures = {
1258+
"pte": (ExecutorchProgramManager, "write_to_file"),
1259+
"ptd": (ExecutorchProgramManager, "write_tensor_data_to_file"),
1260+
}
1261+
for name, (owner, method) in failures.items():
1262+
with self.subTest(write=name), mock.patch.object(
1263+
owner, method, side_effect=OSError("disk full")
1264+
):
1265+
with self.assertRaises(OSError):
1266+
save_pte_program(et, path, directory)
1267+
12291268
def test__transform_override_verifiers(self):
12301269
"""Test that _transform can override verifiers in the exported program."""
12311270

‎extension/export_util/utils.py‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,7 @@ def save_pte_program(
141141
else:
142142
filename = os.path.join(output_dir, f"{model_name}.pte")
143143

144-
try:
145-
# Write program to file.
146-
with open(filename, "wb") as file:
147-
prog.write_to_file(file)
148-
logging.info(f"Saved exported program to {filename}")
149-
# Write data to file/s.
150-
prog.write_tensor_data_to_file(outdir=output_dir)
151-
except Exception as e:
152-
logging.error(f"Error while saving to {filename}: {e}")
144+
prog.save(filename)
145+
prog.write_tensor_data_to_file(outdir=output_dir)
153146

154147
return filename

0 commit comments

Comments
 (0)