|
| 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 $<BUILD_INTERFACE:${EXECUTORCH_ROOT}> $<INSTALL_INTERFACE:include> |
| 42 | + # PyTorch AOTI headers from ExecutorTorch's torch detection |
| 43 | + ${TORCH_INCLUDE_DIRS} |
| 44 | +) |
| 45 | + |
| 46 | +# Link Metal framework |
| 47 | +find_library(METAL_LIBRARY Metal REQUIRED) |
| 48 | +find_library(FOUNDATION_LIBRARY Foundation REQUIRED) |
| 49 | +find_library(METALPERFORMANCESHADERS_LIBRARY MetalPerformanceShaders REQUIRED) |
| 50 | +find_library( |
| 51 | + METALPERFORMANCESHADERSGRAPH_LIBRARY MetalPerformanceShadersGraph REQUIRED |
| 52 | +) |
| 53 | +target_link_libraries( |
| 54 | + metal_backend |
| 55 | + PUBLIC ${METAL_LIBRARY} ${FOUNDATION_LIBRARY} |
| 56 | + ${METALPERFORMANCESHADERS_LIBRARY} |
| 57 | + ${METALPERFORMANCESHADERSGRAPH_LIBRARY} |
| 58 | +) |
| 59 | + |
| 60 | +target_compile_options(metal_backend PUBLIC -fexceptions -frtti -fPIC) |
| 61 | + |
| 62 | +target_link_options(metal_backend PUBLIC -Wl,-export_dynamic) |
| 63 | + |
| 64 | +# Find PyTorch's OpenMP library specifically for libtorch-less AOTI |
| 65 | +get_torch_base_path(TORCH_BASE_PATH) |
| 66 | +find_library( |
| 67 | + TORCH_OMP_LIBRARY |
| 68 | + NAMES omp libomp |
| 69 | + PATHS "${TORCH_BASE_PATH}/lib" |
| 70 | + NO_DEFAULT_PATH |
| 71 | +) |
| 72 | + |
| 73 | +if(TORCH_OMP_LIBRARY) |
| 74 | + message(STATUS "Found PyTorch OpenMP library: ${TORCH_OMP_LIBRARY}") |
| 75 | + # Get the directory containing the OpenMP library for rpath |
| 76 | + get_filename_component(TORCH_OMP_LIB_DIR ${TORCH_OMP_LIBRARY} DIRECTORY) |
| 77 | + message(STATUS "OpenMP library directory: ${TORCH_OMP_LIB_DIR}") |
| 78 | +else() |
| 79 | + message( |
| 80 | + WARNING "PyTorch OpenMP library not found, may cause runtime linking issues" |
| 81 | + ) |
| 82 | +endif() |
| 83 | + |
| 84 | +# Link against appropriate backends and standard libraries |
| 85 | +target_link_libraries( |
| 86 | + metal_backend PUBLIC aoti_common extension_tensor ${CMAKE_DL_LIBS} |
| 87 | + ${TORCH_OMP_LIBRARY} |
| 88 | +) |
| 89 | + |
| 90 | +# Set rpath for OpenMP library to avoid runtime linking issues |
| 91 | +if(TORCH_OMP_LIBRARY AND TORCH_OMP_LIB_DIR) |
| 92 | + # Add the OpenMP library directory to the rpath |
| 93 | + set_target_properties( |
| 94 | + metal_backend PROPERTIES BUILD_RPATH "${TORCH_OMP_LIB_DIR}" |
| 95 | + INSTALL_RPATH "${TORCH_OMP_LIB_DIR}" |
| 96 | + ) |
| 97 | + # Also try common OpenMP library locations |
| 98 | + target_link_options( |
| 99 | + metal_backend PUBLIC -Wl,-rpath,${TORCH_OMP_LIB_DIR} |
| 100 | + -Wl,-rpath,/usr/local/opt/libomp/lib |
| 101 | + -Wl,-rpath,/opt/homebrew/opt/libomp/lib |
| 102 | + ) |
| 103 | + message(STATUS "Added rpath for OpenMP library: ${TORCH_OMP_LIB_DIR}") |
| 104 | +endif() |
| 105 | + |
| 106 | +executorch_target_link_options_shared_lib(metal_backend) |
| 107 | +install( |
| 108 | + TARGETS metal_backend |
| 109 | + EXPORT ExecuTorchTargets |
| 110 | + DESTINATION lib |
| 111 | +) |
0 commit comments