Skip to content

Commit d654750

Browse files
mresolxning
andauthored
CPP build in pure cmake (#3021)
* Move cpp install into cmake file * add pytest to cpp ci workflow * Fix cpp ci * Add debugging * Change exec dir for test * Start running of tests to ctest; removed from build.sh start fixing unit tests * fix tests * Enable test before adding subdirectory; use make test in build.sh * Remove build.sh * Fix import error in python 3.9 * Fix examples + halde nccl not found * Fix lib not found * convert tokenizer-cpp from submodule to fetch_content * Change skip condition for cpp pytest * Remove kineto submodule * Remove yaml-cpp submodule * Convert llama2.so to fetch_content * Convert llama2.c submodule into fetch_content * fix relateive path --------- Co-authored-by: lxning <[email protected]> Co-authored-by: lxning <[email protected]>
1 parent aab9950 commit d654750

39 files changed

+167
-301
lines changed

.github/workflows/ci-cpu-cpp.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@ jobs:
5353
python ts_scripts/print_env_info.py
5454
- name: Build
5555
run: |
56-
cd cpp && rm -rf _build && sudo mkdir /mnt/_build && sudo chmod 777 /mnt/_build && mkdir _build && sudo mount --bind /mnt/_build _build
57-
./build.sh
56+
cd cpp && rm -rf _build && sudo mkdir /mnt/_build && sudo chmod 777 /mnt/_build && mkdir build && sudo mount --bind /mnt/_build build
57+
cd build && cmake ..
58+
make -j && make install
59+
make test
60+
- name: Run test
61+
run: |
62+
cd test && pytest pytest/test_cpp_backend.py

.gitmodules

-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
11
[submodule "third_party/google/rpc"]
22
path = third_party/google/rpc
33
url = https://github.com/googleapis/googleapis.git
4-
[submodule "cpp/third-party/llama2.c"]
5-
path = cpp/third-party/llama2.c
6-
url = https://github.com/karpathy/llama2.c
7-
[submodule "cpp/third-party/llama2.so"]
8-
path = cpp/third-party/llama2.so
9-
url = https://github.com/mreso/llama2.so.git
10-
[submodule "cpp/third-party/yaml-cpp"]
11-
path = cpp/third-party/yaml-cpp
12-
url = https://github.com/jbeder/yaml-cpp.git
13-
[submodule "cpp/third-party/tokenizers-cpp"]
14-
path = cpp/third-party/tokenizers-cpp
15-
url = https://github.com/mlc-ai/tokenizers-cpp.git
16-
[submodule "cpp/third-party/kineto"]
17-
path = cpp/third-party/kineto
18-
url = https://github.com/pytorch/kineto.git

cpp/CMakeLists.txt

+24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
cmake_minimum_required(VERSION 3.26.4 FATAL_ERROR)
22
project(torchserve_cpp VERSION 0.1)
33

4+
enable_testing()
5+
46
set(CMAKE_CXX_STANDARD 17)
57
set(CMAKE_CXX_STANDARD_REQUIRED True)
68
set(CMAKE_CXX_EXTENSIONS OFF)
79
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Wall -Wextra -fPIC")
10+
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR})
811

912
find_program(CLANG_TIDY_EXE NAMES "clang-tidy" REQUIRED)
1013
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
@@ -20,10 +23,29 @@ if(CLANG_FORMAT_EXE)
2023

2124
endif()
2225

26+
execute_process(COMMAND python -c "import importlib.util; print(importlib.util.find_spec('nvidia') is not None)"
27+
OUTPUT_VARIABLE NVIDIA_AVAILABLE
28+
OUTPUT_STRIP_TRAILING_WHITESPACE)
29+
if(${NVIDIA_AVAILABLE} STREQUAL "True")
30+
execute_process(COMMAND python -c "import nvidia;from pathlib import Path;print(Path(nvidia.__file__).parent/'nccl'/'lib')"
31+
OUTPUT_VARIABLE NCCL_PATH
32+
OUTPUT_STRIP_TRAILING_WHITESPACE)
33+
list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".so.2")
34+
endif()
35+
36+
execute_process(COMMAND python -c "import torch;print(torch.utils.cmake_prefix_path)"
37+
OUTPUT_VARIABLE TORCH_CMAKE_PREFIX_PATH
38+
OUTPUT_STRIP_TRAILING_WHITESPACE)
39+
list(APPEND CMAKE_PREFIX_PATH ${TORCH_CMAKE_PREFIX_PATH})
2340

2441
find_package(Boost REQUIRED)
2542
find_package(Torch REQUIRED)
2643

44+
find_library(NCCL_LIBRARY nccl HINTS ${NCCL_PATH})
45+
if("${NCCL_LIBRARY}" STREQUAL "NCCL_LIBRARY-NOTFOUND")
46+
set(NCCL_LIBRARY "")
47+
endif()
48+
2749
include(FetchContent)
2850

2951
FetchContent_Declare(
@@ -71,3 +93,5 @@ add_subdirectory(src/examples)
7193
add_subdirectory(test)
7294

7395
FILE(COPY src/resources/logging.yaml DESTINATION "${CMAKE_INSTALL_PREFIX}/resources")
96+
97+
install(SCRIPT "install.cmake")

cpp/README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ cd serve/docker
1717
Start the container and optionally bind mount a build directory into the container to persist build artifacts across container runs
1818
```
1919
# For CPU support
20-
docker run [-v /path/to/build/dir:/serve/cpp/_build] -it pytorch/torchserve:cpp-dev-cpu /bin/bash
20+
docker run [-v /path/to/build/dir:/serve/cpp/build] -it pytorch/torchserve:cpp-dev-cpu /bin/bash
2121
# For GPU support
22-
docker run --gpus all [-v /path/to/build/dir:/serve/cpp/_build] -it pytorch/torchserve:cpp-dev-gpu /bin/bash
22+
docker run --gpus all [-v /path/to/build/dir:/serve/cpp/build] -it pytorch/torchserve:cpp-dev-gpu /bin/bash
2323
```
2424
`Warning`: The dev docker container does not install all necessary dependencies or build Torchserve CPP. Please follow the steps below after starting the container.
2525

@@ -41,7 +41,12 @@ Then build the backend:
4141
```
4242
## Dev Build
4343
cd cpp
44-
./build.sh
44+
mkdir build && cd build
45+
# Optionally, you can skip building the tests by adding: -DBUILD_TESTS=OFF
46+
cmake ..
47+
make -j && make install
48+
## Optionally, you can run the tests with
49+
make test
4550
```
4651

4752
### Run TorchServe

cpp/build.sh

-171
This file was deleted.

cpp/install.cmake

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
execute_process(COMMAND python -c "import ts; from pathlib import Path; print(Path(ts.__file__).parent / 'cpp')"
2+
OUTPUT_VARIABLE TARGET_DIR
3+
OUTPUT_STRIP_TRAILING_WHITESPACE)
4+
5+
message("Installing cpp backend into ${TARGET_DIR}")
6+
7+
if(EXISTS ${TARGET_DIR})
8+
execute_process(COMMAND rm -rf ${TARGET_DIR})
9+
endif()
10+
11+
execute_process(COMMAND mkdir ${TARGET_DIR})
12+
execute_process(COMMAND cp -rp ${CMAKE_BINARY_DIR}/bin ${TARGET_DIR}/bin)
13+
execute_process(COMMAND cp -rp ${CMAKE_BINARY_DIR}/libs ${TARGET_DIR}/lib)
14+
execute_process(COMMAND cp -rp ${CMAKE_BINARY_DIR}/resources ${TARGET_DIR}/resources)

cpp/src/backends/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ list(APPEND TS_BACKENDS_PROTOCOL_SOURCE_FILES ${TS_BACKENDS_PROTOCOL_SRC_DIR}/so
2626
add_library(ts_backends_protocol SHARED ${TS_BACKENDS_PROTOCOL_SOURCE_FILES})
2727
target_include_directories(ts_backends_protocol PUBLIC ${TS_BACKENDS_PROTOCOL_SRC_DIR})
2828
target_link_libraries(ts_backends_protocol PRIVATE ts_utils)
29-
install(TARGETS ts_backends_protocol DESTINATION ${torchserve_cpp_SOURCE_DIR}/_build/libs)
29+
install(TARGETS ts_backends_protocol DESTINATION ${CMAKE_INSTALL_PREFIX}/libs)
3030

3131
# build library ts_backend_core
3232
set(BACKEND_SOURCE_FILES "")
@@ -37,7 +37,7 @@ list(APPEND BACKEND_SOURCE_FILES ${TS_BACKENDS_SRC_DIR}/handler/torch_scripted_h
3737
add_library(ts_backends_core SHARED ${BACKEND_SOURCE_FILES})
3838
target_include_directories(ts_backends_core PUBLIC ${TS_BACKENDS_CORE_SRC_DIR})
3939
target_link_libraries(ts_backends_core PUBLIC ts_utils ts_backends_protocol ${TORCH_LIBRARIES})
40-
install(TARGETS ts_backends_core DESTINATION ${torchserve_cpp_SOURCE_DIR}/_build/libs)
40+
install(TARGETS ts_backends_core DESTINATION ${CMAKE_INSTALL_PREFIX}/libs)
4141

4242
# build exe model_worker_socket
4343
add_executable(model_worker_socket
@@ -51,5 +51,5 @@ target_include_directories(model_worker_socket PRIVATE
5151
${TS_BACKENDS_TORCH_SCRIPTED_SRC_DIR}
5252
)
5353
target_link_libraries(model_worker_socket
54-
PRIVATE ts_backends_core ts_backends_protocol ${TORCH_LIBRARIES} gflags)
55-
install(TARGETS model_worker_socket DESTINATION ${torchserve_cpp_SOURCE_DIR}/_build/bin)
54+
PRIVATE ts_backends_core ts_backends_protocol ${TORCH_LIBRARIES} gflags ${NCCL_LIBRARY})
55+
install(TARGETS model_worker_socket DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)

cpp/src/examples/CMakeLists.txt

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../test/resources/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../test/resources/)
3-
41
add_subdirectory("../../../examples/cpp/babyllama/" "${CMAKE_CURRENT_BINARY_DIR}/../../test/resources/examples/babyllama/babyllama_handler/")
52

63
add_subdirectory("../../../examples/cpp/llamacpp/" "${CMAKE_CURRENT_BINARY_DIR}/../../test/resources/examples/llamacpp/llamacpp_handler/")

cpp/src/utils/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ else()
4747
target_link_libraries(ts_utils ${CMAKE_DL_LIBS} ${Boost_LIBRARIES} yaml-cpp nlohmann_json fmt)
4848
endif()
4949

50-
install(TARGETS ts_utils DESTINATION ${torchserve_cpp_SOURCE_DIR}/_build/libs)
50+
install(TARGETS ts_utils DESTINATION ${CMAKE_INSTALL_PREFIX}/libs)
5151

5252
list(APPEND FOO_SOURCE_FILES ${TS_UTILS_SRC_DIR}/ifoo.hh)
5353
list(APPEND FOO_SOURCE_FILES ${TS_UTILS_SRC_DIR}/foo.hh)

cpp/test/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ FetchContent_Declare(
66
)
77
FetchContent_MakeAvailable(googletest)
88
# run google test
9-
enable_testing()
9+
1010

1111
set(TEST_BINARY ${CMAKE_PROJECT_NAME}_test)
1212
file(GLOB_RECURSE TEST_SOURCES LIST_DIRECTORIES false *.cc *.hh)
13+
file(COPY resources/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/resources/)
1314

1415
add_executable(${TEST_BINARY} ${TEST_SOURCES})
15-
target_link_libraries(${TEST_BINARY} gtest_main gmock_main ts_backends_core ts_backends_protocol ts_utils ${TORCH_LIBRARIES})
16+
target_link_libraries(${TEST_BINARY} gtest_main gmock_main ts_backends_core ts_backends_protocol ts_utils ${TORCH_LIBRARIES} ${NCCL_LIBRARY})
1617

1718
include(GoogleTest)
1819
gtest_discover_tests(${TEST_BINARY})

0 commit comments

Comments
 (0)