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

[onnxruntime] Load onnx extenstion if available #3333

Merged
merged 1 commit into from
Jul 12, 2024
Merged
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
7 changes: 3 additions & 4 deletions engines/onnxruntime/onnxruntime-engine/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import java.net.URL

plugins {
ai.djl.javaProject
ai.djl.publish
Expand All @@ -14,13 +12,14 @@ dependencies {
testImplementation(project(":testing"))
testImplementation(project(":engines:pytorch:pytorch-engine"))
testImplementation(project(":extensions:tokenizers"))
testImplementation("com.microsoft.onnxruntime:onnxruntime-extensions:${libs.versions.onnxruntimeExtensions.get()}")

testRuntimeOnly(libs.slf4j.simple)
}

tasks {
processResources {
var basePath = "${project.projectDir}/build/resources/main/nlp"
val basePath = "${project.projectDir}/build/resources/main/nlp"
outputs.dir(basePath)
doLast {
val url = "https://mlrepo.djl.ai/model/nlp"
Expand All @@ -34,7 +33,7 @@ tasks {
for (task in tasks) {
val file = File("$basePath/$task/ai.djl.huggingface.onnxruntime.json")
if (file.exists())
project.logger.lifecycle("model zoo metadata alrady exists: $task")
project.logger.lifecycle("model zoo metadata already exists: $task")
else {
project.logger.lifecycle("Downloading model zoo metadata: $task")
file.parentFile.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ai.djl.Model;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.util.ClassLoaderUtils;
import ai.djl.util.Utils;
import ai.onnxruntime.OrtEnvironment;
import ai.onnxruntime.OrtException;
Expand All @@ -26,9 +27,13 @@
import ai.onnxruntime.OrtSession.SessionOptions.ExecutionMode;
import ai.onnxruntime.OrtSession.SessionOptions.OptLevel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
Expand All @@ -41,6 +46,8 @@
*/
public class OrtModel extends BaseModel {

private static final Logger logger = LoggerFactory.getLogger(OrtModel.class);

private OrtEnvironment env;
private SessionOptions sessionOptions;

Expand Down Expand Up @@ -190,6 +197,9 @@ private SessionOptions getSessionOptions(Map<String, ?> options) throws OrtExcep
}

String customOpLibrary = (String) options.get("customOpLibrary");
if (customOpLibrary == null) {
customOpLibrary = getOrtxLibraryPath();
}
if (customOpLibrary != null) {
ortSession.registerCustomOpLibrary(customOpLibrary);
}
Expand Down Expand Up @@ -223,4 +233,16 @@ private SessionOptions getSessionOptions(Map<String, ?> options) throws OrtExcep
}
return ortSession;
}

private String getOrtxLibraryPath() {
ClassLoader cl = ClassLoaderUtils.getContextClassLoader();
try {
Class<?> clazz = Class.forName("ai.onnxruntime.extensions.OrtxPackage", true, cl);
Method method = clazz.getDeclaredMethod("getLibraryPath");
return (String) method.invoke(null);
} catch (Throwable e) {
logger.debug("Failed to load onnx extension", e);
}
return null;
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tensorflow = "2.16.1"
tensorflowCore = "1.0.0-rc.1"
mxnet = "1.9.1"
onnxruntime = "1.18.0"
onnxruntimeExtensions = "0.11.0"
xgboost = "2.0.3"
lightgbm = "3.2.110"
tensorrt = "8.4.1"
Expand Down
Loading