1 ## MavenStyleProject using code reflection with a Java-based ONNX programming model.
2
3 ### ONNX Runtime running convolution neural network from Java source
4
5 Running the MNIST demo:
6 ```
7 JAVA_HOME=<path to the Babylon JDK home>
8 mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.mnist.MNISTDemo
9 ```
10
11 ### ONNX Runtime with CoreML running facial emotion recognition from Java source
12
13 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.
14
15 Running the FER demo:
16 ```
17 JAVA_HOME=<path to the Babylon JDK home>
18 mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.fer.FERCoreMLDemo
19 ```
20
21 ### ONNX Runtime running an embedding model from Java source
22
23 [all-MiniLM-L6-v2](https://huggingface.co/onnx-community/all-MiniLM-L6-v2-ONNX) is a popular, lightweight, sentence embedding model.
24 This model converts sentences into 384 dimensional vectors, an aspect very useful for semantic search and information retrieval.
25
26 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.
27
28 To run the EmbeddingDemo, execute:
29
30 ```
31 JAVA_HOME=<path to the Babylon JDK home>
32 mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.bert.EmbeddingDemo
33 ```
34
35 #### How to (Re)Generate the Java Bindings
36
37 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.
38 Download ONNX Runtime from a published release.
39
40 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.
41 Prior to running it make sure that `jextract` is in your system `$PATH` :
42
43 ```shell
44 jextract --version
45 ```
46
47 Provide the path to your cloned `onnxruntime` and the script will regenerate the Java bindings inside the `oracle.code.onnx.foreign`:
48
49 ```
50 export ONNX_INCLUDE_DIR=/path/to/onnx/include/onnxruntime/include
51 export ONNX_GEN_AI_INCLUDE_DIR=/path/to/onnx/include/onnxruntime-genai/include
52 sh setup.sh
53 ```
54
55 ### ONNX GenAI running large language model from Java source.
56
57 Setup:
58 - Download [onnxruntime-genai](https://github.com/microsoft/onnxruntime-genai/releases) native library coresponding to your system/architecture, unzip and put it into `cr-examples/onnx/lib` folder.
59 - Download `model_q4.onnx_data`, `tokenizer.json` and `tokenizer_config.json` data files from [Llama-3.2-1B-Instruct-ONNX](https://huggingface.co/onnx-community/Llama-3.2-1B-Instruct-ONNX/tree/main) and put them into `cr-examples/onnx/src/test/resources/oracle/code/onnx/llm` folder.
60
61 Running the Llama demo:
62 ```
63 JAVA_HOME=<path to the Babylon JDK home>
64 mvn process-test-classes exec:java -Dexec.mainClass=oracle.code.onnx.llm.LlamaDemo
65 ```
66
67 ### Lifting ONNX model from binary to Java source.
68
69 OnnxLift is an experimental tool for lifting ONNX binary models to ONNX code reflection model, extraction of weights, and generation of Java model source.
70
71 Running the OnnxLift:
72 ```
73 JAVA_HOME=<path to the Babylon JDK home>
74 mvn package exec:java -Dexec.mainClass=oracle.code.onnx.lift.OnnxLift -Dexec.args="<model.onnx> <target folder> [class simple name]"
75 ```