Skip to content

Commit

Permalink
Port Java bindings (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
likholat authored Nov 9, 2020
1 parent 5189cf8 commit e929f98
Show file tree
Hide file tree
Showing 52 changed files with 4,200 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/code_style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Code Style
on: [push, pull_request]

jobs:
Java:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v1
with:
java-version: '11'

- name: Install dependencies
run: |
wget -nc https://github.com/google/google-java-format/releases/download/google-java-format-1.9/google-java-format-1.9-all-deps.jar
- name: Check code style
run: |
java -jar google-java-format-1.9-all-deps.jar --set-exit-if-changed -a -i $(find . -type f -name "*.java")
- name: Create code style diff
if: failure()
run: |
git diff >java_code_style_diff.patch
- uses: actions/upload-artifact@v2
if: failure()
with:
name: java_code_style_diff
path: java_code_style_diff.patch
36 changes: 36 additions & 0 deletions modules/java_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2020 Intel Corporation

project(inference_engine_java_api)

# Find Java

find_package(Java REQUIRED)
include(UseJava)

set(JAVA_AWT_INCLUDE_PATH NotNeeded)

find_package(JNI REQUIRED)

# Build native part

file(GLOB_RECURSE sources ${CMAKE_CURRENT_SOURCE_DIR}/cpp/*.cpp)

add_library(${PROJECT_NAME} SHARED ${sources})

target_link_libraries(${PROJECT_NAME} PRIVATE inference_engine)
target_include_directories(${PROJECT_NAME} PRIVATE ${JNI_INCLUDE_DIRS})

# Build java part

file(GLOB_RECURSE java_source ${CMAKE_CURRENT_SOURCE_DIR}/org/intel/openvino/*.java)
add_jar(inference_engine_jar ${java_source}
OUTPUT_NAME ${PROJECT_NAME}
OUTPUT_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

if(ENABLE_TESTS)
add_subdirectory(tests)
endif()

if(ENABLE_SAMPLES)
add_subdirectory(samples)
endif()
14 changes: 14 additions & 0 deletions modules/java_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Software Requirements
- OpenJDK (version depends on target OS)

### Linux
To install OpenJDK:

* Ubuntu systems:
```bash
sudo apt-get install -y default-jdk
```

## Building on Linux

To create Inference Engine Java API add ```-DENABLE_JAVA=ON``` flag in cmake command while building the Inference Engine.
229 changes: 229 additions & 0 deletions modules/java_api/cpp/blob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
#include <inference_engine.hpp>

#include "openvino_java.hpp"
#include "jni_common.hpp"

using namespace InferenceEngine;

JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_GetTensorDesc(JNIEnv *env, jobject obj, jlong addr)
{
static const char method_name[] = "GetTensorDesc";
try
{
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
TensorDesc *tDesc = new TensorDesc((*output)->getTensorDesc());

return (jlong)tDesc;
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_GetBlob(JNIEnv *env, jobject obj, jlong tensorDescAddr)
{
static const char method_name[] = "GetBlob";
try
{
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;

Blob::Ptr *blob = new Blob::Ptr();
*blob = make_shared_blob<uint8_t>(*tDesc);

return (jlong)blob;
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobByte(JNIEnv *env, jobject obj, jlong tensorDescAddr, jbyteArray data)
{
static const char method_name[] = "BlobByte";
try
{
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;

Blob::Ptr *blob = new Blob::Ptr();

*blob = make_shared_blob<uint8_t>((*tDesc));
(*blob)->allocate();
env->GetByteArrayRegion(data, 0, (*blob)->size(), (*blob)->buffer());

return (jlong)blob;
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobFloat(JNIEnv *env, jobject obj, jlong tensorDescAddr, jfloatArray data)
{
static const char method_name[] = "BlobFloat";
try
{
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;

Blob::Ptr *blob = new Blob::Ptr();

*blob = make_shared_blob<float>((*tDesc));
(*blob)->allocate();
env->GetFloatArrayRegion(data, 0, (*blob)->size(), (*blob)->buffer());

return (jlong)blob;
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobCArray(JNIEnv *env, jobject obj, jlong tensorDescAddr, jlong matDataAddr)
{
static const char method_name[] = "BlobCArray";
try
{
TensorDesc *tDesc = (TensorDesc *)tensorDescAddr;

auto precision = tDesc->getPrecision();

std::vector<size_t> dims = tDesc->getDims();
Blob::Ptr *blob = new Blob::Ptr();

switch (precision) {
case Precision::FP32:
{
float *data = (float *) matDataAddr;
*blob = make_shared_blob<float>((*tDesc), data);
break;
}
case Precision::Q78:
case Precision::I16:
case Precision::FP16:
{
short *data = (short *) matDataAddr;
*blob = make_shared_blob<short>((*tDesc), data);
break;
}
case Precision::U8:
{
uint8_t *data = (uint8_t *) matDataAddr;
*blob = make_shared_blob<uint8_t>((*tDesc), data);
break;
}
case Precision::I8:
{
int8_t *data = (int8_t *) matDataAddr;
*blob = make_shared_blob<int8_t>((*tDesc), data);
break;
}
case Precision::I32:
{
int32_t *data = (int32_t *) matDataAddr;
*blob = make_shared_blob<int32_t>((*tDesc), data);
break;
}
case Precision::BF16:
{
short *data = (short *) matDataAddr;
*blob = make_shared_blob<short>((*tDesc), data);
break;
}
default:
throw std::runtime_error("Unsupported precision value!");
}

return (jlong)blob;
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT jint JNICALL Java_org_intel_openvino_Blob_size(JNIEnv *env, jobject obj, jlong addr)
{
static const char method_name[] = "size";
try
{
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
int size = (*output)->size();

return size;
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_rmap(JNIEnv *env, jobject obj, jlong addr)
{
static const char method_name[] = "rmap";
try
{
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);

if ((*output)->is<MemoryBlob>()) {
LockedMemory<const void> *lmem = new LockedMemory<const void> (as<MemoryBlob>(*output)->rmap());
return (jlong)lmem;
} else {
throw std::runtime_error("Target Blob cannot be cast to the MemoryBlob!");
}
}
catch (const std::exception &e)
{
throwJavaException(env, &e, method_name);
}
catch (...)
{
throwJavaException(env, 0, method_name);
}

return 0;
}

JNIEXPORT void JNICALL Java_org_intel_openvino_Blob_delete(JNIEnv *, jobject, jlong addr)
{
Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr);
delete output;
}
Loading

0 comments on commit e929f98

Please sign in to comment.