Skip to content

[llm] Add CMake for llama runner #2022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,27 @@ jobs:
uses: ./.github/workflows/_unittest.yml
with:
docker-image: executorch-ubuntu-22.04-clang12

test-llama-runner-cmake:
name: test-llama-runner
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
strategy:
matrix:
include:
- build-tool: cmake
fail-fast: false
with:
runner: linux.2xlarge
docker-image: executorch-ubuntu-22.04-clang12
submodules: 'true'
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
timeout: 90
script: |
# The generic Linux job chooses to use base env, not the one setup by the image
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
conda activate "${CONDA_ENV}"

BUILD_TOOL=${{ matrix.build-tool }}
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "${BUILD_TOOL}"
# Test selective build
PYTHON_EXECUTABLE=python bash examples/models/llama2/test_llama_runner.sh "${BUILD_TOOL}"
125 changes: 74 additions & 51 deletions build/cmake_deps.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,7 @@ excludes = [
"^third-party",
]

[targets.executor_runner]
buck_targets = [
"//examples/portable/executor_runner:executor_runner",
]
filters = [
".cpp$",
]
excludes = [
"^codegen",
]
deps = [
"executorch",
"portable_kernels",
"quantized_kernels",
]
# ---------------------------------- core start ----------------------------------

[targets.executorch]
buck_targets = [
Expand All @@ -40,94 +26,106 @@ filters = [
".cpp$",
]

[targets.extension_data_loader]

[targets.portable_kernels]
buck_targets = [
"//extension/data_loader:buffer_data_loader",
"//extension/data_loader:file_data_loader",
"//extension/data_loader:mmap_data_loader",
"//extension/data_loader:shared_ptr_data_loader",
# //kernels/portable:operators would be more appropriate, but buck2 doesn't
# think it has any "inputs" since its srcs list is empty.
"//kernels/portable:generated_lib_all_ops",
]
filters = [
".cpp$",
]
excludes = [
# Exclude the codegen templates, which are picked up because the buck target
# is the generated_lib and not the unwrapped set of kernels.
"^codegen/templates",
]
deps = [
"executorch",
]

[targets.extension_module]
[targets.quantized_kernels]
buck_targets = [
"//extension/module:module",
"//kernels/quantized:generated_lib",
]
filters = [
".cpp$",
]
excludes = [
# Exclude the codegen templates, which are picked up because the buck target
# is the generated_lib and not the unwrapped set of kernels.
"^codegen/templates",
]
deps = [
"executorch",
"extension_data_loader",
]

[targets.extension_runner_util]
[targets.program_schema]
buck_targets = [
"//extension/runner_util:inputs",
"//schema:program",
]
filters = [
".cpp$",
]
deps = [
"executorch",
".fbs$",
]

[targets.portable_kernels]
# ---------------------------------- core end ----------------------------------
# ---------------------------------- extension start ----------------------------------

[targets.extension_data_loader]
buck_targets = [
# //kernels/portable:operators would be more appropriate, but buck2 doesn't
# think it has any "inputs" since its srcs list is empty.
"//kernels/portable:generated_lib_all_ops",
"//extension/data_loader:buffer_data_loader",
"//extension/data_loader:file_data_loader",
"//extension/data_loader:mmap_data_loader",
"//extension/data_loader:shared_ptr_data_loader",
]
filters = [
".cpp$",
]
excludes = [
# Exclude the codegen templates, which are picked up because the buck target
# is the generated_lib and not the unwrapped set of kernels.
"^codegen/templates",
]
deps = [
"executorch",
]

[targets.quantized_kernels]
[targets.extension_module]
buck_targets = [
"//kernels/quantized:generated_lib",
"//extension/module:module",
]
filters = [
".cpp$",
]
excludes = [
# Exclude the codegen templates, which are picked up because the buck target
# is the generated_lib and not the unwrapped set of kernels.
"^codegen/templates",
]
deps = [
"executorch",
"extension_data_loader",
]

[targets.program_schema]
[targets.extension_runner_util]
buck_targets = [
"//schema:program",
"//extension/runner_util:inputs",
]
filters = [
".fbs$",
".cpp$",
]
deps = [
"executorch",
]

[targets.print_evalue]
# ---------------------------------- extension end ----------------------------------
# ---------------------------------- binary start ----------------------------------

[targets.executor_runner]
buck_targets = [
"//extension/evalue_util:print_evalue",
"//examples/portable/executor_runner:executor_runner",
]
filters = [
".cpp$",
]
excludes = [
"^codegen",
]
deps = [
"executorch",
"portable_kernels",
"quantized_kernels",
]

[targets.size_test]
Expand All @@ -143,7 +141,8 @@ excludes = [
deps = [
"executorch",
]

# ---------------------------------- binary end ----------------------------------
# ---------------------------------- MPS start ----------------------------------
[targets.mps_executor_runner]
buck_targets = [
"//examples/apple/mps/executor_runner:mps_executor_runner",
Expand Down Expand Up @@ -178,6 +177,9 @@ filters = [
".fbs$",
]

# ---------------------------------- MPS end ----------------------------------
# ---------------------------------- XNNPACK start ----------------------------------

[targets.xnn_executor_runner]
buck_targets = [
"//examples/xnnpack:xnn_executor_runner",
Expand Down Expand Up @@ -220,3 +222,24 @@ buck_targets = [
filters = [
".fbs$",
]
# ---------------------------------- XNNPACK end ----------------------------------
# ---------------------------------- LLama start ----------------------------------

[targets.llama_runner]
buck_targets = [
"//examples/models/llama2/runner:runner",
]
filters = [
".cpp$",
]
excludes = [
"^codegen",
]
deps = [
"executorch",
"extension_module",
"portable_kernels",
"quantized_kernels",
"xnnpack_backend",
]
# ---------------------------------- LLama start ----------------------------------
8 changes: 6 additions & 2 deletions build/executorch-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ endif()

set(lib_list
etdump bundled_program extension_data_loader ${FLATCC_LIB} mpsdelegate
qnn_executorch_backend
qnn_executorch_backend portable_ops_lib extension_module
)
foreach(lib ${lib_list})
# Name of the variable which stores result of the find_library search
Expand All @@ -53,7 +53,11 @@ foreach(lib ${lib_list})
message("${lib} library is not found.
If needed rebuild with the proper options in CMakeLists.txt")
else()
add_library(${lib} STATIC IMPORTED)
if("${lib}" STREQUAL "extension_module")
add_library(${lib} SHARED IMPORTED)
else()
add_library(${lib} STATIC IMPORTED)
endif()
set_target_properties(
${lib} PROPERTIES IMPORTED_LOCATION "${${lib_var}}"
)
Expand Down
64 changes: 64 additions & 0 deletions examples/models/llama2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

#
# Simple CMake build system for selective build demo.
#
# ### Editing this file ###
#
# This file should be formatted with
# ~~~
# cmake-format --first-comment-is-literal=True CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#
cmake_minimum_required(VERSION 3.19)
project(llama_runner)

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

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

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
# Can't set to 11 due to executor_runner.cpp make_unique
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)

#
# llama_main: test binary to run llama, with tokenizer and sampler integrated
#
add_executable(llama_main main.cpp)
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
target_link_options(llama_main PRIVATE "LINKER:--gc-sections")
endif()

# find `executorch` libraries
find_package(executorch CONFIG REQUIRED)

# llama_runner library
add_subdirectory(runner)

# XNNPACK pthreadpool cpuinfo
target_link_libraries(llama_main PUBLIC gflags llama_runner portable_ops_lib)
target_link_options(
llama_main PUBLIC "SHELL:LINKER:--whole-archive \
$<TARGET_FILE:portable_ops_lib> \
LINKER:--no-whole-archive")
target_compile_options(llama_main PUBLIC ${_common_compile_options})

# Print all summary
executorch_print_configuration_summary()
40 changes: 40 additions & 0 deletions examples/models/llama2/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

#
# Simple CMake build system for selective build demo.
#
# ### Editing this file ###
#
# This file should be formatted with
# ~~~
# cmake-format --first-comment-is-literal=True CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

include(${EXECUTORCH_ROOT}/build/Utils.cmake)
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
#
# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
#
set(EXECUTORCH_SRCS_FILE
"${CMAKE_CURRENT_BINARY_DIR}/../../../../executorch_srcs.cmake")

extract_sources(${EXECUTORCH_SRCS_FILE})

include(${EXECUTORCH_SRCS_FILE})

# build llama_runner library
list(TRANSFORM _llama_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")

target_include_directories(extension_module
INTERFACE ${_common_include_directories})

add_library(llama_runner SHARED ${_llama_runner__srcs})
target_link_libraries(
llama_runner PUBLIC executorch portable_kernels extension_module
extension_data_loader)
Loading