Skip to content

Commit

Permalink
Works with manual initialization
Browse files Browse the repository at this point in the history
This updates DJL Serving to follow the engine initialization standards set in
deepjavalibrary/djl#2885. First, it updates the
PyEngineProvider to follow the EngineProvider conventions.

It also updates the DependencyManager, fixing some bugs that inspired
deepjavalibrary/djl#2934. The dependency manager before
this change would always re-register the engine with a new provider. Beforehand,
the engine was fully static and unable to re-initialize, so this would not
accidentally re-initialize engines. After this, it would re-initialize engines
that did not support it causing errors. Instead, it should only register new
engines rather than all providers to avoid these accidental re-initialization.
  • Loading branch information
zachgk committed Jan 10, 2024
1 parent edbd155 commit 872533a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class PyEngineProvider implements EngineProvider {

private static final String ENGINE_NAME = "Python";

private Engine engine;
private volatile Engine engine; // NOPMD
private volatile boolean initialized; // NOPMD
protected boolean mpiMode;

/** {@inheritDoc} */
Expand All @@ -38,10 +39,13 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
if (!initialized) {
synchronized (this) {
PyEnv.init();
engine = new PyEngine(getEngineName(), mpiMode);
if (!initialized) {
initialized = true;
PyEnv.init();
engine = new PyEngine(getEngineName(), mpiMode);
}
}
}
return engine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ void refreshProviders() {
// refresh EngineProvider
MutableClassLoader mcl = MutableClassLoader.getInstance();
for (EngineProvider provider : ServiceLoader.load(EngineProvider.class, mcl)) {
Engine.registerEngine(provider);
if (!Engine.hasEngine(provider.getEngineName())) {
Engine.registerEngine(provider);
}
}

// refresh ZooProvider
Expand Down

0 comments on commit 872533a

Please sign in to comment.