Skip to content
Closed
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
31 changes: 20 additions & 11 deletions cr-examples/onnx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ JAVA_HOME=<path to the Babylon JDK home>
mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.mnist.MNISTDemo
```

### ONNX Runtime with CoreML running facial emotion recognition from Java source.
### ONNX Runtime with CoreML running facial emotion recognition from Java source

Download the `.data` file from [emotion-ferplus-8.onnx.data](https://github.com/ammbra/fer-model-weights/raw/refs/heads/main/emotion-ferplus-8.onnx.data) and place it under `cr-examples/onnx/src/test/resources/oracle/code/onnx/fer` folder.

Expand All @@ -18,29 +18,38 @@ JAVA_HOME=<path to the Babylon JDK home>
mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.fer.FERCoreMLDemo
```

#### How to (Re)Generate the CoreML Java Bindings
### ONNX Runtime running an embedding model from Java source

The following instructions are for Mac users only as the CoreML Execution Provider (EP) requires iOS devices with iOS 13 or higher, or Mac computers with macOS 10.15 or higher.
Build and install custom ONNX Runtime with CoreML enabled:
[all-MiniLM-L6-v2](https://huggingface.co/onnx-community/all-MiniLM-L6-v2-ONNX) is a popular, lightweight, sentence embedding model.
This model converts sentences into 384 dimensional vectors, an aspect very useful for semantic search and information retrieval.

Download `model.onnx_data` and `vocab.txt` data files from [all-MiniLM-L6-v2](https://huggingface.co/onnx-community/all-MiniLM-L6-v2-ONNX) and put them into `cr-examples/onnx/src/test/resources/oracle/code/onnx/bert` folder.

To run the EmbeddingDemo, execute:

```
git clone --recursive https://github.com/microsoft/onnxruntime.git
cd onnxruntime
./build.sh --config Release --build_shared_lib --use_coreml --parallel
# get the path to where current built library is available
pwd
JAVA_HOME=<path to the Babylon JDK home>
mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.bert.EmbeddingDemo
```

#### How to (Re)Generate the Java Bindings

The following instructions are for Mac users only as the CoreML Execution Provider (EP) requires iOS devices with iOS 13 or higher, or Mac computers with macOS 10.15 or higher.
Download ONNX Runtime from a published release.

Inside `cr-examples/onnx/opgen` project you will find the `setup.sh` script that takes as argument the path to your cloned `onnxruntime` and uses `jextract` to regenerate the binaries.
Prior to running it make sure that `jextract` is in your system `$PATH` :

```shell
jextract --version
```
Provide the path to your cloned `onnxruntime` and the script will regenerate the CoreML Java bindings inside the `oracle.code.onnx.foreign`:

Provide the path to your cloned `onnxruntime` and the script will regenerate the Java bindings inside the `oracle.code.onnx.foreign`:

```
sh setup.sh path/to/cloned/onnxruntime
export ONNX_INCLUDE_DIR=/path/to/onnx/include/onnxruntime/include
export ONNX_GEN_AI_INCLUDE_DIR=/path/to/onnx/include/onnxruntime-genai/include
sh setup.sh
```

### ONNX GenAI running large language model from Java source.
Expand Down
10 changes: 5 additions & 5 deletions cr-examples/onnx/opgen/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,20 +29,20 @@ questions.
<modelVersion>4.0.0</modelVersion>

<groupId>oracle.code</groupId>
<artifactId>onnx-opgen</artifactId>
<artifactId>opgen</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>26</maven.compiler.source>
<maven.compiler.target>26</maven.compiler.target>
<maven.compiler.source>28</maven.compiler.source>
<maven.compiler.target>28</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<version>6.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
40 changes: 11 additions & 29 deletions cr-examples/onnx/opgen/setup.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
#!/bin/bash

# Check if the search path is provided
if [ -z "$1" ]; then
echo "Please provide the path to search for libonnxruntime.*.dylib"
exit 1
fi

# Search for libonnxruntime.dylib in the given path
LIB_PATH="$(find "$1" -name libonnxruntime.*.dylib -print -quit)"

if [ -z "$LIB_PATH" ]; then
echo "libonnxruntime.*.dylib not found in $1"
exit 1
fi

# Infer other paths based on the location of libonnxruntime.*.dylib
ONNXRT_DIR=$(dirname $(dirname $(dirname $(dirname $LIB_PATH))))
INCLUDE_DIR=$ONNXRT_DIR/include/onnxruntime
OUTPUT_DIR=../src/main/java

## Run jextract
## Run jextract for ONNX runtime
jextract --target-package oracle.code.onnx.foreign \
-l :$LIB_PATH \
--use-system-load-library \
-I $INCLUDE_DIR/core/session \
-I $ONNX_INCLUDE_DIR \
@symbols \
--output $OUTPUT_DIR \
$INCLUDE_DIR/core/providers/coreml/coreml_provider_factory.h
--header-class-name onnxruntime_c_api_h \
$ONNX_INCLUDE_DIR/onnxruntime_c_api.h \
$ONNX_INCLUDE_DIR/coreml_provider_factory.h

#jextract --target-package oracle.code.onnx.foreign \
# -l :$LIB_PATH \
# -I $INCLUDE_DIR \
# --header-class-name onnxruntime_c_api_h \
# --use-system-load-library \
# --output $OUTPUT_DIR \
# $INCLUDE_DIR/core/session/onnxruntime_c_api.h
## Run jextract for Gen AI runtime
jextract --target-package oracle.code.onnx.foreign \
--output $OUTPUT_DIR \
--header-class-name OrtGenApi \
@symbolsai \
$ONNX_GEN_AI_INCLUDE_DIR/ort_genai_c.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -86,7 +86,10 @@ enum ElementType {
FLOAT8E5M2FNUZ(20, Object.class),
UINT4(21, Object.class),
INT4(22, Object.class),
FLOAT4E2M1(23, Object.class)
FLOAT4E2M1(23, Object.class),
FLOAT8E8M0(24, Object.class),
UINT2(25, Object.class),
INT2(26, Object.class)
;

final int id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -63,7 +63,7 @@ void genOpsClass(Writer w_) throws IOException {

w.write("""
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -191,7 +191,7 @@ private void genAttributeEnum(IndentWriter w, OpSchema s) throws IOException {

if (!a.required() && a.default_value() != null) {
switch (a.type()) {
case FLOAT -> w.write(Float.toString((Float) a.default_value()) + "f");
case FLOAT -> w.write(a.default_value() + "f");
case INT -> w.write(Integer.toString((Integer) a.default_value()));
case STRING -> w.write("\"" + a.default_value() + "\"");
default -> throw new IllegalStateException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -63,7 +63,7 @@ void genOpClass(Writer w_) throws IOException {

w.write("""
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,7 +40,7 @@ public class ProtoGen {

static final String COPYRIGHT_NOTICE = """
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
Loading