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

[Summer of Code] build liboneflow.so for Java #5115

Open
wants to merge 74 commits into
base: master
Choose a base branch
from

Conversation

zzk0
Copy link
Contributor

@zzk0 zzk0 commented Jun 7, 2021

构建

  1. clone 指定分支代码: git clone -b libOneFlow_java https://github.com/zzk0/oneflow.git zzk-oneflow --depth=1
  2. 安装依赖: sudo apt install -y libopenblas-dev nasm g++ gcc python3-pip cmake autoconf libtool
  3. 安装 java 依赖: sudo apt install openjdk-11-jdk maven
  4. mkdir build, cd build
  5. cmake .. -DCMAKE_BUILD_TYPE=Debug -DBUILD_JNI=ON -C ../cmake/caches/cn/cuda.cmake -DBUILD_HWLOC=OFF
  6. 构建 jar 包: make -j$(nproc) oneflow-api
  7. 构建动态链接库: make -j$(nproc) oneflow_java
  8. 导出到环境变量 LD_LIBRARY_PATH, export LD_LIBRARY_PATH=/path-to-project/build/oneflow/api/java:$LD_LIBRARY_PATH
  9. 如果想要运行测试,需要进入到 oneflow/oneflow/java 文件夹,然后 mvn test。测试开始的时候,会下载测试需要的资源。

文件结构说明

Java 代码存放在 oneflow/java 目录下面,JNI 相关的代码存放在 oneflow/api/java 目录下面。

Java 代码

(transformers) percent1@ubuntu:~/zzk-oneflow$ tree oneflow/java/src/
oneflow/java/src/
├── main
│   └── java
│       └── org
│           └── oneflow
│               ├── DType.java
│               ├── InferenceSession.java
│               ├── OneFlow.java                # 和 JNI 对齐
│               ├── Option.java
│               ├── tensor
│               │   ├── FloatTensor.java
│               │   └── IntTensor.java
│               └── Tensor.java
└── test
    └── java
        └── org
            └── oneflow
                └── InferenceSessionTest.java   # 测试

JNI 代码

(transformers) percent1@ubuntu:~/zzk-oneflow$ tree oneflow/api/java/
oneflow/api/java/
├── CMakeLists.txt
├── env
│   └── env_api.h          # C API
├── job
│   └── job_api.h          # C API
├── library.cpp            # 从 Java 读取数据、调用封装的 API、发送数据
├── library.h              # 和 OneFlow.java 对齐
├── session
│   └── session_api.h      # C API
└── util
    └── jni_util.h         # 方便 JNI 编程,比如 jstring 和 string 互转,大端小端检测 

使用例子

@Test
public void example() {
    String jobName = "mlp_inference";

    Option option = new Option();
    option.setDeviceTag("gpu")
            .setControlPort(11245)
            .setSavedModelDir("mnist_test/models")
            .setModelVersion("1");

    InferenceSession inferenceSession = new InferenceSession(option);
    inferenceSession.open();

    float[] image = readImage("mnist_test/test_set/00000000_7.png");
    Tensor imageTensor = Tensor.fromBlob(image, new long[]{ 1, 1, 28, 28 });
    Tensor tagTensor = Tensor.fromBlob(new int[]{ 1 }, new long[]{ 1 });
    Map<String, Tensor> tensorMap = new HashMap<>();
    tensorMap.put("Input_14", imageTensor);
    tensorMap.put("Input_15", tagTensor);

    Map<String, Tensor> resultMap = inferenceSession.run(jobName, tensorMap);
    for (Map.Entry<String, Tensor> entry : resultMap.entrySet()) {
        Tensor resTensor = entry.getValue();
        float[] resFloatArray = resTensor.getDataAsFloatArray();
    }

    inferenceSession.close();
}

cmake/oneflow.cmake Outdated Show resolved Hide resolved
cmake/oneflow.cmake Outdated Show resolved Hide resolved
@jackalcooper jackalcooper changed the title [Summer of Code] build liboneflow.so [Summer of Code] build liboneflow.so for Java Jun 7, 2021
oneflow/api/java/CMakeLists.txt Outdated Show resolved Hide resolved
oneflow/api/java/CMakeLists.txt Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants