Skip to content

Commit d22f614

Browse files
dreissfacebook-github-bot
authored andcommitted
Update fbjni and enable PyTorch JNI build
Summary: - Add a "BUILD_JNI" option that enables building PyTorch JNI bindings and fbjni. This is off by default because it adds a dependency on jni.h. - Update to the latest fbjni so we can inhibit building its tests, because they depend on gtest. - Set JAVA_HOME and BUILD_JNI in Linux binary build configurations if we can find jni.h in Docker. Test Plan: - Built on dev server. - Verified that libpytorch_jni links after libtorch when both are built in a parallel build. Differential Revision: D18536828 fbshipit-source-id: 19cb3be8298d3619352d02bb9446ab802c27ec66
1 parent 3f5dc95 commit d22f614

File tree

7 files changed

+57
-1
lines changed

7 files changed

+57
-1
lines changed

.circleci/scripts/binary_populate_env.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ else
6262
fi
6363
export PYTORCH_BUILD_NUMBER=1
6464

65+
66+
JAVA_HOME=
67+
BUILD_JNI=OFF
68+
if [[ "$PACKAGE_TYPE" == libtorch ]]; then
69+
POSSIBLE_JAVA_HOMES=()
70+
POSSIBLE_JAVA_HOMES+=(/usr/local)
71+
POSSIBLE_JAVA_HOMES+=(/usr/lib/jvm/java-8-openjdk-amd64)
72+
# TODO: Fix Mac Java build
73+
#POSSIBLE_JAVA_HOMES+=(/Library/Java/JavaVirtualMachines/*.jdk/Contents/Home)
74+
for JH in "${POSSIBLE_JAVA_HOMES[@]}" ; do
75+
if [[ -e "$JH/include/jni.h" ]] ; then
76+
echo "Found jni.h under $JH"
77+
JAVA_HOME="$JH"
78+
BUILD_JNI=ON
79+
break
80+
fi
81+
done
82+
if [ -z "$JAVA_HOME" ]; then
83+
echo "Did not find jni.h"
84+
fi
85+
fi
86+
6587
cat >>"$envfile" <<EOL
6688
# =================== The following code will be executed inside Docker container ===================
6789
export TZ=UTC
@@ -85,6 +107,8 @@ export TORCH_PACKAGE_NAME='torch'
85107
export TORCH_CONDA_BUILD_FOLDER='pytorch-nightly'
86108
87109
export USE_FBGEMM=1
110+
export JAVA_HOME=$JAVA_HOME
111+
export BUILD_JNI=$BUILD_JNI
88112
export PIP_UPLOAD_FOLDER="$PIP_UPLOAD_FOLDER"
89113
export DOCKER_IMAGE="$DOCKER_IMAGE"
90114

.jenkins/pytorch/build.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ if ! which conda; then
6161
fi
6262
fi
6363

64+
if [[ "$BUILD_ENVIRONMENT" == *libtorch* ]]; then
65+
POSSIBLE_JAVA_HOMES=()
66+
POSSIBLE_JAVA_HOMES+=(/usr/local)
67+
POSSIBLE_JAVA_HOMES+=(/usr/lib/jvm/java-8-openjdk-amd64)
68+
# TODO: Fix Mac Java build
69+
#POSSIBLE_JAVA_HOMES+=(/Library/Java/JavaVirtualMachines/*.jdk/Contents/Home)
70+
for JH in "${POSSIBLE_JAVA_HOMES[@]}" ; do
71+
if [[ -e "$JH/include/jni.h" ]] ; then
72+
echo "Found jni.h under $JH"
73+
export JAVA_HOME="$JH"
74+
export BUILD_JNI=ON
75+
break
76+
fi
77+
done
78+
if [ -z "$JAVA_HOME" ]; then
79+
echo "Did not find jni.h"
80+
fi
81+
fi
82+
6483
# Use special scripts for Android builds
6584
if [[ "${BUILD_ENVIRONMENT}" == *-android* ]]; then
6685
export ANDROID_NDK=/opt/ndk

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ cmake_dependent_option(
120120
CAFFE2_USE_MSVC_STATIC_RUNTIME "Using MSVC static runtime libraries" ON
121121
"NOT BUILD_SHARED_LIBS" OFF)
122122
option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" OFF)
123+
option(BUILD_JNI "Build JNI bindings" OFF)
123124
cmake_dependent_option(
124125
INSTALL_TEST "Install test binaries if BUILD_TEST is on" ON
125126
"BUILD_TEST" OFF)
@@ -646,5 +647,12 @@ if (BUILD_BINARY)
646647
add_subdirectory(binaries)
647648
endif()
648649

650+
# ---[ JNI
651+
if (BUILD_JNI)
652+
set(BUILD_LIBTORCH_WITH_JNI 1)
653+
set(FBJNI_SKIP_TESTS 1)
654+
add_subdirectory(android/pytorch_android)
655+
endif()
656+
649657
include(cmake/Summary.cmake)
650658
caffe2_print_configuration_summary()

android/pytorch_android/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ set(pytorch_android_DIR ${CMAKE_CURRENT_LIST_DIR}/src/main/cpp)
1919
if (ANDROID_ABI)
2020
set(libtorch_include_DIR ${pytorch_android_DIR}/libtorch_include/${ANDROID_ABI})
2121
set(BUILD_SUBDIR ${ANDROID_ABI})
22+
elseif(BUILD_LIBTORCH_WITH_JNI)
23+
# Don't need LIBTORCH_HOME if we're building from within PyTorch.
2224
else()
25+
# Building against a pre-built libtorch.
2326
if (NOT LIBTORCH_HOME)
2427
message(FATAL_ERROR
2528
"pytorch_android requires LIBTORCH_HOME to be defined for non-Android builds.")

cmake/Summary.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function (caffe2_print_configuration_summary)
4444
message(STATUS " BUILD_CAFFE2_OPS : ${BUILD_CAFFE2_OPS}")
4545
message(STATUS " BUILD_SHARED_LIBS : ${BUILD_SHARED_LIBS}")
4646
message(STATUS " BUILD_TEST : ${BUILD_TEST}")
47+
message(STATUS " BUILD_JNI : ${BUILD_JNI}")
4748

4849
message(STATUS " INTERN_BUILD_MOBILE : ${INTERN_BUILD_MOBILE}")
4950

tools/setup_helpers/cmake.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def generate(self, version, cmake_python_library, build_python, build_test, my_e
230230
'CUDNN_INCLUDE_DIR',
231231
'EXPERIMENTAL_SINGLE_THREAD_POOL',
232232
'INSTALL_TEST',
233+
'JAVA_HOME',
233234
'MKL_THREADING',
234235
'MKLDNN_THREADING',
235236
'MSVC_Z7_OVERRIDE',

0 commit comments

Comments
 (0)