microsoft / microsoft/onnxruntime-inference-examples
Can anyone give a simple example to load and run inference on GPT-2 model in JAVA?
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 1.7k
- Forks
- 414
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 14
Description
I have exported the GPT-2 model in the ONNX format but I am not able to find any examples to perform the inference in the Java. Please help and share some code.
Thank you 🙏🏻
I was able to somehow get the following code running but I am sure there can be more like setting temperature, automatically tokenizing the string, sampling, topK etc... I am not sure how.
import ai.onnxruntime.*;
import java.nio.LongBuffer;
import java.util.Map;
public class onnx_importer {
public static void main(String args[]) throws OrtException {
// Load the model and create InferenceSession
System.out.println("This is the model loading");
String modelPath = "decoder_model.onnx";
OrtEnvironment env = OrtEnvironment.getEnvironment();
OrtSession session = env.createSession(modelPath);
// The actual tokens (manually because I don't know where to find the tokenizer!)
long[] inputData = new long[]{32, 1, 2, 35}; // Your input data in the appropriate format
long[] inputShape = new long[]{1, 4}; // Shape of the input data
// Attention mask needed (all ones - same shape as the input ids)
long[] inputData_a = new long[]{1, 1, 1, 1}; // Your input data in the appropriate format
long[] inputShape_a = new long[]{1, 4}; // Shape of the input data
OnnxTensor inputTensor = OnnxTensor.createTensor(env, LongBuffer.wrap(inputData), inputShape);
OnnxTensor attTensor = OnnxTensor.createTensor(env, LongBuffer.wrap(inputData_a), inputShape_a);
// This is the forward pass so that we can get the logits
var inputs = Map.of("input_ids", inputTensor, "attention_mask", attTensor);
var results = session.run(inputs);
// This is the output (one of the outputs)
OnnxValue a = results.get(0);
System.out.println(a.getValue());
System.out.println(a.getInfo());
System.out.println(a.getInfo());
}
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the Java code in the issue and review the repository's existing inference examples for the appropriate entry point. Done would require a clear, runnable GPT-2 ONNX Runtime Java example that explains tokenization and the requested generation options, but the issue does not name a target file or test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- machine-learning
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100