Skip to content

Commit 8aca6d1

Browse files
committed
[llm] Add CMake for llama runner
Summary: As titled. Also add a CI job. Will run a model in next PR. Test Plan: Reviewers: Subscribers: Tasks: Tags: ghstack-source-id: d4b156f Pull Request resolved: #2022
1 parent 0fa67f6 commit 8aca6d1

File tree

8 files changed

+261
-57
lines changed

8 files changed

+261
-57
lines changed

.github/workflows/pull.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,27 @@ jobs:
213213
uses: ./.github/workflows/_unittest.yml
214214
with:
215215
docker-image: executorch-ubuntu-22.04-clang12
216+
217+
test-llama-runner-cmake:
218+
name: test-llama-runner
219+
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
220+
strategy:
221+
matrix:
222+
include:
223+
- build-tool: cmake
224+
fail-fast: false
225+
with:
226+
runner: linux.2xlarge
227+
docker-image: executorch-ubuntu-22.04-clang12
228+
submodules: 'true'
229+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
230+
timeout: 90
231+
script: |
232+
# The generic Linux job chooses to use base env, not the one setup by the image
233+
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
234+
conda activate "${CONDA_ENV}"
235+
236+
BUILD_TOOL=${{ matrix.build-tool }}
237+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "${BUILD_TOOL}"
238+
# Test selective build
239+
PYTHON_EXECUTABLE=python bash examples/models/llama2/test_llama_runner.sh "${BUILD_TOOL}"

build/cmake_deps.toml

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,7 @@ excludes = [
1313
"^third-party",
1414
]
1515

16-
[targets.executor_runner]
17-
buck_targets = [
18-
"//examples/portable/executor_runner:executor_runner",
19-
]
20-
filters = [
21-
".cpp$",
22-
]
23-
excludes = [
24-
"^codegen",
25-
]
26-
deps = [
27-
"executorch",
28-
"portable_kernels",
29-
"quantized_kernels",
30-
]
16+
# ---------------------------------- core start ----------------------------------
3117

3218
[targets.executorch]
3319
buck_targets = [
@@ -40,94 +26,106 @@ filters = [
4026
".cpp$",
4127
]
4228

43-
[targets.extension_data_loader]
29+
30+
[targets.portable_kernels]
4431
buck_targets = [
45-
"//extension/data_loader:buffer_data_loader",
46-
"//extension/data_loader:file_data_loader",
47-
"//extension/data_loader:mmap_data_loader",
48-
"//extension/data_loader:shared_ptr_data_loader",
32+
# //kernels/portable:operators would be more appropriate, but buck2 doesn't
33+
# think it has any "inputs" since its srcs list is empty.
34+
"//kernels/portable:generated_lib_all_ops",
4935
]
5036
filters = [
5137
".cpp$",
5238
]
39+
excludes = [
40+
# Exclude the codegen templates, which are picked up because the buck target
41+
# is the generated_lib and not the unwrapped set of kernels.
42+
"^codegen/templates",
43+
]
5344
deps = [
5445
"executorch",
5546
]
5647

57-
[targets.extension_module]
48+
[targets.quantized_kernels]
5849
buck_targets = [
59-
"//extension/module:module",
50+
"//kernels/quantized:generated_lib",
6051
]
6152
filters = [
6253
".cpp$",
6354
]
55+
excludes = [
56+
# Exclude the codegen templates, which are picked up because the buck target
57+
# is the generated_lib and not the unwrapped set of kernels.
58+
"^codegen/templates",
59+
]
6460
deps = [
6561
"executorch",
66-
"extension_data_loader",
6762
]
6863

69-
[targets.extension_runner_util]
64+
[targets.program_schema]
7065
buck_targets = [
71-
"//extension/runner_util:inputs",
66+
"//schema:program",
7267
]
7368
filters = [
74-
".cpp$",
75-
]
76-
deps = [
77-
"executorch",
69+
".fbs$",
7870
]
7971

80-
[targets.portable_kernels]
72+
# ---------------------------------- core end ----------------------------------
73+
# ---------------------------------- extension start ----------------------------------
74+
75+
[targets.extension_data_loader]
8176
buck_targets = [
82-
# //kernels/portable:operators would be more appropriate, but buck2 doesn't
83-
# think it has any "inputs" since its srcs list is empty.
84-
"//kernels/portable:generated_lib_all_ops",
77+
"//extension/data_loader:buffer_data_loader",
78+
"//extension/data_loader:file_data_loader",
79+
"//extension/data_loader:mmap_data_loader",
80+
"//extension/data_loader:shared_ptr_data_loader",
8581
]
8682
filters = [
8783
".cpp$",
8884
]
89-
excludes = [
90-
# Exclude the codegen templates, which are picked up because the buck target
91-
# is the generated_lib and not the unwrapped set of kernels.
92-
"^codegen/templates",
93-
]
9485
deps = [
9586
"executorch",
9687
]
9788

98-
[targets.quantized_kernels]
89+
[targets.extension_module]
9990
buck_targets = [
100-
"//kernels/quantized:generated_lib",
91+
"//extension/module:module",
10192
]
10293
filters = [
10394
".cpp$",
10495
]
105-
excludes = [
106-
# Exclude the codegen templates, which are picked up because the buck target
107-
# is the generated_lib and not the unwrapped set of kernels.
108-
"^codegen/templates",
109-
]
11096
deps = [
11197
"executorch",
98+
"extension_data_loader",
11299
]
113100

114-
[targets.program_schema]
101+
[targets.extension_runner_util]
115102
buck_targets = [
116-
"//schema:program",
103+
"//extension/runner_util:inputs",
117104
]
118105
filters = [
119-
".fbs$",
106+
".cpp$",
107+
]
108+
deps = [
109+
"executorch",
120110
]
121111

122-
[targets.print_evalue]
112+
# ---------------------------------- extension end ----------------------------------
113+
# ---------------------------------- binary start ----------------------------------
114+
115+
[targets.executor_runner]
123116
buck_targets = [
124-
"//extension/evalue_util:print_evalue",
117+
"//examples/portable/executor_runner:executor_runner",
125118
]
126119
filters = [
127120
".cpp$",
128121
]
122+
excludes = [
123+
"^codegen",
124+
]
129125
deps = [
130126
"executorch",
127+
"portable_kernels",
128+
"quantized_kernels",
131129
]
132130

133131
[targets.size_test]
@@ -143,7 +141,8 @@ excludes = [
143141
deps = [
144142
"executorch",
145143
]
146-
144+
# ---------------------------------- binary end ----------------------------------
145+
# ---------------------------------- MPS start ----------------------------------
147146
[targets.mps_executor_runner]
148147
buck_targets = [
149148
"//examples/apple/mps/executor_runner:mps_executor_runner",
@@ -178,6 +177,9 @@ filters = [
178177
".fbs$",
179178
]
180179

180+
# ---------------------------------- MPS end ----------------------------------
181+
# ---------------------------------- XNNPACK start ----------------------------------
182+
181183
[targets.xnn_executor_runner]
182184
buck_targets = [
183185
"//examples/xnnpack:xnn_executor_runner",
@@ -220,3 +222,24 @@ buck_targets = [
220222
filters = [
221223
".fbs$",
222224
]
225+
# ---------------------------------- XNNPACK end ----------------------------------
226+
# ---------------------------------- LLama start ----------------------------------
227+
228+
[targets.llama_runner]
229+
buck_targets = [
230+
"//examples/models/llama2/runner:runner",
231+
]
232+
filters = [
233+
".cpp$",
234+
]
235+
excludes = [
236+
"^codegen",
237+
]
238+
deps = [
239+
"executorch",
240+
"extension_module",
241+
"portable_kernels",
242+
"quantized_kernels",
243+
"xnnpack_backend",
244+
]
245+
# ---------------------------------- LLama start ----------------------------------

build/executorch-config.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ endif()
4343

4444
set(lib_list
4545
etdump bundled_program extension_data_loader ${FLATCC_LIB} mpsdelegate
46-
qnn_executorch_backend
46+
qnn_executorch_backend portable_ops_lib extension_module
4747
)
4848
foreach(lib ${lib_list})
4949
# Name of the variable which stores result of the find_library search
@@ -53,7 +53,11 @@ foreach(lib ${lib_list})
5353
message("${lib} library is not found.
5454
If needed rebuild with the proper options in CMakeLists.txt")
5555
else()
56-
add_library(${lib} STATIC IMPORTED)
56+
if("${lib}" STREQUAL "extension_module")
57+
add_library(${lib} SHARED IMPORTED)
58+
else()
59+
add_library(${lib} STATIC IMPORTED)
60+
endif()
5761
set_target_properties(
5862
${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}"
5963
)

examples/models/llama2/CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 --first-comment-is-literal=True CMakeLists.txt
15+
# ~~~
16+
# It should also be cmake-lint clean.
17+
#
18+
cmake_minimum_required(VERSION 3.19)
19+
project(llama_runner)
20+
21+
if(NOT PYTHON_EXECUTABLE)
22+
set(PYTHON_EXECUTABLE python3)
23+
endif()
24+
25+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
26+
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
27+
28+
if(NOT CMAKE_CXX_STANDARD)
29+
set(CMAKE_CXX_STANDARD 17)
30+
# Can't set to 11 due to executor_runner.cpp make_unique
31+
endif()
32+
33+
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
34+
35+
# Let files say "include <executorch/path/to/header.h>".
36+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
37+
38+
find_package(gflags REQUIRED PATHS
39+
${CMAKE_CURRENT_BINARY_DIR}/../../../third-party)
40+
41+
#
42+
# llama_main: test binary to run llama, with tokenizer and sampler integrated
43+
#
44+
add_executable(llama_main main.cpp)
45+
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
46+
target_link_options(llama_main PRIVATE "LINKER:--gc-sections")
47+
endif()
48+
49+
# find `executorch` libraries
50+
find_package(executorch CONFIG REQUIRED)
51+
52+
# llama_runner library
53+
add_subdirectory(runner)
54+
55+
# XNNPACK pthreadpool cpuinfo
56+
target_link_libraries(llama_main PUBLIC gflags llama_runner portable_ops_lib)
57+
target_link_options(
58+
llama_main PUBLIC "SHELL:LINKER:--whole-archive \
59+
$<TARGET_FILE:portable_ops_lib> \
60+
LINKER:--no-whole-archive")
61+
target_compile_options(llama_main PUBLIC ${_common_compile_options})
62+
63+
# Print all summary
64+
executorch_print_configuration_summary()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 --first-comment-is-literal=True CMakeLists.txt
15+
# ~~~
16+
# It should also be cmake-lint clean.
17+
#
18+
19+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
20+
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
21+
#
22+
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
23+
#
24+
set(EXECUTORCH_SRCS_FILE
25+
"${CMAKE_CURRENT_BINARY_DIR}/../../../../executorch_srcs.cmake")
26+
27+
extract_sources(${EXECUTORCH_SRCS_FILE})
28+
29+
include(${EXECUTORCH_SRCS_FILE})
30+
31+
# build llama_runner library
32+
list(TRANSFORM _llama_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
33+
34+
target_include_directories(extension_module
35+
INTERFACE ${_common_include_directories})
36+
37+
add_library(llama_runner SHARED ${_llama_runner__srcs})
38+
target_link_libraries(
39+
llama_runner PUBLIC executorch portable_kernels extension_module
40+
extension_data_loader)

0 commit comments

Comments
 (0)