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

enable paddle test and fix buffer rewind issue #490

Merged
merged 3 commits into from
Jan 14, 2021
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
5 changes: 4 additions & 1 deletion .github/workflows/continuous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,17 @@ jobs:
- name: check disk space
run: df -h
- name: Workaround for startup run
run: ./gradlew :pytorch:pytorch-engine:jar clean
run: |
./gradlew :pytorch:pytorch-engine:jar :paddlepaddle:paddlepaddle-engine:jar clean
# If there are code change on JNI, we need to rebuild to pass tests under pytorch folder
- name: Compile PyTorch JNI
run: ./gradlew :pytorch:pytorch-native:compileJNI
- name: Compile Sentencepiece JNI
run: ./gradlew :extension:sentencepiece:compileJNI
- name: Compile fastText JNI
run: ./gradlew :extension:fasttext:compileJNI
- name: Compile PaddlePaddle JNI
run: ./gradlew :paddlepaddle:paddlepaddle-native:compileJNI
- name: Build with Gradle
run: ./gradlew build jRR
- name: API test results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public DlrNDArray create(Buffer data, Shape shape, DataType dataType) {
int numOfBytes = dataType.getNumOfBytes();
ByteBuffer bb = ByteBuffer.allocate(size * numOfBytes);
bb.asFloatBuffer().put((FloatBuffer) data);
bb.rewind();
return new DlrNDArray(this, bb, shape);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ public void set(Buffer data) {
default:
throw new UnsupportedOperationException("data type is not supported!");
}
buf.rewind();
JnaUtils.syncCopyFromCPU(getHandle(), buf, size);
}

Expand Down
2 changes: 0 additions & 2 deletions paddlepaddle/paddlepaddle-engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ dependencies {
}
testImplementation "org.slf4j:slf4j-simple:${slf4j_version}"
testRuntimeOnly "ai.djl.paddlepaddle:paddlepaddle-native-auto:${paddlepaddle_version}-SNAPSHOT"
testRuntimeOnly project(":pytorch:pytorch-engine")
testRuntimeOnly "ai.djl.pytorch:pytorch-native-auto:${pytorch_version}-SNAPSHOT"
}

processResources {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ai.djl.engine.Engine;
import ai.djl.ndarray.NDManager;
import ai.djl.paddlepaddle.jni.JniUtils;
import ai.djl.paddlepaddle.jni.LibUtils;
import ai.djl.training.GradientCollector;

/**
Expand All @@ -38,6 +39,7 @@ private PpEngine() {
}

static Engine newInstance() {
LibUtils.loadLibrary();
return new PpEngine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
/** {@code PpModel} is the PaddlePaddle implementation of {@link Model}. */
public class PpModel extends BaseModel {

private AnalysisConfig config;
private PaddlePredictor paddlePredictor;

/**
Expand Down Expand Up @@ -68,10 +67,9 @@ public void load(Path modelPath, String prefix, Map<String, ?> options) throws I
throw new FileNotFoundException("no __model__ or model file found in: " + modelDir);
}
}
config =
new AnalysisConfig(
JniUtils.createConfig(modelFiles[0], modelFiles[1], manager.getDevice()));
long config = JniUtils.createConfig(modelFiles[0], modelFiles[1], manager.getDevice());
paddlePredictor = new PaddlePredictor(JniUtils.createPredictor(config));
JniUtils.deleteConfig(config);
setBlock(new PpSymbolBlock(paddlePredictor));
}

Expand All @@ -83,6 +81,8 @@ private String[] findModelFile(Path dir) {
Path paramFile = dir.resolve("params");
if (Files.isRegularFile(paramFile)) {
paths[1] = paramFile.toString();
} else {
paths[0] = dir.toString();
}
return paths;
}
Expand All @@ -93,6 +93,8 @@ private String[] findModelFile(Path dir) {
Path paramFile = dir.resolve("__params__");
if (Files.isRegularFile(paramFile)) {
paths[1] = paramFile.toString();
} else {
paths[0] = dir.toString();
}
return paths;
}
Expand Down Expand Up @@ -125,7 +127,6 @@ public <I, O> Predictor<I, O> newPredictor(Translator<I, O> translator) {
@Override
public void close() {
JniUtils.deletePredictor(paddlePredictor);
JniUtils.deleteConfig(config);
super.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public PpNDArray create(Buffer data, Shape shape, DataType dataType) {
default:
throw new AssertionError("Show never happen");
}
buf.rewind();
return JniUtils.createNdArray(this, buf, shape, dataType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import ai.djl.Device;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.paddlepaddle.engine.AnalysisConfig;
import ai.djl.paddlepaddle.engine.PaddlePredictor;
import ai.djl.paddlepaddle.engine.PpDataType;
import ai.djl.paddlepaddle.engine.PpNDArray;
Expand All @@ -37,7 +36,7 @@ public static PpNDArray createNdArray(
int[] intShape = Arrays.stream(shape.getShape()).mapToInt(Math::toIntExact).toArray();
long handle =
PaddleLibrary.LIB.paddleCreateTensor(
data, data.position(), intShape, PpDataType.toPaddlePaddle(dtype));
data, data.remaining(), intShape, PpDataType.toPaddlePaddle(dtype));
return new PpNDArray(manager, handle);
}

Expand Down Expand Up @@ -73,16 +72,16 @@ public static long createConfig(String modelDir, String paramDir, Device device)
return PaddleLibrary.LIB.createAnalysisConfig(modelDir, paramDir, deviceId);
}

public static void useFeedFetchOp(AnalysisConfig config) {
PaddleLibrary.LIB.useFeedFetchOp(config.getHandle());
public static void useFeedFetchOp(long config) {
PaddleLibrary.LIB.useFeedFetchOp(config);
}

public static void deleteConfig(AnalysisConfig config) {
PaddleLibrary.LIB.deleteAnalysisConfig(config.getHandle());
public static void deleteConfig(long config) {
PaddleLibrary.LIB.deleteAnalysisConfig(config);
}

public static long createPredictor(AnalysisConfig config) {
return PaddleLibrary.LIB.createPredictor(config.getHandle());
public static long createPredictor(long config) {
return PaddleLibrary.LIB.createPredictor(config);
}

public static long clonePredictor(PaddlePredictor predictor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -55,28 +56,62 @@ public final class LibUtils {
private LibUtils() {}

public static void loadLibrary() {
String libName = getLibName();
logger.debug("Loading paddle library from: {}", libName);

System.load(libName); // NOPMD
}

public static String getLibName() {
String libName = LibUtils.findOverrideLibrary();
if (libName == null) {
libName = LibUtils.findLibraryInClasspath();
if (libName == null) {
throw new IllegalStateException("Native library not found");
}
}
if (System.getProperty("os.name").startsWith("Linux")) {
loadLinuxDependencies(libName);
} else if (System.getProperty("os.name").startsWith("Win")) {
loadWindowsDependencies(libName);
}
logger.debug("Now loading " + libName);
System.load(libName); // NOPMD
// TODO: change this part to load from cache directory
Path nativeLibDir = Paths.get(libName).getParent();
if (nativeLibDir == null || !nativeLibDir.toFile().isDirectory()) {
throw new IllegalStateException("Native folder cannot be found");
}
libName = copyJniLibraryFromClasspath(nativeLibDir);
return libName;
logger.debug("Loading paddle library from: {}", libName);
System.load(libName); // NOPMD

// post configure extralib path
if (System.getProperty("os.name").startsWith("Linux")) {
Path libDir = Paths.get(libName).getParent();
if (libDir == null) {
throw new IllegalStateException("Native folder cannot be found");
}
String[] args = {
"dummy", "--mklml_dir=\"" + libDir.toAbsolutePath().toString() + "/\""
};
PaddleLibrary.LIB.loadExtraDir(args);
}
}

public static void loadLinuxDependencies(String libName) {
Path libDir = Paths.get(libName).getParent();
List<String> names = Arrays.asList("libiomp5.so", "libdnnl.so.1");
names.forEach(
name -> {
String lib = libDir.resolve(name).toAbsolutePath().toString();
logger.debug("Now loading " + lib);
System.load(lib);
});
}

public static void loadWindowsDependencies(String libName) {
Path libDir = Paths.get(libName).getParent();
List<String> names = Arrays.asList("libiomp5md.dll", "mklml.dll", "mkldnn.dll");
names.forEach(
name -> {
String lib = libDir.resolve(name).toAbsolutePath().toString();
logger.debug("Now loading " + lib);
System.load(libDir.resolve(name).toAbsolutePath().toString());
});
}

private static String findOverrideLibrary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private PaddleLibrary() {}

native String getTensorName(long handle);

native void loadExtraDir(String[] args);

native long createAnalysisConfig(String modelDir, String paramDir, int deviceId);

native void useFeedFetchOp(long handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

public class JniUtilsTest {

@Test(enabled = false)
@Test
void createNDArray() {
// Require user to override PADDLE_LIBRARY_PATH
LibUtils.loadLibrary();
try (NDManager manager = NDManager.newBaseManager(null, "PaddlePaddle")) {
NDArray array = manager.zeros(new Shape(1, 2));
float[] expected = new float[] {0, 0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.paddlepaddle.jni.LibUtils;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
Expand All @@ -36,8 +35,6 @@ public class ImageClassificationTest {
public void testImageClassification()
throws MalformedModelException, ModelNotFoundException, IOException,
TranslateException {
// Require user to override PADDLE_LIBRARY_PATH
LibUtils.loadLibrary();
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 @@ -30,7 +30,6 @@
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.types.Shape;
import ai.djl.paddlepaddle.jni.LibUtils;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
Expand All @@ -53,8 +52,6 @@ public class MaskDetectionTest {

@Test(enabled = false)
public void testMaskDetection() throws IOException, ModelException, TranslateException {
// Require user to override PADDLE_LIBRARY_PATH
LibUtils.loadLibrary();
String url =
"https://raw.githubusercontent.com/PaddlePaddle/PaddleHub/release/v1.5/demo/mask_detection/python/images/mask.jpg";
Image img = ImageFactory.getInstance().fromUrl(url);
Expand Down
4 changes: 2 additions & 2 deletions paddlepaddle/paddlepaddle-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ task prepareNativeLibs() {

def files = [
// "linux/cu101": "",
"linux/cpu" : "https://paddle-inference-lib.bj.bcebos.com/latest-cpu-avx-mkl/fluid_inference.tgz",
"linux/cpu" : "https://alpha-djl-demos.s3.amazonaws.com/temp/paddle_inference_install_dir-2.0-rc1-gcc54.tgz",
"osx/cpu": "https://paddle-inference-lib.bj.bcebos.com/mac/2.0-rc/cpu_avx_openblas/paddle_inference_install_dir.tgz",
// "win/cu101" : "",
"win/cpu": "https://paddle-wheel.bj.bcebos.com/2.0.0-rc0/win-infer/mkl/cpu/paddle_inference_install_dir.zip"
Expand Down Expand Up @@ -96,7 +96,7 @@ task prepareNativeLibs() {
copy {
from(tmpDir) {
include '**/*.dylib'
include '**/*.so'
include '**/*.so*'
}
eachFile {
path = name
Expand Down
8 changes: 3 additions & 5 deletions paddlepaddle/paddlepaddle-native/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ if [[ ! -d "paddle" ]]; then
echo "Folder not found. Downloading C++ package..."
if [[ $PLATFORM == 'linux' ]]; then
if [[ $1 == "cpu" ]]; then
curl -s https://paddle-inference-lib.bj.bcebos.com/latest-cpu-avx-mkl/fluid_inference.tgz -o paddle.tgz
tar -xvzf paddle.tgz
mv fluid_inference paddle
curl -s https://alpha-djl-demos.s3.amazonaws.com/temp/paddle_inference_install_dir-2.0-rc1-gcc54.tgz -o paddle.tgz
else
echo "$1 is not supported."
exit 1
fi
elif [[ $PLATFORM == 'darwin' ]]; then
curl -s https://paddle-inference-lib.bj.bcebos.com/mac%2F2.0-rc%2Fcpu_avx_openblas%2Fpaddle_inference_install_dir.tgz -o paddle.tgz
tar -xvzf paddle.tgz
mv paddle_inference_install_dir paddle
else
echo "$PLATFORM is not supported."
exit 1
fi
tar -xvzf paddle.tgz
mv paddle_inference_install_dir paddle
fi

rm -rf build
Expand Down
Loading