xgboost icon indicating copy to clipboard operation
xgboost copied to clipboard

[jvm-packages] XGBoost Spark hang when nWorkers is set to greater than 1

Open titicaca opened this issue 10 years ago • 13 comments

We tried to run xgboost on local spark 1.5.1 and 1.6.1, but got the following issue:

when we set the nWorkers greater than 1 on local Spark 1.6.1, it stuck, tasks cannot be executed. But when nWorkers = 1, it works.

image

The most weird thing is that, on local Spark 1.5.1, if we set nWorker to 1 and 2, it can work. But when nWorker is greater than 2, it hangs.

Any idea about this problem? Should we limit nWorkers not greater than the number of nodes?

Our codes are as follows:

`

val sparkConf = new SparkConf().setAppName("XGBoost-spark-example").setMaster("local")
    .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
sparkConf.registerKryoClasses(Array(classOf[Booster]))
val sc = new SparkContext(sparkConf)
val filepath = "."
val data = sc.textFile(filepath + "dt.iris.data", 2)
val splits = data.randomSplit(Array(0.8, 0.2), seed = 100L)
val trainData = splits(0).map { line =>
  val parts = line.split(",")
  val label = parts(0).toDouble
  val features = Vectors.dense(parts.tail.map(x => x.toDouble))
  LabeledPoint(label, features);
}
val sep = ","
val configMapStr = List(
  "objective" -> "multi:softmax",
  "objective" -> "multi:softmax",
  "num_class" -> 3,
  "eval_metric" -> "mlogloss",
  "eta" -> 0.3,
  "max_depth" -> 2,
  "gamma" -> 0,
  "subsample" -> 1,
  "colsample_bytree" -> 1,
  "min_child_weight" -> 1).toMap
val model = XGBoost.train(trainData, configMapStr, 100, 1, null, null, false)

`

titicaca avatar Jun 14 '16 06:06 titicaca

@titicaca the number of xgboost workers means that how many parallel trainers (Spark tasks) you want in xgboost,

if you set it to 2, you have to ensure that your machine has at least two cores to run two parallel tasks (if you set a larger value of nthreads, it would be more than 2 cores)

CodingCat avatar Jun 14 '16 19:06 CodingCat

@CodingCat Thanks for the reply. But will it gets stuck when the number of tasks is greater than the total number of cores? It seems the tasks are hanging by rabit init.

titicaca avatar Jun 15 '16 02:06 titicaca

that's exactly the situation I described...rabit.init() hangs to wait for X trainers (tasks) to start

CodingCat avatar Jun 15 '16 02:06 CodingCat

Okay. Now I see. Thank you!

titicaca avatar Jun 15 '16 08:06 titicaca

Hi, sorry to post on this closed issue (seems the most appropriate one). Still I'm not 100% sure about how to set numWorkers in the distributed scenario. Let's say I have a cluster with 3 machines, each with 4 cores. Should I use numWorkers = 12, numWorkers = 3, or numWorkers = 4? Thanks!

fc1plusx avatar Nov 29 '16 09:11 fc1plusx

if you're sure about that you can run 12 tasks in parallel, you can set to 12,

CodingCat avatar Nov 29 '16 14:11 CodingCat

Many thanks for the quick reply! Unfortunately, I fear I'm not understanding your answer completely. I'm sure I've 4 cores per machine, and 3 machines in the cluster. Is there anything else I should worry about?

fc1plusx avatar Nov 29 '16 18:11 fc1plusx

no

CodingCat avatar Nov 30 '16 03:11 CodingCat

It would also work if you set nthread in XGBoost to 4, and configure spark.task.cpus in Spark to 4. Then, use numWorkers = 3, while launching 3 executors.

The level of parallelism is identical on paper, but in practice might be faster due to less Rabit synchronization etc.

xydrolase avatar Nov 30 '16 06:11 xydrolase

Thanks a lot! I tried your suggestion and indeed training seems slightly faster. I'm wondering if this is related only to less synchronisation or also to the different number of partitions (as data is repartitioned according to numWorkers before training starts).

fc1plusx avatar Dec 01 '16 17:12 fc1plusx

It depends on how much data you used for your training set. By using 3 workers, yes you will have less partitions, so each partition ends up with more training samples. But you have have 4 cores for crunching the data in each partition. If there are many samples, XGBoost will switch to quantile sketching to speed up the computation.

If you dataset is not gigantic, the speed-up you've noticed is most likely due to less synchronization between Rabit workers. Also, keep in mind that as you have more workers, the time for building each tree is determined by the slowest worker. If you have multiple worker nodes that have a large variance in computing power (or resources), the overall training time will be prolonged due to slower nodes.

In your case, if you have three executors, you can subsample your training data to about 1/3 of the original size, and train a model on a single node without using Spark, with 4 cores. It will give you an idea about how long does it take to train a model without synchronization, so that you can tune the parameters accordingly down the road.

xydrolase avatar Dec 01 '16 17:12 xydrolase

Many thanks for the explanation and the helpful suggestions!

fc1plusx avatar Dec 02 '16 15:12 fc1plusx

even when i tried with nWorkers = 1 the training gets stuck for me.

kamaldsingh avatar Jun 02 '17 11:06 kamaldsingh