Skip to content

Commit

Permalink
[api] Replace double-check singlton with lazy initialization (deepjav…
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Oct 30, 2023
1 parent 7d68857 commit 3927867
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (LgbmEngineProvider.class) {
engine = LgbmEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = LgbmEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (XgbEngineProvider.class) {
engine = XgbEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = XgbEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (MxEngineProvider.class) {
engine = MxEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = MxEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (OrtEngineProvider.class) {
engine = OrtEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = OrtEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (PpEngineProvider.class) {
engine = PpEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = PpEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (PtEngineProvider.class) {
engine = PtEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = PtEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (TfEngineProvider.class) {
engine = TfEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TfEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (TrtEngineProvider.class) {
engine = TrtEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TrtEngine.newInstance();
}
}
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 (Exception ignore) {
} catch (Throwable 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 (Exception ignore) {
} catch (Throwable 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 (Exception ignore) {
} catch (Throwable 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 (Exception ignore) {
} catch (Throwable 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 (Exception ignore) {
} catch (Throwable 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,8 +18,6 @@
/** {@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 @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (TfLiteEngineProvider.class) {
engine = TfLiteEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TfLiteEngine.newInstance();
}
}

0 comments on commit 3927867

Please sign in to comment.