Skip to content

Commit

Permalink
[api] Tries to use the same device for alternative NDManager (#3146)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu committed May 1, 2024
1 parent 1d3613a commit 00c78f6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions api/src/main/java/ai/djl/ndarray/BaseNDManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import ai.djl.Device;
import ai.djl.engine.Engine;
import ai.djl.engine.StandardCapabilities;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.util.PairList;
Expand Down Expand Up @@ -62,8 +63,13 @@ protected BaseNDManager(NDManager parent, Device device) {
uid = UUID.randomUUID().toString();
Engine engine = getEngine().getAlternativeEngine();
if (engine != null) {
// Use the default device
alternativeManager = engine.newBaseManager();
// Use the same device if possible for efficiency
if (this.device.isGpu() && engine.hasCapability(StandardCapabilities.CUDA)) {
alternativeManager = engine.newBaseManager(this.device);
} else {
// Use the default device
alternativeManager = engine.newBaseManager();
}
}
}

Expand Down

0 comments on commit 00c78f6

Please sign in to comment.