1717Extends `DynamoExporter` to produce an `ExecutorchProgramManager` for mobile and
1818edge deployment. The export pipeline runs:
1919
20- 1. **Backend registration ** (`_BACKENDS `): move the model to the target device/dtype and build
21- the partitioner list, then run the backend's quantize/lower hooks .
20+ 1. **Backend preparation ** (`_BACKEND_PREPARE `): move the model to the target device/dtype
21+ and build the partitioner list .
22222. **Torch patches** (`_PATCHES["executorch"]` via `apply_patches("executorch")`, plus the
2323 backend-specific `_PATCHES[f"executorch.{backend}"]`): reversibly swap `torch` ops the
2424 ExecuTorch backends can't accept (`split_copy`, `avg_pool2d`, …) with decomposed equivalents.
4242import operator
4343import re
4444from collections .abc import MutableMapping
45- from dataclasses import dataclass
46- from typing import Any , Protocol
45+ from typing import Any
4746
4847from ..utils import logging
4948from ..utils .import_utils import is_executorch_available , is_torch_available
106105
107106logger = logging .get_logger (__name__ )
108107
109-
110- # ── Stage 1: Backend registration ─────────────────────────────────────────────
111- # Each backend is a `_ExecutorchBackend(prepare, quantize, lower)` entry in the `_BACKENDS` table
112- # below:
113- # - `prepare_for_*(model, sample_inputs, config)` receives the original model and sample inputs,
114- # applies backend-specific preparation, and returns `(model, sample_inputs, partitioner)`. Common
115- # patterns include moving the model to the target device, casting the model/inputs to the required
116- # dtype (e.g. bfloat16 for CUDA), and building the partitioner list passed to
117- # `to_edge_transform_and_lower`.
118- # - `quantize` runs the backend's PT2E quantization/calibration recipe — the inherited generic
119- # `DynamoExporter._quantize`, unless the backend needs its own (e.g. QNN's `_qnn_quantize`).
120- # - `lower` builds the final `ExecutorchProgramManager` — `_default_lower_to_executorch`, unless the
121- # backend needs its own (e.g. QNN's `_qnn_lower_to_executorch`).
122- # To add a new backend: implement `prepare_for_<name>` (plus dedicated `quantize`/`lower` hooks if
123- # needed) and add an entry to `_BACKENDS` below.
124-
125-
126- class _PrepareHook (Protocol ):
127- def __call__ (
128- self , config : ExecutorchConfig , model : PreTrainedModel , sample_inputs : Any , /
129- ) -> tuple [Any , Any , Any ]: ...
130-
131-
132- class _QuantizeHook (Protocol ):
133- def __call__ (
134- self ,
135- exporter : ExecutorchExporter ,
136- exported_program : ExportedProgram ,
137- config : ExecutorchConfig ,
138- sample_inputs : Any ,
139- dynamic_shapes : Any ,
140- / ,
141- ) -> ExportedProgram : ...
142-
143-
144- class _LowerHook (Protocol ):
145- def __call__ (
146- self , config : ExecutorchConfig , exported_program : ExportedProgram , sample_inputs : Any , partitioner : Any , /
147- ) -> EdgeProgramManager : ...
148-
149-
150- def _default_lower_to_executorch (config , exported_program , sample_inputs , partitioner ):
151- """The plain edge lowering every backend uses unless it registers its own."""
152-
153- edge_program_manager : EdgeProgramManager = to_edge_transform_and_lower (
154- exported_program ,
155- partitioner = partitioner ,
156- compile_config = _get_edge_compile_config (config .backend ),
157- transform_passes = _get_transform_passes (config .backend ),
158- )
159- executorch_programs_manager : ExecutorchProgramManager = edge_program_manager .to_executorch (
160- config = _get_backend_config (config )
161- )
162- return executorch_programs_manager
163-
164-
165- def _qnn_quantize (exporter , exported_program , config , sample_inputs , dynamic_shapes ):
166- from executorch .backends .qualcomm .hf_transformers .api import quantize_for_qnn
167-
168- return quantize_for_qnn (exported_program , config , sample_inputs )
169-
170-
171- def _qnn_lower_to_executorch (config , exported_program , sample_inputs , partitioner ):
172- from executorch .backends .qualcomm .hf_transformers .api import lower_for_qnn
173-
174- return lower_for_qnn (exported_program = exported_program , hf_config = config , sample_inputs = sample_inputs )
175-
176-
177- @dataclass (frozen = True )
178- class _ExecutorchBackend :
179- prepare : _PrepareHook
180- quantize : _QuantizeHook
181- lower : _LowerHook
182-
183-
184- def _get_backend (config : ExecutorchConfig ) -> _ExecutorchBackend :
185- """Look up `config`'s backend entry, raising for an unknown backend name."""
186- backend = _BACKENDS .get (config .backend )
187- if backend is None :
188- raise ValueError (
189- f"Unsupported backend { config .backend !r} for ExecuTorch export; expected one of { sorted (_BACKENDS )} ."
190- )
191- return backend
192-
193-
194108class ExecutorchExporter (DynamoExporter ):
195109 """Exporter that converts a [`PreTrainedModel`] to an ExecuTorch `ExecutorchProgramManager`.
196110
@@ -220,16 +134,29 @@ def export(
220134 elif not isinstance (config , ExecutorchConfig ):
221135 raise TypeError (f"Expected config to be an ExecutorchConfig or dict, got { type (config )} " )
222136
223- backend = _get_backend (config )
137+ prepare_for_backend = _BACKEND_PREPARE .get (config .backend )
138+ if prepare_for_backend is None :
139+ raise ValueError (f"Unsupported backend { config .backend } for ExecuTorch export" )
224140
225- model , sample_inputs , partitioner = backend . prepare (model , sample_inputs , config )
141+ model , sample_inputs , partitioner = prepare_for_backend (model , sample_inputs , config )
226142
227143 with apply_patches ("executorch" ), apply_patches (f"executorch.{ config .backend } " ):
228144 exported_program : ExportedProgram = super ().export (model , sample_inputs , config = config )
229145 apply_fx_program_fixes ("executorch" , exported_program )
230146 apply_fx_node_fixes ("executorch" , exported_program .graph_module )
231- executorch_programs_manager : ExecutorchProgramManager = _lower_to_executorch (
232- config , exported_program , sample_inputs , partitioner
147+
148+ if config .backend == "qnn" :
149+ from executorch .backends .qualcomm .hf_transformers .api import lower_for_qnn
150+ return lower_for_qnn (exported_program = exported_program , hf_config = config , sample_inputs = sample_inputs )
151+
152+ edge_program_manager : EdgeProgramManager = to_edge_transform_and_lower (
153+ exported_program ,
154+ partitioner = partitioner ,
155+ compile_config = _get_edge_compile_config (config .backend ),
156+ transform_passes = _get_transform_passes (config .backend ),
157+ )
158+ executorch_programs_manager : ExecutorchProgramManager = edge_program_manager .to_executorch (
159+ config = _get_backend_config (config )
233160 )
234161
235162 return executorch_programs_manager
@@ -241,14 +168,10 @@ def _quantize(
241168 sample_inputs : Any ,
242169 dynamic_shapes : Any ,
243170 ) -> ExportedProgram :
244- """Dispatch to the backend's quantize hook (see `_BACKENDS`).
245-
246- Overrides `DynamoExporter._quantize`: `DynamoExporter.export` (invoked via
247- `super().export(...)` above) calls `self._quantize(...)` directly, so without this
248- override every backend would silently fall back to the generic PT2E recipe instead
249- of a backend-specific one like QNN's `_qnn_quantize`.
250- """
251- return _get_backend (config ).quantize (self , exported_program , config , sample_inputs , dynamic_shapes )
171+ if config .backend == "qnn" :
172+ from executorch .backends .qualcomm .hf_transformers .api import quantize_for_qnn
173+ return quantize_for_qnn (exported_program , config , sample_inputs )
174+ return super ()._quantize (self , exported_program , config , sample_inputs , dynamic_shapes )
252175
253176
254177def _get_transform_passes (backend : str ):
@@ -290,10 +213,6 @@ def _get_edge_compile_config(backend: str) -> EdgeCompileConfig:
290213 )
291214
292215
293- def _lower_to_executorch (config , exported_program , sample_inputs , partitioner ):
294- return _get_backend (config ).lower (config , exported_program , sample_inputs , partitioner )
295-
296-
297216def _get_backend_config (config ):
298217 """Build the ``ExecutorchBackendConfig`` for ``to_executorch``, or ``None`` for defaults.
299218
@@ -311,6 +230,14 @@ def _get_backend_config(config):
311230 )
312231 )
313232
233+ # ── Stage 1: Backend preparation ──────────────────────────────────────────────
234+ # Each prepare_for_* function receives the original model and sample inputs, applies backend-specific preparation,
235+ # and returns the modified model, the list of partitioners to apply, and the modified sample inputs. Common patterns include:
236+ # - Move the model to the target device.
237+ # - Cast the model and inputs to the required dtype (e.g., bfloat16 for CUDA).
238+ # - Build the backend-specific partitioner list passed to to_edge_transform_and_lower.
239+ # To add a new backend: implement _prepare_for_new_backend and add it to the _BACKEND_PREPARE table.
240+
314241
315242def _make_contiguous (sample_inputs : dict [str , Any ]) -> dict [str , Any ]:
316243 """Materialise input tensors to contiguous for ExecuTorch.
@@ -394,28 +321,11 @@ def prepare_for_qnn(model: PreTrainedModel, sample_inputs: dict[str, Any], confi
394321 return qnn_decoder_model , _make_contiguous (qnn_decoder_model .get_example_inputs ()), None
395322
396323
397- _BACKENDS = {
398- "xnnpack" : _ExecutorchBackend (
399- prepare = prepare_for_xnnpack ,
400- # The inherited generic PT2E recipe, referenced as an unbound method.
401- quantize = DynamoExporter ._quantize ,
402- lower = _default_lower_to_executorch ,
403- ),
404- "cuda" : _ExecutorchBackend (
405- prepare = prepare_for_cuda ,
406- quantize = DynamoExporter ._quantize ,
407- lower = _default_lower_to_executorch ,
408- ),
409- "mlx" : _ExecutorchBackend (
410- prepare = prepare_for_mlx ,
411- quantize = DynamoExporter ._quantize ,
412- lower = _default_lower_to_executorch ,
413- ),
414- "qnn" : _ExecutorchBackend (
415- prepare = prepare_for_qnn ,
416- quantize = _qnn_quantize ,
417- lower = _qnn_lower_to_executorch ,
418- ),
324+ _BACKEND_PREPARE = {
325+ "xnnpack" : prepare_for_xnnpack ,
326+ "cuda" : prepare_for_cuda ,
327+ "mlx" : prepare_for_mlx ,
328+ "qnn" : prepare_for_qnn
419329}
420330
421331
0 commit comments