|
42 | 42 | NeuropilotQuantizer, |
43 | 43 | Precision, |
44 | 44 | ) |
| 45 | +from executorch.exir.backend.backend_api import to_backend, MethodProgramsPartitionerSpec |
45 | 46 | from executorch.exir.backend.backend_details import CompileSpec |
46 | 47 | from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e |
47 | 48 | from tqdm import tqdm |
@@ -331,51 +332,65 @@ def export_to_et_ir( |
331 | 332 | prepared_graph(*example_inputs) # dummy calibration |
332 | 333 | converted_graph = convert_pt2e(prepared_graph, fold_quantize=False) |
333 | 334 |
|
334 | | - print("Getting ATen Dialect Graph") |
| 335 | + method_to_edge_program = {} |
| 336 | + method_to_partitioner = {} |
| 337 | + edge_compile_config=exir.EdgeCompileConfig(_check_ir_validity=False) |
| 338 | + |
| 339 | + model_shared_key_name = f'{exp_name}_{chunk_idx}' |
| 340 | + |
335 | 341 | # Fixed Shape Export Here |
336 | 342 | for shape, ntok_and_cache in export_shapes.items(): |
337 | | - dest_path = get_dest_path(output_folder, exp_name, shape, chunk_idx) |
338 | | - print(f"Exporting Shape {shape} to:\n{dest_path}") |
| 343 | + model_fname = f'{exp_name}_{shape}_{chunk_idx}' |
339 | 344 | example_inputs = model.get_example_inputs(*ntok_and_cache) |
| 345 | + print(f"Getting ATen Dialect Graph for {exp_name} {shape} chunk {chunk_idx}") |
340 | 346 | aten_dialect: exir.ExportedProgram = torch.export.export( |
341 | 347 | converted_graph, example_inputs, strict=True |
342 | 348 | ) |
343 | 349 |
|
344 | | - print("Lowering to Edge Dialect Graph") |
345 | | - edge_program: exir.EdgeProgramManager = exir.to_edge( |
346 | | - aten_dialect, |
347 | | - compile_config=exir.EdgeCompileConfig(_check_ir_validity=False), |
348 | | - ) |
| 350 | + method_to_edge_program[f'{model_fname}'] = exir.to_edge(aten_dialect).exported_program() |
349 | 351 | del aten_dialect |
350 | 352 |
|
351 | | - print("Delegating Edge Program to Neuropilot Backend") |
352 | 353 | compile_spec = [ |
353 | 354 | CompileSpec("gno", struct.pack("3s", b"LTS")), |
354 | 355 | CompileSpec("gno-exp", struct.pack("0s", b"")), |
355 | 356 | CompileSpec("gno-non-4d-tiling", struct.pack("0s", b"")), |
356 | 357 | CompileSpec("ImportForever", struct.pack("?", True)), |
| 358 | + CompileSpec("ExtractSharedBlobKey", model_shared_key_name.encode()), |
357 | 359 | ] |
358 | | - partitioner = NeuropilotPartitioner(compile_spec) |
359 | | - delegated_program = edge_program.to_backend(partitioner) |
360 | | - print("Exported Delegated Program:") |
361 | | - print(delegated_program.exported_program()) |
362 | | - del edge_program |
363 | | - |
364 | | - print("Transforming delegated program to executorch backend") |
365 | | - executorch_program = delegated_program.to_executorch( |
366 | | - config=exir.ExecutorchBackendConfig( |
367 | | - memory_planning_pass=exir.passes.MemoryPlanningPass( |
368 | | - alloc_graph_input=False, |
369 | | - alloc_graph_output=False, |
370 | | - ), |
371 | | - extract_delegate_segments=True, |
372 | | - ) |
| 360 | + method_to_partitioner[f'{model_fname}'] = NeuropilotPartitioner(compile_spec) |
| 361 | + |
| 362 | + print("Delegating Edge Program to Neuropilot Backend") |
| 363 | + delegated_program = to_backend( |
| 364 | + MethodProgramsPartitionerSpec( |
| 365 | + method_to_edge_program, |
| 366 | + method_to_partitioner |
373 | 367 | ) |
| 368 | + ) |
374 | 369 |
|
375 | | - print(f"ET Model Dest: {dest_path}\n") |
376 | | - os.makedirs(dest_path.rsplit("/", 1)[0], exist_ok=True) |
377 | | - with open(dest_path, "wb") as file: |
378 | | - file.write(executorch_program.buffer) |
| 370 | + edge_manager = exir.EdgeProgramManager( |
| 371 | + delegated_program, |
| 372 | + compile_config=edge_compile_config |
| 373 | + ) |
| 374 | + del delegated_program |
| 375 | + |
| 376 | + print("Transforming delegated program to executorch backend") |
| 377 | + executorch_program = edge_manager.to_executorch( |
| 378 | + config=exir.ExecutorchBackendConfig( |
| 379 | + memory_planning_pass=exir.passes.MemoryPlanningPass( |
| 380 | + alloc_graph_input=False, |
| 381 | + alloc_graph_output=False, |
| 382 | + ), |
| 383 | + extract_delegate_segments=True, |
| 384 | + ) |
| 385 | + ) |
| 386 | + del edge_manager |
| 387 | + print(f'\n Model Size: {len(executorch_program.buffer)}') |
| 388 | + |
| 389 | + dest_path = get_dest_path(output_folder, exp_name, None, chunk_idx) |
| 390 | + print(f"{exp_name} ET Model chunk {chunk_idx} Dest: {dest_path}\n") |
| 391 | + os.makedirs(dest_path.rsplit("/", 1)[0], exist_ok=True) |
| 392 | + with open(dest_path, "wb") as file: |
| 393 | + file.write(executorch_program.buffer) |
379 | 394 |
|
380 | 395 |
|
381 | 396 | def main(): |
|
0 commit comments