Repository: deepjavalibrary/djl
Pull request #3008 (2024-02-28 17:39:20)

[api] Fixes typo in CudaUtils
## Description ##

Brief description of what this PR is about

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here



Patch:
diff --git a/api/src/main/java/ai/djl/Model.java b/api/src/main/java/ai/djl/Model.java
index 1903ca392..dab256894 100644
--- a/api/src/main/java/ai/djl/Model.java
+++ b/api/src/main/java/ai/djl/Model.java
@@ -233,6 +233,36 @@ public interface Model extends AutoCloseable {
      */
     void setProperty(String key, String value);
 
+    /**
+     * Returns the property of the model based on property name.
+     *
+     * @param key the name of the property
+     * @param defValue the default value if key not found
+     * @return the value of the property
+     */
+    default int intProperty(String key, int defValue) {
+        String value = getProperty(key);
+        if (value == null || value.isEmpty()) {
+            return defValue;
+        }
+        return Integer.parseInt(value);
+    }
+
+    /**
+     * Returns the property of the model based on property name.
+     *
+     * @param key the name of the property
+     * @param defValue the default value if key not found
+     * @return the value of the property
+     */
+    default long longProperty(String key, long defValue) {
+        String value = getProperty(key);
+        if (value == null || value.isEmpty()) {
+            return defValue;
+        }
+        return Integer.parseInt(value);
+    }
+
     /**
      * Gets the {@link NDManager} from the model.
      *
diff --git a/api/src/main/java/ai/djl/util/cuda/CudaUtils.java b/api/src/main/java/ai/djl/util/cuda/CudaUtils.java
index b30a208f6..1de074ea6 100644
--- a/api/src/main/java/ai/djl/util/cuda/CudaUtils.java
+++ b/api/src/main/java/ai/djl/util/cuda/CudaUtils.java
@@ -57,7 +57,7 @@ public final class CudaUtils {
      */
     @SuppressWarnings("PMD.NonThreadSafeSingleton")
     public static int getGpuCount() {
-        if (Boolean.getBoolean("ai.djl.util.cuda.folk")) {
+        if (Boolean.getBoolean("ai.djl.util.cuda.fork")) {
             if (gpuInfo == null) {
                 gpuInfo = execute(-1); // NOPMD
             }
@@ -95,7 +95,7 @@ public final class CudaUtils {
      */
     @SuppressWarnings("PMD.NonThreadSafeSingleton")
     public static int getCudaVersion() {
-        if (Boolean.getBoolean("ai.djl.util.cuda.folk")) {
+        if (Boolean.getBoolean("ai.djl.util.cuda.fork")) {
             if (gpuInfo == null) {
                 gpuInfo = execute(-1);
             }
@@ -134,7 +134,7 @@ public final class CudaUtils {
      * @return the CUDA compute capability
      */
     public static String getComputeCapability(int device) {
-        if (Boolean.getBoolean("ai.djl.util.cuda.folk")) {
+        if (Boolean.getBoolean("ai.djl.util.cuda.fork")) {
             String[] ret = execute(device);
             if (ret.length != 3) {
                 throw new IllegalArgumentException(ret[0]);
@@ -168,7 +168,7 @@ public final class CudaUtils {
             throw new IllegalArgumentException("Only GPU device is allowed.");
         }
 
-        if (Boolean.getBoolean("ai.djl.util.cuda.folk")) {
+        if (Boolean.getBoolean("ai.djl.util.cuda.fork")) {
             String[] ret = execute(device.getDeviceId());
             if (ret.length != 3) {
                 throw new IllegalArgumentException(ret[0]);
@@ -229,7 +229,7 @@ public final class CudaUtils {
 
     private static CudaLibrary loadLibrary() {
         try {
-            if (Boolean.getBoolean("ai.djl.util.cuda.folk")) {
+            if (Boolean.getBoolean("ai.djl.util.cuda.fork")) {
                 return null;
             }
             if (System.getProperty("os.name").startsWith("Win")) {


Test patch:
diff --git a/api/src/test/java/ai/djl/translate/TranslatorTest.java b/api/src/test/java/ai/djl/translate/TranslatorTest.java
index 5ce63faa2..1a636af77 100644
--- a/api/src/test/java/ai/djl/translate/TranslatorTest.java
+++ b/api/src/test/java/ai/djl/translate/TranslatorTest.java
@@ -83,6 +83,10 @@ public class TranslatorTest {
                 Predictor<Image[], Classifications[]> predictor = model.newPredictor()) {
             Classifications[] res = predictor.predict(inputs);
             Assert.assertEquals(res.length, 2);
+            int intValue = model.intProperty("something", -1);
+            Assert.assertEquals(intValue, -1);
+            long longValue = model.longProperty("something", -1L);
+            Assert.assertEquals(longValue, -1L);
         }
     }
 }
diff --git a/api/src/test/java/ai/djl/util/cuda/CudaUtilsTest.java b/api/src/test/java/ai/djl/util/cuda/CudaUtilsTest.java
index a598d8482..a6ad7e521 100644
--- a/api/src/test/java/ai/djl/util/cuda/CudaUtilsTest.java
+++ b/api/src/test/java/ai/djl/util/cuda/CudaUtilsTest.java
@@ -50,12 +50,12 @@ public class CudaUtilsTest {
     }
 
     @Test
-    public void testCudaUtilsWithFolk() {
-        System.setProperty("ai.djl.util.cuda.folk", "true");
+    public void testCudaUtilsWithFork() {
+        System.setProperty("ai.djl.util.cuda.fork", "true");
         try {
             testCudaUtils();
         } finally {
-            System.clearProperty("ai.djl.util.cuda.folk");
+            System.clearProperty("ai.djl.util.cuda.fork");
         }
     }
 }