(
output_folder,
exp_name,
model,
precision,
max_num_token,
max_cache_size,
chunk_idx,
export_shapes,
platform_b,
cal_dataset=None,
)
| 322 | |
| 323 | |
| 324 | def export_to_et_ir( |
| 325 | output_folder, |
| 326 | exp_name, |
| 327 | model, |
| 328 | precision, |
| 329 | max_num_token, |
| 330 | max_cache_size, |
| 331 | chunk_idx, |
| 332 | export_shapes, |
| 333 | platform_b, |
| 334 | cal_dataset=None, |
| 335 | ): |
| 336 | print(f"Exporting Chunk {chunk_idx} to PTE") |
| 337 | example_inputs, dynamic_shapes = model.get_example_inputs( |
| 338 | max_num_token, max_cache_size, True |
| 339 | ) |
| 340 | print("Getting pre autograd ATen Dialect Graph") |
| 341 | pre_autograd_aten_dialect = torch.export.export( |
| 342 | model, example_inputs, dynamic_shapes=dynamic_shapes, strict=True |
| 343 | ).module() # NOTE: Will be replaced with export |
| 344 | quantizer = NeuropilotQuantizer() |
| 345 | quantizer.setup_precision(getattr(Precision, precision)) |
| 346 | prepared_graph = prepare_pt2e(pre_autograd_aten_dialect, quantizer) |
| 347 | # at this point quant min max are inf |
| 348 | if cal_dataset is not None: |
| 349 | calibrate_model(prepared_graph, cal_dataset, str(chunk_idx)) |
| 350 | else: |
| 351 | prepared_graph(*example_inputs) # dummy calibration |
| 352 | converted_graph = convert_pt2e(prepared_graph, fold_quantize=False) |
| 353 | |
| 354 | method_to_edge_program = {} |
| 355 | method_to_partitioner = {} |
| 356 | edge_compile_config = exir.EdgeCompileConfig(_check_ir_validity=False) |
| 357 | |
| 358 | model_shared_key_name = f"{exp_name}_{chunk_idx}" |
| 359 | |
| 360 | # Fixed Shape Export Here |
| 361 | for shape, ntok_and_cache in export_shapes.items(): |
| 362 | model_fname = f"{exp_name}_{shape}_{chunk_idx}" |
| 363 | example_inputs = model.get_example_inputs(*ntok_and_cache) |
| 364 | print(f"Getting ATen Dialect Graph for {exp_name} {shape} chunk {chunk_idx}") |
| 365 | aten_dialect: exir.ExportedProgram = torch.export.export( |
| 366 | converted_graph, example_inputs, strict=True |
| 367 | ) |
| 368 | |
| 369 | method_to_edge_program[f"{model_fname}"] = exir.to_edge( |
| 370 | aten_dialect |
| 371 | ).exported_program() |
| 372 | del aten_dialect |
| 373 | |
| 374 | compile_spec = [ |
| 375 | CompileSpec("gno", b"LTS"), |
| 376 | CompileSpec("gno-exp", b""), |
| 377 | CompileSpec("gno-non-4d-tiling", b""), |
| 378 | CompileSpec("ImportForever", struct.pack("?", True)), |
| 379 | CompileSpec("platform-config", platform_b), |
| 380 | CompileSpec("ExtractSharedBlobKey", model_shared_key_name.encode()), |
| 381 | ] |
no test coverage detected