Skip to content

Commit ffc28e6

Browse files
dbortfacebook-github-bot
authored andcommitted
Remove direct calls to serialize_to_flatbuffer
Summary: Use `to_executorch().buffer` instead. While I'm in here, remove all other references to "flatbuffer" in these files. Reviewed By: larryliu0820 Differential Revision: D48366594 fbshipit-source-id: a68184cc8c2479047ebbc20396fcb44a03e40ba9
1 parent 1728279 commit ffc28e6

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

backends/xnnpack/test/test_xnnpack_utils.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
from executorch.bundled_program.serialize import (
3232
serialize_from_bundled_program_to_flatbuffer,
3333
)
34+
from executorch.exir import ExecutorchProgram, ExirExportedProgram
3435
from executorch.exir.backend.backend_api import to_backend, validation_disabled
3536

3637
from executorch.exir.passes.spec_prop_pass import SpecPropPass
37-
from executorch.exir.serialize import serialize_to_flatbuffer
3838
from executorch.exir.tracer import _default_decomposition_table
3939

4040
# pyre-ignore[21]: Could not find module `executorch.extension.pybindings.portable`.
@@ -158,16 +158,16 @@ def lower_module_and_test_output(
158158
quantized_dynamic: bool = False,
159159
# TODO: remove this after we migrate to use long term flow
160160
quantizer_api_test: bool = False,
161-
dump_ff: bool = False, # for debugging, dump the generated flatbuffer file
162-
) -> exir.ExirExportedProgram:
161+
dump_bundled_program: bool = False, # for debugging, dump the generated bundled program file
162+
) -> ExirExportedProgram:
163163
"""
164164
Helper testing function that takes a torch.nn.Module and lowers it to XNNPACK with
165165
the given sample inputs. It then runs the lowered module and compares its
166166
outputs with the outputs of the eager module.
167167
"""
168168

169169
if quantizer_api_test:
170-
assert isinstance(module, exir.ExirExportedProgram)
170+
assert isinstance(module, ExirExportedProgram)
171171
edge_program = module
172172
else:
173173

@@ -197,46 +197,42 @@ def forward(self, *args):
197197
edge_program.exported_program, partitioner
198198
)
199199

200-
program = delegated_program.to_executorch(
200+
executorch_program: ExecutorchProgram = delegated_program.to_executorch(
201201
get_xnnpack_executorch_backend_config([SpecPropPass()]),
202-
).program
202+
)
203203
else:
204204
delegated_program = to_backend(
205205
"XnnpackBackend", edge_program.exported_program, []
206206
)
207207

208-
exported_program = capture_graph_for_xnnpack(
208+
exported_program: ExirExportedProgram = capture_graph_for_xnnpack(
209209
delegated_program, sample_inputs
210210
)
211-
program = exported_program.to_executorch(
211+
executorch_program: ExecutorchProgram = exported_program.to_executorch(
212212
get_xnnpack_executorch_backend_config(),
213-
).program
213+
)
214214

215215
# print("Graph Module with delegate:")
216216
# delegated_module.print_readable()
217217

218218
# Assert the backend name is xnnpack
219219
self.assertEqual(
220-
program.execution_plan[0].delegates[0].id,
220+
executorch_program.program.execution_plan[0].delegates[0].id,
221221
XnnpackBackend.__name__,
222222
)
223-
buffer = serialize_to_flatbuffer(program)
224223

225224
ref_output = delegated_program(*sample_inputs)
226-
if dump_ff:
227-
filename = f"/tmp/xnnpack_test_{randint(1, 99999)}.pte"
228-
print(f"Writing flatbuffer to {filename} ...")
229-
225+
if dump_bundled_program:
230226
save_bundled_program(
231227
representative_inputs=sample_inputs,
232-
program=program,
228+
program=executorch_program.program,
233229
ref_output=ref_output,
234-
output_path=filename,
230+
output_path=f"/tmp/xnnpack_test_{randint(1, 99999)}",
235231
)
236232

237233
# Test the model with executor
238-
# pyre-ignore
239-
executorch_module = _load_for_executorch_from_buffer(buffer)
234+
# pyre-ignore[16]: Module `executorch.extension.pybindings` has no attribute `portable`.
235+
executorch_module = _load_for_executorch_from_buffer(executorch_program.buffer)
240236
# pyre-fixme[16]: Module `pytree` has no attribute `tree_flatten`.
241237
inputs_flattened, _ = tree_flatten(sample_inputs)
242238

@@ -353,7 +349,7 @@ def _test_xnnpack_dqlinear(
353349
self,
354350
weight_qconfig,
355351
use_bias: bool,
356-
dump_ff: bool = False,
352+
dump_bundled_program: bool = False,
357353
):
358354
assert weight_qconfig in [
359355
weight_observer_range_neg_127_to_127,
@@ -413,39 +409,39 @@ def forward(self, x):
413409
composite_model = CompositeModule()
414410
composite_model(*example_inputs)
415411

416-
exported_program = capture_graph_for_xnnpack(composite_model, example_inputs)
417-
program = exported_program.to_executorch(
412+
exported_program: ExirExportedProgram = capture_graph_for_xnnpack(
413+
composite_model, example_inputs
414+
)
415+
executorch_program: ExecutorchProgram = exported_program.to_executorch(
418416
get_xnnpack_executorch_backend_config(),
419-
).program
417+
)
420418

421419
self.assertEqual(
422-
program.execution_plan[0].delegates[0].id,
420+
executorch_program.program.execution_plan[0].delegates[0].id,
423421
XnnpackBackend.__name__,
424422
)
425423

426424
ref_output = captured_dqlinear(*example_inputs)
427425
ref_output = composite_model(*example_inputs)
428426
print("ref_output:", ref_output)
429427

430-
if dump_ff:
428+
if dump_bundled_program:
431429
mm_str = "addmm" if use_bias else "mm"
432430
filename = f"/tmp/dqlinear_{mm_str}"
433431
if weight_qconfig == weight_observer_range_neg_127_to_127:
434432
filename = f"{filename}_per_tensor"
435433
else:
436434
filename = f"{filename}_per_channel"
437-
print(f"Writing flatbuffer to {filename} ...")
438435

439436
save_bundled_program(
440437
representative_inputs=example_inputs,
441-
program=program,
438+
program=executorch_program.program,
442439
ref_output=ref_output,
443440
output_path=filename,
444441
)
445442

446-
buffer = serialize_to_flatbuffer(program)
447443
# pyre-ignore
448-
executorch_module = _load_for_executorch_from_buffer(buffer)
444+
executorch_module = _load_for_executorch_from_buffer(executorch_program.buffer)
449445
# pyre-fixme[16]: Module `pytree` has no attribute `tree_flatten`.
450446
inputs_flattened, _ = tree_flatten(example_inputs)
451447

backends/xnnpack/test/tester/tester.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from executorch.exir.backend.backend_api import to_backend, validation_disabled
2929
from executorch.exir.backend.partitioner import Partitioner
3030
from executorch.exir.passes.spec_prop_pass import SpecPropPass
31-
from executorch.exir.serialize import serialize_to_flatbuffer
3231

3332
# pyre-ignore[21]: Could not find module `executorch.pybindings.portable`.
3433
from executorch.extension.pybindings.portable import ( # @manual
@@ -253,7 +252,7 @@ def __init__(self):
253252
self.buffer = None
254253

255254
def run(self, artifact: ExecutorchProgram, inputs=None) -> None:
256-
self.buffer = serialize_to_flatbuffer(artifact.program)
255+
self.buffer = artifact.buffer
257256

258257
@property
259258
def artifact(self) -> bytes:

0 commit comments

Comments
 (0)