MCPcopy Create free account
hub / github.com/pytorch/executorch / save_bundled_program

Function save_bundled_program

backends/vulkan/test/utils.py:717–773  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

715
716
717def 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

Callers

nothing calls this directly

Calls 3

export_model_to_vulkanFunction · 0.85
create_bundled_programFunction · 0.85
writeMethod · 0.80

Tested by

no test coverage detected