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

Added missing javadoc to improve the doc quality #1136

Merged
merged 9 commits into from
Dec 17, 2024
10 changes: 6 additions & 4 deletions src/java/src/main/java/ai/onnxruntime/genai/Adapters.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package ai.onnxruntime.genai;

/** A container of adapters. */
public final class Adapters implements AutoCloseable {
private long nativeHandle = 0;

Expand All @@ -22,13 +23,13 @@ public Adapters(Model model) throws GenAIException {
}

/**
* Load an adapter from the specified path.
* Loads the model adapter from the given adapter file path and adapter name.
*
* @param adapterFilePath The path of the adapter.
* @param adapterName A unique user supplied adapter identifier.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public void loadAdapters(String adapterFilePath, String adapterName) throws GenAIException {
public void loadAdapter(String adapterFilePath, String adapterName) throws GenAIException {
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
}
Expand All @@ -37,12 +38,13 @@ public void loadAdapters(String adapterFilePath, String adapterName) throws GenA
}

/**
* Unload an adapter.
* Unloads the adapter with the given identifier from the previosly loaded adapters. If the
* adapter is not found, or if it cannot be unloaded (when it is in use), an error is returned.
*
* @param adapterName A unique user supplied adapter identifier.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public void unloadAdapters(String adapterName) throws GenAIException {
public void unloadAdapter(String adapterName) throws GenAIException {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
}
Expand Down
23 changes: 23 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,50 @@
*/
package ai.onnxruntime.genai;

/**
* Use Config to set multiple ORT execution providers. The EP used will be chosen based on the
* insertion order.
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
*/
public final class Config implements AutoCloseable {
private long nativeHandle;

/**
* Creates a Config from the given configuration directory.
*
* @param modelPath The path to the configuration directory.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public Config(String modelPath) throws GenAIException {
nativeHandle = createConfig(modelPath);
}

/** Clear all providers. */
public void clearProviders() {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
}
clearProviders(nativeHandle);
}

/**
* Append a provider with the given name.
*
* @param provider_name The provider name.
*/
public void appendProvider(String provider_name) {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
}
appendProvider(nativeHandle, provider_name);
}

/**
* Set options for a provider.
*
* @param provider_name The provider name.
* @param option_name The option name.
* @param option_value The option value.
*/
public void setProviderOption(String provider_name, String option_name, String option_value) {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
Expand Down
4 changes: 2 additions & 2 deletions src/java/src/main/java/ai/onnxruntime/genai/GenAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class GenAI {
/** The short name of the ONNX runtime shared library */
static final String ONNXRUNTIME_LIBRARY_NAME = "onnxruntime";

/** The value of the {@link #GENAI_NATIVE_PATH} system property */
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
/** The value of the GENAI_NATIVE_PATH system property */
private static String libraryDirPathProperty;

/** The OS & CPU architecture string */
Expand Down Expand Up @@ -268,7 +268,7 @@ private static Optional<File> extractFromResources(String library) {

/**
* Maps the library name into a platform dependent library filename. Converts macOS's "jnilib" to
* "dylib" but otherwise is the same as {@link System#mapLibraryName(String)}.
* "dylib" but otherwise is the same as System#mapLibraryName(String).
*
* @param library The library name
* @return The library filename.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

/** An exception which contains the error message and code produced by the native layer. */
public final class GenAIException extends Exception {
public GenAIException(String message) {
GenAIException(String message) {
super(message);
}

public GenAIException(String message, Exception innerException) {
GenAIException(String message, Exception innerException) {
super(message, innerException);
}
}
1 change: 1 addition & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public int getLastTokenInSequence(long sequenceIndex) throws GenAIException {
* Fetches and returns the output tensor with the given name.
*
* @param name The name of the output needed.
* @return The tensor.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public Tensor getOutput(String name) throws GenAIException {
Expand Down
18 changes: 16 additions & 2 deletions src/java/src/main/java/ai/onnxruntime/genai/GeneratorParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.nio.ByteBuffer;

/**
* The `GeneratorParams` class represents the parameters used for generating sequences with a model.
* Set the prompt using setInput, and any other search options using setSearchOption.
* Represents the parameters used for generating sequences with a model. Set the prompt using
* setInput, and any other search options using setSearchOption.
*/
public final class GeneratorParams implements AutoCloseable {
private long nativeHandle = 0;
Expand All @@ -22,6 +22,13 @@ public final class GeneratorParams implements AutoCloseable {
nativeHandle = createGeneratorParams(model.nativeHandle());
}

/**
* Set seach option with double value.
*
* @param optionName The option name.
* @param value The option value.
* @throws GenAIException
*/
public void setSearchOption(String optionName, double value) throws GenAIException {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
Expand All @@ -30,6 +37,13 @@ public void setSearchOption(String optionName, double value) throws GenAIExcepti
setSearchOptionNumber(nativeHandle, optionName, value);
}

/**
* Set search option with boolean value.
*
* @param optionName The option name.
* @param value The option value.
* @throws GenAIException
*/
public void setSearchOption(String optionName, boolean value) throws GenAIException {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
Expand Down
7 changes: 7 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/Images.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
*/
package ai.onnxruntime.genai;

/** This class can load images from the given path and prepare them for processing. */
public class Images implements AutoCloseable {
private long nativeHandle;

/**
* Construct a Images instance.
*
* @param imagePath The image path.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public Images(String imagePath) throws GenAIException {
nativeHandle = loadImages(imagePath);
}
Expand Down
13 changes: 13 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
*/
package ai.onnxruntime.genai;

/** An ORT GenAI model. */
public final class Model implements AutoCloseable {
private long nativeHandle;

/**
* Construct a Model from folder path.
*
* @param modelPath The path of the GenAI model.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public Model(String modelPath) throws GenAIException {
nativeHandle = createModel(modelPath);
}

/**
* Construct a Model from Config
*
* @param config The config to use.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public Model(Config config) throws GenAIException {
nativeHandle = createModelFromConfig(config.nativeHandle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
public class MultiModalProcessor implements AutoCloseable {
private long nativeHandle;

/**
* Construct a MultiModalProcessor for a given model.
*
* @param model The model to be used.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public MultiModalProcessor(Model model) throws GenAIException {
assert (model.nativeHandle() != 0); // internal code should never pass an invalid model

Expand Down
9 changes: 9 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/NamedTensors.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
*/
package ai.onnxruntime.genai;

/**
* This class is an intermediate storage class that bridges the output of preprocessing and the
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
* input of the ONNX model.
*/
public class NamedTensors implements AutoCloseable {
private long nativeHandle;

/**
* Construct a NamedTensor from native handle.
*
* @param handle The native handle.
*/
public NamedTensors(long handle) {
nativeHandle = handle;
}
Expand Down
6 changes: 6 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/SimpleGenAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class SimpleGenAI implements AutoCloseable {
private Model model;
private Tokenizer tokenizer;

/**
* Construct a SimpleGenAI instance from model path.
*
* @param modelPath The path to the GenAI model.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public SimpleGenAI(String modelPath) throws GenAIException {
model = new Model(modelPath);
tokenizer = model.createTokenizer();
Expand Down
2 changes: 2 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/Tensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/** Wraps ORT native Tensor. */
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
public final class Tensor implements AutoCloseable {
private long nativeHandle = 0;
private final ElementType elementType;
Expand All @@ -17,6 +18,7 @@ public final class Tensor implements AutoCloseable {

// The values in this enum must match ONNX values
// https://github.com/onnx/onnx/blob/159fa47b7c4d40e6d9740fcf14c36fff1d11ccd8/onnx/onnx.proto#L499-L544
/** ORT native element types. */
skyline75489 marked this conversation as resolved.
Show resolved Hide resolved
public enum ElementType {
undefined,
float32,
Expand Down
12 changes: 12 additions & 0 deletions src/java/src/main/java/ai/onnxruntime/genai/TokenizerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ public class TokenizerStream implements AutoCloseable {

private long nativeHandle = 0;

/**
* Construct a TokenizerStream.
*
* @param tokenizerStreamHandle The native handle.
*/
TokenizerStream(long tokenizerStreamHandle) {
assert (tokenizerStreamHandle != 0); // internal usage should never pass an invalid handle
nativeHandle = tokenizerStreamHandle;
}

/**
* Decode one token.
*
* @param token The token.
* @return The decoded result.
* @throws GenAIException If the call to the GenAI native API fails.
*/
public String decode(int token) throws GenAIException {
if (nativeHandle == 0) {
throw new IllegalStateException("Instance has been freed and is invalid");
Expand Down
15 changes: 7 additions & 8 deletions src/java/src/main/java/ai/onnxruntime/genai/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
*
* <p>There are two shared libraries required: <code>onnxruntime-genai</code> and <code>
* onnxruntime-genai-jni
* </code>. The loader is in {@link ai.onnxruntime.genai.GenAI} and the logic is in this order:
* </code>. The loader is in ai.onnxruntime.genai.GenAI and the logic is in this order:
*
* <ol>
* <li>The user may signal to skip loading of a shared library using a property in the form <code>
* onnxruntime-genai.native.LIB_NAME.skip</code> with a value of <code>true</code>. This means
* the user has decided to load the library by some other means.
* <li>The user may specify an explicit location of all native library files using a property in
* the form <code>onnxruntime-genai.native.path</code>. This uses {@link
* java.lang.System#load}.
* the form <code>onnxruntime-genai.native.path</code>. This uses {java.lang.System#load}.
* <li>The user may specify an explicit location of the shared library file using a property in
* the form <code>onnxruntime-genai.native.LIB_NAME.path</code>. This uses {@link
* java.lang.System#load}.
* the form <code>onnxruntime-genai.native.LIB_NAME.path</code>. This uses
* {java.lang.System#load}.
* <li>The shared library is autodiscovered:
* <ol>
* <li>If the shared library is present in the classpath resources, load using {@link
* <li>If the shared library is present in the classpath resources, load using {
* java.lang.System#load} via a temporary file. Ideally, this should be the default use
* case when adding JAR's/dependencies containing the shared libraries to your
* classpath.
* <li>If the shared library is not present in the classpath resources, then load using
* {@link java.lang.System#loadLibrary}, which usually looks elsewhere on the filesystem
* for the library. The semantics and behavior of that method are system/JVM dependent.
* {java.lang.System#loadLibrary}, which usually looks elsewhere on the filesystem for
* the library. The semantics and behavior of that method are system/JVM dependent.
* Typically, the <code>java.library.path</code> property is used to specify the
* location of native libraries.
* </ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testUsageWithAdapters() throws GenAIException {
try (Adapters adapters = new Adapters(model);
Generator generator = new Generator(model, params); ) {
generator.appendTokenSequences(sequences);
adapters.loadAdapters(TestUtils.testAdapterTestAdaptersPath(), "adapters_a_and_b");
adapters.loadAdapter(TestUtils.testAdapterTestAdaptersPath(), "adapters_a_and_b");
generator.setActiveAdapter(adapters, "adapters_a_and_b");
while (!generator.isDone()) {
generator.generateNextToken();
Expand Down
Loading