Skip to content

Commit

Permalink
Revert "[api] Replace double-check singlton with lazy initialization (d…
Browse files Browse the repository at this point in the history
…eepjavalibrary#2826)"

This reverts commit 3927867.
  • Loading branch information
zachgk committed Dec 5, 2023
1 parent 134c581 commit 0be9c25
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code LgbmEngineProvider} is the LightGBM implementation of {@link EngineProvider}. */
public class LgbmEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = LgbmEngine.newInstance();
if (engine == null) {
synchronized (LgbmEngineProvider.class) {
engine = LgbmEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code XgbEngineProvider} is the XGBoost implementation of {@link EngineProvider}. */
public class XgbEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = XgbEngine.newInstance();
if (engine == null) {
synchronized (XgbEngineProvider.class) {
engine = XgbEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code MxEngineProvider} is the MXNet implementation of {@link EngineProvider}. */
public class MxEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = MxEngine.newInstance();
if (engine == null) {
synchronized (MxEngineProvider.class) {
engine = MxEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code OrtEngineProvider} is the ONNX Runtime implementation of {@link EngineProvider}. */
public class OrtEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = OrtEngine.newInstance();
if (engine == null) {
synchronized (OrtEngineProvider.class) {
engine = OrtEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code PpEngineProvider} is the PaddlePaddle implementation of {@link EngineProvider}. */
public class PpEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = PpEngine.newInstance();
if (engine == null) {
synchronized (PpEngineProvider.class) {
engine = PpEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code PtEngineProvider} is the PyTorch implementation of {@link EngineProvider}. */
public class PtEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = PtEngine.newInstance();
if (engine == null) {
synchronized (PtEngineProvider.class) {
engine = PtEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code TfEngineProvider} is the TensorFlow implementation of {@link EngineProvider}. */
public class TfEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TfEngine.newInstance();
if (engine == null) {
synchronized (TfEngineProvider.class) {
engine = TfEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code TrtEngineProvider} is the TensorRT implementation of {@link EngineProvider}. */
public class TrtEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TrtEngine.newInstance();
if (engine == null) {
synchronized (TrtEngineProvider.class) {
engine = TrtEngine.newInstance();
}
}
return engine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void getVersion() {
try {
Engine engine = Engine.getEngine("TensorRT");
version = engine.getVersion();
} catch (Throwable ignore) {
} catch (Exception ignore) {
throw new SkipException("Your os configuration doesn't support TensorRT.");
}
Assert.assertEquals(version, "8.4.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testNDArray() {
Engine engine;
try {
engine = Engine.getEngine("TensorRT");
} catch (Throwable ignore) {
} catch (Exception ignore) {
throw new SkipException("Your os configuration doesn't support TensorRT.");
}
if (!engine.defaultDevice().isGpu()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testTrtOnnx() throws ModelException, IOException, TranslateException
Engine engine;
try {
engine = Engine.getEngine("TensorRT");
} catch (Throwable ignore) {
} catch (Exception ignore) {
throw new SkipException("Your os configuration doesn't support TensorRT.");
}
if (!engine.defaultDevice().isGpu()) {
Expand All @@ -75,7 +75,7 @@ public void testTrtUff() throws ModelException, IOException, TranslateException
Engine engine;
try {
engine = Engine.getEngine("TensorRT");
} catch (Throwable ignore) {
} catch (Exception ignore) {
throw new SkipException("Your os configuration doesn't support TensorRT.");
}
if (!engine.defaultDevice().isGpu()) {
Expand Down Expand Up @@ -112,7 +112,7 @@ public void testSerializedEngine() throws ModelException, IOException, Translate
Engine engine;
try {
engine = Engine.getEngine("TensorRT");
} catch (Throwable ignore) {
} catch (Exception ignore) {
throw new SkipException("Your os configuration doesn't support TensorRT.");
}
Device device = engine.defaultDevice();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/** {@code TfLiteEngineProvider} is the TFLite implementation of {@link EngineProvider}. */
public class TfLiteEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -33,10 +35,11 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TfLiteEngine.newInstance();
if (engine == null) {
synchronized (TfLiteEngineProvider.class) {
engine = TfLiteEngine.newInstance();
}
}
return engine;
}
}

0 comments on commit 0be9c25

Please sign in to comment.