1load("@fbcode//target_determinator/macros:ci.bzl", "ci") 2load("@fbcode_macros//build_defs:native_rules.bzl", "buck_genrule") 3load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") 4load("@fbsource//tools/build_defs:platform_defs.bzl", "ANDROID", "CXX", "FBCODE") 5 6 7def get_vulkan_compiler_flags(): 8 return ["-Wno-missing-prototypes", "-Wno-global-constructors"] 9 10def get_labels(no_volk): 11 if no_volk: 12 return ci.labels(ci.linux(ci.mode("fbsource//arvr/mode/android/mac/dbg"))) 13 else: 14 return [] 15 16def get_platforms(no_volk): 17 if no_volk: 18 return [ANDROID] 19 else: 20 return [ANDROID, CXX] 21 22def vulkan_spv_shader_lib(name, spv_filegroups, is_fbcode = False, no_volk = False): 23 gen_vulkan_spv_target = "//xplat/executorch/backends/vulkan:gen_vulkan_spv_bin" 24 glslc_path = "//xplat/caffe2/fb/vulkan/dotslash:glslc" 25 26 if is_fbcode: 27 gen_vulkan_spv_target = "//executorch/backends/vulkan:gen_vulkan_spv_bin" 28 glslc_path = "//caffe2/fb/vulkan/tools:glslc" 29 30 glsl_paths = [] 31 32 # TODO(ssjia): remove the need for subpath once subdir_glob is enabled in OSS 33 for target, subpath in spv_filegroups.items(): 34 glsl_paths.append("$(location {})/{}".format(target, subpath)) 35 36 genrule_cmd = ( 37 "$(exe {}) ".format(gen_vulkan_spv_target) + 38 "--glsl-paths {} ".format(" ".join(glsl_paths)) + 39 "--output-path $OUT " + 40 "--glslc-path=$(exe {}) ".format(glslc_path) + 41 "--tmp-dir-path=$OUT " + 42 select({ 43 "DEFAULT": "", 44 "ovr_config//os:android": "--optimize", 45 "ovr_config//os:linux": "--replace-u16vecn", 46 }) 47 ) 48 49 genrule_name = "gen_{}_cpp".format(name) 50 buck_genrule( 51 name = genrule_name, 52 outs = { 53 "{}.cpp".format(name): ["spv.cpp"], 54 }, 55 cmd = genrule_cmd, 56 default_outs = ["."], 57 labels = ["uses_dotslash"], 58 ) 59 60 suffix = "_no_volk" if no_volk else "" 61 runtime.cxx_library( 62 name = name, 63 srcs = [ 64 ":{}[{}.cpp]".format(genrule_name, name), 65 ], 66 compiler_flags = get_vulkan_compiler_flags(), 67 labels = get_labels(no_volk), 68 platforms = get_platforms(no_volk), 69 define_static_target = True, 70 # Static initialization is used to register shaders to the global shader registry, 71 # therefore link_whole must be True to make sure unused symbols are not discarded. 72 # @lint-ignore BUCKLINT: Avoid `link_whole=True` 73 link_whole = True, 74 # Define a soname that can be used for dynamic loading in Java, Python, etc. 75 soname = "lib{}.$(ext)".format(name), 76 exported_deps = [ 77 "//executorch/backends/vulkan:vulkan_compute_api{}".format(suffix), 78 ], 79 ) 80 81def define_common_targets(is_fbcode = False): 82 runtime.python_library( 83 name = "gen_vulkan_spv_lib", 84 srcs = [ 85 "runtime/gen_vulkan_spv.py", 86 ], 87 base_module = "", 88 external_deps = ["torchgen"], 89 ) 90 91 runtime.python_binary( 92 name = "gen_vulkan_spv_bin", 93 main_module = "runtime.gen_vulkan_spv", 94 visibility = [ 95 "//executorch/backends/vulkan/...", 96 "@EXECUTORCH_CLIENTS", 97 ], 98 deps = [ 99 ":gen_vulkan_spv_lib", 100 ], 101 ) 102 103 runtime.filegroup( 104 name = "vulkan_graph_runtime_shaders", 105 srcs = native.glob([ 106 "runtime/graph/ops/glsl/*", 107 ]), 108 ) 109 110 for no_volk in [True, False]: 111 # No volk builds only available on xplat to build for Android 112 if no_volk and is_fbcode: 113 continue 114 115 suffix = "_no_volk" if no_volk else "" 116 117 VK_API_PREPROCESSOR_FLAGS = [] 118 VK_API_DEPS = [ 119 "fbsource//third-party/VulkanMemoryAllocator/3.0.1:VulkanMemoryAllocator_xplat", 120 ] 121 122 default_deps = [] 123 android_deps = ["fbsource//third-party/toolchains:android"] 124 default_flags = [] 125 android_flags = [] 126 127 if no_volk: 128 android_deps.append("fbsource//third-party/toolchains:vulkan") 129 else: 130 for deps in [default_deps, android_deps]: 131 deps.append("fbsource//third-party/volk:volk") 132 for flags in [default_flags, android_flags]: 133 flags.append("-DUSE_VULKAN_WRAPPER") 134 flags.append("-DUSE_VULKAN_VOLK") 135 android_flags.append("-DVK_ANDROID_external_memory_android_hardware_buffer") 136 137 if is_fbcode: 138 VK_API_DEPS += [ 139 "fbsource//third-party/swiftshader:swiftshader_vk_headers", 140 "fbsource//third-party/swiftshader/lib/linux-x64:libvk_swiftshader_fbcode", 141 "fbsource//third-party/swiftshader/lib/linux-x64:libvk_swiftshader_so", 142 ] 143 else: 144 VK_API_DEPS += select({ 145 "DEFAULT": default_deps, 146 "ovr_config//os:android": android_deps, 147 "ovr_config//os:macos": [ 148 "//third-party/khronos:moltenVK_static" 149 ], 150 }) 151 VK_API_PREPROCESSOR_FLAGS += select({ 152 "DEFAULT": default_flags, 153 "ovr_config//os:android": android_flags, 154 "ovr_config//os:macos": [] 155 }) 156 157 runtime.cxx_library( 158 name = "vulkan_compute_api{}".format(suffix), 159 compiler_flags = get_vulkan_compiler_flags(), 160 srcs = native.glob([ 161 "runtime/api/**/*.cpp", 162 "runtime/utils/**/*.cpp", 163 "runtime/vk_api/**/*.cpp", 164 ]), 165 exported_headers = native.glob([ 166 "runtime/api/**/*.h", 167 "runtime/utils/**/*.h", 168 "runtime/vk_api/**/*.h", 169 ]), 170 labels = get_labels(no_volk), 171 platforms = get_platforms(no_volk), 172 visibility = [ 173 "//executorch/backends/vulkan/...", 174 "@EXECUTORCH_CLIENTS", 175 ], 176 exported_preprocessor_flags = VK_API_PREPROCESSOR_FLAGS, 177 exported_deps = VK_API_DEPS, 178 ) 179 180 runtime.cxx_library( 181 name = "vulkan_graph_runtime{}".format(suffix), 182 srcs = native.glob([ 183 "runtime/graph/**/*.cpp", 184 ]), 185 compiler_flags = get_vulkan_compiler_flags(), 186 exported_headers = native.glob([ 187 "runtime/graph/**/*.h", 188 ]), 189 labels = get_labels(no_volk), 190 platforms = get_platforms(no_volk), 191 visibility = [ 192 "//executorch/backends/...", 193 "//executorch/extension/pybindings/...", 194 "//executorch/test/...", 195 "@EXECUTORCH_CLIENTS", 196 ], 197 exported_deps = [ 198 ":vulkan_graph_runtime_shaderlib{}".format(suffix), 199 ], 200 define_static_target = True, 201 # Static initialization is used to register operators to the global operator registry, 202 # therefore link_whole must be True to make sure unused symbols are not discarded. 203 # @lint-ignore BUCKLINT: Avoid `link_whole=True` 204 link_whole = True, 205 # Define an soname that can be used for dynamic loading in Java, Python, etc. 206 soname = "libvulkan_graph_runtime.$(ext)", 207 ) 208 209 vulkan_spv_shader_lib( 210 name = "vulkan_graph_runtime_shaderlib{}".format(suffix), 211 spv_filegroups = { 212 ":vulkan_graph_runtime_shaders": "runtime/graph/ops/glsl", 213 }, 214 is_fbcode = is_fbcode, 215 no_volk = no_volk, 216 ) 217 218 runtime.cxx_library( 219 name = "vulkan_backend_lib{}".format(suffix), 220 srcs = native.glob([ 221 "runtime/*.cpp", 222 ]), 223 compiler_flags = get_vulkan_compiler_flags(), 224 headers = native.glob([ 225 "runtime/*.h", 226 ]), 227 labels = get_labels(no_volk), 228 platforms = get_platforms(no_volk), 229 visibility = [ 230 "//executorch/backends/...", 231 "//executorch/extension/pybindings/...", 232 "//executorch/test/...", 233 "@EXECUTORCH_CLIENTS", 234 ], 235 deps = [ 236 ":vulkan_graph_runtime{}".format(suffix), 237 "//executorch/backends/vulkan/serialization:vk_delegate_schema", 238 "//executorch/runtime/core:event_tracer", 239 "//executorch/runtime/backend:interface", 240 "//executorch/runtime/core/exec_aten/util:tensor_util", 241 ], 242 define_static_target = True, 243 # VulkanBackend.cpp needs to compile with executor as whole 244 # @lint-ignore BUCKLINT: Avoid `link_whole=True` (https://fburl.com/avoid-link-whole) 245 link_whole = True, 246 ) 247 248 ## 249 ## AOT targets 250 ## 251 if is_fbcode: 252 runtime.python_library( 253 name = "utils_lib", 254 srcs = [ 255 "utils.py", 256 ], 257 visibility = [ 258 "//executorch/backends/vulkan/...", 259 ], 260 deps = [ 261 "//caffe2:torch", 262 "//executorch/exir:tensor", 263 "//executorch/backends/vulkan/serialization:lib", 264 ] 265 ) 266 267 runtime.python_library( 268 name = "custom_ops_lib", 269 srcs = [ 270 "custom_ops_lib.py" 271 ], 272 visibility = [ 273 "//executorch/...", 274 "//executorch/vulkan/...", 275 "@EXECUTORCH_CLIENTS", 276 ], 277 deps = [ 278 "//caffe2:torch", 279 ] 280 ) 281 282 runtime.python_library( 283 name = "op_registry", 284 srcs = [ 285 "op_registry.py", 286 ], 287 visibility = [ 288 "//executorch/...", 289 "//executorch/vulkan/...", 290 "@EXECUTORCH_CLIENTS", 291 ], 292 deps = [ 293 ":custom_ops_lib", 294 ":utils_lib", 295 "//caffe2:torch", 296 "//executorch/exir/dialects:lib", 297 "//executorch/backends/vulkan/serialization:lib", 298 ] 299 ) 300 301 runtime.python_library( 302 name = "vulkan_preprocess", 303 srcs = [ 304 "vulkan_preprocess.py", 305 ], 306 visibility = [ 307 "//executorch/...", 308 "//executorch/vulkan/...", 309 "@EXECUTORCH_CLIENTS", 310 ], 311 deps = [ 312 "//executorch/backends/transforms:addmm_mm_to_linear", 313 "//executorch/backends/transforms:fuse_batch_norm_with_conv", 314 "//executorch/backends/transforms:fuse_conv_with_clamp", 315 "//executorch/backends/transforms:fuse_dequant_linear", 316 "//executorch/backends/transforms:fuse_view_copy", 317 "//executorch/backends/transforms:remove_clone_ops", 318 "//executorch/backends/vulkan/_passes:vulkan_passes", 319 "//executorch/backends/vulkan/serialization:lib", 320 "//executorch/exir/backend:backend_details", 321 ], 322 ) 323