Skip to content

Commit

Permalink
[pytorch] Updates PyTorch to 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu committed May 14, 2024
1 parent 30f7b4d commit 19c3702
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/nightly_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ jobs:
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.13.1 -Psnapshot
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=2.1.2 -Psnapshot
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=2.2.2 -Psnapshot
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=2.3.0 -Psnapshot
./gradlew clean engines:ml:xgboost:publish -Pgpu -Psnapshot
./gradlew clean publish -Psnapshot
cd bom
Expand All @@ -206,6 +207,7 @@ jobs:
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=1.13.1 -P${{ github.event.inputs.mode }}
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=2.1.2 -P${{ github.event.inputs.mode }}
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=2.2.2 -P${{ github.event.inputs.mode }}
./gradlew clean engines:pytorch:pytorch-jni:publish -Ppt_version=2.3.0 -P${{ github.event.inputs.mode }}
./gradlew clean engines:ml:xgboost:publish -Pgpu -P${{ github.event.inputs.mode }}
./gradlew clean publish -P${{ github.event.inputs.mode }}
cd bom
Expand Down
1 change: 1 addition & 0 deletions engines/pytorch/pytorch-engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
exclude group: "junit", module: "junit"
}
testImplementation "org.slf4j:slf4j-simple:${slf4j_version}"
testImplementation(project(":testing"))
testRuntimeOnly project(":engines:pytorch:pytorch-model-zoo")
testRuntimeOnly project(":engines:pytorch:pytorch-jni")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package ai.djl.pytorch.integration;

import ai.djl.engine.Engine;
import ai.djl.testing.TestRequirements;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -23,6 +24,8 @@ public class ALibUtilsTest {

@BeforeClass
public void setup() {
TestRequirements.notMacX86();

System.setProperty("ai.djl.pytorch.native_helper", ALibUtilsTest.class.getName());
System.setProperty("STDCXX_LIBRARY_PATH", "/usr/lib/non-exists");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ai.djl.pytorch.jni.IValue;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.testing.TestRequirements;
import ai.djl.training.util.ProgressBar;

import org.testng.Assert;
Expand All @@ -37,6 +38,8 @@ public class IValueTest {

@Test
public void testIValue() {
TestRequirements.notMacX86();

try (PtNDManager manager = (PtNDManager) NDManager.newBaseManager()) {
PtNDArray array1 = (PtNDArray) manager.zeros(new Shape(1));
PtNDArray array2 = (PtNDArray) manager.ones(new Shape(1));
Expand Down Expand Up @@ -199,6 +202,8 @@ public void testIValue() {

@Test
public void testIValueModel() throws IOException, ModelException {
TestRequirements.notMacX86();

Criteria<NDList, NDList> criteria =
Criteria.builder()
.setTypes(NDList.class, NDList.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/** The file is for testing PyTorch MKLDNN functionalities. */
public class MkldnnTest {

@Test
@Test(enabled = false)
public void testMkldnn() {
if (!"amd64".equals(System.getProperty("os.arch"))) {
throw new SkipException("MKLDNN Test requires x86_64 arch.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.Shape;
import ai.djl.testing.TestRequirements;

import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;

public class MpsTest {

@Test
@Test(enabled = false)
public void testMps() {
if (!"aarch64".equals(System.getProperty("os.arch"))
|| !System.getProperty("os.name").startsWith("Mac")) {
throw new SkipException("MPS test requires M1 macOS.");
}
TestRequirements.macosM1();

Device device = Device.of("mps", -1);
try (NDManager manager = NDManager.newBaseManager(device)) {
Expand All @@ -41,16 +38,9 @@ public void testMps() {
}
}

private static boolean checkMpsCompatible() {
return "aarch64".equals(System.getProperty("os.arch"))
&& System.getProperty("os.name").startsWith("Mac");
}

@Test
@Test(enabled = false)
public void testToTensorMPS() {
if (!checkMpsCompatible()) {
throw new SkipException("MPS toTensor test requires Apple Silicon macOS.");
}
TestRequirements.macosM1();

// Test that toTensor does not fail on MPS (e.g. due to use of float64 for division)
try (NDManager manager = NDManager.newBaseManager(Device.fromName("mps"))) {
Expand All @@ -60,11 +50,9 @@ public void testToTensorMPS() {
}
}

@Test
@Test(enabled = false)
public void testClassificationsMPS() {
if (!checkMpsCompatible()) {
throw new SkipException("MPS classification test requires Apple Silicon macOS.");
}
TestRequirements.macosM1();

// Test that classifications do not fail on MPS (e.g. due to conversion of probabilities to
// float64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.testing.TestRequirements;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;

Expand All @@ -47,6 +48,8 @@ public void testProfiler()
ModelNotFoundException,
IOException,
TranslateException {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
ImageClassificationTranslator translator =
ImageClassificationTranslator.builder().addTransform(new ToTensor()).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.testing.TestRequirements;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.NoopTranslator;
import ai.djl.translate.TranslateException;
Expand All @@ -36,6 +37,8 @@ public class PtModelTest {

@Test
public void testLoadFromStream() throws IOException, TranslateException, ModelException {
TestRequirements.notMacX86();

Criteria<NDList, NDList> criteria =
Criteria.builder()
.setTypes(NDList.class, NDList.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.testing.TestRequirements;

import org.testng.Assert;
import org.testng.annotations.Test;
Expand All @@ -27,6 +28,8 @@ public class PtNDArrayTest {

@Test
public void testStringTensor() {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
String[] str = {"a", "b", "c"};
NDArray arr = manager.create(str);
Expand All @@ -40,6 +43,8 @@ public void testStringTensor() {

@Test
public void testLargeTensor() {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
NDArray array = manager.zeros(new Shape(10 * 2850, 18944), DataType.FLOAT32);
Assert.assertThrows(EngineException.class, array::toByteArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ai.djl.pytorch.jni.JniUtils;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.testing.TestRequirements;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;

Expand All @@ -42,6 +43,8 @@ public class TorchScriptTest {

@Test
public void testDictInput() throws ModelException, IOException, TranslateException {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
Criteria<NDList, NDList> criteria =
Criteria.builder()
Expand Down Expand Up @@ -76,6 +79,8 @@ public void testDictInput() throws ModelException, IOException, TranslateExcepti

@Test
public void testInputOutput() throws IOException, ModelException {
TestRequirements.notMacX86();

Criteria<NDList, NDList> criteria =
Criteria.builder()
.setTypes(NDList.class, NDList.class)
Expand All @@ -99,6 +104,8 @@ public void testInputOutput() throws IOException, ModelException {

@Test
public void testGetMethodNames() throws ModelException, IOException {
TestRequirements.notMacX86();

Criteria<NDList, NDList> criteria =
Criteria.builder()
.setTypes(NDList.class, NDList.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.Shape;
import ai.djl.pytorch.engine.PtNDManager;
import ai.djl.testing.TestRequirements;
import ai.djl.util.Pair;

import org.testng.Assert;
Expand All @@ -28,6 +29,8 @@ public class IValueUtilsTest {

@Test
public void testTuple() {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
NDArray array1 = manager.zeros(new Shape(1));
array1.setName("input1()");
Expand All @@ -50,6 +53,8 @@ public void testTuple() {

@Test
public void testTupleOfTuple() {
TestRequirements.notMacX86();

try (PtNDManager manager = (PtNDManager) NDManager.newBaseManager()) {
NDArray array1 = manager.zeros(new Shape(1));
array1.setName("input1(2,3)");
Expand Down Expand Up @@ -91,6 +96,8 @@ public void testTupleOfTuple() {

@Test
public void testMapOfTensor() {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
NDArray array1 = manager.zeros(new Shape(1));
array1.setName("input1.key1");
Expand Down Expand Up @@ -119,6 +126,8 @@ public void testMapOfTensor() {

@Test
public void testListOfTensor() {
TestRequirements.notMacX86();

try (NDManager manager = NDManager.newBaseManager()) {
NDArray array1 = manager.zeros(new Shape(1));
array1.setName("input1[]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
*/
package ai.djl.pytorch.jni;

import ai.djl.testing.TestRequirements;

import org.testng.annotations.Test;

public class JniUtilsTest {

@Test
public void testClearGpuCache() {
TestRequirements.notMacX86();

JniUtils.emptyCudaCache();
}
}
6 changes: 5 additions & 1 deletion engines/pytorch/pytorch-jni/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ processResources {
"osx-aarch64/cpu/libdjl_torch.dylib",
"win-x86_64/cpu/djl_torch.dll"
]
if (ptVersion.startsWith("2.1.") || ptVersion.startsWith("2.2.")) {
if (ptVersion.startsWith("2.3.")) {
files.add("linux-x86_64/cu121/libdjl_torch.so")
files.add("linux-x86_64/cu121-precxx11/libdjl_torch.so")
files.add("win-x86_64/cu121/djl_torch.dll")
} else if (ptVersion.startsWith("2.1.") || ptVersion.startsWith("2.2.")) {
files.add("linux-x86_64/cu121/libdjl_torch.so")
files.add("linux-x86_64/cu121-precxx11/libdjl_torch.so")
files.add("win-x86_64/cu121/djl_torch.dll")
Expand Down
1 change: 1 addition & 0 deletions engines/pytorch/pytorch-model-zoo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
exclude group: "junit", module: "junit"
}
testImplementation "org.slf4j:slf4j-simple:${slf4j_version}"
testImplementation(project(":testing"))
}

tasks.register('syncS3', Exec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ai.djl.nn.LambdaBlock;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.testing.TestRequirements;
import ai.djl.translate.TranslateException;

import org.testng.Assert;
Expand All @@ -38,6 +39,8 @@ public class GptTranslatorTest {

@Test
public void testGpt2() throws TranslateException, ModelException, IOException {
TestRequirements.notMacX86();

// This is a fake model that simulates language models like GPT2: NDList(inputIds, posIds,
// attnMask) -> NDList(logits(1), pastKv(12*2)[, hiddenStates(13)])
Block block =
Expand Down
8 changes: 0 additions & 8 deletions engines/pytorch/pytorch-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def prepareNativeLib(String binaryRoot, String ver) {

def files = [
"cpu/libtorch-cxx11-abi-shared-with-deps-${ver}%2Bcpu.zip" : "cpu/linux-x86_64",
"cpu/libtorch-macos-x86_64-${ver}.zip" : "cpu/osx-x86_64",
"cpu/libtorch-macos-arm64-${ver}.zip" : "cpu/osx-aarch64",
"cpu/libtorch-win-shared-with-deps-${ver}%2Bcpu.zip" : "cpu/win-x86_64",
"${cuda}/libtorch-cxx11-abi-shared-with-deps-${ver}%2B${cuda}.zip": "${cuda}/linux-x86_64",
Expand All @@ -96,12 +95,6 @@ def prepareNativeLib(String binaryRoot, String ver) {
copyNativeLibToOutputDir(files, binaryRoot, officialPytorchUrl)
copyNativeLibToOutputDir(aarch64Files, binaryRoot, aarch64PytorchUrl)

exec {
commandLine 'install_name_tool', '-add_rpath', '@loader_path', "${binaryRoot}/cpu/osx-x86_64/native/lib/libtorch_cpu.dylib"
}
exec {
commandLine 'install_name_tool', '-add_rpath', '@loader_path', "${binaryRoot}/cpu/osx-x86_64/native/lib/libtorch.dylib"
}
exec {
commandLine 'install_name_tool', '-add_rpath', '@loader_path', "${binaryRoot}/cpu/osx-aarch64/native/lib/libtorch_cpu.dylib"
}
Expand Down Expand Up @@ -269,7 +262,6 @@ tasks.register('uploadS3') {
def uploadDirs = [
"${BINARY_ROOT}/cpu/linux-x86_64/native/lib/",
"${BINARY_ROOT}/cpu/osx-aarch64/native/lib/",
"${BINARY_ROOT}/cpu/osx-x86_64/native/lib/",
"${BINARY_ROOT}/cpu/win-x86_64/native/lib/",
"${BINARY_ROOT}/cpu-precxx11/linux-aarch64/native/lib/",
"${BINARY_ROOT}/cpu-precxx11/linux-x86_64/native/lib/",
Expand Down
Loading

0 comments on commit 19c3702

Please sign in to comment.