Manages the export process for ExecuTorch models. This class handles the export process through a pipeline of stages: 1. (Optional) Quantize - Apply post-training quantization to the model 2. Export - Export PyTorch model to ExportedProgram 3. EdgeTransformAndLower - Transform
| 108 | "This API and all of its related functionality such as ExportSession and ExportRecipe are experimental." |
| 109 | ) |
| 110 | class ExportSession: |
| 111 | """ |
| 112 | Manages the export process for ExecuTorch models. |
| 113 | |
| 114 | This class handles the export process through a pipeline of stages: |
| 115 | 1. (Optional) Quantize - Apply post-training quantization to the model |
| 116 | 2. Export - Export PyTorch model to ExportedProgram |
| 117 | 3. EdgeTransformAndLower - Transform and lower to EdgeProgramManager |
| 118 | 4. Executorch - Convert to ExecutorchProgramManager for final execution |
| 119 | """ |
| 120 | |
| 121 | def __init__( |
| 122 | self, |
| 123 | model: Union[ |
| 124 | nn.Module, |
| 125 | Dict[str, nn.Module], |
| 126 | GraphModule, |
| 127 | Dict[str, GraphModule], |
| 128 | ExportedProgram, |
| 129 | Dict[str, ExportedProgram], |
| 130 | str, |
| 131 | ], |
| 132 | example_inputs: Optional[ |
| 133 | Union[ |
| 134 | List[tuple[torch.Tensor, ...]], |
| 135 | Dict[str, List[tuple[torch.Tensor, ...]]], |
| 136 | ] |
| 137 | ] = None, |
| 138 | export_recipe: ExportRecipe = None, |
| 139 | name: Optional[str] = None, |
| 140 | dynamic_shapes: Optional[Union[Any, Dict[str, Any]]] = None, |
| 141 | constant_methods: Optional[Union[Dict[str, Callable]]] = None, |
| 142 | artifact_dir: Optional[str] = None, |
| 143 | generate_etrecord: Optional[bool] = False, |
| 144 | ) -> None: |
| 145 | """ |
| 146 | Initialize the ExportSession with model, inputs, and recipe. |
| 147 | |
| 148 | Args: |
| 149 | model: The PyTorch model(s) to export. Can be: |
| 150 | - nn.Module or Dict[str, nn.Module]: Eager PyTorch model(s) |
| 151 | - GraphModule or Dict[str, GraphModule]: Quantized model(s) |
| 152 | - ExportedProgram or Dict[str, ExportedProgram]: Already exported model(s) |
| 153 | - str: Path to load an ExportedProgram from disk |
| 154 | example_inputs: Example inputs for the model(s), either a list of input tuples |
| 155 | or a dictionary mapping method names to lists of input tuples. |
| 156 | First sample (index 0) is used for torch.export.export() to export the model. |
| 157 | All samples are used as calibration dataset in PT2E Quantize stage, |
| 158 | Optional when model is ExportedProgram (not needed). |
| 159 | export_recipe: Contains the configuration for the export process |
| 160 | name: Optional name for the export |
| 161 | dynamic_shapes: Optional dynamic shape specifications |
| 162 | constant_methods: Optional dictionary of constant methods |
| 163 | artifact_dir: Optional directory to store artifacts |
| 164 | generate_etrecord: Optional flag to generate an etrecord |
| 165 | """ |
| 166 | # Load model from file if string path provided |
| 167 | if isinstance(model, str): |
no outgoing calls