From 39278672bebe9c5f9d590d73914a2a9e2f53fb91 Mon Sep 17 00:00:00 2001 From: Frank Liu Date: Sun, 29 Oct 2023 23:17:57 -0700 Subject: [PATCH] [api] Replace double-check singlton with lazy initialization (#2826) --- .../java/ai/djl/ml/lightgbm/LgbmEngineProvider.java | 13 +++++-------- .../java/ai/djl/ml/xgboost/XgbEngineProvider.java | 13 +++++-------- .../java/ai/djl/mxnet/engine/MxEngineProvider.java | 13 +++++-------- .../djl/onnxruntime/engine/OrtEngineProvider.java | 13 +++++-------- .../djl/paddlepaddle/engine/PpEngineProvider.java | 13 +++++-------- .../ai/djl/pytorch/engine/PtEngineProvider.java | 13 +++++-------- .../ai/djl/tensorflow/engine/TfEngineProvider.java | 13 +++++-------- .../ai/djl/tensorrt/engine/TrtEngineProvider.java | 13 +++++-------- .../java/ai/djl/tensorrt/engine/TrtEngineTest.java | 2 +- .../ai/djl/tensorrt/engine/TrtNDManagerTest.java | 2 +- .../java/ai/djl/tensorrt/integration/TrtTest.java | 6 +++--- .../ai/djl/tflite/engine/TfLiteEngineProvider.java | 13 +++++-------- 12 files changed, 50 insertions(+), 77 deletions(-) diff --git a/engines/ml/lightgbm/src/main/java/ai/djl/ml/lightgbm/LgbmEngineProvider.java b/engines/ml/lightgbm/src/main/java/ai/djl/ml/lightgbm/LgbmEngineProvider.java index a253ce3d246..f8c84c753ef 100644 --- a/engines/ml/lightgbm/src/main/java/ai/djl/ml/lightgbm/LgbmEngineProvider.java +++ b/engines/ml/lightgbm/src/main/java/ai/djl/ml/lightgbm/LgbmEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbEngineProvider.java b/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbEngineProvider.java index 19cba32cc71..5859f3f344d 100644 --- a/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbEngineProvider.java +++ b/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/MxEngineProvider.java b/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/MxEngineProvider.java index f30a6a89252..5f45116f615 100644 --- a/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/MxEngineProvider.java +++ b/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/MxEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/onnxruntime/onnxruntime-engine/src/main/java/ai/djl/onnxruntime/engine/OrtEngineProvider.java b/engines/onnxruntime/onnxruntime-engine/src/main/java/ai/djl/onnxruntime/engine/OrtEngineProvider.java index c673b3dcbf1..005c0fa25f1 100644 --- a/engines/onnxruntime/onnxruntime-engine/src/main/java/ai/djl/onnxruntime/engine/OrtEngineProvider.java +++ b/engines/onnxruntime/onnxruntime-engine/src/main/java/ai/djl/onnxruntime/engine/OrtEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/paddlepaddle/paddlepaddle-engine/src/main/java/ai/djl/paddlepaddle/engine/PpEngineProvider.java b/engines/paddlepaddle/paddlepaddle-engine/src/main/java/ai/djl/paddlepaddle/engine/PpEngineProvider.java index e2b5bdd35a0..59e5cd90724 100644 --- a/engines/paddlepaddle/paddlepaddle-engine/src/main/java/ai/djl/paddlepaddle/engine/PpEngineProvider.java +++ b/engines/paddlepaddle/paddlepaddle-engine/src/main/java/ai/djl/paddlepaddle/engine/PpEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngineProvider.java b/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngineProvider.java index 57ae6c09d34..1b9cdd0ab19 100644 --- a/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngineProvider.java +++ b/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfEngineProvider.java b/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfEngineProvider.java index d964ea5c295..f42f691d222 100644 --- a/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfEngineProvider.java +++ b/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/tensorrt/src/main/java/ai/djl/tensorrt/engine/TrtEngineProvider.java b/engines/tensorrt/src/main/java/ai/djl/tensorrt/engine/TrtEngineProvider.java index 05a7eceeb41..d92ed9e449d 100644 --- a/engines/tensorrt/src/main/java/ai/djl/tensorrt/engine/TrtEngineProvider.java +++ b/engines/tensorrt/src/main/java/ai/djl/tensorrt/engine/TrtEngineProvider.java @@ -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() { @@ -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(); } } diff --git a/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtEngineTest.java b/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtEngineTest.java index 96066b380e1..efd9d89e509 100644 --- a/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtEngineTest.java +++ b/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtEngineTest.java @@ -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"); diff --git a/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtNDManagerTest.java b/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtNDManagerTest.java index 24d734af54c..09001f0e2da 100644 --- a/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtNDManagerTest.java +++ b/engines/tensorrt/src/test/java/ai/djl/tensorrt/engine/TrtNDManagerTest.java @@ -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()) { diff --git a/engines/tensorrt/src/test/java/ai/djl/tensorrt/integration/TrtTest.java b/engines/tensorrt/src/test/java/ai/djl/tensorrt/integration/TrtTest.java index 105e057ba0a..99cbc6f763e 100644 --- a/engines/tensorrt/src/test/java/ai/djl/tensorrt/integration/TrtTest.java +++ b/engines/tensorrt/src/test/java/ai/djl/tensorrt/integration/TrtTest.java @@ -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()) { @@ -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()) { @@ -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(); diff --git a/engines/tflite/tflite-engine/src/main/java/ai/djl/tflite/engine/TfLiteEngineProvider.java b/engines/tflite/tflite-engine/src/main/java/ai/djl/tflite/engine/TfLiteEngineProvider.java index aa0fdb73d21..fb61551a3bf 100644 --- a/engines/tflite/tflite-engine/src/main/java/ai/djl/tflite/engine/TfLiteEngineProvider.java +++ b/engines/tflite/tflite-engine/src/main/java/ai/djl/tflite/engine/TfLiteEngineProvider.java @@ -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() { @@ -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(); } }