Export *model* with XNNPACK, save to a temp .pte, and run it via Runtime.
(
model: torch.nn.Module, example_inputs: tuple[torch.Tensor, ...]
)
| 26 | |
| 27 | |
| 28 | def _export_and_execute( |
| 29 | model: torch.nn.Module, example_inputs: tuple[torch.Tensor, ...] |
| 30 | ): |
| 31 | """Export *model* with XNNPACK, save to a temp .pte, and run it via Runtime.""" |
| 32 | with tempfile.TemporaryDirectory() as temp_dir, torch.no_grad(): |
| 33 | model.eval() |
| 34 | aten = export(model, example_inputs, strict=True) |
| 35 | edge = to_edge_transform_and_lower( |
| 36 | aten, |
| 37 | compile_config=EdgeCompileConfig(_check_ir_validity=False), |
| 38 | partitioner=[XnnpackPartitioner()], |
| 39 | ) |
| 40 | et = edge.to_executorch() |
| 41 | |
| 42 | pte_path = Path(temp_dir) / "xnnpack_runtime_test.pte" |
| 43 | et.save(str(pte_path)) |
| 44 | |
| 45 | runtime = Runtime.get() |
| 46 | program = runtime.load_program(pte_path, verification=Verification.Minimal) |
| 47 | method = program.load_method("forward") |
| 48 | assert method is not None, "forward method should exist in exported program" |
| 49 | return method.execute(example_inputs) |
| 50 | |
| 51 | |
| 52 | @unittest.skipUnless( |
no test coverage detected