Repository: deepjavalibrary/djl
Pull request #1571 (2022-04-14 00:37:59)

[xgb] Set default missing value to NaN
Upgrade XGBoost to 1.5.2

Change-Id: Ie47428f3c0a3dd5fadcb25ea05e655073a40f81f

## 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/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbNDManager.java b/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbNDManager.java
index c6443e2b0..87b6af06a 100644
--- a/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbNDManager.java
+++ b/engines/ml/xgboost/src/main/java/ai/djl/ml/xgboost/XgbNDManager.java
@@ -32,6 +32,8 @@ public class XgbNDManager extends BaseNDManager {
 
     private static final XgbNDManager SYSTEM_MANAGER = new SystemManager();
 
+    private float missingValue = Float.NaN;
+
     private XgbNDManager(NDManager parent, Device device) {
         super(parent, device);
     }
@@ -40,6 +42,15 @@ public class XgbNDManager extends BaseNDManager {
         return SYSTEM_MANAGER;
     }
 
+    /**
+     * Sets the default missing value.
+     *
+     * @param missingValue the default missing value
+     */
+    public void setMissingValue(float missingValue) {
+        this.missingValue = missingValue;
+    }
+
     /** {@inheritDoc} */
     @Override
     public ByteBuffer allocateDirect(int capacity) {
@@ -95,7 +106,7 @@ public class XgbNDManager extends BaseNDManager {
 
         if (data.isDirect() && data instanceof ByteBuffer) {
             // TODO: allow user to set missing value
-            long handle = JniUtils.createDMatrix(data, shape, 0.0f);
+            long handle = JniUtils.createDMatrix(data, shape, missingValue);
             return new XgbNDArray(this, alternativeManager, handle, shape, SparseFormat.DENSE);
         }
 
@@ -109,7 +120,7 @@ public class XgbNDManager extends BaseNDManager {
         ByteBuffer buf = allocateDirect(size);
         buf.asFloatBuffer().put((FloatBuffer) data);
         buf.rewind();
-        long handle = JniUtils.createDMatrix(buf, shape, 0.0f);
+        long handle = JniUtils.createDMatrix(buf, shape, missingValue);
         return new XgbNDArray(this, alternativeManager, handle, shape, SparseFormat.DENSE);
     }
 
diff --git a/gradle.properties b/gradle.properties
index e5805ecb5..a2efedbbe 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -19,7 +19,7 @@ paddlepaddle_version=2.0.2
 sentencepiece_version=0.1.95
 tokenizers_version=0.11.0
 fasttext_version=0.9.2
-xgboost_version=1.4.1
+xgboost_version=1.5.2
 
 commons_cli_version=1.5.0
 commons_compress_version=1.21


Test patch:
diff --git a/engines/ml/xgboost/src/test/java/ai/djl/ml/xgboost/XgbModelTest.java b/engines/ml/xgboost/src/test/java/ai/djl/ml/xgboost/XgbModelTest.java
index 97ccb3460..4310b3426 100644
--- a/engines/ml/xgboost/src/test/java/ai/djl/ml/xgboost/XgbModelTest.java
+++ b/engines/ml/xgboost/src/test/java/ai/djl/ml/xgboost/XgbModelTest.java
@@ -68,6 +68,7 @@ public class XgbModelTest {
 
         try (XgbNDManager manager =
                 (XgbNDManager) XgbNDManager.getSystemManager().newSubManager()) {
+            manager.setMissingValue(Float.NaN);
             NDArray zeros = manager.zeros(new Shape(1, 2));
             Assert.expectThrows(UnsupportedOperationException.class, zeros::toFloatArray);