-
Notifications
You must be signed in to change notification settings - Fork 863
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
Fix cpp ci #2958
Fix cpp ci #2958
Changes from all commits
fae0bcd
801db4a
f1ff55b
c6996c9
9e777ba
72ddc3e
00c8627
2875fbf
ff432ba
4da43ae
11377a0
114bf43
f9d12a7
e153cde
0308189
3c77ec5
0a59947
563c2da
b4c95db
e5379a7
03ba212
17a0da3
ef62412
d9d338b
18a1164
1537eb6
e6ff2ac
ae31141
5faba3e
da76049
966fc9e
91665cf
5867e86
a2f1d25
f754432
c17d288
f16f7e1
71ba328
9a55564
d90722f
8630fdd
77fa78b
6ebce56
1a10eec
9e009c5
1a55615
907c3bd
d44e968
9bdfc92
9db8ec6
deb808a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,6 @@ if(CLANG_FORMAT_EXE) | |
${PROJECT_SOURCE_DIR}/test/*.hh | ||
) | ||
|
||
add_custom_target(format | ||
COMMAND | ||
${CLANG_FORMAT_EXE} -i -style=google ${ALL_CXX_SOURCE_FILES} | ||
) | ||
endif() | ||
|
||
|
||
|
@@ -31,6 +27,21 @@ find_package(fmt REQUIRED) | |
find_package(gflags REQUIRED) | ||
find_package(Torch REQUIRED) | ||
|
||
include(FetchContent) | ||
|
||
FetchContent_Declare( | ||
yaml-cpp | ||
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git | ||
GIT_TAG 0.8.0 # Can be a tag (yaml-cpp-x.x.x), a commit hash, or a branch name (master) | ||
) | ||
FetchContent_GetProperties(yaml-cpp) | ||
|
||
if(NOT yaml-cpp_POPULATED) | ||
message(STATUS "Fetching yaml-cpp...") | ||
FetchContent_Populate(yaml-cpp) | ||
add_subdirectory(${yaml-cpp_SOURCE_DIR} ${yaml-cpp_BINARY_DIR}) | ||
endif() | ||
|
||
Comment on lines
+30
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need build yaml-cpp with -DCMAKE_CXX_FLAGS="-fPIC" (see https://github.com/pytorch/serve/blob/master/cpp/build.sh#L133C84-L133C109) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") | ||
|
||
include_directories(${TORCH_INCLUDE_DIRS}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,36 +5,34 @@ | |
* cmake version: 3.18+ | ||
## Installation and Running TorchServe CPP | ||
|
||
This installation instruction assumes that TorchServe is already installed through pip/conda/source. If this is not the case install it after the `Install dependencies` step through your preferred method. | ||
|
||
### Install dependencies | ||
``` | ||
cd serve | ||
python ts_scripts/install_dependencies.py --cpp --environment dev [--cuda=cu121|cu118] | ||
``` | ||
### Building the backend | ||
Don't forget to install or update TorchServe at this point if it wasn't previously installed. | ||
``` | ||
## Dev Build | ||
cd cpp | ||
./build.sh [-g cu121|cu118] | ||
|
||
## Install TorchServe from source | ||
cd .. | ||
python ts_scripts/install_from_src.py | ||
``` | ||
### Set Environment Var | ||
#### On Mac | ||
``` | ||
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$(pwd)/_build/_deps/libtorch/lib | ||
``` | ||
#### On Ubuntu | ||
``` | ||
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/_build/_deps/libtorch/lib | ||
``` | ||
|
||
### Run TorchServe | ||
``` | ||
mkdir model_store | ||
torchserve --ncs --start --model-store model_store | ||
``` | ||
|
||
### Clean the build directory | ||
To clean the build directory in order to rebuild from scratch simply delete the cpp/_build directory with | ||
``` | ||
rm -rf cpp/_build | ||
``` | ||
|
||
Comment on lines
+29
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we keep this so that cx can clean up env in case of debugging? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lxning Not sure if I understand. This is meant to be executed if you want to rebuild from scratch. Like a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We haven't set clean rule at this moment. We need let cx know how to clean. |
||
## Backend | ||
TorchServe cpp backend can run as a process, which is similar to [TorchServe Python backend](https://github.com/pytorch/serve/tree/master/ts). By default, TorchServe supports torch scripted model in cpp backend. Other platforms such as MxNet, ONNX can be supported through custom handlers following the TorchScript example [src/backends/handler/torch_scripted_handler.hh](https://github.com/pytorch/serve/blob/master/cpp/src/backends/handler/torch_scripted_handler.hh). | ||
### Custom Handler | ||
|
@@ -89,11 +87,11 @@ python -c "import ts; from pathlib import Path; print((Path(ts.__file__).parent | |
3. Make sure you have the right conda/venv environment activated during building that you're also using to run TorchServe. | ||
|
||
Q: Build on Mac fails with `Library not loaded: @rpath/libomp.dylib` | ||
A: Install libomp with brew and link in /usr/local/lib | ||
A: Install libomp with brew and link in /usr/local/lib | ||
```bash | ||
brew install libomp | ||
sudo ln -s /opt/homebrew/opt/libomp/lib/libomp.dylib /usr/local/lib/libomp.dylib | ||
``` | ||
|
||
Q: When loading a handler which uses a model exported with torch._export.aot_compile the handler dies with "error: Error in dlopen: MODEL.SO : undefined symbol: SOME_SYMBOL". | ||
A: Make sure that you are using matching libtorch and Pytorch versions for inference and export, respectively. | ||
A: Make sure that you are using matching libtorch and Pytorch versions for inference and export, respectively. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,9 @@ function install_folly() { | |
echo -e "${COLOR_GREEN}[ INFO ] Building Folly ${COLOR_OFF}" | ||
cd $FOLLY_SRC_DIR | ||
|
||
if [ "$PLATFORM" = "Linux" ]; then | ||
SUDO="sudo" | ||
elif [ "$PLATFORM" = "Mac" ]; then | ||
SUDO="" | ||
fi | ||
$SUDO ./build/fbcode_builder/getdeps.py install-system-deps --recursive | ||
./build/fbcode_builder/getdeps.py install-system-deps --recursive | ||
|
||
$SUDO ./build/fbcode_builder/getdeps.py build \ | ||
./build/fbcode_builder/getdeps.py build \ | ||
--allow-system-packages \ | ||
--scratch-path $FOLLY_BUILD_DIR \ | ||
--extra-cmake-defines='{"CMAKE_CXX_FLAGS": "-fPIC -D_GLIBCXX_USE_CXX11_ABI=1"}' | ||
|
@@ -47,36 +42,29 @@ function install_folly() { | |
echo "$FOLLY_BUILD_DIR/installed" | ||
} | ||
|
||
function install_kineto() { | ||
if [ "$PLATFORM" = "Linux" ]; then | ||
echo -e "${COLOR_GREEN}[ INFO ] Skip install kineto on Linux ${COLOR_OFF}" | ||
elif [ "$PLATFORM" = "Mac" ]; then | ||
KINETO_SRC_DIR=$BASE_DIR/third-party/kineto | ||
|
||
if [ ! -d "$KINETO_SRC_DIR/libkineto/build" ] ; then | ||
cd $KINETO_SRC_DIR/libkineto | ||
mkdir build && cd build | ||
cmake .. | ||
make install | ||
fi | ||
fi | ||
|
||
cd "$BWD" || exit | ||
} | ||
|
||
function install_libtorch() { | ||
cd "$DEPS_DIR" || exit | ||
TORCH_VERSION="2.2.1" | ||
if [ -d "$DEPS_DIR/libtorch" ]; then | ||
RAW_VERSION=`cat "$DEPS_DIR/libtorch/build-version"` | ||
VERSION=`cat "$DEPS_DIR/libtorch/build-version" | cut -d "+" -f 1` | ||
if [ "$USE_NIGHTLIES" = "true" ] && [[ ! "${RAW_VERSION}" =~ .*"dev".* ]]; then | ||
rm -rf "$DEPS_DIR/libtorch" | ||
elif [ "$USE_NIGHTLIES" == "" ] && [ "$VERSION" != "$TORCH_VERSION" ]; then | ||
rm -rf "$DEPS_DIR/libtorch" | ||
fi | ||
fi | ||
if [ "$PLATFORM" = "Mac" ]; then | ||
if [ ! -d "$DEPS_DIR/libtorch" ]; then | ||
if [[ $(uname -m) == 'x86_64' ]]; then | ||
echo -e "${COLOR_GREEN}[ INFO ] Install libtorch on Mac x86_64 ${COLOR_OFF}" | ||
wget https://download.pytorch.org/libtorch/cpu/libtorch-macos-x86_64-${TORCH_VERSION}.zip | ||
unzip libtorch-macos-x86_64-${TORCH_VERSION}.zip | ||
wget -q https://download.pytorch.org/libtorch/cpu/libtorch-macos-x86_64-${TORCH_VERSION}.zip | ||
unzip -q libtorch-macos-x86_64-${TORCH_VERSION}.zip | ||
rm libtorch-macos-x86_64-${TORCH_VERSION}.zip | ||
else | ||
echo -e "${COLOR_GREEN}[ INFO ] Install libtorch on Mac arm64 ${COLOR_OFF}" | ||
wget https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-${TORCH_VERSION}.zip | ||
unzip libtorch-macos-arm64-${TORCH_VERSION}.zip | ||
wget -q https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-${TORCH_VERSION}.zip | ||
unzip -q libtorch-macos-arm64-${TORCH_VERSION}.zip | ||
rm libtorch-macos-arm64-${TORCH_VERSION}.zip | ||
fi | ||
fi | ||
|
@@ -86,27 +74,17 @@ function install_libtorch() { | |
echo -e "${COLOR_RED}[ ERROR ] Unknown platform: $PLATFORM ${COLOR_OFF}" | ||
exit 1 | ||
else # Linux | ||
if [ -d "$DEPS_DIR/libtorch" ]; then | ||
RAW_VERSION=`cat "$DEPS_DIR/libtorch/build-version"` | ||
VERSION=`cat "$DEPS_DIR/libtorch/build-version" | cut -d "+" -f 1` | ||
if [ "$USE_NIGHTLIES" = "true" ] && [[ ! "${RAW_VERSION}" =~ .*"dev".* ]]; then | ||
rm -rf "$DEPS_DIR/libtorch" | ||
elif [ "$USE_NIGHTLIES" == "" ] && [ "$VERSION" != "$TORCH_VERSION" ]; then | ||
rm -rf "$DEPS_DIR/libtorch" | ||
fi | ||
fi | ||
if [ ! -d "$DEPS_DIR/libtorch" ]; then | ||
cd "$DEPS_DIR" || exit | ||
echo -e "${COLOR_GREEN}[ INFO ] Install libtorch on Linux ${COLOR_OFF}" | ||
if [ "$USE_NIGHTLIES" == true ]; then | ||
URL=https://download.pytorch.org/libtorch/nightly/${CUDA}/libtorch-cxx11-abi-shared-with-deps-latest.zip | ||
else | ||
URL=https://download.pytorch.org/libtorch/${CUDA}/libtorch-cxx11-abi-shared-with-deps-${TORCH_VERSION}%2B${CUDA}.zip | ||
fi | ||
wget $URL | ||
wget -q $URL | ||
ZIP_FILE=$(basename "$URL") | ||
ZIP_FILE="${ZIP_FILE//%2B/+}" | ||
unzip $ZIP_FILE | ||
unzip -q $ZIP_FILE | ||
rm $ZIP_FILE | ||
fi | ||
echo -e "${COLOR_GREEN}[ INFO ] libtorch is installed ${COLOR_OFF}" | ||
|
@@ -115,58 +93,22 @@ function install_libtorch() { | |
cd "$BWD" || exit | ||
} | ||
|
||
function install_yaml_cpp() { | ||
YAML_CPP_SRC_DIR=$BASE_DIR/third-party/yaml-cpp | ||
YAML_CPP_BUILD_DIR=$DEPS_DIR/yaml-cpp-build | ||
|
||
if [ ! -d "$YAML_CPP_BUILD_DIR" ] ; then | ||
echo -e "${COLOR_GREEN}[ INFO ] Building yaml-cpp ${COLOR_OFF}" | ||
|
||
if [ "$PLATFORM" = "Linux" ]; then | ||
SUDO="sudo" | ||
elif [ "$PLATFORM" = "Mac" ]; then | ||
SUDO="" | ||
fi | ||
|
||
mkdir $YAML_CPP_BUILD_DIR | ||
cd $YAML_CPP_BUILD_DIR | ||
cmake $YAML_CPP_SRC_DIR -DYAML_BUILD_SHARED_LIBS=ON -DYAML_CPP_BUILD_TESTS=OFF -DCMAKE_CXX_FLAGS="-fPIC" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need build yaml-cpp with DCMAKE_CXX_FLAGS="-fPIC" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above |
||
$SUDO make install | ||
|
||
echo -e "${COLOR_GREEN}[ INFO ] yaml-cpp is installed ${COLOR_OFF}" | ||
fi | ||
|
||
cd "$BWD" || exit | ||
} | ||
|
||
function build_llama_cpp() { | ||
BWD=$(pwd) | ||
LLAMA_CPP_SRC_DIR=$BASE_DIR/third-party/llama.cpp | ||
cd "${LLAMA_CPP_SRC_DIR}" | ||
if [ "$PLATFORM" = "Mac" ]; then | ||
make LLAMA_METAL=OFF -j | ||
else | ||
make -j | ||
fi | ||
cd "$BWD" || exit | ||
} | ||
|
||
function prepare_test_files() { | ||
echo -e "${COLOR_GREEN}[ INFO ]Preparing test files ${COLOR_OFF}" | ||
local EX_DIR="${TR_DIR}/examples/" | ||
rsync -a --link-dest=../../test/resources/ ${BASE_DIR}/test/resources/ ${TR_DIR}/ | ||
if [ ! -f "${EX_DIR}/babyllama/babyllama_handler/tokenizer.bin" ]; then | ||
wget https://github.com/karpathy/llama2.c/raw/master/tokenizer.bin -O "${EX_DIR}/babyllama/babyllama_handler/tokenizer.bin" | ||
wget -q https://github.com/karpathy/llama2.c/raw/master/tokenizer.bin -O "${EX_DIR}/babyllama/babyllama_handler/tokenizer.bin" | ||
fi | ||
if [ ! -f "${EX_DIR}/babyllama/babyllama_handler/stories15M.bin" ]; then | ||
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin -O "${EX_DIR}/babyllama/babyllama_handler/stories15M.bin" | ||
wget -q https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.bin -O "${EX_DIR}/babyllama/babyllama_handler/stories15M.bin" | ||
fi | ||
# PT2.2 torch.expport does not support Mac | ||
if [ "$PLATFORM" = "Linux" ]; then | ||
if [ ! -f "${EX_DIR}/aot_inductor/llama_handler/stories15M.so" ]; then | ||
local HANDLER_DIR=${EX_DIR}/aot_inductor/llama_handler/ | ||
if [ ! -f "${HANDLER_DIR}/stories15M.pt" ]; then | ||
wget https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.pt?download=true -O "${HANDLER_DIR}/stories15M.pt" | ||
wget -q https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.pt?download=true -O "${HANDLER_DIR}/stories15M.pt" | ||
fi | ||
local LLAMA_SO_DIR=${BASE_DIR}/third-party/llama2.so/ | ||
PYTHONPATH=${LLAMA_SO_DIR}:${PYTHONPATH} python ${BASE_DIR}/../examples/cpp/aot_inductor/llama2/compile.py --checkpoint ${HANDLER_DIR}/stories15M.pt ${HANDLER_DIR}/stories15M.so | ||
|
@@ -221,12 +163,11 @@ function build() { | |
|
||
# Build torchserve_cpp with cmake | ||
cd "$BWD" || exit | ||
YAML_CPP_CMAKE_DIR=$DEPS_DIR/yaml-cpp-build | ||
FOLLY_CMAKE_DIR=$DEPS_DIR/folly-build/installed | ||
find $FOLLY_CMAKE_DIR -name "lib*.*" -exec ln -s "{}" $LIBS_DIR/ \; | ||
if [ "$PLATFORM" = "Linux" ]; then | ||
cmake \ | ||
-DCMAKE_PREFIX_PATH="$DEPS_DIR;$FOLLY_CMAKE_DIR;$YAML_CPP_CMAKE_DIR;$DEPS_DIR/libtorch" \ | ||
-DCMAKE_PREFIX_PATH="$DEPS_DIR;$FOLLY_CMAKE_DIR;$DEPS_DIR/libtorch" \ | ||
-DCMAKE_INSTALL_PREFIX="$PREFIX" \ | ||
"$MAYBE_BUILD_QUIC" \ | ||
"$MAYBE_BUILD_TESTS" \ | ||
|
@@ -242,8 +183,10 @@ function build() { | |
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda/bin/nvcc | ||
fi | ||
elif [ "$PLATFORM" = "Mac" ]; then | ||
export LIBRARY_PATH=${LIBRARY_PATH}:`brew --prefix icu4c`/lib:`brew --prefix libomp`/lib | ||
|
||
cmake \ | ||
-DCMAKE_PREFIX_PATH="$DEPS_DIR;$FOLLY_CMAKE_DIR;$YAML_CPP_CMAKE_DIR;$DEPS_DIR/libtorch" \ | ||
-DCMAKE_PREFIX_PATH="$DEPS_DIR;$FOLLY_CMAKE_DIR;$DEPS_DIR/libtorch" \ | ||
-DCMAKE_INSTALL_PREFIX="$PREFIX" \ | ||
"$MAYBE_BUILD_QUIC" \ | ||
"$MAYBE_BUILD_TESTS" \ | ||
|
@@ -252,9 +195,10 @@ function build() { | |
"$MAYBE_USE_STATIC_DEPS" \ | ||
"$MAYBE_LIB_FUZZING_ENGINE" \ | ||
"$MAYBE_NIGHTLIES" \ | ||
"-DLLAMA_METAL=OFF" \ | ||
.. | ||
|
||
export LIBRARY_PATH=${LIBRARY_PATH}:/usr/local/opt/icu4c/lib | ||
|
||
else | ||
# TODO: Windows | ||
echo -e "${COLOR_RED}[ ERROR ] Unknown platform: $PLATFORM ${COLOR_OFF}" | ||
|
@@ -282,16 +226,8 @@ function symlink_torch_libs() { | |
fi | ||
} | ||
|
||
function symlink_yaml_cpp_lib() { | ||
if [ "$PLATFORM" = "Linux" ]; then | ||
ln -sf ${DEPS_DIR}/yaml-cpp-build/*.so* ${LIBS_DIR} | ||
elif [ "$PLATFORM" = "Mac" ]; then | ||
ln -sf ${DEPS_DIR}/yaml-cpp-build/*.dylib* ${LIBS_DIR} | ||
fi | ||
} | ||
|
||
function install_torchserve_cpp() { | ||
TARGET_DIR=$BASE_DIR/../ts/cpp/ | ||
TARGET_DIR=`python -c "import ts; from pathlib import Path; print(Path(ts.__file__).parent / 'cpp')"` | ||
|
||
if [ -d $TARGET_DIR ]; then | ||
rm -rf $TARGET_DIR | ||
|
@@ -370,12 +306,8 @@ cd $BASE_DIR | |
git submodule update --init --recursive | ||
|
||
install_folly | ||
#install_kineto | ||
install_libtorch | ||
install_yaml_cpp | ||
build_llama_cpp | ||
prepare_test_files | ||
build | ||
symlink_torch_libs | ||
symlink_yaml_cpp_lib | ||
install_torchserve_cpp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we check cmake version before we proceed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lxning not sure what you mean? Inside the workflow file? What would be the consequences of the check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
install_dependency with option "cpp" will install cmake (eg. mac, linux). Currently some cmake version can cause cpp compile error. Better to check cmake version before cijob proceed to save debug time.