Skip to content

Commit ff2e779

Browse files
committed
[Executorch][to_backend] Introduce preprocess_multimethod
ghstack-source-id: ef43a13f55a422d375ff16ed06b230afd99f641e ghstack-comment-id: 2770905622 Pull Request resolved: #9823
1 parent 1808272 commit ff2e779

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

exir/backend/backend_details.py

+63-9
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ class BackendDetails(ABC):
5050
the decorators, this interface will be static, abstract and all inheritances are
5151
enforced to implement this method.
5252
53-
Args:
54-
edge_program: The original exported program. It will not be modified in place.
55-
compile_specs: List of values needed for compilation
56-
57-
Returns:
58-
PreprocessResult: It wraps the following information:
59-
processed_bytes -> bytes: A compiled blob - a binary that can run the desired program in the backend.
60-
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)
61-
to debug handle id attached in the original exported program.
6253
"""
6354

6455
@staticmethod
@@ -70,6 +61,69 @@ def preprocess(
7061
edge_program: ExportedProgram,
7162
compile_specs: List[CompileSpec],
7263
) -> PreprocessResult:
64+
"""
65+
Preprocesses an edge program and returns the preprocess result fo the given backend
66+
67+
Args:
68+
edge_program: The original exported program. It will not be modified in place.
69+
compile_specs: List of values needed for compilation
70+
71+
Returns:
72+
PreprocessResult: It wraps the following information:
73+
processed_bytes -> bytes: A compiled blob - a binary that can run the desired
74+
program in the backend.
75+
debug_handle_map (Optional[Dict[int, Tuple[int]]]): For profiling purposes, a
76+
map from the node_id in the final graph (either EXIR or the user's self-defined
77+
IR) to debug handle id attached in the original exported program.
78+
"""
7379
# Users should return a compiled blob - a binary that can run the desired
7480
# program in the backend.
7581
pass
82+
83+
@classmethod
84+
def preprocess_multimethod(
85+
cls,
86+
edge_programs: Dict[str, List[ExportedProgram]],
87+
compile_specs: Dict[str, List[List[CompileSpec]]],
88+
) -> Dict[str, list[PreprocessResult]]:
89+
"""
90+
Runs preprocess on all partitioned Edge Programs across multiple methods. This allows
91+
backends to share information across partitioned graphs. Backend can serialize shared
92+
data by putting the shared data into the data_store_output of the preprocess results.
93+
This will record the shared data used by that specific partition.
94+
95+
Default implementation is running the existing preprocess implementation on all
96+
97+
Args:
98+
edge_programs: Dictionary mapping the method name to a list of all the partitioned
99+
edge_programs from that method to be lowered.
100+
compile_specs: Dictionary mapping the method name to a list of compile_specs. The
101+
list of compile specs maps directly to the list of edge_programs for the
102+
same given method name i.e. edge_program[method_name][i] --> compile_specs[method_name][i]
103+
104+
Returns:
105+
Dictionary mapping the method name to a list of PreprocessResults. The list of
106+
PreprocessResults maps directly to the list of edge_programs for the same given
107+
method name. i.e. edge_program[method_name][i] --> result[method_name][i]
108+
109+
110+
"""
111+
preprocess_results = {}
112+
for method_name, programs in edge_programs.items():
113+
assert (
114+
method_name in compile_specs
115+
), f"Error: missing compile specs for {method_name}"
116+
compile_specs_for_method = compile_specs[method_name]
117+
assert len(compile_specs_for_method) == len(
118+
programs
119+
), f"Error: method {method_name} has {len(programs)} partitions but only {len(compile_specs_for_method)}"
120+
results_for_method = []
121+
for program, compile_spec_for_program in zip(
122+
programs, compile_specs_for_method
123+
):
124+
preprocess_result = cls.preprocess(program, compile_spec_for_program)
125+
results_for_method.append(preprocess_result)
126+
127+
preprocess_results[method_name] = results_for_method
128+
129+
return preprocess_results

0 commit comments

Comments
 (0)