Skip to content

Commit

Permalink
Fix llama runner cmake for android (#2673)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kimishpatel authored and facebook-github-bot committed Mar 28, 2024
1 parent a0977b2 commit 649eea9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
27 changes: 25 additions & 2 deletions examples/models/llama2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ project(llama_runner)

option(EXECUTORCH_BUILD_OPTIMIZED "Build the optimized kernels" OFF)

if(NOT PYTHON_EXECUTABLE)
set(PYTHON_EXECUTABLE python3)
endif()

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)

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

if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
set(CMAKE_TOOLCHAIN_IOS ON)
else()
set(CMAKE_TOOLCHAIN_IOS OFF)
endif()

set(_common_compile_options -Wno-deprecated-declarations -fPIC)

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

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

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

# find `executorch` libraries
# Same as for gflags
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch)
find_package(executorch CONFIG REQUIRED)
if(CMAKE_TOOLCHAIN_IOS OR ANDROID)
target_link_options_shared_lib(executorch)
endif()

# custom ops library
add_subdirectory(custom_ops)
Expand Down Expand Up @@ -91,6 +108,12 @@ if(TARGET vulkan_backend)
target_link_options_shared_lib(vulkan_backend)
endif()

# This one is needed for cpuinfo where it uses android
# specific log lib
if(ANDROID)
list(APPEND link_libraries log)
endif()

target_compile_options(llama_main PUBLIC ${_common_compile_options}
-DET_USE_THREADPOOL)
target_link_libraries(llama_main PUBLIC ${link_libraries})
Expand Down
2 changes: 1 addition & 1 deletion examples/models/llama2/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ list(TRANSFORM _llama_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
target_include_directories(extension_module
INTERFACE ${_common_include_directories})

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

0 comments on commit 649eea9

Please sign in to comment.