Skip to content

Commit 10cd168

Browse files
PaulZhang12facebook-github-bot
authored andcommitted
Fix TorchRec inference solution with updated torch.fx + TorchScript Solution (#2101)
Summary: Previously, the TorchRec inference solution used torch.deploy, which isn't supported, and the solution itself was outdated and broken. This PR revamps the inference example with Torch.FX and TorchScript, which more accurately represents what is used in production for RecSys currently. Furthermore, the example was constructed with simplicity as the top priority, with fewer package requirements and steps to run. This is the first iteration of the inference example, with more to come! Pull Request resolved: #2101 Differential Revision: D58478461 Pulled By: PaulZhang12
1 parent 6337072 commit 10cd168

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2843
-567
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

torchrec/inference/CMakeLists.txt

Lines changed: 37 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,110 +4,60 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
8-
project(inference)
7+
cmake_minimum_required(VERSION 3.8)
98

10-
# This step is crucial to ensure that the
11-
# _REFLECTION, _GRPC_GRPCPP and _PROTOBUF_LIBPROTOBUF variables are set.
12-
# e.g. ~/gprc/examples/cpp/cmake/common.cmake
13-
include(${GRPC_COMMON_CMAKE_PATH}/common.cmake)
9+
project(inference C CXX)
1410

11+
include(/home/paulzhan/grpc/examples/cpp/cmake/common.cmake)
1512

16-
# abi and other flags
1713

18-
if(DEFINED GLIBCXX_USE_CXX11_ABI)
19-
if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
20-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1")
21-
set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=1")
22-
endif()
23-
endif()
14+
# Proto file
15+
get_filename_component(hw_proto "/home/paulzhan/torchrec/torchrec/inference/protos/predictor.proto" ABSOLUTE)
16+
get_filename_component(hw_proto_path "${hw_proto}" PATH)
2417

25-
# keep it static for now since folly-shared version is broken
26-
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
27-
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
18+
# Generated sources
19+
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/predictor.pb.cc")
20+
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/predictor.pb.h")
21+
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/predictor.grpc.pb.cc")
22+
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/predictor.grpc.pb.h")
23+
add_custom_command(
24+
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
25+
COMMAND ${_PROTOBUF_PROTOC}
26+
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
27+
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
28+
-I "${hw_proto_path}"
29+
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
30+
"${hw_proto}"
31+
DEPENDS "${hw_proto}")
2832

29-
# dependencies
30-
find_package(Boost REQUIRED)
31-
find_package(Torch REQUIRED)
32-
find_package(folly REQUIRED)
33-
find_package(gflags REQUIRED)
34-
35-
include_directories(${Torch_INCLUDE_DIRS})
36-
include_directories(${folly_INCLUDE_DIRS})
37-
include_directories(${PYTORCH_FMT_INCLUDE_PATH})
33+
# Include generated *.pb.h files
34+
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
3835

3936
set(CMAKE_CXX_STANDARD 17)
4037

41-
# torch deploy library
42-
add_library(torch_deploy_internal STATIC
43-
${DEPLOY_INTERPRETER_PATH}/libtorch_deployinterpreter.o
44-
${DEPLOY_SRC_PATH}/deploy.cpp
45-
${DEPLOY_SRC_PATH}/loader.cpp
46-
${DEPLOY_SRC_PATH}/path_environment.cpp
47-
${DEPLOY_SRC_PATH}/elf_file.cpp)
48-
49-
# For python builtins. caffe2_interface_library properly
50-
# makes use of the --whole-archive option.
51-
target_link_libraries(torch_deploy_internal PRIVATE
52-
crypt pthread dl util m z ffi lzma readline nsl ncursesw panelw
53-
)
54-
target_link_libraries(torch_deploy_internal
55-
PUBLIC shm torch ${PYTORCH_LIB_FMT}
56-
)
57-
caffe2_interface_library(torch_deploy_internal torch_deploy)
58-
59-
# inference library
60-
61-
# for our own header files
62-
include_directories(include/)
63-
include_directories(gen/)
64-
65-
# define our library target
66-
add_library(inference STATIC
67-
src/Batching.cpp
68-
src/BatchingQueue.cpp
69-
src/GPUExecutor.cpp
70-
src/ResultSplit.cpp
71-
src/Exception.cpp
72-
src/ResourceManager.cpp
73-
)
74-
75-
# -rdynamic is needed to link against the static library
76-
target_link_libraries(inference "-Wl,--no-as-needed -rdynamic"
77-
dl torch_deploy "${TORCH_LIBRARIES}" ${FBGEMM_LIB} ${FOLLY_LIBRARIES}
78-
)
79-
80-
# for generated protobuf
81-
82-
# grpc headers. e.g. ~/.local/include
83-
include_directories(${GRPC_HEADER_INCLUDE_PATH})
84-
85-
set(pred_grpc_srcs "gen/torchrec/inference/predictor.grpc.pb.cc")
86-
set(pred_grpc_hdrs "gen/torchrec/inference/predictor.grpc.pb.h")
87-
set(pred_proto_srcs "gen/torchrec/inference/predictor.pb.cc")
88-
set(pred_proto_hdrs "gen/torchrec/inference/predictor.pb.h")
38+
# Torch + FBGEMM
39+
find_package(Torch REQUIRED)
40+
add_library( fbgemm SHARED IMPORTED GLOBAL )
41+
set_target_properties(fbgemm PROPERTIES IMPORTED_LOCATION ${FBGEMM_LIB})
8942

90-
add_library(pred_grpc_proto STATIC
91-
${pred_grpc_srcs}
92-
${pred_grpc_hdrs}
93-
${pred_proto_srcs}
94-
${pred_proto_hdrs})
9543

96-
target_link_libraries(pred_grpc_proto
44+
add_library(hw_grpc_proto STATIC
45+
${hw_grpc_srcs}
46+
${hw_grpc_hdrs}
47+
${hw_proto_srcs}
48+
${hw_proto_hdrs})
49+
target_link_libraries(hw_grpc_proto
9750
${_REFLECTION}
9851
${_GRPC_GRPCPP}
9952
${_PROTOBUF_LIBPROTOBUF})
10053

101-
# server
54+
# Targets greeter_[async_](client|server)
10255
add_executable(server server.cpp)
10356
target_link_libraries(server
104-
inference
105-
torch_deploy
106-
pred_grpc_proto
10757
"${TORCH_LIBRARIES}"
108-
${FOLLY_LIBRARIES}
109-
${PYTORCH_LIB_FMT}
110-
${FBGEMM_LIB}
58+
fbgemm
59+
hw_grpc_proto
11160
${_REFLECTION}
11261
${_GRPC_GRPCPP}
113-
${_PROTOBUF_LIBPROTOBUF})
62+
${_PROTOBUF_LIBPROTOBUF}
63+
)

0 commit comments

Comments
 (0)