xref: /aosp_15_r20/external/executorch/backends/example/example_backend.py (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1*523fa7a6SAndroid Build Coastguard Worker# Copyright (c) Meta Platforms, Inc. and affiliates.
2*523fa7a6SAndroid Build Coastguard Worker# All rights reserved.
3*523fa7a6SAndroid Build Coastguard Worker#
4*523fa7a6SAndroid Build Coastguard Worker# This source code is licensed under the BSD-style license found in the
5*523fa7a6SAndroid Build Coastguard Worker# LICENSE file in the root directory of this source tree.
6*523fa7a6SAndroid Build Coastguard Worker
7*523fa7a6SAndroid Build Coastguard Workerimport copy
8*523fa7a6SAndroid Build Coastguard Workerfrom typing import final, List
9*523fa7a6SAndroid Build Coastguard Worker
10*523fa7a6SAndroid Build Coastguard Workerfrom executorch.backends.example.example_backend_delegate_passes.merge_to_dim_pass import (
11*523fa7a6SAndroid Build Coastguard Worker    MergeToDimPass,
12*523fa7a6SAndroid Build Coastguard Worker)
13*523fa7a6SAndroid Build Coastguard Workerfrom executorch.backends.example.example_backend_delegate_passes.permute_memory_formats_pass import (
14*523fa7a6SAndroid Build Coastguard Worker    PermuteMemoryFormatsPass,
15*523fa7a6SAndroid Build Coastguard Worker)
16*523fa7a6SAndroid Build Coastguard Worker
17*523fa7a6SAndroid Build Coastguard Workerfrom executorch.exir.backend.backend_details import (
18*523fa7a6SAndroid Build Coastguard Worker    BackendDetails,
19*523fa7a6SAndroid Build Coastguard Worker    ExportedProgram,
20*523fa7a6SAndroid Build Coastguard Worker    PreprocessResult,
21*523fa7a6SAndroid Build Coastguard Worker)
22*523fa7a6SAndroid Build Coastguard Workerfrom executorch.exir.backend.compile_spec_schema import CompileSpec
23*523fa7a6SAndroid Build Coastguard Worker
24*523fa7a6SAndroid Build Coastguard Worker
25*523fa7a6SAndroid Build Coastguard Worker@final
26*523fa7a6SAndroid Build Coastguard Workerclass ExampleBackend(BackendDetails):
27*523fa7a6SAndroid Build Coastguard Worker    @staticmethod
28*523fa7a6SAndroid Build Coastguard Worker    def preprocess(
29*523fa7a6SAndroid Build Coastguard Worker        edge_program: ExportedProgram,
30*523fa7a6SAndroid Build Coastguard Worker        compile_specs: List[CompileSpec],
31*523fa7a6SAndroid Build Coastguard Worker    ) -> PreprocessResult:
32*523fa7a6SAndroid Build Coastguard Worker        print("entering  the lowerable parts in ExampleBackend.preprocess....")
33*523fa7a6SAndroid Build Coastguard Worker
34*523fa7a6SAndroid Build Coastguard Worker        copy_edge_program = copy.deepcopy(edge_program)
35*523fa7a6SAndroid Build Coastguard Worker        graph_module = copy_edge_program.graph_module
36*523fa7a6SAndroid Build Coastguard Worker        graph_module_res = PermuteMemoryFormatsPass()(graph_module)
37*523fa7a6SAndroid Build Coastguard Worker        assert graph_module_res is not None
38*523fa7a6SAndroid Build Coastguard Worker        graph_module_res = MergeToDimPass()(graph_module_res.graph_module)
39*523fa7a6SAndroid Build Coastguard Worker        assert graph_module_res is not None
40*523fa7a6SAndroid Build Coastguard Worker        processed_bytes = str(graph_module_res.graph_module.graph)
41*523fa7a6SAndroid Build Coastguard Worker        return PreprocessResult(bytes(processed_bytes, encoding="utf8"))
42