Skip to content

Commit 6377138

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 886e06f commit 6377138

Some content is hidden

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

56 files changed

+2619
-553
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

torchrec/inference/CMakeLists.txt

Lines changed: 54 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,76 @@
1-
# Copyright (c) Meta Platforms, Inc. and affiliates.
2-
# All rights reserved.
1+
# Copyright 2018 gRPC authors.
32
#
4-
# This source code is licensed under the BSD-style license found in the
5-
# LICENSE file in the root directory of this source tree.
6-
7-
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
8-
project(inference)
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# cmake build file for C++ helloworld example.
16+
# Assumes protobuf and gRPC have been installed using cmake.
17+
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
18+
# that automatically builds all the dependencies before building helloworld.
919

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)
20+
cmake_minimum_required(VERSION 3.8)
1421

22+
project(inference C CXX)
1523

16-
# abi and other flags
24+
include(/home/paulzhan/grpc/examples/cpp/cmake/common.cmake)
1725

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()
2426

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")
27+
# Proto file
28+
get_filename_component(hw_proto "/home/paulzhan/torchrec/torchrec/inference/protos/predictor.proto" ABSOLUTE)
29+
get_filename_component(hw_proto_path "${hw_proto}" PATH)
2830

29-
# dependencies
30-
find_package(Boost REQUIRED)
31-
find_package(Torch REQUIRED)
32-
find_package(folly REQUIRED)
33-
find_package(gflags REQUIRED)
31+
# Generated sources
32+
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/predictor.pb.cc")
33+
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/predictor.pb.h")
34+
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/predictor.grpc.pb.cc")
35+
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/predictor.grpc.pb.h")
36+
add_custom_command(
37+
OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
38+
COMMAND ${_PROTOBUF_PROTOC}
39+
ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
40+
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
41+
-I "${hw_proto_path}"
42+
--plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
43+
"${hw_proto}"
44+
DEPENDS "${hw_proto}")
3445

35-
include_directories(${Torch_INCLUDE_DIRS})
36-
include_directories(${folly_INCLUDE_DIRS})
37-
include_directories(${PYTORCH_FMT_INCLUDE_PATH})
46+
# Include generated *.pb.h files
47+
include_directories("${CMAKE_CURRENT_BINARY_DIR}")
3848

3949
set(CMAKE_CXX_STANDARD 17)
4050

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")
51+
# Torch + FBGEMM
52+
find_package(Torch REQUIRED)
53+
add_library( fbgemm SHARED IMPORTED GLOBAL )
54+
set_target_properties(fbgemm PROPERTIES IMPORTED_LOCATION ${FBGEMM_LIB})
8955

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

96-
target_link_libraries(pred_grpc_proto
57+
add_library(hw_grpc_proto STATIC
58+
${hw_grpc_srcs}
59+
${hw_grpc_hdrs}
60+
${hw_proto_srcs}
61+
${hw_proto_hdrs})
62+
target_link_libraries(hw_grpc_proto
9763
${_REFLECTION}
9864
${_GRPC_GRPCPP}
9965
${_PROTOBUF_LIBPROTOBUF})
10066

101-
# server
67+
# Targets greeter_[async_](client|server)
10268
add_executable(server server.cpp)
10369
target_link_libraries(server
104-
inference
105-
torch_deploy
106-
pred_grpc_proto
10770
"${TORCH_LIBRARIES}"
108-
${FOLLY_LIBRARIES}
109-
${PYTORCH_LIB_FMT}
110-
${FBGEMM_LIB}
71+
fbgemm
72+
hw_grpc_proto
11173
${_REFLECTION}
11274
${_GRPC_GRPCPP}
113-
${_PROTOBUF_LIBPROTOBUF})
75+
${_PROTOBUF_LIBPROTOBUF}
76+
)

0 commit comments

Comments
 (0)