xref: /aosp_15_r20/external/executorch/examples/models/llama/CMakeLists.txt (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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#
8# Simple CMake build system for selective build demo.
9#
10# ### Editing this file ###
11#
12# This file should be formatted with
13# ~~~
14# cmake-format -i CMakeLists.txt
15# ~~~
16# It should also be cmake-lint clean.
17#
18cmake_minimum_required(VERSION 3.19)
19project(llama_runner)
20
21# Duplicating options as root CMakeLists.txt
22option(EXECUTORCH_BUILD_KERNELS_OPTIMIZED "Build the optimized kernels" OFF)
23
24include(CMakeDependentOption)
25#
26# pthreadpool: build pthreadpool library. Disable on unsupported platforms
27#
28cmake_dependent_option(
29  EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library." ON
30  "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
31)
32#
33# cpuinfo: build cpuinfo library. Disable on unsupported platforms
34#
35cmake_dependent_option(
36  EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON
37  "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
38)
39
40option(EXECUTORCH_BUILD_TORCHAO "Build the torchao kernels" OFF)
41
42if(NOT PYTHON_EXECUTABLE)
43  set(PYTHON_EXECUTABLE python3)
44endif()
45
46set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
47set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
48
49include(${EXECUTORCH_ROOT}/build/Utils.cmake)
50
51if(NOT PYTHON_EXECUTABLE)
52  resolve_python_executable()
53endif()
54
55if(NOT CMAKE_CXX_STANDARD)
56  set(CMAKE_CXX_STANDARD 17)
57  # Can't set to 11 due to executor_runner.cpp make_unique
58endif()
59
60if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
61  set(CMAKE_TOOLCHAIN_IOS ON)
62else()
63  set(CMAKE_TOOLCHAIN_IOS OFF)
64endif()
65
66set(_common_compile_options -Wno-deprecated-declarations -fPIC)
67
68# Let files say "include <executorch/path/to/header.h>".
69set(_common_include_directories ${EXECUTORCH_ROOT}/..)
70
71# For some reason android build is not able to find where gflags is and hence
72# cannot find corresponding .cmake file
73set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/gflags)
74find_package(gflags REQUIRED)
75
76#
77# llama_main: test binary to run llama, with tokenizer and sampler integrated
78#
79
80# find `executorch` libraries Same as for gflags
81set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch)
82find_package(executorch CONFIG REQUIRED)
83if(CMAKE_TOOLCHAIN_IOS OR ANDROID)
84  target_link_options_shared_lib(executorch)
85endif()
86
87# custom ops library
88if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
89  add_subdirectory(
90    ${CMAKE_CURRENT_SOURCE_DIR}/../../../extension/llm/custom_ops
91    ${CMAKE_CURRENT_BINARY_DIR}/../../../extension/llm/custom_ops
92  )
93endif()
94
95# llama_runner library
96add_subdirectory(runner)
97
98set(link_libraries gflags)
99set(_srcs main.cpp)
100
101if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
102  list(
103    APPEND
104    link_libraries
105    optimized_native_cpu_ops_lib
106    optimized_kernels
107    portable_kernels
108    cpublas
109    eigen_blas
110  )
111  target_link_options_shared_lib(optimized_native_cpu_ops_lib)
112else()
113  list(APPEND link_libraries portable_ops_lib portable_kernels)
114  target_link_options_shared_lib(portable_ops_lib)
115endif()
116
117# quantized_ops_lib: Register quantized op kernels into the runtime
118target_link_options_shared_lib(quantized_ops_lib)
119list(APPEND link_libraries quantized_kernels quantized_ops_lib)
120
121if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
122  target_link_options_shared_lib(custom_ops)
123  list(APPEND link_libraries custom_ops)
124endif()
125
126if(EXECUTORCH_BUILD_TORCHAO)
127  set(TORCHAO_BUILD_EXECUTORCH_OPS ON)
128  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../third-party/ao/torchao/experimental ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/ao/torchao/experimental)
129  target_link_options_shared_lib(torchao_ops_executorch)
130  list(APPEND link_libraries torchao_ops_executorch)
131endif()
132
133set(XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack)
134# Extra compile option and include dir for pthreadpool
135if(EXECUTORCH_BUILD_PTHREADPOOL)
136  list(APPEND _common_compile_options -DET_USE_THREADPOOL)
137  list(APPEND link_libraries extension_threadpool pthreadpool)
138  list(APPEND _common_include_directories
139       ${XNNPACK_ROOT}/third-party/pthreadpool/include
140  )
141endif()
142
143# Extra sources for cpuinfo
144if(EXECUTORCH_BUILD_CPUINFO)
145  list(APPEND link_libraries extension_threadpool cpuinfo)
146  list(APPEND _common_include_directories
147       ${XNNPACK_ROOT}/third-party/cpuinfo/include
148  )
149endif()
150
151# XNNPACK
152if(TARGET xnnpack_backend)
153  set(xnnpack_backend_libs xnnpack_backend XNNPACK microkernels-prod)
154  if(TARGET kleidiai)
155    list(APPEND xnnpack_backend_libs kleidiai)
156  endif()
157  list(APPEND link_libraries ${xnnpack_backend_libs})
158  target_link_options_shared_lib(xnnpack_backend)
159endif()
160
161# Vulkan backend
162if(TARGET vulkan_backend)
163  list(APPEND link_libraries vulkan_backend)
164  target_link_options_shared_lib(vulkan_backend)
165endif()
166
167# Qnn backend
168if(TARGET qnn_executorch_backend)
169  list(APPEND link_libraries qnn_executorch_backend)
170  target_link_options_shared_lib(qnn_executorch_backend)
171endif()
172
173# MPS backend
174if(TARGET mpsdelegate)
175  list(
176    APPEND
177    link_libraries
178    mpsdelegate
179    "-framework Foundation"
180    "-weak_framework MetalPerformanceShaders"
181    "-weak_framework MetalPerformanceShadersGraph"
182    "-weak_framework Metal"
183  )
184  target_link_options_shared_lib(mpsdelegate)
185endif()
186
187if(TARGET coremldelegate)
188  find_library(SQLITE_LIBRARY sqlite3)
189  list(
190    APPEND
191    link_libraries
192    coremldelegate
193    sqlite3
194    "-framework Foundation"
195    "-framework CoreML"
196    "-framework Accelerate"
197  )
198  target_link_options_shared_lib(coremldelegate)
199endif()
200
201# This one is needed for cpuinfo where it uses android specific log lib
202if(ANDROID)
203  list(APPEND link_libraries log)
204endif()
205
206add_executable(llama_main ${_srcs})
207if(CMAKE_BUILD_TYPE STREQUAL "Release")
208  if(APPLE)
209    target_link_options(llama_main PRIVATE "LINKER:-dead_strip")
210  else()
211    target_link_options(llama_main PRIVATE "LINKER:--gc-sections,-s")
212  endif()
213endif()
214
215target_include_directories(llama_main PUBLIC ${_common_include_directories})
216target_link_libraries(llama_main PUBLIC llama_runner ${link_libraries})
217target_compile_options(llama_main PUBLIC ${_common_compile_options})
218
219if(APPLE)
220  target_link_options_shared_lib(executorch)
221endif()
222
223# Print all summary
224executorch_print_configuration_summary()
225