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

Method __init__

export/export.py:121–235  ·  view source on GitHub ↗

Initialize the ExportSession with model, inputs, and recipe. Args: model: The PyTorch model(s) to export. Can be: - nn.Module or Dict[str, nn.Module]: Eager PyTorch model(s) - GraphModule or Dict[str, GraphModule]: Quantized model(s)

(
        self,
        model: Union[
            nn.Module,
            Dict[str, nn.Module],
            GraphModule,
            Dict[str, GraphModule],
            ExportedProgram,
            Dict[str, ExportedProgram],
            str,
        ],
        example_inputs: Optional[
            Union[
                List[tuple[torch.Tensor, ...]],
                Dict[str, List[tuple[torch.Tensor, ...]]],
            ]
        ] = None,
        export_recipe: ExportRecipe = None,
        name: Optional[str] = None,
        dynamic_shapes: Optional[Union[Any, Dict[str, Any]]] = None,
        constant_methods: Optional[Union[Dict[str, Callable]]] = None,
        artifact_dir: Optional[str] = None,
        generate_etrecord: Optional[bool] = False,
    )

Source from the content-addressed store, hash-verified

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):
168 model = torch.export.load(model)
169 logging.info(f"Loaded ExportedProgram from {model}")
170
171 # Detect input model type to determine which stages to skip
172 self._input_model_type = self._detect_model_type(model)
173
174 # Standardize model to dictionary format
175 self._model = model if isinstance(model, dict) else {"forward": model}
176
177 # Validate and standardize example_inputs to dictionary format
178 # example_inputs not required for ExportedProgram input

Callers

nothing calls this directly

Calls 5

_detect_model_typeMethod · 0.95
_get_default_pipelineMethod · 0.95
_build_stagesMethod · 0.95
infoMethod · 0.80
loadMethod · 0.45

Tested by

no test coverage detected