(
model, output_file="whisper_preprocess.pte", backend="xnnpack"
)
| 189 | |
| 190 | |
| 191 | def _export_processor_model( |
| 192 | model, output_file="whisper_preprocess.pte", backend="xnnpack" |
| 193 | ): |
| 194 | |
| 195 | if model.streaming: |
| 196 | # Streaming processes small windows per step. 2 seconds gives |
| 197 | # comfortable headroom while keeping the memory plan tight. |
| 198 | max_samples = 2 * model.sampling_rate |
| 199 | else: |
| 200 | max_samples = model.max_audio_len * model.sampling_rate |
| 201 | audio_tensor = torch.randn(min(93680, max_samples)) |
| 202 | shapes_collection = torch.export.ShapesCollection() |
| 203 | shapes_collection[audio_tensor] = {0: Dim.DYNAMIC(max=max_samples)} |
| 204 | with torch.no_grad(), torch.fx.experimental._config.patch( |
| 205 | backed_size_oblivious=True |
| 206 | ): |
| 207 | ep = torch.export.export( |
| 208 | model, (audio_tensor,), dynamic_shapes=shapes_collection, strict=True |
| 209 | ) |
| 210 | logging.debug(ep) |
| 211 | |
| 212 | if backend == "mlx": |
| 213 | from executorch.backends.mlx.partitioner import MLXPartitioner |
| 214 | |
| 215 | partitioner = [MLXPartitioner()] |
| 216 | else: |
| 217 | partitioner = [XnnpackPartitioner()] |
| 218 | |
| 219 | # to edge |
| 220 | edge: EdgeProgramManager = to_edge_transform_and_lower( |
| 221 | ep, |
| 222 | partitioner=partitioner, |
| 223 | compile_config=EdgeCompileConfig( |
| 224 | _check_ir_validity=False, |
| 225 | ), |
| 226 | ) |
| 227 | logging.debug(edge.exported_program()) |
| 228 | |
| 229 | # to executorch |
| 230 | exec_prog = edge.to_executorch() |
| 231 | os.makedirs(os.path.dirname(output_file) or ".", exist_ok=True) |
| 232 | with open(output_file, "wb") as file: |
| 233 | exec_prog.write_to_file(file) |
| 234 | |
| 235 | logging.debug("Done") |
| 236 | |
| 237 | |
| 238 | def export_processor( |
no test coverage detected