Skip to content

Commit 649eea9

Browse files
kimishpatelfacebook-github-bot
authored andcommitted
Fix llama runner cmake for android (#2673)
Summary: Pull Request resolved: #2673 This diff fixes: 1. Make sure we buidl static variant lib for ANDROID 2. Compile libexecutorch as whole-archive to not drop prim ops registration 3. gflags and executorch's *-config.cmake wasn't found so explicitly set the directory path. 4. Add log as dep for android as cpuinfo depends on it. ghstack-source-id: 220378206 exported-using-ghexport Pre-existing lint failures bypass-github-export-checks bypass-github-pytorch-ci-checks Reviewed By: digantdesai, mcr229 Differential Revision: D55344017 fbshipit-source-id: 6c2158db60d22c8441584ea5a9fbad1087aa621c
1 parent a0977b2 commit 649eea9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

examples/models/llama2/CMakeLists.txt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ project(llama_runner)
2020

2121
option(EXECUTORCH_BUILD_OPTIMIZED "Build the optimized kernels" OFF)
2222

23+
if(NOT PYTHON_EXECUTABLE)
24+
set(PYTHON_EXECUTABLE python3)
25+
endif()
26+
2327
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
2428
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
2529

@@ -34,13 +38,21 @@ if(NOT CMAKE_CXX_STANDARD)
3438
# Can't set to 11 due to executor_runner.cpp make_unique
3539
endif()
3640

41+
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
42+
set(CMAKE_TOOLCHAIN_IOS ON)
43+
else()
44+
set(CMAKE_TOOLCHAIN_IOS OFF)
45+
endif()
46+
3747
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
3848

3949
# Let files say "include <executorch/path/to/header.h>".
4050
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
4151

42-
find_package(gflags REQUIRED PATHS
43-
${CMAKE_CURRENT_BINARY_DIR}/../../../third-party)
52+
# For some reason android build is not able to find where gflags is
53+
# and hence cannot find corresponding .cmake file
54+
set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/gflags)
55+
find_package(gflags REQUIRED)
4456

4557
#
4658
# llama_main: test binary to run llama, with tokenizer and sampler integrated
@@ -52,7 +64,12 @@ if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
5264
endif()
5365

5466
# find `executorch` libraries
67+
# Same as for gflags
68+
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch)
5569
find_package(executorch CONFIG REQUIRED)
70+
if(CMAKE_TOOLCHAIN_IOS OR ANDROID)
71+
target_link_options_shared_lib(executorch)
72+
endif()
5673

5774
# custom ops library
5875
add_subdirectory(custom_ops)
@@ -91,6 +108,12 @@ if(TARGET vulkan_backend)
91108
target_link_options_shared_lib(vulkan_backend)
92109
endif()
93110

111+
# This one is needed for cpuinfo where it uses android
112+
# specific log lib
113+
if(ANDROID)
114+
list(APPEND link_libraries log)
115+
endif()
116+
94117
target_compile_options(llama_main PUBLIC ${_common_compile_options}
95118
-DET_USE_THREADPOOL)
96119
target_link_libraries(llama_main PUBLIC ${link_libraries})

examples/models/llama2/runner/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ list(TRANSFORM _llama_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
3939
target_include_directories(extension_module
4040
INTERFACE ${_common_include_directories})
4141

42-
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID OR APPLE)
42+
if(CMAKE_TOOLCHAIN_IOS OR ANDROID OR APPLE)
4343
# Building a share library on iOS requires code signing
4444
# On Android we see duplicated registration when using shared lib
4545
add_library(llama_runner STATIC ${_llama_runner__srcs})

0 commit comments

Comments
 (0)