|
| 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 | +# |
| 7 | +# Build AOTI Metal backend for runtime. |
| 8 | +# |
| 9 | +# ### Editing this file ### |
| 10 | +# |
| 11 | +# This file should be formatted with |
| 12 | +# ~~~ |
| 13 | +# cmake-format -i CMakeLists.txt |
| 14 | +# ~~~ |
| 15 | +# It should also be cmake-lint clean. |
| 16 | +# |
| 17 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 18 | + |
| 19 | +# Source root directory for executorch. |
| 20 | +if(NOT EXECUTORCH_ROOT) |
| 21 | + set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) |
| 22 | +endif() |
| 23 | + |
| 24 | +include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake) |
| 25 | +# Use full torch package to get library paths, but only link specific libraries |
| 26 | +find_package_torch() |
| 27 | + |
| 28 | +set(_aoti_metal_sources |
| 29 | + runtime/metal_backend.cpp |
| 30 | + runtime/shims/memory.cpp |
| 31 | + runtime/shims/et_metal.mm |
| 32 | + runtime/shims/et_metal_ops.mm |
| 33 | + runtime/shims/shim_mps.mm |
| 34 | + runtime/shims/tensor_attribute.cpp |
| 35 | + runtime/shims/utils.cpp |
| 36 | + ) |
| 37 | + |
| 38 | +add_library(metal_backend STATIC ${_aoti_metal_sources}) |
| 39 | +target_include_directories( |
| 40 | + metal_backend |
| 41 | + PUBLIC |
| 42 | + $<BUILD_INTERFACE:${EXECUTORCH_ROOT}> |
| 43 | + $<INSTALL_INTERFACE:include> |
| 44 | + # PyTorch AOTI headers from ExecutorTorch's torch detection |
| 45 | + ${TORCH_INCLUDE_DIRS} |
| 46 | +) |
| 47 | + |
| 48 | +# Link Metal framework |
| 49 | +find_library(METAL_LIBRARY Metal REQUIRED) |
| 50 | +find_library(FOUNDATION_LIBRARY Foundation REQUIRED) |
| 51 | +find_library(METALPERFORMANCESHADERS_LIBRARY MetalPerformanceShaders REQUIRED) |
| 52 | +find_library(METALPERFORMANCESHADERSGRAPH_LIBRARY MetalPerformanceShadersGraph REQUIRED) |
| 53 | +target_link_libraries(metal_backend PUBLIC ${METAL_LIBRARY} ${FOUNDATION_LIBRARY} ${METALPERFORMANCESHADERS_LIBRARY} ${METALPERFORMANCESHADERSGRAPH_LIBRARY}) |
| 54 | + |
| 55 | +target_compile_options(metal_backend PUBLIC -fexceptions -frtti -fPIC) |
| 56 | + |
| 57 | +target_link_options(metal_backend PUBLIC -Wl,-export_dynamic) |
| 58 | + |
| 59 | +# Find PyTorch's OpenMP library specifically for libtorch-less AOTI |
| 60 | +get_torch_base_path(TORCH_BASE_PATH) |
| 61 | +find_library(TORCH_OMP_LIBRARY |
| 62 | + NAMES omp libomp |
| 63 | + PATHS "${TORCH_BASE_PATH}/lib" |
| 64 | + NO_DEFAULT_PATH |
| 65 | +) |
| 66 | + |
| 67 | +if(TORCH_OMP_LIBRARY) |
| 68 | + message(STATUS "Found PyTorch OpenMP library: ${TORCH_OMP_LIBRARY}") |
| 69 | + # Get the directory containing the OpenMP library for rpath |
| 70 | + get_filename_component(TORCH_OMP_LIB_DIR ${TORCH_OMP_LIBRARY} DIRECTORY) |
| 71 | + message(STATUS "OpenMP library directory: ${TORCH_OMP_LIB_DIR}") |
| 72 | +else() |
| 73 | + message(WARNING "PyTorch OpenMP library not found, may cause runtime linking issues") |
| 74 | +endif() |
| 75 | + |
| 76 | +# Link against appropriate backends and standard libraries |
| 77 | +target_link_libraries( |
| 78 | + metal_backend |
| 79 | + PUBLIC |
| 80 | + aoti_common |
| 81 | + extension_tensor |
| 82 | + ${CMAKE_DL_LIBS} |
| 83 | + ${TORCH_OMP_LIBRARY} |
| 84 | +) |
| 85 | + |
| 86 | +# Set rpath for OpenMP library to avoid runtime linking issues |
| 87 | +if(TORCH_OMP_LIBRARY AND TORCH_OMP_LIB_DIR) |
| 88 | + # Add the OpenMP library directory to the rpath |
| 89 | + set_target_properties(metal_backend PROPERTIES |
| 90 | + BUILD_RPATH "${TORCH_OMP_LIB_DIR}" |
| 91 | + INSTALL_RPATH "${TORCH_OMP_LIB_DIR}" |
| 92 | + ) |
| 93 | + # Also try common OpenMP library locations |
| 94 | + target_link_options(metal_backend PUBLIC |
| 95 | + -Wl,-rpath,${TORCH_OMP_LIB_DIR} |
| 96 | + -Wl,-rpath,/usr/local/opt/libomp/lib |
| 97 | + -Wl,-rpath,/opt/homebrew/opt/libomp/lib |
| 98 | + ) |
| 99 | + message(STATUS "Added rpath for OpenMP library: ${TORCH_OMP_LIB_DIR}") |
| 100 | +endif() |
| 101 | + |
| 102 | +executorch_target_link_options_shared_lib(metal_backend) |
| 103 | +install( |
| 104 | + TARGETS metal_backend |
| 105 | + EXPORT ExecuTorchTargets |
| 106 | + DESTINATION lib |
| 107 | +) |
0 commit comments