Skip to content

Commit 251ee69

Browse files
mattiasbrandmeta-codesync[bot]
authored andcommitted
Build the CUDA backend outside fbcode, with the LLM cache optional
Summary: The CUDA backend's C++ runtime builds only in fbcode, and only with the LLM extension's KV cache linked in. So a C++ application that runs an exported vision or audio model on an NVIDIA GPU can't use it from any other Buck cell, cross-compiled aarch64 targets such as Jetson included. To run the same AOTInductor-compiled kernels it has to link LibTorch instead. XNNPACK, Vulkan and QNN already define their C++ targets in both cells; this does the same for CUDA. - **Targets in both cells.** The `aoti/slim`, `extension/cuda` and `cuda/runtime` C++ targets are now defined outside fbcode too. There they carry `compatible_with = ["ovr_config//cuda:has_cuda"]`, so CPU-only builds skip them instead of failing to compile, and SlimTensor `storage` picks up its CUDA deps through a `select`. The Python AOTI targets stay fbcode-only. - **Public headers.** Headers that dependents include (`slim_tensor.h`, `storage.h`, `runtime_api.h`, the shim and factory headers) move from `headers` to `exported_headers`, and `runtime_api`'s CUDA dependency becomes an exported external dep. Outside fbcode `headers` are private to the target, so nothing could compile against them. fbcode already exports both, so nothing changes there. - **LLM cache optional, as in CMake.** CMake already builds `cuda_kv_cache.cpp` and `cuda_kv_pool.cpp` only with `EXECUTORCH_BUILD_EXTENSION_LLM`. Buck now does the same outside fbcode: the cache code in `cuda_backend.cpp` and `cuda_kv_cache.h` is behind that define, and the define is exported so that dependents see the same declarations as the backend. CMake now exports it too (`PUBLIC` instead of `PRIVATE` on `aoti_cuda_backend`), since the header's declarations depend on it; `test_cuda_kv_cache` and `cuda_batching` include that header. fbcode keeps the cache. A backend built without it still refuses a program whose KV cache lives off-graph, because `requires_offgraph_kv_storage` reads that from the program's constants and sits outside the gate. - **Build flags.** The `nvcc` architecture flags stay fbcode-only. Outside fbcode the shims undefine `CPU_CAPABILITY_AVX2` and `CPU_CAPABILITY_AVX512` for CUDA sources: c10 exports them on x86, and `Half.h` then pulls host AVX2 `vec_half` code into nvcc compiles. `cuda-lazy` maps to `libcudart`, and the shims link `curand` and `thrust` for `rand.cu` and `sort.cu`. fbcode builds keep the same sources and flags, and the CMake build is untouched. Differential Revision: D129654471
1 parent f2674fb commit 251ee69

18 files changed

Lines changed: 130 additions & 129 deletions

File tree

‎backends/aoti/slim/c10/core/targets.bzl‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2-
load("@fbsource//tools/build_defs:fbsource_utils.bzl", "is_fbcode")
32

43
def define_common_targets():
54
"""Define targets for SlimTensor c10 core module."""
65

7-
if not is_fbcode():
8-
return
9-
106
# Header-only library for DeviceType
117
runtime.cxx_library(
128
name = "device_type",
13-
headers = [
9+
exported_headers = [
1410
"DeviceType.h",
1511
],
1612
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -22,7 +18,7 @@ def define_common_targets():
2218
# Header-only library for Device
2319
runtime.cxx_library(
2420
name = "device",
25-
headers = [
21+
exported_headers = [
2622
"Device.h",
2723
],
2824
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -35,7 +31,7 @@ def define_common_targets():
3531
# Header-only library for ScalarType
3632
runtime.cxx_library(
3733
name = "scalar_type",
38-
headers = [
34+
exported_headers = [
3935
"ScalarType.h",
4036
],
4137
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -48,7 +44,7 @@ def define_common_targets():
4844
# Header-only library for SizesAndStrides
4945
runtime.cxx_library(
5046
name = "sizes_and_strides",
51-
headers = [
47+
exported_headers = [
5248
"SizesAndStrides.h",
5349
],
5450
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -62,7 +58,7 @@ def define_common_targets():
6258
# Header-only library for Contiguity
6359
runtime.cxx_library(
6460
name = "contiguity",
65-
headers = [
61+
exported_headers = [
6662
"Contiguity.h",
6763
],
6864
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -74,7 +70,7 @@ def define_common_targets():
7470
# Header-only library for WrapDimMinimal
7571
runtime.cxx_library(
7672
name = "wrap_dim_minimal",
77-
headers = [
73+
exported_headers = [
7874
"WrapDimMinimal.h",
7975
],
8076
visibility = ["@EXECUTORCH_CLIENTS"],

‎backends/aoti/slim/c10/cuda/BUCK‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
2-
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
31
load(":targets.bzl", "define_common_targets")
42

53
oncall("executorch")
64

7-
fbcode_target(_kind = define_common_targets,)
5+
define_common_targets()

‎backends/aoti/slim/c10/macros/BUCK‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
21
load("targets.bzl", "define_common_targets")
32

43
oncall("executorch")
54

6-
fbcode_target(_kind = define_common_targets,)
5+
define_common_targets()

‎backends/aoti/slim/c10/macros/targets.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def define_common_targets():
66
# Header-only library for Macros
77
runtime.cxx_library(
88
name = "macros",
9-
headers = [
9+
exported_headers = [
1010
"Macros.h",
1111
],
1212
visibility = ["@EXECUTORCH_CLIENTS"],

‎backends/aoti/slim/core/BUCK‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
21
load("targets.bzl", "define_common_targets")
32

43
oncall("executorch")
54

6-
fbcode_target(_kind = define_common_targets,)
5+
define_common_targets()

‎backends/aoti/slim/core/targets.bzl‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@fbsource//tools/build_defs:fbsource_utils.bzl", "is_fbcode")
12
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
23

34
def define_common_targets():
@@ -6,7 +7,7 @@ def define_common_targets():
67
# Header-only library for Storage
78
runtime.cxx_library(
89
name = "storage",
9-
headers = [
10+
exported_headers = [
1011
"storage.h",
1112
],
1213
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -17,16 +18,27 @@ def define_common_targets():
1718
"//executorch/backends/aoti/slim/util:shared_ptr",
1819
"//executorch/backends/aoti/slim/util:size_util",
1920
"//executorch/runtime/platform:platform",
20-
"//executorch/backends/aoti/slim/c10/cuda:exception",
21-
"//executorch/backends/aoti/slim/cuda:guard",
22-
"//executorch/extension/cuda:caller_stream",
23-
"//executorch/extension/cuda:cuda_allocator",
24-
],
21+
] + (
22+
[
23+
"//executorch/backends/aoti/slim/c10/cuda:exception",
24+
"//executorch/backends/aoti/slim/cuda:guard",
25+
"//executorch/extension/cuda:caller_stream",
26+
"//executorch/extension/cuda:cuda_allocator",
27+
] if is_fbcode() or runtime.is_oss else select({
28+
"DEFAULT": [],
29+
"ovr_config//cuda:has_cuda": [
30+
"//executorch/backends/aoti/slim/c10/cuda:exception",
31+
"//executorch/backends/aoti/slim/cuda:guard",
32+
"//executorch/extension/cuda:caller_stream",
33+
"//executorch/extension/cuda:cuda_allocator",
34+
],
35+
})
36+
),
2537
)
2638

2739
runtime.cxx_library(
2840
name = "slimtensor",
29-
headers = [
41+
exported_headers = [
3042
"slim_tensor.h",
3143
"slim_tensor_view_incl.h",
3244
],
@@ -41,6 +53,5 @@ def define_common_targets():
4153
"//executorch/backends/aoti/slim/util:array_ref_util",
4254
"//executorch/backends/aoti/slim/util:size_util",
4355
"//executorch/runtime/platform:platform",
44-
"//executorch/backends/aoti/slim/c10/cuda:exception",
4556
],
4657
)

‎backends/aoti/slim/cuda/BUCK‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
2-
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
31
load(":targets.bzl", "define_common_targets")
42

53
oncall("executorch")
64

7-
fbcode_target(_kind = define_common_targets,)
5+
define_common_targets()

‎backends/aoti/slim/cuda/targets.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def define_common_targets():
88
srcs = [
99
"guard.cpp",
1010
],
11-
headers = [
11+
exported_headers = [
1212
"guard.h",
1313
],
1414
visibility = ["PUBLIC"],

‎backends/aoti/slim/factory/BUCK‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
21
load("targets.bzl", "define_common_targets")
32

43
oncall("executorch")
54

6-
fbcode_target(_kind = define_common_targets,)
5+
define_common_targets()

‎backends/aoti/slim/factory/targets.bzl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def define_common_targets():
66
# Header-only library for empty tensor factory functions
77
runtime.cxx_library(
88
name = "empty",
9-
headers = [
9+
exported_headers = [
1010
"empty.h",
1111
],
1212
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -19,7 +19,7 @@ def define_common_targets():
1919

2020
runtime.cxx_library(
2121
name = "from_blob",
22-
headers = [
22+
exported_headers = [
2323
"from_blob.h",
2424
],
2525
visibility = ["@EXECUTORCH_CLIENTS"],
@@ -32,7 +32,7 @@ def define_common_targets():
3232

3333
runtime.cxx_library(
3434
name = "from_etensor",
35-
headers = [
35+
exported_headers = [
3636
"from_etensor.h",
3737
],
3838
visibility = ["@EXECUTORCH_CLIENTS"],

0 commit comments

Comments
 (0)