Skip to content

Commit fa0f1e3

Browse files
authored
Fix partial compile error on mac x86 (#2927)
* fix compile error on mac x86 * update install libtorch
1 parent 3627ee6 commit fa0f1e3

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

cpp/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
22
project(torchserve_cpp VERSION 0.1)
33

44
set(CMAKE_CXX_STANDARD 17)

cpp/build.sh

+51-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function install_folly() {
2828
echo -e "${COLOR_GREEN}[ INFO ] Cloning folly repo ${COLOR_OFF}"
2929
git clone https://github.com/facebook/folly.git "$FOLLY_SRC_DIR"
3030
cd $FOLLY_SRC_DIR
31-
git checkout tags/v2022.06.27.00
31+
git checkout tags/v2024.01.29.00
3232
fi
3333

3434
if [ ! -d "$FOLLY_BUILD_DIR" ] ; then
@@ -74,11 +74,21 @@ function install_kineto() {
7474
}
7575

7676
function install_libtorch() {
77-
if [ "$PLATFORM" = "Mac" ]; then
78-
echo -e "${COLOR_GREEN}[ INFO ] Skip install libtorch on Mac ${COLOR_OFF}"
79-
elif [ ! -d "$DEPS_DIR/libtorch" ] ; then
77+
if [ ! -d "$DEPS_DIR/libtorch" ] ; then
8078
cd "$DEPS_DIR" || exit
81-
if [ "$PLATFORM" = "Linux" ]; then
79+
if [ "$PLATFORM" = "Mac" ]; then
80+
if [[ $(uname -m) == 'x86_64' ]]; then
81+
echo -e "${COLOR_GREEN}[ INFO ] Install libtorch on Mac x86_64 ${COLOR_OFF}"
82+
wget https://download.pytorch.org/libtorch/cpu/libtorch-macos-x86_64-2.2.0.zip
83+
unzip libtorch-macos-x86_64-2.2.0.zip
84+
rm libtorch-macos-x86_64-2.2.0.zip
85+
else
86+
echo -e "${COLOR_GREEN}[ INFO ] Install libtorch on Mac arm64 ${COLOR_OFF}"
87+
wget https://download.pytorch.org/libtorch/cpu/libtorch-macos-arm64-2.2.0.zip
88+
unzip libtorch-macos-arm64-2.2.0.zip
89+
rm libtorch-macos-arm64-2.2.0.zip
90+
fi
91+
elif [ "$PLATFORM" = "Linux" ]; then
8292
echo -e "${COLOR_GREEN}[ INFO ] Install libtorch on Linux ${COLOR_OFF}"
8393
if [ "$CUDA" = "cu118" ]; then
8494
wget https://download.pytorch.org/libtorch/cu118/libtorch-cxx11-abi-shared-with-deps-2.1.1%2Bcu118.zip
@@ -113,7 +123,7 @@ function install_yaml_cpp() {
113123
echo -e "${COLOR_GREEN}[ INFO ] Cloning yaml-cpp repo ${COLOR_OFF}"
114124
git clone https://github.com/jbeder/yaml-cpp.git "$YAML_CPP_SRC_DIR"
115125
cd $YAML_CPP_SRC_DIR
116-
git checkout tags/yaml-cpp-0.7.0
126+
git checkout tags/0.8.0
117127
fi
118128

119129
if [ ! -d "$YAML_CPP_BUILD_DIR" ] ; then
@@ -136,11 +146,42 @@ function install_yaml_cpp() {
136146
cd "$BWD" || exit
137147
}
138148

149+
function install_sentencepiece() {
150+
SENTENCEPIECE_SRC_DIR=$BASE_DIR/third-party/sentencepiece
151+
SENTENCEPIECE_BUILD_DIR=$DEPS_DIR/sentencepiece-build
152+
153+
if [ ! -d "$SENTENCEPIECE_SRC_DIR" ] ; then
154+
echo -e "${COLOR_GREEN}[ INFO ] Cloning sentencepiece repo ${COLOR_OFF}"
155+
git clone https://github.com/google/sentencepiece.git "$SENTENCEPIECE_SRC_DIR"
156+
cd $SENTENCEPIECE_SRC_DIR
157+
git checkout tags/v0.1.99
158+
fi
159+
160+
if [ ! -d "$SENTENCEPIECE_BUILD_DIR" ] ; then
161+
echo -e "${COLOR_GREEN}[ INFO ] Building sentencepiece ${COLOR_OFF}"
162+
163+
mkdir $SENTENCEPIECE_BUILD_DIR
164+
cd $SENTENCEPIECE_BUILD_DIR
165+
cmake $SENTENCEPIECE_SRC_DIR
166+
make -i $(nproc)
167+
if [ "$PLATFORM" = "Linux" ]; then
168+
sudo make install
169+
sudo ldconfig -v
170+
elif [ "$PLATFORM" = "Mac" ]; then
171+
make install
172+
fi
173+
174+
echo -e "${COLOR_GREEN}[ INFO ] sentencepiece is installed ${COLOR_OFF}"
175+
fi
176+
177+
cd "$BWD" || exit
178+
}
179+
139180
function build_llama_cpp() {
140181
BWD=$(pwd)
141182
LLAMA_CPP_SRC_DIR=$BASE_DIR/third-party/llama.cpp
142183
cd "${LLAMA_CPP_SRC_DIR}"
143-
make
184+
make LLAMA_METAL=OFF
144185
cd "$BWD" || exit
145186
}
146187

@@ -191,7 +232,7 @@ function build() {
191232
fi
192233
elif [ "$PLATFORM" = "Mac" ]; then
193234
cmake \
194-
-DCMAKE_PREFIX_PATH="$(python -c 'import torch; print(torch.utils.cmake_prefix_path)');$DEPS_DIR;$FOLLY_CMAKE_DIR;$YAML_CPP_CMAKE_DIR" \
235+
-DCMAKE_PREFIX_PATH="$DEPS_DIR;$FOLLY_CMAKE_DIR;$YAML_CPP_CMAKE_DIR;$DEPS_DIR/libtorch" \
195236
-DCMAKE_INSTALL_PREFIX="$PREFIX" \
196237
"$MAYBE_BUILD_QUIC" \
197238
"$MAYBE_BUILD_TESTS" \
@@ -225,7 +266,7 @@ function build() {
225266

226267
function symlink_torch_libs() {
227268
if [ "$PLATFORM" = "Linux" ]; then
228-
ln -sf ${DEPS_DIR}/libtorch/lib/*.so* ${BUILD_DIR}/libs/
269+
ln -sf ${DEPS_DIR}/libtorch/lib/*.so* ${LIBS_DIR}
229270
fi
230271
}
231272

@@ -315,6 +356,7 @@ install_folly
315356
install_kineto
316357
install_libtorch
317358
install_yaml_cpp
359+
install_sentencepiece
318360
build_llama_cpp
319361
build
320362
symlink_torch_libs

cpp/src/backends/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ list(APPEND BACKEND_SOURCE_FILES ${TS_BACKENDS_SRC_DIR}/handler/base_handler.cc)
2222
list(APPEND BACKEND_SOURCE_FILES ${TS_BACKENDS_SRC_DIR}/handler/torch_scripted_handler.cc)
2323
add_library(ts_backends_core SHARED ${BACKEND_SOURCE_FILES})
2424
target_include_directories(ts_backends_core PUBLIC ${TS_BACKENDS_CORE_SRC_DIR})
25-
target_link_libraries(ts_backends_core PUBLIC ts_utils ts_backends_protocol ${FOLLY_LIBRARIES})
25+
target_link_libraries(ts_backends_core PUBLIC ts_utils ts_backends_protocol ${FOLLY_LIBRARIES} ${TORCH_LIBRARIES})
2626
install(TARGETS ts_backends_core DESTINATION ${torchserve_cpp_SOURCE_DIR}/_build/libs)
2727

2828
# build exe model_worker_socket

cpp/src/examples/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
add_subdirectory("../../../examples/cpp/babyllama/" "../../../test/resources/examples/babyllama/babyllama_handler/")
2+
add_subdirectory("../../../examples/cpp/babyllama/" "${CMAKE_CURRENT_BINARY_DIR}/../../test/resources/examples/babyllama/babyllama_handler/")
33

4-
add_subdirectory("../../../examples/cpp/llamacpp/" "../../../test/resources/examples/llamacpp/llamacpp_handler/")
4+
add_subdirectory("../../../examples/cpp/llamacpp/" "${CMAKE_CURRENT_BINARY_DIR}/../../test/resources/examples/llamacpp/llamacpp_handler/")
55

6-
add_subdirectory("../../../examples/cpp/mnist/" "../../../test/resources/examples/mnist/mnist_handler/")
6+
add_subdirectory("../../../examples/cpp/mnist/" "${CMAKE_CURRENT_BINARY_DIR}/../../test/resources/examples/mnist/mnist_handler/")

cpp/src/utils/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ list(APPEND TS_UTILS_SOURCE_FILES ${TS_UTILS_SRC_DIR}/metrics/registry.cc)
1212
add_library(ts_utils SHARED ${TS_UTILS_SOURCE_FILES})
1313
target_include_directories(ts_utils PUBLIC ${TS_UTILS_SRC_DIR})
1414
target_include_directories(ts_utils PRIVATE ${Boost_INCLUDE_DIRS})
15-
target_link_libraries(ts_utils ${FOLLY_LIBRARIES} ${CMAKE_DL_LIBS} ${Boost_LIBRARIES} yaml-cpp)
15+
target_link_libraries(ts_utils ${FOLLY_LIBRARIES} ${CMAKE_DL_LIBS} ${Boost_LIBRARIES} yaml-cpp::yaml-cpp)
1616
install(TARGETS ts_utils DESTINATION ${torchserve_cpp_SOURCE_DIR}/_build/libs)
1717

1818
list(APPEND FOO_SOURCE_FILES ${TS_UTILS_SRC_DIR}/ifoo.hh)

0 commit comments

Comments
 (0)