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