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

[onnx] Allows to customize onnxruntime optimization level #2137

Merged
merged 1 commit into from
Jul 2, 2024
Merged
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
12 changes: 9 additions & 3 deletions wlm/src/main/java/ai/djl/serving/wlm/LmiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ static void convertOnnxModel(ModelInfo<?, ?> info) throws IOException {
if (modelId == null) {
modelId = repo.toString();
}
info.modelUrl = convertOnnx(modelId).toUri().toURL().toString();
String optimization = info.prop.getProperty("option.optimization");
info.modelUrl = convertOnnx(modelId, optimization).toUri().toURL().toString();
}

private static Path convertOnnx(String modelId) throws IOException {
private static Path convertOnnx(String modelId, String optimization) throws IOException {
logger.info("Converting model to onnx artifacts");
String hash = Utils.hash(modelId);
String download = Utils.getenv("SERVING_DOWNLOAD_DIR", null);
Expand All @@ -174,6 +175,11 @@ private static Path convertOnnx(String modelId) throws IOException {

Engine onnx = Engine.getEngine("OnnxRuntime");
boolean hasCuda = onnx.getGpuCount() > 0;
if (optimization == null || optimization.isBlank()) {
optimization = hasCuda ? "O4" : "O2";
} else if (!optimization.matches("O\\d")) {
throw new IllegalArgumentException("Unsupported optimization level: " + optimization);
}

String[] cmd = {
"djl-convert",
Expand All @@ -184,7 +190,7 @@ private static Path convertOnnx(String modelId) throws IOException {
"-m",
modelId,
"--optimize",
hasCuda ? "O4" : "O2",
optimization,
"--device",
hasCuda ? "cuda" : "cpu"
};
Expand Down
Loading