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] add tensorRT option #1611

Merged
merged 2 commits into from
Apr 29, 2022
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
8 changes: 8 additions & 0 deletions engines/onnxruntime/onnxruntime-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ Gradle:
}
implementation "com.microsoft.onnxruntime:onnxruntime_gpu:1.11.0"
```

#### Enable TensorRT execution

ONNXRuntime offers TensorRT execution as the backend. In DJL, user can specify the followings in the Criteria to enable:

```
optOption("ortDevice", "TensorRT")
```
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public void load(Path modelPath, String prefix, Map<String, ?> options)

try {
SessionOptions ortOptions = getSessionOptions(options);
Device device = manager.getDevice();
if (device.isGpu()) {
ortOptions.addCUDA(manager.getDevice().getDeviceId());
}
OrtSession session = env.createSession(modelFile.toString(), ortOptions);
block = new OrtSymbolBlock(session, (OrtNDManager) manager);
} catch (OrtException e) {
Expand All @@ -100,10 +96,6 @@ public void load(InputStream is, Map<String, ?> options)
try {
byte[] buf = Utils.toByteArray(is);
SessionOptions ortOptions = getSessionOptions(options);
Device device = manager.getDevice();
if (device.isGpu()) {
ortOptions.addCUDA(manager.getDevice().getDeviceId());
}
OrtSession session = env.createSession(buf, ortOptions);
block = new OrtSymbolBlock(session, (OrtNDManager) manager);
} catch (OrtException e) {
Expand Down Expand Up @@ -186,6 +178,28 @@ private SessionOptions getSessionOptions(Map<String, ?> options) throws OrtExcep
ortSession.setCPUArenaAllocator(true);
}

Device device = manager.getDevice();
if (options.containsKey("ortDevice")) {
String ortDevice = (String) options.get("ortDevice");
switch (ortDevice) {
case "TensorRT":
if (!device.isGpu()) {
throw new IllegalArgumentException("TensorRT required GPU device.");
}
ortSession.addTensorrt(device.getDeviceId());
break;
case "ROCM":
ortSession.addROCM();
break;
case "CoreML":
ortSession.addCoreML();
break;
default:
throw new IllegalArgumentException("Invalid ortDevice: " + ortDevice);
}
} else if (device.isGpu()) {
ortSession.addCUDA(device.getDeviceId());
}
return ortSession;
}
}