Skip to content

[Executorch][to_backend] Introduce preprocess_multimethod #9823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 16, 2025
72 changes: 63 additions & 9 deletions exir/backend/backend_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ class BackendDetails(ABC):
the decorators, this interface will be static, abstract and all inheritances are
enforced to implement this method.

Args:
edge_program: The original exported program. It will not be modified in place.
compile_specs: List of values needed for compilation

Returns:
PreprocessResult: It wraps the following information:
processed_bytes -> bytes: A compiled blob - a binary that can run the desired program in the backend.
debug_handle_map (Optional[Dict[int, Tuple[int]]]): For profiling purposes, a map from the node_id in the final graph (either EXIR or the user's self-defined IR)
to debug handle id attached in the original exported program.
"""

@staticmethod
Expand All @@ -70,6 +61,69 @@ def preprocess(
edge_program: ExportedProgram,
compile_specs: List[CompileSpec],
) -> PreprocessResult:
"""
Preprocesses an edge program and returns the preprocess result fo the given backend

Args:
edge_program: The original exported program. It will not be modified in place.
compile_specs: List of values needed for compilation

Returns:
PreprocessResult: It wraps the following information:
processed_bytes -> bytes: A compiled blob - a binary that can run the desired
program in the backend.
debug_handle_map (Optional[Dict[int, Tuple[int]]]): For profiling purposes, a
map from the node_id in the final graph (either EXIR or the user's self-defined
IR) to debug handle id attached in the original exported program.
"""
# Users should return a compiled blob - a binary that can run the desired
# program in the backend.
pass

@classmethod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it will be the default method if the backend didn't implement their own preprocess multimethod, is it correct? If so, let's add some tests.

def preprocess_multimethod(
cls,
edge_programs: Dict[str, List[ExportedProgram]],
compile_specs: Dict[str, List[List[CompileSpec]]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the finest granularity which I believe cover all the use cases. For majority of the use case, we probably can support List[CompileSpec], which will be applied to all edge programs for all methods.

) -> Dict[str, list[PreprocessResult]]:
"""
Runs preprocess on all partitioned Edge Programs across multiple methods. This allows
backends to share information across partitioned graphs. Backend can serialize shared
data by putting the shared data into the data_store_output of the preprocess results.
This will record the shared data used by that specific partition.

Default implementation is running the existing preprocess implementation on all

Args:
edge_programs: Dictionary mapping the method name to a list of all the partitioned
edge_programs from that method to be lowered.
compile_specs: Dictionary mapping the method name to a list of compile_specs. The
list of compile specs maps directly to the list of edge_programs for the
same given method name i.e. edge_program[method_name][i] --> compile_specs[method_name][i]

Returns:
Dictionary mapping the method name to a list of PreprocessResults. The list of
PreprocessResults maps directly to the list of edge_programs for the same given
method name. i.e. edge_program[method_name][i] --> result[method_name][i]


"""
preprocess_results = {}
for method_name, programs in edge_programs.items():
assert (
method_name in compile_specs
), f"Error: missing compile specs for {method_name}"
compile_specs_for_method = compile_specs[method_name]
assert len(compile_specs_for_method) == len(
programs
), f"Error: method {method_name} has {len(programs)} partitions but only {len(compile_specs_for_method)}"
results_for_method = []
for program, compile_spec_for_program in zip(
programs, compile_specs_for_method
):
preprocess_result = cls.preprocess(program, compile_spec_for_program)
results_for_method.append(preprocess_result)

preprocess_results[method_name] = results_for_method

return preprocess_results
Loading