xgboost icon indicating copy to clipboard operation
xgboost copied to clipboard

[xgboost4j-spark] Error CHECK(batch.index != nullptr) at simple_csr_source.cc:44

Open fc1plusx opened this issue 10 years ago • 15 comments

Using xgboost4j-spark I had the above error when testing (transform method). I could speculate that this was related to the data containing an empty partition and/or too many small partitions, since apparently it was solved by repartitioning it. Nevertheless, I was unfortunately unable to replicate the issue in a minimal example with local toy datasets. Maybe this could also be related to #940. The error happens both when testing locally and on a EMR cluster.

Environment: xgboost version is the one available on the master as of 23 Nov 2016, scala 2.11.8, spark 2.0.1

fc1plusx avatar Nov 30 '16 11:11 fc1plusx

can you provide more information about the test you run? how many partitions? how to reproduce it?

CodingCat avatar Dec 01 '16 03:12 CodingCat

Sure! The original data is about 30-40 MB, with some empty partitions. After loading and some pre-processing it results into 150K elements divided into 288 partitions. When running transform directly, the program fails with the above error (see also the stack trace below). If I repartition to 287, or even to 288 (so just a shuffle of the data), it works nicely.

As I said in the first comment, unfortunately, I was not able to reproduce the issue with a small hand-crafted dataset and a toy program, but for this failing dataset, the error is really reproducible: it always happens, and it can always be solved by repartitioning.

ml.dmlc.xgboost4j.java.XGBoostError: [18:11:28] src/data/simple_csr_source.cc:44: Check failed: batch.index != nullptr at ml.dmlc.xgboost4j.java.JNIErrorHandle.checkCall(JNIErrorHandle.java:48) at ml.dmlc.xgboost4j.java.DMatrix.(DMatrix.java:68) at ml.dmlc.xgboost4j.scala.DMatrix.(DMatrix.scala:42) at ml.dmlc.xgboost4j.scala.spark.XGBoostModel$$anonfun$produceRowRDD$1.apply(XGBoostModel.scala:254) at ml.dmlc.xgboost4j.scala.spark.XGBoostModel$$anonfun$produceRowRDD$1.apply(XGBoostModel.scala:239) at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:785) at org.apache.spark.rdd.RDD$$anonfun$mapPartitions$1$$anonfun$apply$23.apply(RDD.scala:785) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD.iterator(RDD.scala:283) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD.iterator(RDD.scala:283) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD.iterator(RDD.scala:283) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD.iterator(RDD.scala:283) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD.iterator(RDD.scala:283) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD.iterator(RDD.scala:283) at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:38) at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:319) at org.apache.spark.rdd.RDD$$anonfun$8.apply(RDD.scala:332) at org.apache.spark.rdd.RDD$$anonfun$8.apply(RDD.scala:330) at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:951) at org.apache.spark.storage.BlockManager$$anonfun$doPutIterator$1.apply(BlockManager.scala:926) at org.apache.spark.storage.BlockManager.doPut(BlockManager.scala:866) at org.apache.spark.storage.BlockManager.doPutIterator(BlockManager.scala:926) at org.apache.spark.storage.BlockManager.getOrElseUpdate(BlockManager.scala:670) at org.apache.spark.rdd.RDD.getOrCompute(RDD.scala:330) at org.apache.spark.rdd.RDD.iterator(RDD.scala:281) at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:70) at org.apache.spark.scheduler.Task.run(Task.scala:86) at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:274) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

fc1plusx avatar Dec 01 '16 18:12 fc1plusx

I think it is a potential bug

in the current code base, there have been some if clauses to prevent processing empty partitions...so we shall not call native layer code with a empty partition

if possible, would you check what is the value of batch.label (it is an array), https://github.com/dmlc/xgboost/blob/master/src/data/simple_csr_source.cc#L38 ?

CodingCat avatar Dec 02 '16 05:12 CodingCat

Right before the failure, batch.label contains only zeros. Does this help in identifying the issue?

fc1plusx avatar Dec 02 '16 14:12 fc1plusx

Don't know if this helps but could be worth to mention: the last message in the log (after Spark retries the execute the task, and re-fails) is "rabit::Init is already called in this thread". This does not appear every time, but most of them.

fc1plusx avatar Dec 02 '16 15:12 fc1plusx

It's more than one zeros? Just like a partition of data?

CodingCat avatar Dec 02 '16 15:12 CodingCat

The size (batch.size) is more than one (don't remember the exact value, is it important?) and all the values in batch.label are 0.

fc1plusx avatar Dec 02 '16 15:12 fc1plusx

ok, I will check it

CodingCat avatar Dec 03 '16 04:12 CodingCat

@CodingCat Wondering what's the current state of this bug? I'm had the exact same error when trying to predict for one example

glennq avatar Jan 13 '17 21:01 glennq

@CodingCat I also have the same error on this simple example:

` import ml.dmlc.xgboost4j.scala.{XGBoost => XGBoostLocal, DMatrix} import ml.dmlc.xgboost4j.scala.spark.{XGBoost => XGBoostSpark, XGBoostRegressionModel} import org.apache.spark.ml.linalg.{Vector => MLVector, Vectors} import org.apache.spark.sql.Row

case class Test(features: MLVector, id: Int)

val booster = XGBoostLocal.loadModel("xgboost.model") val model = new XGBoostRegressionModel(booster)

val testDF = spark.createDataFrame(Seq(Test(Vectors.sparse(5000, Array.empty, Array.empty), 1)))

model.transform(testDF).show `

Here Test(Vectors.sparse(5000, Array.empty, Array.empty) means row of zeros. The following code evaluates without error:

` import ml.dmlc.xgboost4j.scala.{XGBoost => XGBoostLocal, DMatrix} import ml.dmlc.xgboost4j.scala.spark.{XGBoost => XGBoostSpark, XGBoostRegressionModel} import org.apache.spark.ml.linalg.{Vector => MLVector, Vectors} import org.apache.spark.sql.Row

case class Test(features: MLVector, id: Int)

val booster = XGBoostLocal.loadModel("xgboost.model") val model = new XGBoostRegressionModel(booster)

val testDF = spark.createDataFrame(Seq(Test(Vectors.sparse(5000, Array(1), Array(0.0)), 1)))

model.transform(testDF).show `
So, it seems to happen only if array of indices is empty

abelousova avatar Feb 03 '17 13:02 abelousova

I have also recently encountered this assertion. @abelousova is correct, in that the cause of this issue is attempting to pass data that is entirely sparse to XGBoost. Here is an even shorter unit test that I have added to my code that demonstrates the problem.

import ml.dmlc.xgboost4j.scala.spark.DataUtils
import org.apache.spark.ml.linalg.{SparseVector, Vector}
import org.testng.Assert.assertNotEquals
import org.testng.annotations.Test

class XGBoostReadSparseVectorTest {
  @Test
  def testConvertSparseVector() : Unit = {
    val features : Seq[Vector] = Seq(new SparseVector(size = 10, indices = Array[Int](), values = Array[Double]()))

    import DataUtils._
    val xgbIterator = DataUtils.fromSparkVectorToXGBoostPointsJava(features.iterator)

    // Check that the matrix was actually initialized
    val javaDMatrix = new ml.dmlc.xgboost4j.java.DMatrix(xgbIterator, null)
    assertNotEquals(javaDMatrix.getHandle, 0)
  }
}

This unit test will fail with the batch.index != nullptr assertion. I have been tracing the problem through to the underlying C++ code, and the following things happen. First, the Java DMatrix class calls native code through JNI that ends up in the native XGDMatrixCreateFromDataIter method in ./src/c_api/c_api.cc. This method creates a NativeDataIter parser(data_handle, **callback) and calls DMatrix::Create(&parser, scache).

The NativeDataIter class has a SetData method that does the following:

class NativeDataIter : public dmlc::Parser<uint32_t> {
  ...
  // callback to set the data
  void SetData(const XGBoostBatchCSR& batch) {
    ...
    if (batch.index != nullptr) {
      index_.insert(index_.end(), batch.index + offset_[0], batch.index + offset_.back());
    }
    ...
    block_.index = dmlc::BeginPtr(index_);
  }
}

// From: dmlc-core/include/dmlc/base.h, the dmlc::BeginPtr method does this:
template<typename T> inline T *BeginPtr(std::vector<T> &vec) {  // NOLINT(*)
  if (vec.size() == 0) {
    return NULL;
  } else {
    return &vec[0];
  }
}

So you can see that if you pass entirely sparse data to XGBoost, the NativeDataIter parser will get a batch of rows (that is entirely sparse), and will end up setting block_.index = NULL since the feature index on the batch is also null.

I think this code all makes complete sense. However, the null index ends up propagating through to src/data/simple_csr_source.cc, causing the assertion to fail.

convexquad avatar Apr 20 '17 19:04 convexquad

Basically, I think the solution to this issue is to remove the failing assertion in src/data/simple_csr_source.cc, i.e. comment out the line CHECK(batch.index != nullptr).

I think this assertion is invalid. When batch.index == NULL, it indicates that the batch of data is entirely sparse. This should be a valid case handled by XGBoost without error.

There is an argument to be made that users should never pass XGBoost entirely sparse data, as it is likely that this condition indicates that something is wrong with the data. However, this argument is not right. At LinkedIn, we are using trained XGBoost models to evaluate larger and larger incoming datasets.

Our data workflows often one-hot encode the incoming features, since the data is often fed into multiple machine learning models (most of which use non-XGBoost linear methods). Because the distribution of values used with the one-hot encoding tend to shift over time, it is often the case that the incoming features no longer match many of the one-hot encoded features used to train the model (and are thus dropped). Every once in a while, none of the one-hot features match the features used to train the model (and are thus all dropped), and so the input to the model ends up being entirely sparse. While it's true that the output of the model won't be very helpful, this should definitely NOT cause an assertion failure in production.

That's my argument for why dropping this assertion makes sense. Please note - this is as far as I have traced the code. Currently I don't believe that dropping this assertion can make other problems appear, but I am not sure about that! Perhaps others that know the code better can double-check.

I have posted (the very short) pull request https://github.com/dmlc/xgboost/pull/2213.

convexquad avatar Apr 20 '17 19:04 convexquad

@convexquad , thanks for finding it out

I am not sure about whether completely moving the assertion will break other things,

I didn't track into that part, but will changing the assertion to

CHECK(batch.size == 0 || batch.index != nullptr)

be safer?

CodingCat avatar Apr 20 '17 19:04 CodingCat

@CodingCat I don't think that changing the assertion to CHECK(batch.size == 0 || batch.index != nullptr) will help, because batch.size refers to the number of rows, while I think batch.index refers to the first column (feature) with dense values.

It is possible to give XGBoost a batch of several rows at once, but all with completely sparse data - this will trigger this assertion (but my claim is that it should not).

convexquad avatar Apr 21 '17 00:04 convexquad

I see, I thought size refers to vector length as in Spark, then your change makes sense

CodingCat avatar Apr 21 '17 00:04 CodingCat