Skip to content
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

[pytorch] Upgrade to pytorch 1.11.0 #1583

Merged
merged 6 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions .github/workflows/native_jni_s3_pytorch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ jobs:
- name: Install Environment
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y locales cmake curl unzip software-properties-common gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
DEBIAN_FRONTEND=noninteractive apt-get install -y locales curl unzip software-properties-common
add-apt-repository -y ppa:deadsnakes/ppa
apt-get update
apt-get install -y python3 python3-distutils
curl -O https://bootstrap.pypa.io/pip/3.6/get-pip.py
python3 get-pip.py
pip3 install awscli --upgrade
pip3 install awscli cmake
ln -s /usr/local/bin/cmake /usr/bin/cmake
- name: Release JNI prep
run: |
PYTORCH_VERSION=${{ github.event.inputs.pt_version }}
Expand All @@ -84,8 +85,6 @@ jobs:
./gradlew :engines:pytorch:pytorch-native:compileJNI -Pcu10 -Ppt_version=$PYTORCH_VERSION
./gradlew :engines:pytorch:pytorch-native:cleanJNI
./gradlew :engines:pytorch:pytorch-native:compileJNI -Pcu11 -Ppt_version=$PYTORCH_VERSION
./gradlew :engines:pytorch:pytorch-native:cleanJNI
CXX=aarch64-linux-gnu-gcc ./gradlew :engines:pytorch:pytorch-native:compileJNI -Paarch64 -Ppt_version=$PYTORCH_VERSION
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand Down Expand Up @@ -161,33 +160,27 @@ jobs:
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Install CUDA 10.2
- name: Install CUDA 11.3
shell: cmd
run: |
curl.exe -L http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_441.22_windows.exe -o cuda102.exe
curl.exe -L https://developer.download.nvidia.com/compute/redist/cudnn/v7.6.4/cudnn-10.1-windows7-x64-v7.6.4.38.zip -o cudnn.zip
cuda102.exe -s
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
curl.exe -L https://developer.download.nvidia.com/compute/cuda/11.3.1/local_installers/cuda_11.3.1_465.89_win10.exe -o cuda.exe
curl.exe -L https://developer.download.nvidia.com/compute/redist/cudnn/v8.2.1/cudnn-11.3-windows-x64-v8.2.1.32.zip -o cudnn.zip
cuda.exe -s
mkdir cuda
unzip.exe cudnn.zip
cp.exe -a cuda/include cuda/lib cuda/bin "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/"
rm.exe -Rf cuda102.exe cuda.exe cudnn.zip cuda
cp.exe -a cuda/include cuda/lib cuda/bin "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.3/"
rm.exe -Rf cuda.exe cuda.exe cudnn.zip cuda
- name: Release CPU JNI
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
gradlew :engines:pytorch:pytorch-native:compileJNI -Ppt_version=${{ github.event.inputs.pt_version }}
- name: Release cuda10 JNI
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
set "CUDA_PATH=%ProgramFiles%\NVIDIA GPU Computing Toolkit\CUDA\v10.2"
set "PATH=%CUDA_PATH%\bin;%CUDA_PATH%\libnvvp;%PATH%"
gradlew :engines:pytorch:pytorch-native:cleanJNI :engines:pytorch:pytorch-native:compileJNI -Pcu10 -Ppt_version=${{ github.event.inputs.pt_version }}
- name: Release cuda11 JNI
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
set "CUDA_PATH=%ProgramFiles%\NVIDIA GPU Computing Toolkit\CUDA\v10.2"
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
set "CUDA_PATH=%ProgramFiles%\NVIDIA GPU Computing Toolkit\CUDA\v11.3"
set "PATH=%CUDA_PATH%\bin;%CUDA_PATH%\libnvvp;%PATH%"
gradlew :engines:pytorch:pytorch-native:cleanJNI :engines:pytorch:pytorch-native:compileJNI -Pcu11 -Ppt_version=${{ github.event.inputs.pt_version }}
- name: Configure AWS Credentials
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/nightly_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ jobs:
- name: Publish to snapshot repository
if: ${{ github.event.inputs.mode == '' || github.event.inputs.mode == 'snapshot' }}
run: |
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.8.1 -Psnapshot
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.9.1 -Psnapshot
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.10.0 -Psnapshot
./gradlew clean publish -Psnapshot
cd bom
./gradlew publish -Psnapshot
Expand All @@ -121,8 +121,8 @@ jobs:
- name: Publish to staging repository
if: ${{ github.event.inputs.mode == 'staging' }}
run: |
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.8.1 -P${{ github.event.inputs.mode }}
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.9.1 -P${{ github.event.inputs.mode }}
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.10.0 -P${{ github.event.inputs.mode }}
./gradlew clean publish -P${{ github.event.inputs.mode }}
cd bom
./gradlew publish -P${{ github.event.inputs.mode }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.Shape;
import java.util.Arrays;
import org.testng.SkipException;
import org.testng.annotations.Test;

/** The file is for testing PyTorch MKLDNN functionalities. */
public class MkldnnTest {

@Test
public void testMkldnn() {
if (!"amd64".equals(System.getProperty("os.arch"))) {
throw new SkipException("MKLDNN Test requires x86_64 arch.");
}

System.setProperty("ai.djl.pytorch.use_mkldnn", "true");
try (NDManager manager = NDManager.newBaseManager()) {
NDArray[] arrays = {
Expand Down
12 changes: 9 additions & 3 deletions engines/pytorch/pytorch-jni/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@ processResources {
"linux-x86_64/cu102/libdjl_torch.so",
"linux-x86_64/cpu-precxx11/libdjl_torch.so",
"osx-x86_64/cpu/libdjl_torch.dylib",
"win-x86_64/cpu/djl_torch.dll",
"win-x86_64/cu102/djl_torch.dll"
"win-x86_64/cpu/djl_torch.dll"
]
if (ptVersion.startsWith("1.10.")) {
if (ptVersion.startsWith("1.11.")) {
files.add("linux-aarch64/cpu/libdjl_torch.so")
files.add("linux-x86_64/cu113/libdjl_torch.so")
files.add("linux-x86_64/cu113-precxx11/libdjl_torch.so")
files.add("win-x86_64/cu113/djl_torch.dll")
} else if (ptVersion.startsWith("1.10.")) {
files.add("linux-x86_64/cu113/libdjl_torch.so")
files.add("linux-x86_64/cu113-precxx11/libdjl_torch.so")
files.add("win-x86_64/cu113/djl_torch.dll")
files.add("win-x86_64/cu102/djl_torch.dll")
} else {
files.add("linux-x86_64/cu111/libdjl_torch.so")
files.add("linux-x86_64/cu111-precxx11/libdjl_torch.so")
files.add("win-x86_64/cu111/djl_torch.dll")
files.add("win-x86_64/cu102/djl_torch.dll")
}
String jnilibDir = "${project.projectDir}/jnilib/${djl_version}"
files.each { entry ->
Expand Down
4 changes: 4 additions & 0 deletions engines/pytorch/pytorch-native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ set(SOURCE_FILES
"src/main/native/ai_djl_pytorch_jni_cache.h"
"src/main/native/ai_djl_pytorch_jni_cache.cc")

if(PT_OLD_VERSION)
add_compile_definitions(V1_10_X)
endif()

add_library(djl_torch SHARED ${SOURCE_FILES})
# build host
if(NOT BUILD_ANDROID)
Expand Down
14 changes: 12 additions & 2 deletions engines/pytorch/pytorch-native/build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@rem choco install jdk8 -y

set FILEPATH="libtorch"
set VERSION="%1"
set VERSION=%1
if "%2" == "cpu" (
set DOWNLOAD_URL="https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-%VERSION%%%2Bcpu.zip"
) else if "%2" == "cu102" (
Expand All @@ -28,9 +28,19 @@ if exist %FILEPATH% (
echo Finished downloading libtorch
)

if "%VERSION%" == "1.11.0" (
copy /y src\main\patch\cuda.cmake libtorch\share\cmake\Caffe2\public\
)
if "%VERSION%" == "1.10.0" (
set PT_OLD_VERSION=1
)
if "%VERSION%" == "1.9.1" (
set PT_OLD_VERSION=1
)

if exist build rd /q /s build
md build\classes
cd build
javac -sourcepath ..\..\pytorch-engine\src\main\java\ ..\..\pytorch-engine\src\main\java\ai\djl\pytorch\jni\PyTorchLibrary.java -h include -d classes
cmake -DCMAKE_PREFIX_PATH=libtorch ..
cmake -DCMAKE_PREFIX_PATH=libtorch -DPT_OLD_VERSION=%PT_OLD_VERSION% ..
cmake --build . --config Release
18 changes: 8 additions & 10 deletions engines/pytorch/pytorch-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (project.hasProperty("pt_version") && project.property("pt_version") != "") {
}
boolean isRelease = project.hasProperty("release") || project.hasProperty("staging")
boolean isPrecxx11 = project.hasProperty("precxx11")
boolean isAarch64 = project.hasProperty("aarch64")
boolean isAarch64 = project.hasProperty("aarch64") || System.properties["os.arch"] == "aarch64"

String FLAVOR = "cpu"
if (project.hasProperty("cu10")) {
Expand Down Expand Up @@ -91,14 +91,13 @@ def prepareNativeLib(String binaryRoot, String ver) {
"cpu/libtorch-win-shared-with-deps-${ver}%2Bcpu.zip" : "cpu/win-x86_64",
"cu102/libtorch-cxx11-abi-shared-with-deps-${ver}%2Bcu102.zip" : "cu102/linux-x86_64",
"${cu11}/libtorch-cxx11-abi-shared-with-deps-${ver}%2B${cu11}.zip": "${cu11}/linux-x86_64",
"cu102/libtorch-win-shared-with-deps-${ver}%2Bcu102.zip" : "cu102/win-x86_64",
"${cu11}/libtorch-win-shared-with-deps-${ver}%2B${cu11}.zip" : "${cu11}/win-x86_64",
"cpu/libtorch-shared-with-deps-${ver}%2Bcpu.zip" : "cpu-precxx11/linux-x86_64",
"${cu11}/libtorch-shared-with-deps-${ver}%2B${cu11}.zip" : "${cu11}-precxx11/linux-x86_64"
]

def aarch64Files = [
"${ver}/libtorch-cxx11-shared-with-deps-${ver}-aarch64.zip" : "cpu/linux-aarch64"
"${ver}/libtorch-cxx11-shared-with-deps-${ver}-aarch64.zip": "cpu/linux-aarch64"
]

copyNativeLibToOutputDir(files, binaryRoot, officialPytorchUrl)
Expand Down Expand Up @@ -130,7 +129,7 @@ def copyNativeLibToOutputDir(Map<String, String> fileStoreMap, String binaryRoot
}
into("${outputDir}/native/lib")
}
if (entry.value.contains("-precxx11")) {
if (entry.value.contains("-precxx11") && !entry.value.contains("aarch64")) {
def libstd = new File("${outputDir}/native/lib/libstdc++.so.6")
new URL("https://publish.djl.ai/extra/libstdc%2B%2B.so.6").withInputStream {
i -> libstd.withOutputStream { it << i }
Expand Down Expand Up @@ -253,11 +252,10 @@ task uploadS3 {
"${BINARY_ROOT}/cpu/win-x86_64/native/lib/",
"${BINARY_ROOT}/cpu-precxx11/linux-x86_64/native/lib/",
"${BINARY_ROOT}/cu102/linux/native/lib/",
"${BINARY_ROOT}/cu102/win-x86_64/native/lib/",
"${BINARY_ROOT}/cu113/linux-x86_64/native/lib/",
"${BINARY_ROOT}/cu113/win-x86_64/native/lib/",
"${BINARY_ROOT}/cu113-precxx11/linux-x86_64/native/lib/",
"${BINARY_ROOT}/cpu/linux-aarch64/native/lib"
"${BINARY_ROOT}/cpu/linux-aarch64/native/lib/"
]
uploadDirs.each { item ->
fileTree(item).files.name.each {
Expand Down Expand Up @@ -315,11 +313,11 @@ flavorNames.each { flavor ->
libstd.text = new URL("https://publish.djl.ai/extra/THIRD-PARTY-LICENSES_qHnMKgbdWa.txt").text
}
}
from ("${BINARY_ROOT}/${flavor}/${osName}/native/lib") {
into ("pytorch/${flavor}/${osName}")
from("${BINARY_ROOT}/${flavor}/${osName}/native/lib") {
into("pytorch/${flavor}/${osName}")
}
from ("${BINARY_ROOT}/pytorch.properties") {
into ("native/lib")
from("${BINARY_ROOT}/pytorch.properties") {
into("native/lib")
}
from "src/main/resources"
archiveClassifier = "${osName}"
Expand Down
9 changes: 7 additions & 2 deletions engines/pytorch/pytorch-native/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ fi
PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
VERSION=$1
FLAVOR=$2
AARCH64_CXX11ABI="-cxx11"
CXX11ABI="-cxx11-abi"
if [[ $3 == "precxx11" ]]; then
CXX11ABI=""
AARCH64_CXX11ABI=""
fi
ARCH=$4

Expand All @@ -27,7 +29,7 @@ if [[ ! -d "libtorch" ]]; then
fi

if [[ $ARCH == 'aarch64' ]]; then
curl -s https://djl-ai.s3.amazonaws.com/publish/pytorch/${VERSION}/libtorch-cxx11-shared-with-deps-${VERSION}-aarch64.zip | jar xv
curl -s https://djl-ai.s3.amazonaws.com/publish/pytorch/${VERSION}/libtorch${AARCH64_CXX11ABI}-shared-with-deps-${VERSION}-aarch64.zip | jar xv
else
curl -s https://download.pytorch.org/libtorch/${FLAVOR}/libtorch${CXX11ABI}-shared-with-deps-${VERSION}%2B${FLAVOR}.zip | jar xv
fi
Expand All @@ -40,13 +42,16 @@ if [[ ! -d "libtorch" ]]; then
fi
fi

if [[ "$VERSION" =~ ^1\.10\..*|^1\.9\..* ]]; then
PT_OLD_VERSION=1
fi
pushd .

rm -rf build
mkdir build && cd build
mkdir classes
javac -sourcepath ../../pytorch-engine/src/main/java/ ../../pytorch-engine/src/main/java/ai/djl/pytorch/jni/PyTorchLibrary.java -h include -d classes
cmake -DCMAKE_PREFIX_PATH=libtorch ..
cmake -DCMAKE_PREFIX_PATH=libtorch -DPT_OLD_VERSION=${PT_OLD_VERSION} ..
cmake --build . --config Release -- -j "${NUM_PROC}"

if [[ $PLATFORM == 'darwin' ]]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
*/
#include <torch/torch.h>
// clang-format off
#include <torch/csrc/jit/frontend/code_template.h>
#ifdef V1_10_X
#include <torch/csrc/jit/frontend/code_template.h>
#else
#include <ATen/code_template.h>
#endif
#include <ATen/core/jit_type.h>
// clang-format on

#include <sstream>
Expand Down Expand Up @@ -162,7 +167,8 @@ inline std::string FormatMemory(int64_t bytes) {
return oss.str();
}

// the code snippet is copied from torch/csrc/autograd/profiler.cpp
// the code snippet is copied from torch/csrc/autograd/profiler_legacy.cpp
#ifdef V1_10_X
static torch::jit::CodeTemplate event_template(R"(
{
"name": "${name}",
Expand All @@ -175,6 +181,20 @@ static torch::jit::CodeTemplate event_template(R"(
"cpu mem": "${cpu_mem}",
"args": {}
})");
#else
static const at::jit::CodeTemplate event_template(R"(
{
"name": "${name}",
"ph": "X",
"ts": ${ts},
"dur": ${dur},
"tid": ${tid},
"pid": "CPU Functions",
"shape": ${shape},
"cpu mem": "${cpu_mem}",
"args": {}
})");
#endif

// The function doesn't support GPU yet
// You can refer to
Expand Down Expand Up @@ -227,7 +247,11 @@ void WriteProfilerEventsToStream(std::ostream& out, const std::vector<std::vecto
LegacyEvent* start = it->second;
int64_t memory_usage = mem_it->second;

#ifdef V1_10_X
torch::jit::TemplateEnv env;
#else
at::jit::TemplateEnv env;
#endif
env.s("name", start->name());
env.d("ts", profiler_start->cpuElapsedUs(*start));
env.d("dur", start->cpuElapsedUs(*evt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ namespace utils {

#if !defined(__ANDROID__)
// for image interpolation
#ifdef V1_10_X
typedef torch::variant<torch::enumtype::kNearest, torch::enumtype::kLinear, torch::enumtype::kBilinear,
torch::enumtype::kBicubic, torch::enumtype::kTrilinear, torch::enumtype::kArea>
mode_t;
#else
typedef torch::variant<torch::enumtype::kNearest, torch::enumtype::kLinear, torch::enumtype::kBilinear,
torch::enumtype::kBicubic, torch::enumtype::kTrilinear, torch::enumtype::kArea, torch::enumtype::kNearestExact>
mode_t;
#endif
#endif

inline jint GetDTypeFromScalarType(const torch::ScalarType& type) {
Expand Down Expand Up @@ -108,6 +114,10 @@ inline mode_t GetInterpolationMode(jint jmode) {
return torch::kTrilinear;
case 5:
return torch::kArea;
case 6:
#ifndef V1_10_X
return torch::kNearestExact;
#endif
default:
throw;
}
Expand Down
Loading