xref: /aosp_15_r20/external/executorch/test/models/targets.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2
3def define_common_targets():
4    """
5    Defines targets that should be shared between fbcode and xplat.
6
7    The directory containing this targets.bzl file should also contain both
8    TARGETS and BUCK files that call this function.
9    """
10    runtime.python_library(
11        name = "linear_model",
12        srcs = ["linear_model.py"],
13        deps = [
14            "//caffe2:torch",
15        ],
16        visibility = [],  # Private
17    )
18
19    runtime.python_library(
20        name = "generate_linear_out_bundled_program_lib",
21        srcs = ["generate_linear_out_bundled_program.py"],
22        deps = [
23            ":linear_model",
24            "//caffe2:torch",
25            "//executorch/devtools/bundled_program:config",
26            "//executorch/devtools:lib",
27            "//executorch/devtools/bundled_program/serialize:lib",
28            "//executorch/exir:lib",
29            "//executorch/exir/_serialize:lib",
30        ],
31    )
32
33    runtime.python_binary(
34        name = "generate_linear_out_bundled_program",
35        main_module = "executorch.test.models.generate_linear_out_bundled_program",
36        deps = [
37            ":generate_linear_out_bundled_program_lib",
38        ],
39    )
40
41    runtime.python_library(
42        name = "export_program_lib",
43        srcs = ["export_program.py"],
44        deps = [
45            "//caffe2:torch",
46            "//executorch/test/end2end:exported_module",
47        ],
48        visibility = [],  # Private
49    )
50
51    runtime.python_binary(
52        name = "export_program",
53        main_module = "executorch.test.models.export_program",
54        deps = [
55            ":export_program_lib",
56        ],
57        visibility = [],  # Private
58    )
59
60    # Class names of nn.Modules for :exported_programs to export.
61    MODULES_TO_EXPORT = [
62        "ModuleAdd",
63        "ModuleAddHalf",
64        "ModuleBasic",
65        "ModuleLinear",
66        "ModuleMultipleEntry",
67        "ModuleIndex",
68        "ModuleDynamicCatUnallocatedIO",
69        "ModuleSimpleTrain",
70    ]
71
72    # Generates Executorch .pte program files for various modules at build time.
73    # To use one, depend on a target like ":exported_programs[ModuleAdd.pte]".
74    runtime.genrule(
75        name = "exported_programs",
76        cmd = "$(exe :export_program) --modules " + ",".join(MODULES_TO_EXPORT) + " --outdir $OUT",
77        outs = {
78            fname + seg_suffix + ".pte": [fname + seg_suffix + ".pte"]
79            for fname in MODULES_TO_EXPORT
80            for seg_suffix in ["", "-no-constant-segment"]
81        },
82        default_outs = ["."],
83        visibility = [
84            "//executorch/...",
85            # This genrule can't run in xplat since it uses EXIR, so make its
86            # output visible to xplat tests. This is an exceptional case, and
87            # typically shouldn't be done.
88            "fbsource//xplat/executorch/...",
89        ],
90        # Allow the xplat entry in the visibility list. This is an exceptional
91        # case, and typically shouldn't be done.
92        _is_external_target = True,
93    )
94
95    runtime.python_library(
96        name = "export_delegated_program_lib",
97        srcs = ["export_delegated_program.py"],
98        deps = [
99            "//caffe2:torch",
100            "//executorch/exir/backend:backend_api",
101            "//executorch/exir/backend/test:backend_with_compiler_demo",
102            "//executorch/exir:lib",
103        ],
104        visibility = [],  # Private
105    )
106
107    runtime.python_binary(
108        name = "export_delegated_program",
109        main_module = "executorch.test.models.export_delegated_program",
110        # Use the https://www.internalfb.com/intern/wiki/XAR/ format so that
111        # python files in the archive have predictable names/paths even in opt
112        # mode. Without this `par_style` override, torch dynamo fails to skip
113        # the tracing of files under the `caffe2/torch/_dynamo` directory; the
114        # skips are based on the paths in the `__file__` strings at runtime, but
115        # normal PAR mangles them in an incompatible way in opt mode. See
116        # T151983912 for more background.
117        par_style = "xar",
118        deps = [
119            ":export_delegated_program_lib",
120        ],
121        visibility = [],  # Private
122    )
123
124    # Class names of nn.Modules for :exported_delegated_programs to export.
125    DELEGATED_MODULES_TO_EXPORT = [
126        "ModuleAddMul",
127    ]
128
129    # Name of the backend to use when exporting delegated programs.
130    BACKEND_ID = "StubBackend"
131
132    # Generates Executorch .pte program files for various modules at build time.
133    # To use one, depend on a target like
134    # ":exported_delegated_programs[ModuleAdd.pte]" or
135    # ":exported_delegated_programs[ModuleAdd-nosegments.pte]" (which does not
136    # extract the delegate data blobs into segments).
137    runtime.genrule(
138        name = "exported_delegated_programs",
139        cmd = "$(exe :export_delegated_program)" +
140              " --modules " + ",".join(DELEGATED_MODULES_TO_EXPORT) +
141              " --backend_id " + BACKEND_ID +
142              " --outdir $OUT",
143        outs = {
144            fname + seg_suffix + da_suffix + ".pte": [fname + seg_suffix + da_suffix + ".pte"]
145            for fname in DELEGATED_MODULES_TO_EXPORT
146            for seg_suffix in ["", "-nosegments"]
147            # "da" = delegate alignment
148            for da_suffix in ["", "-da1024"]
149        },
150        default_outs = ["."],
151        visibility = [
152            "//executorch/runtime/executor/test/...",
153            "//executorch/test/...",
154        ],
155    )
156