xref: /aosp_15_r20/external/pytorch/tools/BUCK.bzl (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# @lint-ignore-every FBCODEBZLADDLOADS
2load("//tools/build_defs:glob_defs.bzl", "subdir_glob")
3
4# shared by internal and OSS BUCK
5def define_tools_targets(
6        python_binary,
7        python_library,
8        python_test,
9        third_party,
10        torchgen_deps,
11        contacts = []):
12    python_library(
13        name = "substitutelib",
14        srcs = ["substitute.py"],
15        base_module = "",
16    )
17
18    python_binary(
19        name = "substitute",
20        main_module = "substitute",
21        visibility = ["PUBLIC"],
22        deps = [
23            ":substitutelib",
24        ],
25    )
26
27    python_library(
28        name = "jit",
29        srcs = glob([
30            "jit/*.py",
31            "jit/templates/*",
32        ]),
33        base_module = "tools",
34        visibility = ["PUBLIC"],
35        deps = [
36            torchgen_deps,
37        ],
38    )
39
40    python_binary(
41        name = "gen_unboxing_bin",
42        main_module = "tools.jit.gen_unboxing",
43        visibility = [
44            "PUBLIC",
45        ],
46        deps = [
47            ":jit",
48        ],
49    )
50
51    python_library(
52        name = "gen_selected_mobile_ops_header",
53        srcs = ["lite_interpreter/gen_selected_mobile_ops_header.py"],
54        base_module = "tools",
55        visibility = ["PUBLIC"],
56    )
57
58    python_library(
59        name = "gen_oplist_lib",
60        srcs = subdir_glob([
61            ("code_analyzer", "gen_oplist.py"),
62            ("code_analyzer", "gen_op_registration_allowlist.py"),
63        ]),
64        base_module = "tools.code_analyzer",
65        tests = [
66            ":gen_oplist_test",
67        ],
68        visibility = ["PUBLIC"],
69        deps = [
70            ":gen_selected_mobile_ops_header",
71            torchgen_deps,
72            third_party("pyyaml"),
73        ],
74    )
75
76    python_binary(
77        name = "gen_oplist",
78        main_module = "tools.code_analyzer.gen_oplist",
79        visibility = ["PUBLIC"],
80        deps = [
81            ":gen_oplist_lib",
82        ],
83    )
84
85    python_library(
86        name = "gen_operators_yaml_lib",
87        srcs = subdir_glob([
88            ("code_analyzer", "gen_operators_yaml.py"),
89            ("code_analyzer", "gen_op_registration_allowlist.py"),
90        ]),
91        base_module = "",
92        tests = [
93            ":gen_operators_yaml_test",
94        ],
95        deps = [
96            third_party("pyyaml"),
97            torchgen_deps,
98        ],
99    )
100
101    python_binary(
102        name = "gen_operators_yaml",
103        main_module = "gen_operators_yaml",
104        visibility = ["PUBLIC"],
105        deps = [
106            ":gen_operators_yaml_lib",
107        ],
108    )
109
110    python_library(
111        name = "autograd",
112        srcs = glob(["autograd/*.py"]),
113        base_module = "tools",
114        resources = [
115            "autograd/deprecated.yaml",
116            "autograd/derivatives.yaml",
117            "autograd/templates/ADInplaceOrViewType.cpp",
118            "autograd/templates/Functions.cpp",
119            "autograd/templates/Functions.h",
120            "autograd/templates/TraceType.cpp",
121            "autograd/templates/VariableType.cpp",
122            "autograd/templates/VariableType.h",
123            "autograd/templates/ViewFuncs.cpp",
124            "autograd/templates/ViewFuncs.h",
125            "autograd/templates/annotated_fn_args.py.in",
126            "autograd/templates/python_enum_tag.cpp",
127            "autograd/templates/python_fft_functions.cpp",
128            "autograd/templates/python_functions.cpp",
129            "autograd/templates/python_functions.h",
130            "autograd/templates/python_linalg_functions.cpp",
131            "autograd/templates/python_nested_functions.cpp",
132            "autograd/templates/python_nn_functions.cpp",
133            "autograd/templates/python_return_types.h",
134            "autograd/templates/python_return_types.cpp",
135            "autograd/templates/python_sparse_functions.cpp",
136            "autograd/templates/python_special_functions.cpp",
137            "autograd/templates/python_torch_functions.cpp",
138            "autograd/templates/python_variable_methods.cpp",
139            "autograd/templates/variable_factories.h",
140        ],
141        visibility = ["PUBLIC"],
142        deps = [
143            third_party("pyyaml"),
144            torchgen_deps,
145        ],
146    )
147
148    python_library(
149        name = "generate_code",
150        srcs = [
151            "setup_helpers/generate_code.py",
152        ],
153        base_module = "tools",
154        deps = [
155            ":autograd",
156            ":jit",
157            torchgen_deps,
158        ],
159    )
160
161    python_binary(
162        name = "generate_code_bin",
163        main_module = "tools.setup_helpers.generate_code",
164        # Windows does not support inplace:
165        # https://github.com/facebook/buck/issues/2161.
166        #
167        # Note that //arvr/mode/embedded/win/clang-aarch64-release sets
168        # its target platform to
169        # ovr_config//platform/embedded:clang-aarch64-linux-release, hence
170        # that is why we are selecting that OS to trigger this behavior.
171        package_style = select({
172            "DEFAULT": "inplace",
173            "ovr_config//os:linux-arm64": "standalone",
174        }),
175        visibility = ["PUBLIC"],
176        # Because Windows does not support inplace packaging, we need to
177        # ensure it is unzipped before executing it, otherwise it will not
178        # be able to find any resources using path manipulation.
179        #
180        # See note above about why the OS is Linux here and not Windows.
181        zip_safe = select({
182            "DEFAULT": True,
183            "ovr_config//os:linux-arm64": False,
184        }),
185        deps = [
186            ":generate_code",
187        ],
188    )
189
190    python_library(
191        name = "gen-version-header-lib",
192        srcs = [
193            "setup_helpers/gen_version_header.py",
194        ],
195        base_module = "",
196        deps = [],
197    )
198
199    python_binary(
200        name = "gen-version-header",
201        main_module = "setup_helpers.gen_version_header",
202        visibility = ["PUBLIC"],
203        deps = [
204            ":gen-version-header-lib",
205        ],
206    )
207
208    python_library(
209        name = "gen_aten_vulkan_spv_lib",
210        srcs = [
211            "gen_vulkan_spv.py",
212        ],
213        base_module = "tools",
214        deps = [
215            torchgen_deps,
216        ],
217    )
218
219    python_binary(
220        name = "gen_aten_vulkan_spv_bin",
221        main_module = "tools.gen_vulkan_spv",
222        visibility = [
223            "PUBLIC",
224        ],
225        deps = [
226            ":gen_aten_vulkan_spv_lib",
227        ],
228    )
229
230    python_test(
231        name = "vulkan_codegen_test",
232        srcs = [
233            "test/test_vulkan_codegen.py",
234        ],
235        contacts = contacts,
236        visibility = ["PUBLIC"],
237        deps = [
238            ":gen_aten_vulkan_spv_lib",
239        ],
240    )
241
242    python_test(
243        name = "selective_build_test",
244        srcs = [
245            "test/test_selective_build.py",
246        ],
247        contacts = contacts,
248        visibility = ["PUBLIC"],
249        deps = [
250            torchgen_deps,
251        ],
252    )
253
254    python_test(
255        name = "gen_oplist_test",
256        srcs = [
257            "test/gen_oplist_test.py",
258        ],
259        contacts = contacts,
260        visibility = ["PUBLIC"],
261        deps = [
262            ":gen_oplist_lib",
263        ],
264    )
265
266    python_test(
267        name = "gen_operators_yaml_test",
268        srcs = [
269            "test/gen_operators_yaml_test.py",
270        ],
271        visibility = ["PUBLIC"],
272        contacts = contacts,
273        deps = [
274            ":gen_operators_yaml_lib",
275        ],
276    )
277
278    python_test(
279        name = "test_codegen",
280        srcs = [
281            "test/test_codegen.py",
282        ],
283        contacts = contacts,
284        visibility = ["PUBLIC"],
285        deps = [
286            torchgen_deps,
287            ":autograd",
288        ],
289    )
290
291    python_test(
292        name = "test_torchgen_executorch",
293        srcs = [
294            "test/test_executorch_gen.py",
295            "test/test_executorch_signatures.py",
296            "test/test_executorch_types.py",
297            "test/test_executorch_unboxing.py",
298        ],
299        contacts = contacts,
300        visibility = ["PUBLIC"],
301        deps = [
302            torchgen_deps,
303        ],
304    )
305