Export a bundled .pte file containing the model and test cases. Args: model: The PyTorch model to export sample_inputs: Sample inputs for the model output_path: Path where the bundled .pte file should be saved (should end with .bpte) method_name: Name of the
(
model: torch.nn.Module,
sample_inputs: Tuple[torch.Tensor],
output_path: str,
method_name: str = "forward",
sample_kwargs=None,
et_program: Optional[ExecutorchProgramManager] = None,
dynamic_shapes=None,
)
| 715 | |
| 716 | |
| 717 | def save_bundled_program( |
| 718 | model: torch.nn.Module, |
| 719 | sample_inputs: Tuple[torch.Tensor], |
| 720 | output_path: str, |
| 721 | method_name: str = "forward", |
| 722 | sample_kwargs=None, |
| 723 | et_program: Optional[ExecutorchProgramManager] = None, |
| 724 | dynamic_shapes=None, |
| 725 | ) -> str: |
| 726 | """ |
| 727 | Export a bundled .pte file containing the model and test cases. |
| 728 | |
| 729 | Args: |
| 730 | model: The PyTorch model to export |
| 731 | sample_inputs: Sample inputs for the model |
| 732 | output_path: Path where the bundled .pte file should be saved (should end with .bpte) |
| 733 | method_name: Name of the method to test (default: "forward") |
| 734 | et_program: Optional pre-exported ExecutorchProgramManager. If None, will export to Vulkan |
| 735 | dynamic_shapes: Optional dynamic shapes for export |
| 736 | |
| 737 | Returns: |
| 738 | str: Path to the saved bundled program file |
| 739 | """ |
| 740 | # If no ExecutorchProgramManager provided, export to Vulkan |
| 741 | if et_program is None: |
| 742 | et_program = export_model_to_vulkan( |
| 743 | model, |
| 744 | sample_inputs, |
| 745 | sample_kwargs=sample_kwargs, |
| 746 | dynamic_shapes=dynamic_shapes, |
| 747 | ) |
| 748 | |
| 749 | if sample_kwargs is None: |
| 750 | sample_kwargs = {} |
| 751 | |
| 752 | # Generate expected outputs by running the model |
| 753 | expected_outputs = [getattr(model, method_name)(*sample_inputs, **sample_kwargs)] |
| 754 | |
| 755 | # Flatten sample inputs with kwargs to match expected format |
| 756 | inputs_flattened, _ = tree_flatten((sample_inputs, sample_kwargs)) |
| 757 | |
| 758 | # Create bundled program |
| 759 | bp_buffer = create_bundled_program( |
| 760 | et_program, |
| 761 | tuple(inputs_flattened), |
| 762 | expected_outputs, |
| 763 | method_name, |
| 764 | ) |
| 765 | |
| 766 | # Ensure output path has correct extension |
| 767 | if not output_path.endswith(".bpte"): |
| 768 | output_path = output_path + ".bpte" |
| 769 | |
| 770 | # Write to file |
| 771 | with open(output_path, "wb") as file: |
| 772 | file.write(bp_buffer) |
| 773 | return output_path |
| 774 |
nothing calls this directly
no test coverage detected