:func:`to_edge` constructs an EdgeProgramManager from a set of exported programs in ATen dialect. Upon construction those programs are transformed into edge dialect. Args: programs: Can be a single ExportedProgram or a dictionary mapping function names to their corresponding Ex
(
programs: Union[ExportedProgram, Dict[str, ExportedProgram]],
constant_methods: Optional[Dict[str, Any]] = None,
compile_config: Optional[EdgeCompileConfig] = None,
generate_etrecord: bool = False,
)
| 1449 | |
| 1450 | @et_logger("to_edge") |
| 1451 | def to_edge( |
| 1452 | programs: Union[ExportedProgram, Dict[str, ExportedProgram]], |
| 1453 | constant_methods: Optional[Dict[str, Any]] = None, |
| 1454 | compile_config: Optional[EdgeCompileConfig] = None, |
| 1455 | generate_etrecord: bool = False, |
| 1456 | ) -> "EdgeProgramManager": |
| 1457 | """ |
| 1458 | :func:`to_edge` constructs an EdgeProgramManager from a set of exported programs in |
| 1459 | ATen dialect. Upon construction those programs are transformed into edge dialect. |
| 1460 | |
| 1461 | Args: |
| 1462 | programs: Can be a single ExportedProgram or a dictionary mapping function names to their corresponding ExportedPrograms. If only a single ExportedProgram is provided it will be assigned the name "forward". |
| 1463 | |
| 1464 | constant_methods: An optional dictionary of method name to the constant value returned by that method in eager mode. Often used to store config information on Edge models. |
| 1465 | |
| 1466 | compile_config: An optional argument used to provide greater control over the transformation to edge dialect process. |
| 1467 | |
| 1468 | generate_etrecord: An optional argument used to generate an etrecord for debugging purposes. Default is False. |
| 1469 | |
| 1470 | Returns: |
| 1471 | EdgeProgramManager |
| 1472 | """ |
| 1473 | assert not isinstance(constant_methods, EdgeCompileConfig) |
| 1474 | config = compile_config or EdgeCompileConfig() |
| 1475 | if not isinstance(programs, dict): |
| 1476 | aten_programs = {"forward": programs} |
| 1477 | else: |
| 1478 | aten_programs = programs |
| 1479 | |
| 1480 | edge_programs: Dict[str, ExportedProgram] = {} |
| 1481 | |
| 1482 | for name, program in aten_programs.items(): |
| 1483 | # Decompose to Core ATen |
| 1484 | table = _default_decomposition_table() |
| 1485 | preserve_ops = [] |
| 1486 | if compile_config: |
| 1487 | preserve_ops = compile_config.preserve_ops |
| 1488 | for op in compile_config.preserve_ops: |
| 1489 | table.pop(op, None) |
| 1490 | program = program.run_decompositions(table) |
| 1491 | |
| 1492 | if config._check_ir_validity: |
| 1493 | # Remove invalid assert ops, such as _assert_tensor_metadata. |
| 1494 | # This pass is run in _generate_edge_program; it is required here to |
| 1495 | # ensure the graph is in ATen dialect before verification. |
| 1496 | gm = program.graph_module |
| 1497 | gm_res = RemoveNonCoreAtenOpGraphAssertsPass()(gm) |
| 1498 | assert gm_res is not None |
| 1499 | gm = gm_res.graph_module |
| 1500 | try: |
| 1501 | EXIRATenDialectVerifier( |
| 1502 | edge_compile_config=config, |
| 1503 | class_only=False, |
| 1504 | )(gm) |
| 1505 | except ExportError as e: |
| 1506 | logging.info(f"Input program {name} is not in ATen dialect.") |
| 1507 | raise e |
| 1508 |