Export to ExecuTorch API Reference¶
- exir.to_edge(programs, constant_methods=None, compile_config=None)[source]¶
to_edge()
constructs an EdgeProgramManger from a set of exported programs in ATen dialect. Upon construction those programs are transformed into edge dialect.- Parameters
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”.
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.
compile_config – An optional argument used to provide greater control over the transformation to edge dialect process.
- Returns
EdgeProgramManager
- class exir.EdgeProgramManager(edge_programs, constant_methods=None)[source]¶
Package of one or more :class:’ExportedPrograms’ in Edge dialect. Designed to simplify lowering to ExecuTorch.
Allows easy applications of transforms across a collection of exported programs including the delegation of subgraphs.
Manages the second link in the lowering chain of ATen -> Edge -> ExecuTorch.
- property config_methods¶
Returns the set of config methods in this EdgeProgramManager.
- exported_program(method_name='forward')[source]¶
Returns the ExportedProgram specified by ‘method_name’.
- property methods¶
Returns the set of methods in this EdgeProgramManager.
- to_backend(partitioner)[source]¶
Returns a semantically-equivalent program to the one given as input, but with portions of each program in the EdgeProgramManager targeted for delegation as determined by the partitioner.
- Parameters
partitioner –
The partitioner can either be a Partitioner subclass, or a dictionary mapping method names to Partitioner subclass. If it is a Partitioner subclass, all programs in the given EdgeProgramManager will be lowered using the given partitioner. If it is a dictionary, only method names specified in the dictionary will be lowered with the given partitioner.
The Partitioner subclass is in charge with tagging portions of the input program for delegation. A valid partitioner must have partition_tags: Dict[str, DelegationSpec], where each key is a tag name and the nodes with same tag will be fused a one subgraph and delegated to backend specififed in delegation spec.
- Returns
A copy of the calling EdgeProgramManager with the specified subgraphs lowered.
- Return type
- to_executorch(config=None)[source]¶
Transforms the program to the ExecuTorch backend.
- Parameters
config – An optional argument used to provide greater control over
backend. (the transformation to the ExecuTorch) –
- Returns
A manager representing the state of the EdgeProgramManager after it has been transformed to the ExecuTorch backend.
- Return type
- transform(passes)[source]¶
Transforms the program according to the provided passes.
- Parameters
passes (just a list of) – The passes can either be a list of passes, or a
is (dictionary mapping method names to lists of passes. If it) –
passes –
EdgeProgramManager (all methods in the given) –
a (will be transformed with the provided passes. If it is) –
dictionary –
be (only method names specified in the dictionary will) –
passes. (transformed with their corresponding) –
- Returns
A copy of the calling EdgeProgramManager with the transformations applied.
- Return type
- class exir.ExecutorchProgramManager(execution_programs, config_methods=None, backend_config=None)[source]¶
Package of one or more :class:’ExportedPrograms’ in Execution dialect. Designed to simplify lowering to ExecuTorch.
When the ExecutorchProgramManager is constructed the ExportedPrograms in execution dialect are used to form the executorch binary (in a process called emission) and then serialized to a buffer.
Manages the final link in the lowering chain of ATen -> Edge -> ExecuTorch.
- property buffer¶
Returns a buffer containing the serialized ExecuTorch binary.
- property config_methods¶
Returns the set of config methods in this ExecutorchProgramManager.
- dump_executorch_program(verbose=False)[source]¶
Prints the ExecuTorch binary in a human readable format.
- Parameters
verbose (bool) – If False prints the binary in a condensed format. If True prints the binary 1-1 with the specification in the schema.
- exported_program(method_name='forward')[source]¶
Returns the ExportedProgram specified by ‘method_name’.
- property methods¶
Returns the set of methods in this ExecutorchProgramManager.
- exir.backend.backend_api.to_backend(args)[source]¶
- exir.backend.backend_api.to_backend(backend_id, edge_program, compile_specs)
- exir.backend.backend_api.to_backend(edge_program, partitioner)
A generic function the dispatch happens on the type of the first argument. There are currently to overloaded to_backend function:
Note: Python is dynamically-typed language and therefore cannot have proper method overloading as that requires the language to be able to discriminate between types at compile-time. @to_backend.register will attach the function to to_backend() base on the type of the first argument (type annotation is required). However, it can’t take multiple types as arguments.
def to_backend( backend_id: str, edge_graph_module: ExportedProgram, compile_specs: List[CompileSpec], ) -> LoweredBackendModule: def to_backend( graph_module: torch.fx.GraphModule, partitioner: Type[TPartitioner], ) -> torch.fx.GraphModule
- class exir.backend.backend_api.LoweredBackendModule(edge_program, backend_id, processed_bytes, compile_specs)[source]¶
A subclass of nn.Module that is generated for modules containing delegated functions. This is can be created by calling to_backend.
- Private Attributes:
backend_id: The backend’s name
processed_bytes: The delegate blobs created from backend.preprocess
- compile_specs: A list of backend-specific objects with static
metadata to configure the “compilation” process.
original_module: The original EXIR module
- program(emit_stacktrace=False)[source]¶
The idea in this function is to create a module based on the original module. The original module will look something like following:
opcode name target args kwargs ————- ——————- —————- —————————————— ——– placeholder arg0_1 arg0_1 () {} placeholder arg1_1 arg1_1 () {} call_function aten_repeat_default * (arg1_1, [4, 1]) {} call_function aten_mul_tensor * (aten_repeat_default, aten_repeat_default) {} call_function aten_add_tensor * (arg1_1, arg1_1) {} output output output ([aten_mul_tensor, aten_add_tensor],) {}
if the whole module is lowered, the resulting lowered module look like
opcode name target args kwargs ————- ———————— ————————— ———————————- ——– placeholder arg0_1 arg0_1 () {} placeholder arg1_1 arg1_1 () {} get_attr lowered_module_0 lowered_module_0 () {} call_function executorch_call_delegate executorch_call_delegate (lowered_module_0, arg0_1, arg1_1) {} call_function getitem <built-in function getitem> (executorch_call_delegate, 0) {} call_function getitem_1 <built-in function getitem> (executorch_call_delegate, 1) {} output output_1 output ([getitem, getitem_1],) {}
We’ll remove all call_function nodes, insert an call_delegate node, inserting getitems nodes to get the result for call_delegate node and return the list of getitems as the output