diff --git a/CHANGES.md b/CHANGES.md index eb55fc747..b2346799f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -41,6 +41,9 @@ on going at master - Fixed possible problem of poisson regression for R. * Python module now throw exception instead of crash terminal when a parameter error happens. * Python module now has importance plot and tree plot functions. +* Python module now accepts different learning rates for each boosting round. +* Python now allows model training continuation from previously saved model. +* Additional parameters added for sklearn wrapper * Java api is ready for use * Added more test cases and continuous integration to make each build more robust * Improvements in sklearn compatible module diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 6233f7ce0..d87b4c529 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -13,6 +13,8 @@ Committers are people who have made substantial contribution to the project and - Bing is the original creator of xgboost python package and currently the maintainer of [XGBoost.jl](https://github.com/antinucleon/XGBoost.jl). * [Michael Benesty](https://github.com/pommedeterresautee) - Micheal is a lawyer, data scientist in France, he is the creator of xgboost interactive analysis module in R. +* [Yuan Tang](https://github.com/terrytangyuan) + - Yuan is a data scientist in Chicago, US. He contributed mostly in R and Python packages. Become a Comitter ----------------- @@ -34,7 +36,6 @@ List of Contributors * [Zygmunt Zając](https://github.com/zygmuntz) - Zygmunt is the master behind the early stopping feature frequently used by kagglers. * [Ajinkya Kale](https://github.com/ajkl) -* [Yuan Tang](https://github.com/terrytangyuan) * [Boliang Chen](https://github.com/cblsjtu) * [Vadim Khotilovich](https://github.com/khotilov) * [Yangqing Men](https://github.com/yanqingmen) @@ -49,5 +50,9 @@ List of Contributors - Masaaki is the initial creator of xgboost python plotting module. * [Hongliang Liu](https://github.com/phunterlau) - Hongliang is the maintainer of xgboost python PyPI package for pip installation. +* [daiyl0320](https://github.com/daiyl0320) + - daiyl0320 contributed patch to xgboost distributed version more robust, and scales stably on TB scale datasets. * [Huayi Zhang](https://github.com/irachex) * [Johan Manders](https://github.com/johanmanders) +* [yoori](https://github.com/yoori) +* [Mathias Müller](https://github.com/far0n) diff --git a/Makefile b/Makefile index 6685b0c6d..9474ce31c 100644 --- a/Makefile +++ b/Makefile @@ -189,7 +189,6 @@ pythonpack: cp -r multi-node xgboost-deploy/xgboost cp -r windows xgboost-deploy/xgboost cp -r src xgboost-deploy/xgboost - #make python pythonbuild: diff --git a/R-package/R/getinfo.xgb.DMatrix.R b/R-package/R/getinfo.xgb.DMatrix.R index 26523699a..dc734bce1 100644 --- a/R-package/R/getinfo.xgb.DMatrix.R +++ b/R-package/R/getinfo.xgb.DMatrix.R @@ -35,7 +35,7 @@ getinfo <- function(object, ...){ #' @param ... other parameters #' @rdname getinfo #' @method getinfo xgb.DMatrix -setMethod("getinfo", signature = "xgb.DMatrix", +setMethod("getinfo", signature = "xgb.DMatrix", definition = function(object, name) { if (typeof(name) != "character") { stop("xgb.getinfo: name must be character") @@ -43,7 +43,7 @@ setMethod("getinfo", signature = "xgb.DMatrix", if (class(object) != "xgb.DMatrix") { stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix") } - if (name != "label" && name != "weight" && + if (name != "label" && name != "weight" && name != "base_margin" && name != "nrow") { stop(paste("xgb.getinfo: unknown info name", name)) } @@ -54,4 +54,3 @@ setMethod("getinfo", signature = "xgb.DMatrix", } return(ret) }) - diff --git a/R-package/R/predict.xgb.Booster.R b/R-package/R/predict.xgb.Booster.R index 902260258..432581e76 100644 --- a/R-package/R/predict.xgb.Booster.R +++ b/R-package/R/predict.xgb.Booster.R @@ -30,8 +30,8 @@ setClass("xgb.Booster", #' pred <- predict(bst, test$data) #' @export #' -setMethod("predict", signature = "xgb.Booster", - definition = function(object, newdata, missing = NA, +setMethod("predict", signature = "xgb.Booster", + definition = function(object, newdata, missing = NA, outputmargin = FALSE, ntreelimit = NULL, predleaf = FALSE) { if (class(object) != "xgb.Booster"){ stop("predict: model in prediction must be of class xgb.Booster") @@ -48,14 +48,14 @@ setMethod("predict", signature = "xgb.Booster", stop("predict: ntreelimit must be equal to or greater than 1") } } - option = 0 + option <- 0 if (outputmargin) { option <- option + 1 } if (predleaf) { option <- option + 2 } - ret <- .Call("XGBoosterPredict_R", object$handle, newdata, as.integer(option), + ret <- .Call("XGBoosterPredict_R", object$handle, newdata, as.integer(option), as.integer(ntreelimit), PACKAGE = "xgboost") if (predleaf){ len <- getinfo(newdata, "nrow") @@ -68,4 +68,3 @@ setMethod("predict", signature = "xgb.Booster", } return(ret) }) - diff --git a/R-package/R/predict.xgb.Booster.handle.R b/R-package/R/predict.xgb.Booster.handle.R index 685318f12..3e4013b75 100644 --- a/R-package/R/predict.xgb.Booster.handle.R +++ b/R-package/R/predict.xgb.Booster.handle.R @@ -5,15 +5,14 @@ #' @param object Object of class "xgb.Boost.handle" #' @param ... Parameters pass to \code{predict.xgb.Booster} #' -setMethod("predict", signature = "xgb.Booster.handle", +setMethod("predict", signature = "xgb.Booster.handle", definition = function(object, ...) { if (class(object) != "xgb.Booster.handle"){ stop("predict: model in prediction must be of class xgb.Booster.handle") } - + bst <- xgb.handleToBooster(object) - - ret = predict(bst, ...) + + ret <- predict(bst, ...) return(ret) }) - diff --git a/R-package/R/setinfo.xgb.DMatrix.R b/R-package/R/setinfo.xgb.DMatrix.R index 61019d8e2..4bee161b7 100644 --- a/R-package/R/setinfo.xgb.DMatrix.R +++ b/R-package/R/setinfo.xgb.DMatrix.R @@ -32,7 +32,7 @@ setinfo <- function(object, ...){ #' @param ... other parameters #' @rdname setinfo #' @method setinfo xgb.DMatrix -setMethod("setinfo", signature = "xgb.DMatrix", +setMethod("setinfo", signature = "xgb.DMatrix", definition = function(object, name, info) { xgb.setinfo(object, name, info) }) diff --git a/R-package/R/slice.xgb.DMatrix.R b/R-package/R/slice.xgb.DMatrix.R index b70a8ee92..3b025e1dd 100644 --- a/R-package/R/slice.xgb.DMatrix.R +++ b/R-package/R/slice.xgb.DMatrix.R @@ -23,19 +23,19 @@ slice <- function(object, ...){ #' @param ... other parameters #' @rdname slice #' @method slice xgb.DMatrix -setMethod("slice", signature = "xgb.DMatrix", +setMethod("slice", signature = "xgb.DMatrix", definition = function(object, idxset, ...) { if (class(object) != "xgb.DMatrix") { stop("slice: first argument dtrain must be xgb.DMatrix") } - ret <- .Call("XGDMatrixSliceDMatrix_R", object, idxset, + ret <- .Call("XGDMatrixSliceDMatrix_R", object, idxset, PACKAGE = "xgboost") - + attr_list <- attributes(object) nr <- xgb.numrow(object) len <- sapply(attr_list,length) - ind <- which(len==nr) - if (length(ind)>0) { + ind <- which(len == nr) + if (length(ind) > 0) { nms <- names(attr_list)[ind] for (i in 1:length(ind)) { attr(ret,nms[i]) <- attr(object,nms[i])[idxset] diff --git a/R-package/R/utils.R b/R-package/R/utils.R index eecc5e260..b4f4a371f 100644 --- a/R-package/R/utils.R +++ b/R-package/R/utils.R @@ -1,4 +1,4 @@ -#' @importClassesFrom Matrix dgCMatrix dgeMatrix + #' @importClassesFrom Matrix dgCMatrix dgeMatrix #' @import methods # depends on matrix @@ -15,30 +15,30 @@ xgb.setinfo <- function(dmat, name, info) { stop("xgb.setinfo: first argument dtrain must be xgb.DMatrix") } if (name == "label") { - if (length(info)!=xgb.numrow(dmat)) + if (length(info) != xgb.numrow(dmat)) stop("The length of labels must equal to the number of rows in the input data") - .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), PACKAGE = "xgboost") return(TRUE) } if (name == "weight") { - if (length(info)!=xgb.numrow(dmat)) + if (length(info) != xgb.numrow(dmat)) stop("The length of weights must equal to the number of rows in the input data") - .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), PACKAGE = "xgboost") return(TRUE) } if (name == "base_margin") { # if (length(info)!=xgb.numrow(dmat)) # stop("The length of base margin must equal to the number of rows in the input data") - .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), + .Call("XGDMatrixSetInfo_R", dmat, name, as.numeric(info), PACKAGE = "xgboost") return(TRUE) } if (name == "group") { - if (sum(info)!=xgb.numrow(dmat)) + if (sum(info) != xgb.numrow(dmat)) stop("The sum of groups must equal to the number of rows in the input data") - .Call("XGDMatrixSetInfo_R", dmat, name, as.integer(info), + .Call("XGDMatrixSetInfo_R", dmat, name, as.integer(info), PACKAGE = "xgboost") return(TRUE) } @@ -68,7 +68,7 @@ xgb.Booster <- function(params = list(), cachelist = list(), modelfile = NULL) { if (typeof(modelfile) == "character") { .Call("XGBoosterLoadModel_R", handle, modelfile, PACKAGE = "xgboost") } else if (typeof(modelfile) == "raw") { - .Call("XGBoosterLoadModelFromRaw_R", handle, modelfile, PACKAGE = "xgboost") + .Call("XGBoosterLoadModelFromRaw_R", handle, modelfile, PACKAGE = "xgboost") } else { stop("xgb.Booster: modelfile must be character or raw vector") } @@ -122,7 +122,7 @@ xgb.get.DMatrix <- function(data, label = NULL, missing = NA, weight = NULL) { } else if (inClass == "xgb.DMatrix") { dtrain <- data } else if (inClass == "data.frame") { - stop("xgboost only support numerical matrix input, + stop("xgboost only support numerical matrix input, use 'data.frame' to transform the data.") } else { stop("xgboost: Invalid input of data") @@ -142,8 +142,7 @@ xgb.iter.boost <- function(booster, dtrain, gpair) { if (class(dtrain) != "xgb.DMatrix") { stop("xgb.iter.update: second argument must be type xgb.DMatrix") } - .Call("XGBoosterBoostOneIter_R", booster, dtrain, gpair$grad, gpair$hess, - PACKAGE = "xgboost") + .Call("XGBoosterBoostOneIter_R", booster, dtrain, gpair$grad, gpair$hess, PACKAGE = "xgboost") return(TRUE) } @@ -157,9 +156,9 @@ xgb.iter.update <- function(booster, dtrain, iter, obj = NULL) { } if (is.null(obj)) { - .Call("XGBoosterUpdateOneIter_R", booster, as.integer(iter), dtrain, + .Call("XGBoosterUpdateOneIter_R", booster, as.integer(iter), dtrain, PACKAGE = "xgboost") - } else { + } else { pred <- predict(booster, dtrain) gpair <- obj(pred, dtrain) succ <- xgb.iter.boost(booster, dtrain, gpair) @@ -190,7 +189,7 @@ xgb.iter.eval <- function(booster, watchlist, iter, feval = NULL, prediction = F } evnames <- append(evnames, names(w)) } - msg <- .Call("XGBoosterEvalOneIter_R", booster, as.integer(iter), watchlist, + msg <- .Call("XGBoosterEvalOneIter_R", booster, as.integer(iter), watchlist, evnames, PACKAGE = "xgboost") } else { msg <- paste("[", iter, "]", sep="") @@ -248,21 +247,21 @@ xgb.cv.mknfold <- function(dall, nfold, param, stratified, folds) { if (length(unique(y)) <= 5) y <- factor(y) } folds <- xgb.createFolds(y, nfold) - } else { + } else { # make simple non-stratified folds kstep <- length(randidx) %/% nfold folds <- list() - for (i in 1:(nfold-1)) { - folds[[i]] = randidx[1:kstep] - randidx = setdiff(randidx, folds[[i]]) + for (i in 1:(nfold - 1)) { + folds[[i]] <- randidx[1:kstep] + randidx <- setdiff(randidx, folds[[i]]) } - folds[[nfold]] = randidx + folds[[nfold]] <- randidx } } ret <- list() for (k in 1:nfold) { dtest <- slice(dall, folds[[k]]) - didx = c() + didx <- c() for (i in 1:nfold) { if (i != k) { didx <- append(didx, folds[[i]]) @@ -270,7 +269,7 @@ xgb.cv.mknfold <- function(dall, nfold, param, stratified, folds) { } dtrain <- slice(dall, didx) bst <- xgb.Booster(param, list(dtrain, dtest)) - watchlist = list(train=dtrain, test=dtest) + watchlist <- list(train=dtrain, test=dtest) ret[[k]] <- list(dtrain=dtrain, booster=bst, watchlist=watchlist, index=folds[[k]]) } return (ret) @@ -283,7 +282,7 @@ xgb.cv.aggcv <- function(res, showsd = TRUE) { kv <- strsplit(header[i], ":")[[1]] ret <- paste(ret, "\t", kv[1], ":", sep="") stats <- c() - stats[1] <- as.numeric(kv[2]) + stats[1] <- as.numeric(kv[2]) for (j in 2:length(res)) { tkv <- strsplit(res[[j]][i], ":")[[1]] stats[j] <- as.numeric(tkv[2]) @@ -311,9 +310,9 @@ xgb.createFolds <- function(y, k = 10) ## At most, we will use quantiles. If the sample ## is too small, we just do regular unstratified ## CV - cuts <- floor(length(y)/k) - if(cuts < 2) cuts <- 2 - if(cuts > 5) cuts <- 5 + cuts <- floor(length(y) / k) + if (cuts < 2) cuts <- 2 + if (cuts > 5) cuts <- 5 y <- cut(y, unique(stats::quantile(y, probs = seq(0, 1, length = cuts))), include.lowest = TRUE) @@ -325,7 +324,7 @@ xgb.createFolds <- function(y, k = 10) y <- factor(as.character(y)) numInClass <- table(y) foldVector <- vector(mode = "integer", length(y)) - + ## For each class, balance the fold allocation as far ## as possible, then resample the remainder. ## The final assignment of folds is also randomized. diff --git a/R-package/R/xgb.DMatrix.R b/R-package/R/xgb.DMatrix.R index 970fab394..20a3276c0 100644 --- a/R-package/R/xgb.DMatrix.R +++ b/R-package/R/xgb.DMatrix.R @@ -20,26 +20,26 @@ #' xgb.DMatrix <- function(data, info = list(), missing = NA, ...) { if (typeof(data) == "character") { - handle <- .Call("XGDMatrixCreateFromFile_R", data, as.integer(FALSE), + handle <- .Call("XGDMatrixCreateFromFile_R", data, as.integer(FALSE), PACKAGE = "xgboost") } else if (is.matrix(data)) { - handle <- .Call("XGDMatrixCreateFromMat_R", data, missing, + handle <- .Call("XGDMatrixCreateFromMat_R", data, missing, PACKAGE = "xgboost") } else if (class(data) == "dgCMatrix") { - handle <- .Call("XGDMatrixCreateFromCSC_R", data@p, data@i, data@x, + handle <- .Call("XGDMatrixCreateFromCSC_R", data@p, data@i, data@x, PACKAGE = "xgboost") } else { - stop(paste("xgb.DMatrix: does not support to construct from ", + stop(paste("xgb.DMatrix: does not support to construct from ", typeof(data))) } dmat <- structure(handle, class = "xgb.DMatrix") - + info <- append(info, list(...)) - if (length(info) == 0) + if (length(info) == 0) return(dmat) for (i in 1:length(info)) { p <- info[i] xgb.setinfo(dmat, names(p), p[[1]]) } return(dmat) -} +} diff --git a/R-package/R/xgb.DMatrix.save.R b/R-package/R/xgb.DMatrix.save.R index d58dc09de..7a9ac611d 100644 --- a/R-package/R/xgb.DMatrix.save.R +++ b/R-package/R/xgb.DMatrix.save.R @@ -18,10 +18,10 @@ xgb.DMatrix.save <- function(DMatrix, fname) { stop("xgb.save: fname must be character") } if (class(DMatrix) == "xgb.DMatrix") { - .Call("XGDMatrixSaveBinary_R", DMatrix, fname, as.integer(FALSE), + .Call("XGDMatrixSaveBinary_R", DMatrix, fname, as.integer(FALSE), PACKAGE = "xgboost") return(TRUE) } stop("xgb.DMatrix.save: the input must be xgb.DMatrix") return(FALSE) -} +} diff --git a/R-package/R/xgb.cv.R b/R-package/R/xgb.cv.R index 9811bba38..5f964c4f8 100644 --- a/R-package/R/xgb.cv.R +++ b/R-package/R/xgb.cv.R @@ -91,15 +91,15 @@ #' print(history) #' @export #' -xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing = NA, - prediction = FALSE, showsd = TRUE, metrics=list(), +xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing = NA, + prediction = FALSE, showsd = TRUE, metrics=list(), obj = NULL, feval = NULL, stratified = TRUE, folds = NULL, verbose = T, print.every.n=1L, early.stop.round = NULL, maximize = NULL, ...) { if (typeof(params) != "list") { stop("xgb.cv: first argument params must be list") } if(!is.null(folds)) { - if(class(folds)!="list" | length(folds) < 2) { + if(class(folds) != "list" | length(folds) < 2) { stop("folds must be a list with 2 or more elements that are vectors of indices for each CV-fold") } nfold <- length(folds) @@ -108,33 +108,33 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing = stop("nfold must be bigger than 1") } dtrain <- xgb.get.DMatrix(data, label, missing) - dot.params = list(...) - nms.params = names(params) - nms.dot.params = names(dot.params) - if (length(intersect(nms.params,nms.dot.params))>0) + dot.params <- list(...) + nms.params <- names(params) + nms.dot.params <- names(dot.params) + if (length(intersect(nms.params,nms.dot.params)) > 0) stop("Duplicated defined term in parameters. Please check your list of params.") params <- append(params, dot.params) params <- append(params, list(silent=1)) for (mc in metrics) { params <- append(params, list("eval_metric"=mc)) } - + # customized objective and evaluation metric interface if (!is.null(params$objective) && !is.null(obj)) stop("xgb.cv: cannot assign two different objectives") if (!is.null(params$objective)) - if (class(params$objective)=='function') { - obj = params$objective - params[['objective']] = NULL + if (class(params$objective) == 'function') { + obj <- params$objective + params[['objective']] <- NULL } # if (!is.null(params$eval_metric) && !is.null(feval)) # stop("xgb.cv: cannot assign two different evaluation metrics") if (!is.null(params$eval_metric)) - if (class(params$eval_metric)=='function') { - feval = params$eval_metric - params[['eval_metric']] = NULL + if (class(params$eval_metric) == 'function') { + feval <- params$eval_metric + params[['eval_metric']] <- NULL } - + # Early Stopping if (!is.null(early.stop.round)){ if (!is.null(feval) && is.null(maximize)) @@ -144,39 +144,39 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing = if (is.null(maximize)) { if (params$eval_metric %in% c('rmse','logloss','error','merror','mlogloss')) { - maximize = FALSE + maximize <- FALSE } else { - maximize = TRUE + maximize <- TRUE } } - + if (maximize) { - bestScore = 0 + bestScore <- 0 } else { - bestScore = Inf + bestScore <- Inf } - bestInd = 0 - earlyStopflag = FALSE - - if (length(metrics)>1) + bestInd <- 0 + earlyStopflag <- FALSE + + if (length(metrics) > 1) warning('Only the first metric is used for early stopping process.') } - + xgb_folds <- xgb.cv.mknfold(dtrain, nfold, params, stratified, folds) - obj_type = params[['objective']] - mat_pred = FALSE - if (!is.null(obj_type) && obj_type=='multi:softprob') + obj_type <- params[['objective']] + mat_pred <- FALSE + if (!is.null(obj_type) && obj_type == 'multi:softprob') { - num_class = params[['num_class']] + num_class <- params[['num_class']] if (is.null(num_class)) stop('must set num_class to use softmax') predictValues <- matrix(0,xgb.numrow(dtrain),num_class) - mat_pred = TRUE + mat_pred <- TRUE } else predictValues <- rep(0,xgb.numrow(dtrain)) history <- c() - print.every.n = max(as.integer(print.every.n), 1L) + print.every.n <- max(as.integer(print.every.n), 1L) for (i in 1:nrounds) { msg <- list() for (k in 1:nfold) { @@ -187,62 +187,60 @@ xgb.cv <- function(params=list(), data, nrounds, nfold, label = NULL, missing = ret <- xgb.cv.aggcv(msg, showsd) history <- c(history, ret) if(verbose) - if (0==(i-1L)%%print.every.n) + if (0 == (i - 1L) %% print.every.n) cat(ret, "\n", sep="") - + # early_Stopping if (!is.null(early.stop.round)){ - score = strsplit(ret,'\\s+')[[1]][1+length(metrics)+2] - score = strsplit(score,'\\+|:')[[1]][[2]] - score = as.numeric(score) - if ((maximize && score>bestScore) || (!maximize && score bestScore) || (!maximize && score < bestScore)) { + bestScore <- score + bestInd <- i } else { - if (i-bestInd>=early.stop.round) { - earlyStopflag = TRUE + if (i - bestInd >= early.stop.round) { + earlyStopflag <- TRUE cat('Stopping. Best iteration:',bestInd) break } } } - } - + if (prediction) { for (k in 1:nfold) { - fd = xgb_folds[[k]] + fd <- xgb_folds[[k]] if (!is.null(early.stop.round) && earlyStopflag) { - res = xgb.iter.eval(fd$booster, fd$watchlist, bestInd - 1, feval, prediction) + res <- xgb.iter.eval(fd$booster, fd$watchlist, bestInd - 1, feval, prediction) } else { - res = xgb.iter.eval(fd$booster, fd$watchlist, nrounds - 1, feval, prediction) + res <- xgb.iter.eval(fd$booster, fd$watchlist, nrounds - 1, feval, prediction) } if (mat_pred) { - pred_mat = matrix(res[[2]],num_class,length(fd$index)) - predictValues[fd$index,] = t(pred_mat) + pred_mat <- matrix(res[[2]],num_class,length(fd$index)) + predictValues[fd$index,] <- t(pred_mat) } else { - predictValues[fd$index] = res[[2]] + predictValues[fd$index] <- res[[2]] } } } - - + colnames <- str_split(string = history[1], pattern = "\t")[[1]] %>% .[2:length(.)] %>% str_extract(".*:") %>% str_replace(":","") %>% str_replace("-", ".") colnamesMean <- paste(colnames, "mean") if(showsd) colnamesStd <- paste(colnames, "std") - + colnames <- c() if(showsd) for(i in 1:length(colnamesMean)) colnames <- c(colnames, colnamesMean[i], colnamesStd[i]) else colnames <- colnamesMean - + type <- rep(x = "numeric", times = length(colnames)) dt <- utils::read.table(text = "", colClasses = type, col.names = colnames) %>% as.data.table split <- str_split(string = history, pattern = "\t") - - for(line in split) dt <- line[2:length(line)] %>% str_extract_all(pattern = "\\d*\\.+\\d*") %>% unlist %>% as.numeric %>% as.list %>% {rbindlist(list(dt, .), use.names = F, fill = F)} - + + for(line in split) dt <- line[2:length(line)] %>% str_extract_all(pattern = "\\d*\\.+\\d*") %>% unlist %>% as.numeric %>% as.list %>% {rbindlist( list( dt, .), use.names = F, fill = F)} + if (prediction) { - return(list(dt = dt,pred = predictValues)) + return( list( dt = dt,pred = predictValues)) } return(dt) } diff --git a/R-package/R/xgb.dump.R b/R-package/R/xgb.dump.R index fae1c7d2b..856ec0888 100644 --- a/R-package/R/xgb.dump.R +++ b/R-package/R/xgb.dump.R @@ -49,13 +49,13 @@ xgb.dump <- function(model = NULL, fname = NULL, fmap = "", with.stats=FALSE) { if (!(class(fmap) %in% c("character", "NULL") && length(fname) <= 1)) { stop("fmap: argument must be type character (when provided)") } - + longString <- .Call("XGBoosterDumpModel_R", model$handle, fmap, as.integer(with.stats), PACKAGE = "xgboost") - + dt <- fread(paste(longString, collapse = ""), sep = "\n", header = F) setnames(dt, "Lines") - + if(is.null(fname)) { result <- dt[Lines != "0"][, Lines := str_replace(Lines, "^\t+", "")][Lines != ""][, paste(Lines)] return(result) diff --git a/R-package/R/xgb.importance.R b/R-package/R/xgb.importance.R index f7696d53e..478438a79 100644 --- a/R-package/R/xgb.importance.R +++ b/R-package/R/xgb.importance.R @@ -66,42 +66,42 @@ #' xgb.importance(train$data@@Dimnames[[2]], model = bst, data = train$data, label = train$label) #' #' @export -xgb.importance <- function(feature_names = NULL, filename_dump = NULL, model = NULL, data = NULL, label = NULL, target = function(x) ((x + label) == 2)){ - if (!class(feature_names) %in% c("character", "NULL")) { +xgb.importance <- function(feature_names = NULL, filename_dump = NULL, model = NULL, data = NULL, label = NULL, target = function(x) ( (x + label) == 2)){ + if (!class(feature_names) %in% c("character", "NULL")) { stop("feature_names: Has to be a vector of character or NULL if the model dump already contains feature name. Look at this function documentation to see where to get feature names.") } - + if (!(class(filename_dump) %in% c("character", "NULL") && length(filename_dump) <= 1)) { stop("filename_dump: Has to be a path to the model dump file.") } - + if (!class(model) %in% c("xgb.Booster", "NULL")) { stop("model: Has to be an object of class xgb.Booster model generaged by the xgb.train function.") } - - if((is.null(data) & !is.null(label)) |(!is.null(data) & is.null(label))) { + + if((is.null(data) & !is.null(label)) | (!is.null(data) & is.null(label))) { stop("data/label: Provide the two arguments if you want co-occurence computation or none of them if you are not interested but not one of them only.") } - + if(class(label) == "numeric"){ if(sum(label == 0) / length(label) > 0.5) label <- as(label, "sparseVector") } - + if(is.null(model)){ - text <- readLines(filename_dump) + text <- readLines(filename_dump) } else { text <- xgb.dump(model = model, with.stats = T) - } - + } + if(text[2] == "bias:"){ result <- readLines(filename_dump) %>% linearDump(feature_names, .) if(!is.null(data) | !is.null(label)) warning("data/label: these parameters should only be provided with decision tree based models.") } else { result <- treeDump(feature_names, text = text, keepDetail = !is.null(data)) - + # Co-occurence computation if(!is.null(data) & !is.null(label) & nrow(result) > 0) { - # Take care of missing column + # Take care of missing column a <- data[, result[MissingNo == T,Feature], drop=FALSE] != 0 # Bind the two Matrix and reorder columns c <- data[, result[MissingNo == F,Feature], drop=FALSE] %>% cBind(a,.) %>% .[,result[,Feature]] @@ -109,23 +109,23 @@ xgb.importance <- function(feature_names = NULL, filename_dump = NULL, model = N # Apply split d <- data[, result[,Feature], drop=FALSE] < as.numeric(result[,Split]) apply(c & d, 2, . %>% target %>% sum) -> vec - - result <- result[, "RealCover":= as.numeric(vec), with = F][, "RealCover %" := RealCover / sum(label)][,MissingNo:=NULL] - } + + result <- result[, "RealCover" := as.numeric(vec), with = F][, "RealCover %" := RealCover / sum(label)][,MissingNo := NULL] + } } result } treeDump <- function(feature_names, text, keepDetail){ if(keepDetail) groupBy <- c("Feature", "Split", "MissingNo") else groupBy <- "Feature" - - result <- xgb.model.dt.tree(feature_names = feature_names, text = text)[,"MissingNo":= Missing == No ][Feature!="Leaf",.(Gain = sum(Quality), Cover = sum(Cover), Frequence = .N), by = groupBy, with = T][,`:=`(Gain = Gain/sum(Gain), Cover = Cover/sum(Cover), Frequence = Frequence/sum(Frequence))][order(Gain, decreasing = T)] - - result + + result <- xgb.model.dt.tree(feature_names = feature_names, text = text)[,"MissingNo" := Missing == No ][Feature != "Leaf",.(Gain = sum(Quality), Cover = sum(Cover), Frequence = .N), by = groupBy, with = T][,`:=`(Gain = Gain / sum(Gain), Cover = Cover / sum(Cover), Frequence = Frequence / sum(Frequence))][order(Gain, decreasing = T)] + + result } linearDump <- function(feature_names, text){ - which(text == "weight:") %>% {a=.+1;text[a:length(text)]} %>% as.numeric %>% data.table(Feature = feature_names, Weight = .) + which(text == "weight:") %>% {a =. + 1; text[a:length(text)]} %>% as.numeric %>% data.table(Feature = feature_names, Weight = .) } # Avoid error messages during CRAN check. diff --git a/R-package/R/xgb.load.R b/R-package/R/xgb.load.R index b69a719cf..2a2598dd8 100644 --- a/R-package/R/xgb.load.R +++ b/R-package/R/xgb.load.R @@ -17,9 +17,9 @@ #' @export #' xgb.load <- function(modelfile) { - if (is.null(modelfile)) + if (is.null(modelfile)) stop("xgb.load: modelfile cannot be NULL") - + handle <- xgb.Booster(modelfile = modelfile) # re-use modelfile if it is raw so we donot need to serialize if (typeof(modelfile) == "raw") { @@ -29,4 +29,4 @@ xgb.load <- function(modelfile) { } bst <- xgb.Booster.check(bst) return(bst) -} +} diff --git a/R-package/R/xgb.model.dt.tree.R b/R-package/R/xgb.model.dt.tree.R index d083566a5..882ac6c1f 100644 --- a/R-package/R/xgb.model.dt.tree.R +++ b/R-package/R/xgb.model.dt.tree.R @@ -56,8 +56,8 @@ #' #' @export xgb.model.dt.tree <- function(feature_names = NULL, filename_dump = NULL, model = NULL, text = NULL, n_first_tree = NULL){ - - if (!class(feature_names) %in% c("character", "NULL")) { + + if (!class(feature_names) %in% c("character", "NULL")) { stop("feature_names: Has to be a vector of character or NULL if the model dump already contains feature name. Look at this function documentation to see where to get feature names.") } if (!(class(filename_dump) %in% c("character", "NULL") && length(filename_dump) <= 1)) { @@ -67,59 +67,59 @@ xgb.model.dt.tree <- function(feature_names = NULL, filename_dump = NULL, model } else if(is.null(filename_dump) && is.null(model) && is.null(text)){ stop("filename_dump & model & text: no path to dump model, no model, no text dump, have been provided.") } - + if (!class(model) %in% c("xgb.Booster", "NULL")) { stop("model: Has to be an object of class xgb.Booster model generaged by the xgb.train function.") } - - if (!class(text) %in% c("character", "NULL")) { + + if (!class(text) %in% c("character", "NULL")) { stop("text: Has to be a vector of character or NULL if a path to the model dump has already been provided.") } - + if (!class(n_first_tree) %in% c("numeric", "NULL") | length(n_first_tree) > 1) { stop("n_first_tree: Has to be a numeric vector of size 1.") } - + if(!is.null(model)){ - text = xgb.dump(model = model, with.stats = T) + text <- xgb.dump(model = model, with.stats = T) } else if(!is.null(filename_dump)){ - text <- readLines(filename_dump) %>% str_trim(side = "both") + text <- readLines(filename_dump) %>% str_trim(side = "both") } - - position <- str_match(text, "booster") %>% is.na %>% not %>% which %>% c(length(text)+1) - + + position <- str_match(text, "booster") %>% is.na %>% not %>% which %>% c(length(text) + 1) + extract <- function(x, pattern) str_extract(x, pattern) %>% str_split("=") %>% lapply(function(x) x[2] %>% as.numeric) %>% unlist - + n_round <- min(length(position) - 1, n_first_tree) - + addTreeId <- function(x, i) paste(i,x,sep = "-") - + allTrees <- data.table() - - anynumber_regex<-"[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" - for(i in 1:n_round){ - - tree <- text[(position[i]+1):(position[i+1]-1)] - + + anynumber_regex <- "[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" + for (i in 1:n_round){ + + tree <- text[(position[i] + 1):(position[i + 1] - 1)] + # avoid tree made of a leaf only (no split) - if(length(tree) <2) next - - treeID <- i-1 - + if(length(tree) < 2) next + + treeID <- i - 1 + notLeaf <- str_match(tree, "leaf") %>% is.na leaf <- notLeaf %>% not %>% tree[.] branch <- notLeaf %>% tree[.] idBranch <- str_extract(branch, "\\d*:") %>% str_replace(":", "") %>% addTreeId(treeID) idLeaf <- str_extract(leaf, "\\d*:") %>% str_replace(":", "") %>% addTreeId(treeID) - featureBranch <- str_extract(branch, "f\\d*<") %>% str_replace("<", "") %>% str_replace("f", "") %>% as.numeric + featureBranch <- str_extract(branch, "f\\d*<") %>% str_replace("<", "") %>% str_replace("f", "") %>% as.numeric if(!is.null(feature_names)){ featureBranch <- feature_names[featureBranch + 1] } featureLeaf <- rep("Leaf", length(leaf)) - splitBranch <- str_extract(branch, paste0("<",anynumber_regex,"\\]")) %>% str_replace("<", "") %>% str_replace("\\]", "") - splitLeaf <- rep(NA, length(leaf)) + splitBranch <- str_extract(branch, paste0("<",anynumber_regex,"\\]")) %>% str_replace("<", "") %>% str_replace("\\]", "") + splitLeaf <- rep(NA, length(leaf)) yesBranch <- extract(branch, "yes=\\d*") %>% addTreeId(treeID) - yesLeaf <- rep(NA, length(leaf)) + yesLeaf <- rep(NA, length(leaf)) noBranch <- extract(branch, "no=\\d*") %>% addTreeId(treeID) noLeaf <- rep(NA, length(leaf)) missingBranch <- extract(branch, "missing=\\d+") %>% addTreeId(treeID) @@ -128,38 +128,38 @@ xgb.model.dt.tree <- function(feature_names = NULL, filename_dump = NULL, model qualityLeaf <- extract(leaf, paste0("leaf=",anynumber_regex)) coverBranch <- extract(branch, "cover=\\d*\\.*\\d*") coverLeaf <- extract(leaf, "cover=\\d*\\.*\\d*") - dt <- data.table(ID = c(idBranch, idLeaf), Feature = c(featureBranch, featureLeaf), Split = c(splitBranch, splitLeaf), Yes = c(yesBranch, yesLeaf), No = c(noBranch, noLeaf), Missing = c(missingBranch, missingLeaf), Quality = c(qualityBranch, qualityLeaf), Cover = c(coverBranch, coverLeaf))[order(ID)][,Tree:=treeID] - + dt <- data.table(ID = c(idBranch, idLeaf), Feature = c(featureBranch, featureLeaf), Split = c(splitBranch, splitLeaf), Yes = c(yesBranch, yesLeaf), No = c(noBranch, noLeaf), Missing = c(missingBranch, missingLeaf), Quality = c(qualityBranch, qualityLeaf), Cover = c(coverBranch, coverLeaf))[order(ID)][,Tree := treeID] + allTrees <- rbindlist(list(allTrees, dt), use.names = T, fill = F) } - + yes <- allTrees[!is.na(Yes), Yes] - - set(allTrees, i = which(allTrees[, Feature] != "Leaf"), - j = "Yes.Feature", + + set(allTrees, i = which(allTrees[, Feature] != "Leaf"), + j = "Yes.Feature", value = allTrees[ID %in% yes, Feature]) - + set(allTrees, i = which(allTrees[, Feature] != "Leaf"), - j = "Yes.Cover", + j = "Yes.Cover", value = allTrees[ID %in% yes, Cover]) - + set(allTrees, i = which(allTrees[, Feature] != "Leaf"), - j = "Yes.Quality", + j = "Yes.Quality", value = allTrees[ID %in% yes, Quality]) no <- allTrees[!is.na(No), No] - + set(allTrees, i = which(allTrees[, Feature] != "Leaf"), - j = "No.Feature", + j = "No.Feature", value = allTrees[ID %in% no, Feature]) - + set(allTrees, i = which(allTrees[, Feature] != "Leaf"), - j = "No.Cover", + j = "No.Cover", value = allTrees[ID %in% no, Cover]) - - set(allTrees, i = which(allTrees[, Feature] != "Leaf"), - j = "No.Quality", + + set(allTrees, i = which(allTrees[, Feature] != "Leaf"), + j = "No.Quality", value = allTrees[ID %in% no, Quality]) - + allTrees } diff --git a/R-package/R/xgb.plot.importance.R b/R-package/R/xgb.plot.importance.R index f126dfe46..92399516d 100644 --- a/R-package/R/xgb.plot.importance.R +++ b/R-package/R/xgb.plot.importance.R @@ -30,7 +30,7 @@ #' #' @export xgb.plot.importance <- function(importance_matrix = NULL, numberOfClusters = c(1:10)){ - if (!"data.table" %in% class(importance_matrix)) { + if (!"data.table" %in% class(importance_matrix)) { stop("importance_matrix: Should be a data.table.") } if (!requireNamespace("ggplot2", quietly = TRUE)) { @@ -42,13 +42,13 @@ xgb.plot.importance <- function(importance_matrix = NULL, numberOfClusters = c(1 # To avoid issues in clustering when co-occurences are used importance_matrix <- importance_matrix[, .(Gain = sum(Gain)), by = Feature] - + clusters <- suppressWarnings(Ckmeans.1d.dp::Ckmeans.1d.dp(importance_matrix[,Gain], numberOfClusters)) - importance_matrix[,"Cluster":=clusters$cluster %>% as.character] - - plot <- ggplot2::ggplot(importance_matrix, ggplot2::aes(x=stats::reorder(Feature, Gain), y = Gain, width= 0.05), environment = environment())+ ggplot2::geom_bar(ggplot2::aes(fill=Cluster), stat="identity", position="identity") + ggplot2::coord_flip() + ggplot2::xlab("Features") + ggplot2::ylab("Gain") + ggplot2::ggtitle("Feature importance") + ggplot2::theme(plot.title = ggplot2::element_text(lineheight=.9, face="bold"), panel.grid.major.y = ggplot2::element_blank() ) - - return(plot) + importance_matrix[,"Cluster" := clusters$cluster %>% as.character] + + plot <- ggplot2::ggplot(importance_matrix, ggplot2::aes(x=stats::reorder(Feature, Gain), y = Gain, width = 0.05), environment = environment()) + ggplot2::geom_bar(ggplot2::aes(fill=Cluster), stat="identity", position="identity") + ggplot2::coord_flip() + ggplot2::xlab("Features") + ggplot2::ylab("Gain") + ggplot2::ggtitle("Feature importance") + ggplot2::theme(plot.title = ggplot2::element_text(lineheight=.9, face="bold"), panel.grid.major.y = ggplot2::element_blank() ) + + return(plot) } # Avoid error messages during CRAN check. diff --git a/R-package/R/xgb.plot.tree.R b/R-package/R/xgb.plot.tree.R index edcd5f47f..5e359219a 100644 --- a/R-package/R/xgb.plot.tree.R +++ b/R-package/R/xgb.plot.tree.R @@ -54,40 +54,39 @@ #' #' @export #' -xgb.plot.tree <- function(feature_names = NULL, filename_dump = NULL, model = NULL, n_first_tree = NULL, CSSstyle = NULL, width = NULL, height = NULL){ - +xgb.plot.tree <- function(feature_names = NULL, filename_dump = NULL, model = NULL, n_first_tree = NULL, CSSstyle = NULL, width = NULL, height = NULL){ + if (!(class(CSSstyle) %in% c("character", "NULL") && length(CSSstyle) <= 1)) { stop("style: Has to be a character vector of size 1.") } - + if (!class(model) %in% c("xgb.Booster", "NULL")) { stop("model: Has to be an object of class xgb.Booster model generaged by the xgb.train function.") } - + if (!requireNamespace("DiagrammeR", quietly = TRUE)) { stop("DiagrammeR package is required for xgb.plot.tree", call. = FALSE) } - + if(is.null(model)){ - allTrees <- xgb.model.dt.tree(feature_names = feature_names, filename_dump = filename_dump, n_first_tree = n_first_tree) + allTrees <- xgb.model.dt.tree(feature_names = feature_names, filename_dump = filename_dump, n_first_tree = n_first_tree) } else { - allTrees <- xgb.model.dt.tree(feature_names = feature_names, model = model, n_first_tree = n_first_tree) + allTrees <- xgb.model.dt.tree(feature_names = feature_names, model = model, n_first_tree = n_first_tree) } - - allTrees[Feature!="Leaf" ,yesPath:= paste(ID,"(", Feature, "
Cover: ", Cover, "
Gain: ", Quality, ")-->|< ", Split, "|", Yes, ">", Yes.Feature, "]", sep = "")] - - allTrees[Feature!="Leaf" ,noPath:= paste(ID,"(", Feature, ")-->|>= ", Split, "|", No, ">", No.Feature, "]", sep = "")] - - + + allTrees[Feature != "Leaf" ,yesPath := paste(ID,"(", Feature, "
Cover: ", Cover, "
Gain: ", Quality, ")-->|< ", Split, "|", Yes, ">", Yes.Feature, "]", sep = "")] + + allTrees[Feature != "Leaf" ,noPath := paste(ID,"(", Feature, ")-->|>= ", Split, "|", No, ">", No.Feature, "]", sep = "")] + if(is.null(CSSstyle)){ - CSSstyle <- "classDef greenNode fill:#A2EB86, stroke:#04C4AB, stroke-width:2px;classDef redNode fill:#FFA070, stroke:#FF5E5E, stroke-width:2px" - } - - yes <- allTrees[Feature!="Leaf", c(Yes)] %>% paste(collapse = ",") %>% paste("class ", ., " greenNode", sep = "") - - no <- allTrees[Feature!="Leaf", c(No)] %>% paste(collapse = ",") %>% paste("class ", ., " redNode", sep = "") - - path <- allTrees[Feature!="Leaf", c(yesPath, noPath)] %>% .[order(.)] %>% paste(sep = "", collapse = ";") %>% paste("graph LR", .,collapse = "", sep = ";") %>% paste(CSSstyle, yes, no, sep = ";") + CSSstyle <- "classDef greenNode fill:#A2EB86, stroke:#04C4AB, stroke-width:2px;classDef redNode fill:#FFA070, stroke:#FF5E5E, stroke-width:2px" + } + + yes <- allTrees[Feature != "Leaf", c(Yes)] %>% paste(collapse = ",") %>% paste("class ", ., " greenNode", sep = "") + + no <- allTrees[Feature != "Leaf", c(No)] %>% paste(collapse = ",") %>% paste("class ", ., " redNode", sep = "") + + path <- allTrees[Feature != "Leaf", c(yesPath, noPath)] %>% .[order(.)] %>% paste(sep = "", collapse = ";") %>% paste("graph LR", .,collapse = "", sep = ";") %>% paste(CSSstyle, yes, no, sep = ";") DiagrammeR::mermaid(path, width, height) } diff --git a/R-package/R/xgb.save.R b/R-package/R/xgb.save.R index 2600b8cff..ad3cc8b12 100644 --- a/R-package/R/xgb.save.R +++ b/R-package/R/xgb.save.R @@ -29,4 +29,4 @@ xgb.save <- function(model, fname) { stop("xgb.save: the input must be xgb.Booster. Use xgb.DMatrix.save to save xgb.DMatrix object.") return(FALSE) -} +} diff --git a/R-package/R/xgb.train.R b/R-package/R/xgb.train.R index b1d79d866..8e839af5c 100644 --- a/R-package/R/xgb.train.R +++ b/R-package/R/xgb.train.R @@ -120,9 +120,9 @@ #' bst <- xgb.train(param, dtrain, nthread = 2, nround = 2, watchlist) #' @export #' -xgb.train <- function(params=list(), data, nrounds, watchlist = list(), +xgb.train <- function(params=list(), data, nrounds, watchlist = list(), obj = NULL, feval = NULL, verbose = 1, print.every.n=1L, - early.stop.round = NULL, maximize = NULL, + early.stop.round = NULL, maximize = NULL, save_period = 0, save_name = "xgboost.model", ...) { dtrain <- data if (typeof(params) != "list") { @@ -139,30 +139,30 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), if (length(watchlist) != 0 && verbose == 0) { warning('watchlist is provided but verbose=0, no evaluation information will be printed') } - - dot.params = list(...) - nms.params = names(params) - nms.dot.params = names(dot.params) - if (length(intersect(nms.params,nms.dot.params))>0) + + dot.params <- list(...) + nms.params <- names(params) + nms.dot.params <- names(dot.params) + if (length(intersect(nms.params,nms.dot.params)) > 0) stop("Duplicated term in parameters. Please check your list of params.") - params = append(params, dot.params) - + params <- append(params, dot.params) + # customized objective and evaluation metric interface if (!is.null(params$objective) && !is.null(obj)) stop("xgb.train: cannot assign two different objectives") if (!is.null(params$objective)) - if (class(params$objective)=='function') { - obj = params$objective - params$objective = NULL + if (class(params$objective) == 'function') { + obj <- params$objective + params$objective <- NULL } if (!is.null(params$eval_metric) && !is.null(feval)) stop("xgb.train: cannot assign two different evaluation metrics") if (!is.null(params$eval_metric)) - if (class(params$eval_metric)=='function') { - feval = params$eval_metric - params$eval_metric = NULL + if (class(params$eval_metric) == 'function') { + feval <- params$eval_metric + params$eval_metric <- NULL } - + # Early stopping if (!is.null(early.stop.round)){ if (!is.null(feval) && is.null(maximize)) @@ -174,44 +174,43 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), if (is.null(maximize)) { if (params$eval_metric %in% c('rmse','logloss','error','merror','mlogloss')) { - maximize = FALSE + maximize <- FALSE } else { - maximize = TRUE + maximize <- TRUE } } - + if (maximize) { - bestScore = 0 + bestScore <- 0 } else { - bestScore = Inf + bestScore <- Inf } - bestInd = 0 + bestInd <- 0 earlyStopflag = FALSE - - if (length(watchlist)>1) + + if (length(watchlist) > 1) warning('Only the first data set in watchlist is used for early stopping process.') } - - + handle <- xgb.Booster(params, append(watchlist, dtrain)) bst <- xgb.handleToBooster(handle) - print.every.n=max( as.integer(print.every.n), 1L) + print.every.n <- max( as.integer(print.every.n), 1L) for (i in 1:nrounds) { succ <- xgb.iter.update(bst$handle, dtrain, i - 1, obj) if (length(watchlist) != 0) { msg <- xgb.iter.eval(bst$handle, watchlist, i - 1, feval) - if (0== ( (i-1) %% print.every.n)) - cat(paste(msg, "\n", sep="")) + if (0 == ( (i - 1) %% print.every.n)) + cat(paste(msg, "\n", sep = "")) if (!is.null(early.stop.round)) { - score = strsplit(msg,':|\\s+')[[1]][3] - score = as.numeric(score) - if ((maximize && score>bestScore) || (!maximize && score bestScore) || (!maximize && score < bestScore)) { + bestScore <- score + bestInd <- i } else { - if (i-bestInd>=early.stop.round) { - earlyStopflag = TRUE + earlyStopflag = TRUE + if (i - bestInd >= early.stop.round) { cat('Stopping. Best iteration:',bestInd) break } @@ -226,8 +225,8 @@ xgb.train <- function(params=list(), data, nrounds, watchlist = list(), } bst <- xgb.Booster.check(bst) if (!is.null(early.stop.round)) { - bst$bestScore = bestScore - bst$bestInd = bestInd + bst$bestScore <- bestScore + bst$bestInd <- bestInd } return(bst) -} +} diff --git a/R-package/R/xgboost.R b/R-package/R/xgboost.R index e11052add..122d2f492 100644 --- a/R-package/R/xgboost.R +++ b/R-package/R/xgboost.R @@ -59,28 +59,26 @@ #' #' @export #' -xgboost <- function(data = NULL, label = NULL, missing = NA, weight = NULL, - params = list(), nrounds, +xgboost <- function(data = NULL, label = NULL, missing = NA, weight = NULL, + params = list(), nrounds, verbose = 1, print.every.n = 1L, early.stop.round = NULL, maximize = NULL, save_period = 0, save_name = "xgboost.model", ...) { dtrain <- xgb.get.DMatrix(data, label, missing, weight) - + params <- append(params, list(...)) - + if (verbose > 0) { watchlist <- list(train = dtrain) } else { watchlist <- list() } - + bst <- xgb.train(params, dtrain, nrounds, watchlist, verbose = verbose, print.every.n=print.every.n, early.stop.round = early.stop.round, maximize = maximize, save_period = save_period, save_name = save_name) - + return(bst) -} - - +} #' Training part from Mushroom Data Set #' #' This data set is originally from the Mushroom data set, diff --git a/R-package/man/predict-xgb.Booster-method.Rd b/R-package/man/predict-xgb.Booster-method.Rd index 3ce2e2025..682df1f4b 100644 --- a/R-package/man/predict-xgb.Booster-method.Rd +++ b/R-package/man/predict-xgb.Booster-method.Rd @@ -5,7 +5,7 @@ \alias{predict,xgb.Booster-method} \title{Predict method for eXtreme Gradient Boosting model} \usage{ -\S4method{predict}{xgb.Booster}(object, newdata, missing = NULL, +\S4method{predict}{xgb.Booster}(object, newdata, missing = NA, outputmargin = FALSE, ntreelimit = NULL, predleaf = FALSE) } \arguments{ diff --git a/R-package/man/xgb.DMatrix.Rd b/R-package/man/xgb.DMatrix.Rd index 9d4d19d37..9432ce319 100644 --- a/R-package/man/xgb.DMatrix.Rd +++ b/R-package/man/xgb.DMatrix.Rd @@ -4,7 +4,7 @@ \alias{xgb.DMatrix} \title{Contruct xgb.DMatrix object} \usage{ -xgb.DMatrix(data, info = list(), missing = 0, ...) +xgb.DMatrix(data, info = list(), missing = NA, ...) } \arguments{ \item{data}{a \code{matrix} object, a \code{dgCMatrix} object or a character diff --git a/R-package/man/xgb.cv.Rd b/R-package/man/xgb.cv.Rd index bb23992a2..f918a003c 100644 --- a/R-package/man/xgb.cv.Rd +++ b/R-package/man/xgb.cv.Rd @@ -4,11 +4,10 @@ \alias{xgb.cv} \title{Cross Validation} \usage{ -xgb.cv(params = list(), data, nrounds, nfold, label = NULL, - missing = NULL, prediction = FALSE, showsd = TRUE, metrics = list(), - obj = NULL, feval = NULL, stratified = TRUE, folds = NULL, - verbose = T, print.every.n = 1L, early.stop.round = NULL, - maximize = NULL, ...) +xgb.cv(params = list(), data, nrounds, nfold, label = NULL, missing = NA, + prediction = FALSE, showsd = TRUE, metrics = list(), obj = NULL, + feval = NULL, stratified = TRUE, folds = NULL, verbose = T, + print.every.n = 1L, early.stop.round = NULL, maximize = NULL, ...) } \arguments{ \item{params}{the list of parameters. Commonly used ones are: diff --git a/R-package/man/xgboost.Rd b/R-package/man/xgboost.Rd index a05560a19..79c33007e 100644 --- a/R-package/man/xgboost.Rd +++ b/R-package/man/xgboost.Rd @@ -4,7 +4,7 @@ \alias{xgboost} \title{eXtreme Gradient Boosting (Tree) library} \usage{ -xgboost(data = NULL, label = NULL, missing = NULL, weight = NULL, +xgboost(data = NULL, label = NULL, missing = NA, weight = NULL, params = list(), nrounds, verbose = 1, print.every.n = 1L, early.stop.round = NULL, maximize = NULL, save_period = 0, save_name = "xgboost.model", ...) diff --git a/R-package/tests/testthat/test_basic.R b/R-package/tests/testthat/test_basic.R index 791f1246c..34d47103f 100644 --- a/R-package/tests/testthat/test_basic.R +++ b/R-package/tests/testthat/test_basic.R @@ -4,30 +4,33 @@ context("basic functions") data(agaricus.train, package='xgboost') data(agaricus.test, package='xgboost') -train = agaricus.train -test = agaricus.test +train <- agaricus.train +test <- agaricus.test +set.seed(1994) test_that("train and predict", { - bst = xgboost(data = train$data, label = train$label, max.depth = 2, + bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 1, nthread = 2, nround = 2, objective = "binary:logistic") - pred = predict(bst, test$data) + pred <- predict(bst, test$data) + expect_equal(length(pred), 1611) }) - test_that("early stopping", { - res = xgb.cv(data = train$data, label = train$label, max.depth = 2, nfold = 5, + res <- xgb.cv(data = train$data, label = train$label, max.depth = 2, nfold = 5, eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic", early.stop.round = 3, maximize = FALSE) - expect_true(nrow(res)<20) - bst = xgboost(data = train$data, label = train$label, max.depth = 2, + expect_true(nrow(res) < 20) + bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic", early.stop.round = 3, maximize = FALSE) - pred = predict(bst, test$data) + pred <- predict(bst, test$data) + expect_equal(length(pred), 1611) }) test_that("save_period", { - bst = xgboost(data = train$data, label = train$label, max.depth = 2, + bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic", save_period = 10, save_name = "xgb.model") - pred = predict(bst, test$data) + pred <- predict(bst, test$data) + expect_equal(length(pred), 1611) }) diff --git a/R-package/tests/testthat/test_custom_objective.R b/R-package/tests/testthat/test_custom_objective.R index 9fcbeca4d..7407246c6 100644 --- a/R-package/tests/testthat/test_custom_objective.R +++ b/R-package/tests/testthat/test_custom_objective.R @@ -2,46 +2,47 @@ context('Test models with custom objective') require(xgboost) +data(agaricus.train, package='xgboost') +data(agaricus.test, package='xgboost') +dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label) +dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label) + test_that("custom objective works", { - data(agaricus.train, package='xgboost') - data(agaricus.test, package='xgboost') - dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label) - dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label) - + watchlist <- list(eval = dtest, train = dtrain) num_round <- 2 - + logregobj <- function(preds, dtrain) { labels <- getinfo(dtrain, "label") - preds <- 1/(1 + exp(-preds)) + preds <- 1 / (1 + exp(-preds)) grad <- preds - labels hess <- preds * (1 - preds) return(list(grad = grad, hess = hess)) } evalerror <- function(preds, dtrain) { labels <- getinfo(dtrain, "label") - err <- as.numeric(sum(labels != (preds > 0)))/length(labels) + err <- as.numeric(sum(labels != (preds > 0))) / length(labels) return(list(metric = "error", value = err)) } - - param <- list(max.depth=2, eta=1, nthread = 2, silent=1, + + param <- list(max.depth=2, eta=1, nthread = 2, silent=1, objective=logregobj, eval_metric=evalerror) - + bst <- xgb.train(param, dtrain, num_round, watchlist) expect_equal(class(bst), "xgb.Booster") expect_equal(length(bst$raw), 1064) attr(dtrain, 'label') <- getinfo(dtrain, 'label') - + logregobjattr <- function(preds, dtrain) { labels <- attr(dtrain, 'label') - preds <- 1/(1 + exp(-preds)) + preds <- 1 / (1 + exp(-preds)) grad <- preds - labels hess <- preds * (1 - preds) return(list(grad = grad, hess = hess)) } - param <- list(max.depth=2, eta=1, nthread = 2, silent=1, - objective=logregobjattr, eval_metric=evalerror) + param <- list(max.depth=2, eta=1, nthread = 2, silent = 1, + objective = logregobjattr, eval_metric = evalerror) bst <- xgb.train(param, dtrain, num_round, watchlist) expect_equal(class(bst), "xgb.Booster") expect_equal(length(bst$raw), 1064) -}) \ No newline at end of file +}) diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R index 4d80146e3..d8f69ae72 100644 --- a/R-package/tests/testthat/test_helpers.R +++ b/R-package/tests/testthat/test_helpers.R @@ -5,26 +5,27 @@ require(data.table) require(Matrix) require(vcd) +set.seed(1994) data(Arthritis) data(agaricus.train, package='xgboost') df <- data.table(Arthritis, keep.rownames = F) -df[,AgeDiscret:= as.factor(round(Age/10,0))] -df[,AgeCat:= as.factor(ifelse(Age > 30, "Old", "Young"))] -df[,ID:=NULL] -sparse_matrix = sparse.model.matrix(Improved~.-1, data = df) -output_vector = df[,Y:=0][Improved == "Marked",Y:=1][,Y] +df[,AgeDiscret := as.factor(round(Age / 10,0))] +df[,AgeCat := as.factor(ifelse(Age > 30, "Old", "Young"))] +df[,ID := NULL] +sparse_matrix <- sparse.model.matrix(Improved~.-1, data = df) +output_vector <- df[,Y := 0][Improved == "Marked",Y := 1][,Y] bst <- xgboost(data = sparse_matrix, label = output_vector, max.depth = 9, eta = 1, nthread = 2, nround = 10,objective = "binary:logistic") - test_that("xgb.dump works", { capture.output(print(xgb.dump(bst))) }) test_that("xgb.importance works", { - xgb.dump(bst, 'xgb.model.dump', with.stats = T) + expect_true(xgb.dump(bst, 'xgb.model.dump', with.stats = T)) importance <- xgb.importance(sparse_matrix@Dimnames[[2]], 'xgb.model.dump') expect_equal(dim(importance), c(7, 4)) + expect_equal(colnames(importance), c("Feature", "Gain", "Cover", "Frequence")) }) test_that("xgb.plot.tree works", { diff --git a/R-package/tests/testthat/test_lint.R b/R-package/tests/testthat/test_lint.R new file mode 100644 index 000000000..2f2a07d54 --- /dev/null +++ b/R-package/tests/testthat/test_lint.R @@ -0,0 +1,27 @@ +context("Code is of high quality and lint free") +test_that("Code Lint", { + skip_on_cran() + skip_on_travis() + skip_if_not_installed("lintr") + my_linters <- list( + absolute_paths_linter=lintr::absolute_paths_linter, + assignment_linter=lintr::assignment_linter, + closed_curly_linter=lintr::closed_curly_linter, + commas_linter=lintr::commas_linter, + # commented_code_linter=lintr::commented_code_linter, + infix_spaces_linter=lintr::infix_spaces_linter, + line_length_linter=lintr::line_length_linter, + no_tab_linter=lintr::no_tab_linter, + object_usage_linter=lintr::object_usage_linter, + # snake_case_linter=lintr::snake_case_linter, + # multiple_dots_linter=lintr::multiple_dots_linter, + object_length_linter=lintr::object_length_linter, + open_curly_linter=lintr::open_curly_linter, + # single_quotes_linter=lintr::single_quotes_linter, + spaces_inside_linter=lintr::spaces_inside_linter, + spaces_left_parentheses_linter=lintr::spaces_left_parentheses_linter, + trailing_blank_lines_linter=lintr::trailing_blank_lines_linter, + trailing_whitespace_linter=lintr::trailing_whitespace_linter + ) + # lintr::expect_lint_free(linters=my_linters) # uncomment this if you want to check code quality +}) diff --git a/R-package/tests/testthat/test_poisson_regression.R b/R-package/tests/testthat/test_poisson_regression.R index 5d3d78e27..c5389dd0f 100644 --- a/R-package/tests/testthat/test_poisson_regression.R +++ b/R-package/tests/testthat/test_poisson_regression.R @@ -1,13 +1,14 @@ context('Test poisson regression model') require(xgboost) +set.seed(1994) test_that("poisson regression works", { data(mtcars) - bst = xgboost(data=as.matrix(mtcars[,-11]),label=mtcars[,11], - objective='count:poisson',nrounds=5) + bst <- xgboost(data = as.matrix(mtcars[,-11]),label = mtcars[,11], + objective = 'count:poisson', nrounds=5) expect_equal(class(bst), "xgb.Booster") - pred = predict(bst,as.matrix(mtcars[,-11])) + pred <- predict(bst,as.matrix(mtcars[, -11])) expect_equal(length(pred), 32) - sqrt(mean((pred-mtcars[,11])^2)) -}) \ No newline at end of file + expect_equal(sqrt(mean( (pred - mtcars[,11]) ^ 2)), 1.16, tolerance = 0.01) +}) diff --git a/doc/model.md b/doc/model.md index d9ecd2620..9a28ea95a 100644 --- a/doc/model.md +++ b/doc/model.md @@ -53,22 +53,22 @@ The tradeoff between the two is also referred as bias-variance tradeoff in machi ### Why introduce the general principle -The elements introduced in above forms the basic elements of supervised learning, and they are naturally the building blocks of machine learning toolkits. -For example, you should be able to answer what is the difference and common parts between boosted trees and random forest. +The elements introduced above form the basic elements of supervised learning, and they are naturally the building blocks of machine learning toolkits. +For example, you should be able to describe the differences and commonalities between boosted trees and random forests. Understanding the process in a formalized way also helps us to understand the objective that we are learning and the reason behind the heurestics such as pruning and smoothing. Tree Ensemble ------------- Now that we have introduced the elements of supervised learning, let us get started with real trees. -To begin with, let us first learn what is the ***model*** of xgboost: tree ensembles. +To begin with, let us first learn about the ***model*** of xgboost: tree ensembles. The tree ensemble model is a set of classification and regression trees (CART). Here's a simple example of a CART -that classifies is someone will like computer games. +that classifies whether someone will like computer games. ![CART](img/cart.png) -We classify the members in thie family into different leaves, and assign them the score on corresponding leaf. -A CART is a bit different from decision trees, where the leaf only contain decision values. In CART, a real score +We classify the members of a family into different leaves, and assign them the score on corresponding leaf. +A CART is a bit different from decision trees, where the leaf only contains decision values. In CART, a real score is associated with each of the leaves, which gives us richer interpretations that go beyond classification. This also makes the unified optimization step easier, as we will see in later part of this tutorial. diff --git a/doc/parameter.md b/doc/parameter.md index ba0a18870..057e52c99 100644 --- a/doc/parameter.md +++ b/doc/parameter.md @@ -97,9 +97,9 @@ Command Line Parameters ----------------------- The following parameters are only used in the console version of xgboost * use_buffer [ default=1 ] - - whether create binary buffer for text input, this normally will speedup loading when do + - Whether to create a binary buffer from text input. Doing so normally will speed up loading times * num_round - - the number of round for boosting. + - The number of rounds for boosting * data - The path of training data * test:data diff --git a/doc/python/python_intro.md b/doc/python/python_intro.md index b46358877..37f017c7f 100644 --- a/doc/python/python_intro.md +++ b/doc/python/python_intro.md @@ -8,7 +8,7 @@ This document gives a basic walkthrough of xgboost python package. Install XGBoost --------------- -To install XGBoost, do the following steps. +To install XGBoost, do the following steps: * You need to run `make` in the root directory of the project * In the `python-package` directory run @@ -22,34 +22,39 @@ import xgboost as xgb Data Interface -------------- -XGBoost python module is able to loading from libsvm txt format file, Numpy 2D array and xgboost binary buffer file. The data will be store in ```DMatrix``` object. +The XGBoost python module is able to load data from: +- libsvm txt format file +- Numpy 2D array, and +- xgboost binary buffer file. -* To load libsvm text format file and XGBoost binary file into ```DMatrix```, the usage is like +The data will be store in a ```DMatrix``` object. + +* To load a libsvm text file or a XGBoost binary file into ```DMatrix```, the command is: ```python dtrain = xgb.DMatrix('train.svm.txt') dtest = xgb.DMatrix('test.svm.buffer') ``` -* To load numpy array into ```DMatrix```, the usage is like +* To load a numpy array into ```DMatrix```, the command is: ```python data = np.random.rand(5,10) # 5 entities, each contains 10 features label = np.random.randint(2, size=5) # binary target dtrain = xgb.DMatrix( data, label=label) ``` -* Build ```DMatrix``` from ```scipy.sparse``` +* To load a scpiy.sparse array into ```DMatrix```, the command is: ```python csr = scipy.sparse.csr_matrix((dat, (row, col))) dtrain = xgb.DMatrix(csr) ``` -* Saving ```DMatrix``` into XGBoost binary file will make loading faster in next time. The usage is like: +* Saving ```DMatrix``` into XGBoost binary file will make loading faster in next time: ```python dtrain = xgb.DMatrix('train.svm.txt') dtrain.save_binary("train.buffer") ``` -* To handle missing value in ```DMatrix```, you can initialize the ```DMatrix``` like: +* To handle missing value in ```DMatrix```, you can initialize the ```DMatrix``` by specifying missing values: ```python dtrain = xgb.DMatrix(data, label=label, missing = -999.0) ``` -* Weight can be set when needed, like +* Weight can be set when needed: ```python w = np.random.rand(5, 1) dtrain = xgb.DMatrix(data, label=label, missing = -999.0, weight=w) @@ -150,4 +155,4 @@ When you use ``IPython``, you can use ``to_graphviz`` function which converts th ```python xgb.to_graphviz(bst, num_trees=2) -``` \ No newline at end of file +``` diff --git a/python-package/MANIFEST.in b/python-package/MANIFEST.in index 2d93429a9..01ea397c1 100644 --- a/python-package/MANIFEST.in +++ b/python-package/MANIFEST.in @@ -5,3 +5,10 @@ recursive-include xgboost/windows * recursive-include xgboost/subtree * recursive-include xgboost/src * recursive-include xgboost/multi-node * +#exclude pre-compiled .o file for less confusions +#include the pre-compiled .so is needed as a placeholder +#since it will be copy after compiling on the fly +global-exclude xgboost/wrapper/*.so.gz +global-exclude xgboost/*.o +global-exclude *.pyo +global-exclude *.pyc diff --git a/python-package/build_trouble_shooting.md b/python-package/build_trouble_shooting.md new file mode 100644 index 000000000..504575514 --- /dev/null +++ b/python-package/build_trouble_shooting.md @@ -0,0 +1,48 @@ +XGBoost Python Package Troubleshooting +====================== +Windows platform +------------ +The current best solution for installing xgboost on windows machine is building from github. Please go to [windows](/windows/), build with the Visual Studio project file, and install. Additional detailed instruction can be found at this [installation tutorial](https://www.kaggle.com/c/otto-group-product-classification-challenge/forums/t/13043/run-xgboost-from-windows-and-python) from Kaggle Otto Forum. + +`pip install xgboost` is **not** tested nor supported in windows platform for now. + +Linux platform (also Mac OS X in general) +------------ +**Trouble 0**: I see error messages like this when install from github using `python setup.py install`. + + XGBoostLibraryNotFound: Cannot find XGBoost Libarary in the candicate path, did you install compilers and run build.sh in root path? + List of candidates: + /home/dmlc/anaconda/lib/python2.7/site-packages/xgboost-0.4-py2.7.egg/xgboost/libxgboostwrapper.so + /home/dmlc/anaconda/lib/python2.7/site-packages/xgboost-0.4-py2.7.egg/xgboost/../../wrapper/libxgboostwrapper.so + /home/dmlc/anaconda/lib/python2.7/site-packages/xgboost-0.4-py2.7.egg/xgboost/./wrapper/libxgboostwrapper.so + +**Solution 0**: Please check if you have: + +* installed C++ compilers, for example `g++` and `gcc` (Linux) or `clang LLVM` (Mac OS X). Recommended compilers are `g++-5` or newer (Linux and Mac), or `clang` comes with Xcode in Mac OS X. For installting compilers, please refer to your system package management commands, e.g. `apt-get` `yum` or `brew`(Mac). +* compilers in your `$PATH`. Try typing `gcc` and see if your have it in your path. + +**Trouble 1**: I see the same error message in **Trouble 0** when install from `pip install xgboost`. + +**Solution 1**: the problem is the same as in **Trouble 0**, please see **Solution 0**. + +**Trouble 2**: I see this error message when `pip install xgboost`. It says I have `libxgboostwrapper.so` but it is not valid. + + OSError: /home/dmlc/anaconda/lib/python2.7/site-packages/xgboost/./wrapper/libxgboostwrapper.so: invalid ELF header + +**Solution 2**: Solution is as in 0 and 1 by installing `g++` compiler. The reason for this rare error is that, `pip` ships with a pre-compiled `libxgboostwrapper.so` with Mac for placeholder for allowing `setup.py` to find the right lib path. If a system doesn't compile, it may refer to this placeholder lib and fail. This placeholder `libxgboostwrapper.so` will be automatically removed and correctly generated by the compiling on-the-fly for the system. + +**Trouble 3**: My system's `pip` says it can't find a valid `xgboost` installation release on `PyPI`. +**Solution 3**: Some linux system comes with an old `pip` version. Please update to the latest `pip` by following the official installation document at + +**Trouble 4**: I tried `python setup.py install` but it says `setuptools` import fail. +**Solution 4**: Please make sure you have [setuptools](https://pypi.python.org/pypi/setuptools) before installing the python package. + +Mac OS X (specific) +------------ +Most of the troubles and solutions are the same with that in the Linux platform. Mac has the following specific problems. + +**Trouble 0**: I successfully installed `xgboost` using github installation/using `pip install xgboost`. But it runs very slow with only single thread, what is going on? +**Solution 0**: `clang LLVM` compiler on Mac OS X from Xcode doesn't support OpenMP multi-thread. An alternative choice is installing `homebrew` and `brew install g++-5` which provides multi-thread OpenMP support. + +**Trouble 1**: Can I install `clang-omp` for supporting OpenMP without using `gcc`? +**Solution 1**: it is not support and may have linking errors. \ No newline at end of file diff --git a/python-package/setup.py b/python-package/setup.py index c9dfa415c..f266e7fb2 100644 --- a/python-package/setup.py +++ b/python-package/setup.py @@ -2,21 +2,10 @@ """Setup xgboost package.""" from __future__ import absolute_import import sys -from setuptools import setup, find_packages -import subprocess -sys.path.insert(0, '.') - import os -#build on the fly if install in pip -#otherwise, use build.sh in the parent directory - -if 'pip' in __file__: - if not os.name == 'nt': #if not windows - build_sh = subprocess.Popen(['sh', 'xgboost/build-python.sh']) - build_sh.wait() - output = build_sh.communicate() - print(output) - +from setuptools import setup, find_packages +#import subprocess +sys.path.insert(0, '.') CURRENT_DIR = os.path.dirname(__file__) @@ -28,15 +17,12 @@ libpath = {'__file__': libpath_py} exec(compile(open(libpath_py, "rb").read(), libpath_py, 'exec'), libpath, libpath) LIB_PATH = libpath['find_lib_path']() -#print LIB_PATH -#to deploy to pip, please use -#make pythonpack -#python setup.py register sdist upload -#and be sure to test it firstly using "python setup.py register sdist upload -r pypitest" +#Please use setup_pip.py for generating and deploying pip installation +#detailed instruction in setup_pip.py setup(name='xgboost', version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(), - #version='0.4a13', + #version='0.4a23', description=open(os.path.join(CURRENT_DIR, 'README.md')).read(), install_requires=[ 'numpy', @@ -46,10 +32,6 @@ setup(name='xgboost', maintainer_email='phunter.lau@gmail.com', zip_safe=False, packages=find_packages(), - #don't need this and don't use this, give everything to MANIFEST.in - #package_dir = {'':'xgboost'}, - #package_data = {'': ['*.txt','*.md','*.sh'], - # } #this will use MANIFEST.in during install where we specify additional files, #this is the golden line include_package_data=True, diff --git a/python-package/setup_pip.py b/python-package/setup_pip.py new file mode 100644 index 000000000..83d907c25 --- /dev/null +++ b/python-package/setup_pip.py @@ -0,0 +1,58 @@ +# pylint: disable=invalid-name, exec-used +"""Setup xgboost package.""" +from __future__ import absolute_import +import sys +import os +from setuptools import setup, find_packages +#import subprocess +sys.path.insert(0, '.') + +#this script is for packing and shipping pip installation +#it builds xgboost code on the fly and packs for pip +#please don't use this file for installing from github + +if not os.name == 'nt': #if not windows, compile and install + os.system('sh ./xgboost/build-python.sh') +else: + print('Windows users please use github installation.') + sys.exit() + +CURRENT_DIR = os.path.dirname(__file__) + +# We can not import `xgboost.libpath` in setup.py directly since xgboost/__init__.py +# import `xgboost.core` and finally will import `numpy` and `scipy` which are setup +# `install_requires`. That's why we're using `exec` here. +libpath_py = os.path.join(CURRENT_DIR, 'xgboost/libpath.py') +libpath = {'__file__': libpath_py} +exec(compile(open(libpath_py, "rb").read(), libpath_py, 'exec'), libpath, libpath) + +LIB_PATH = libpath['find_lib_path']() + +#to deploy to pip, please use +#make pythonpack +#python setup.py register sdist upload +#and be sure to test it firstly using "python setup.py register sdist upload -r pypitest" +setup(name='xgboost', + #version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(), + version='0.4a24', + description=open(os.path.join(CURRENT_DIR, 'README.md')).read(), + install_requires=[ + 'numpy', + 'scipy', + ], + maintainer='Hongliang Liu', + maintainer_email='phunter.lau@gmail.com', + zip_safe=False, + packages=find_packages(), + #don't need this and don't use this, give everything to MANIFEST.in + #package_dir = {'':'xgboost'}, + #package_data = {'': ['*.txt','*.md','*.sh'], + # } + #this will use MANIFEST.in during install where we specify additional files, + #this is the golden line + include_package_data=True, + #!!! don't use data_files for creating pip installation, + #otherwise install_data process will copy it to + #root directory for some machines, and cause confusions on building + #data_files=[('xgboost', LIB_PATH)], + url='https://github.com/dmlc/xgboost') diff --git a/python-package/xgboost/build-python.sh b/python-package/xgboost/build-python.sh index 398b076b8..ecc336e61 100755 --- a/python-package/xgboost/build-python.sh +++ b/python-package/xgboost/build-python.sh @@ -11,6 +11,8 @@ pushd xgboost +#remove the pre-compiled .so and trigger the system's on-the-fly compiling +make clean if make python; then echo "Successfully build multi-thread xgboost" else diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 77ef9533b..d27c34f64 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -249,7 +249,7 @@ class DMatrix(object): csr = scipy.sparse.csr_matrix(data) self._init_from_csr(csr) except: - raise TypeError('can not intialize DMatrix from {}'.format(type(data).__name__)) + raise TypeError('can not initialize DMatrix from {}'.format(type(data).__name__)) if label is not None: self.set_label(label) if weight is not None: diff --git a/python-package/xgboost/libpath.py b/python-package/xgboost/libpath.py index 293719f01..5df72dd3d 100644 --- a/python-package/xgboost/libpath.py +++ b/python-package/xgboost/libpath.py @@ -36,9 +36,10 @@ def find_lib_path(): else: dll_path = [os.path.join(p, 'libxgboostwrapper.so') for p in dll_path] lib_path = [p for p in dll_path if os.path.exists(p) and os.path.isfile(p)] + #From github issues, most of installation errors come from machines w/o compilers if len(lib_path) == 0 and not os.environ.get('XGBOOST_BUILD_DOC', False): raise XGBoostLibraryNotFound( 'Cannot find XGBoost Libarary in the candicate path, ' + - 'did you run build.sh in root path?\n' + 'did you install compilers and run build.sh in root path?\n' 'List of candidates:\n' + ('\n'.join(dll_path))) return lib_path diff --git a/python-package/xgboost/plotting.py b/python-package/xgboost/plotting.py index 50a844a1e..f8489a6f8 100644 --- a/python-package/xgboost/plotting.py +++ b/python-package/xgboost/plotting.py @@ -7,11 +7,12 @@ from __future__ import absolute_import import re import numpy as np from .core import Booster +from .sklearn import XGBModel from io import BytesIO def plot_importance(booster, ax=None, height=0.2, - xlim=None, title='Feature importance', + xlim=None, ylim=None, title='Feature importance', xlabel='F score', ylabel='Features', grid=True, **kwargs): @@ -19,14 +20,16 @@ def plot_importance(booster, ax=None, height=0.2, Parameters ---------- - booster : Booster or dict - Booster instance, or dict taken by Booster.get_fscore() + booster : Booster, XGBModel or dict + Booster or XGBModel instance, or dict taken by Booster.get_fscore() ax : matplotlib Axes, default None Target axes instance. If None, new figure and axes will be created. height : float, default 0.2 Bar height, passed to ax.barh() xlim : tuple, default None Tuple passed to axes.xlim() + ylim : tuple, default None + Tuple passed to axes.ylim() title : str, default "Feature importance" Axes title. To disable, pass None. xlabel : str, default "F score" @@ -46,12 +49,14 @@ def plot_importance(booster, ax=None, height=0.2, except ImportError: raise ImportError('You must install matplotlib to plot importance') - if isinstance(booster, Booster): + if isinstance(booster, XGBModel): + importance = booster.booster().get_fscore() + elif isinstance(booster, Booster): importance = booster.get_fscore() elif isinstance(booster, dict): importance = booster else: - raise ValueError('tree must be Booster or dict instance') + raise ValueError('tree must be Booster, XGBModel or dict instance') if len(importance) == 0: raise ValueError('Booster.get_fscore() results in empty') @@ -73,12 +78,19 @@ def plot_importance(booster, ax=None, height=0.2, ax.set_yticklabels(labels) if xlim is not None: - if not isinstance(xlim, tuple) or len(xlim, 2): + if not isinstance(xlim, tuple) or len(xlim) != 2: raise ValueError('xlim must be a tuple of 2 elements') else: xlim = (0, max(values) * 1.1) ax.set_xlim(xlim) + if ylim is not None: + if not isinstance(ylim, tuple) or len(ylim) != 2: + raise ValueError('ylim must be a tuple of 2 elements') + else: + ylim = (-1, len(importance)) + ax.set_ylim(ylim) + if title is not None: ax.set_title(title) if xlabel is not None: @@ -142,8 +154,8 @@ def to_graphviz(booster, num_trees=0, rankdir='UT', Parameters ---------- - booster : Booster - Booster instance + booster : Booster, XGBModel + Booster or XGBModel instance num_trees : int, default 0 Specify the ordinal number of target tree rankdir : str, default "UT" @@ -165,8 +177,11 @@ def to_graphviz(booster, num_trees=0, rankdir='UT', except ImportError: raise ImportError('You must install graphviz to plot tree') - if not isinstance(booster, Booster): - raise ValueError('booster must be Booster instance') + if not isinstance(booster, (Booster, XGBModel)): + raise ValueError('booster must be Booster or XGBModel instance') + + if isinstance(booster, XGBModel): + booster = booster.booster() tree = booster.get_dump()[num_trees] tree = tree.split() @@ -193,8 +208,8 @@ def plot_tree(booster, num_trees=0, rankdir='UT', ax=None, **kwargs): Parameters ---------- - booster : Booster - Booster instance + booster : Booster, XGBModel + Booster or XGBModel instance num_trees : int, default 0 Specify the ordinal number of target tree rankdir : str, default "UT" @@ -216,7 +231,6 @@ def plot_tree(booster, num_trees=0, rankdir='UT', ax=None, **kwargs): except ImportError: raise ImportError('You must install matplotlib to plot tree') - if ax is None: _, ax = plt.subplots(1, 1) diff --git a/python-package/xgboost/sklearn.py b/python-package/xgboost/sklearn.py index 3bf747b58..158d61887 100644 --- a/python-package/xgboost/sklearn.py +++ b/python-package/xgboost/sklearn.py @@ -54,6 +54,14 @@ class XGBModel(XGBModelBase): Subsample ratio of the training instance. colsample_bytree : float Subsample ratio of columns when constructing each tree. + colsample_bylevel : float + Subsample ratio of columns for each split, in each level. + reg_alpha : float (xgb's alpha) + L2 regularization term on weights + reg_lambda : float (xgb's lambda) + L1 regularization term on weights + scale_pos_weight : float + Balancing of positive and negative weights. base_score: The initial prediction score of all instances, global bias. @@ -66,7 +74,8 @@ class XGBModel(XGBModelBase): def __init__(self, max_depth=3, learning_rate=0.1, n_estimators=100, silent=True, objective="reg:linear", nthread=-1, gamma=0, min_child_weight=1, max_delta_step=0, - subsample=1, colsample_bytree=1, + subsample=1, colsample_bytree=1, colsample_bylevel=1, + reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5, seed=0, missing=None): if not SKLEARN_INSTALLED: raise XGBoostError('sklearn needs to be installed in order to use this module') @@ -82,6 +91,10 @@ class XGBModel(XGBModelBase): self.max_delta_step = max_delta_step self.subsample = subsample self.colsample_bytree = colsample_bytree + self.colsample_bylevel = colsample_bylevel + self.reg_alpha = reg_alpha + self.reg_lambda = reg_lambda + self.scale_pos_weight = scale_pos_weight self.base_score = base_score self.seed = seed @@ -190,7 +203,7 @@ class XGBModel(XGBModelBase): if evals_result: for val in evals_result.items(): - evals_result_key = val[1].keys()[0] + evals_result_key = list(val[1].keys())[0] evals_result[val[0]][evals_result_key] = val[1][evals_result_key] self.evals_result_ = evals_result @@ -199,10 +212,12 @@ class XGBModel(XGBModelBase): self.best_iteration = self._Booster.best_iteration return self - def predict(self, data): + def predict(self, data, output_margin=False, ntree_limit=0): # pylint: disable=missing-docstring,invalid-name test_dmatrix = DMatrix(data, missing=self.missing) - return self.booster().predict(test_dmatrix) + return self.booster().predict(test_dmatrix, + output_margin=output_margin, + ntree_limit=ntree_limit) def evals_result(self): """Return the evaluation results. @@ -251,14 +266,16 @@ class XGBClassifier(XGBModel, XGBClassifierBase): n_estimators=100, silent=True, objective="binary:logistic", nthread=-1, gamma=0, min_child_weight=1, - max_delta_step=0, subsample=1, colsample_bytree=1, + max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1, + reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5, seed=0, missing=None): super(XGBClassifier, self).__init__(max_depth, learning_rate, n_estimators, silent, objective, nthread, gamma, min_child_weight, max_delta_step, subsample, - colsample_bytree, - base_score, seed, missing) + colsample_bytree, colsample_bylevel, + reg_alpha, reg_lambda, + scale_pos_weight, base_score, seed, missing) def fit(self, X, y, sample_weight=None, eval_set=None, eval_metric=None, early_stopping_rounds=None, verbose=True): @@ -341,7 +358,7 @@ class XGBClassifier(XGBModel, XGBClassifierBase): if evals_result: for val in evals_result.items(): - evals_result_key = val[1].keys()[0] + evals_result_key = list(val[1].keys())[0] evals_result[val[0]][evals_result_key] = val[1][evals_result_key] self.evals_result_ = evals_result @@ -351,9 +368,11 @@ class XGBClassifier(XGBModel, XGBClassifierBase): return self - def predict(self, data): + def predict(self, data, output_margin=False, ntree_limit=0): test_dmatrix = DMatrix(data, missing=self.missing) - class_probs = self.booster().predict(test_dmatrix) + class_probs = self.booster().predict(test_dmatrix, + output_margin=output_margin, + ntree_limit=ntree_limit) if len(class_probs.shape) > 1: column_indexes = np.argmax(class_probs, axis=1) else: @@ -361,9 +380,11 @@ class XGBClassifier(XGBModel, XGBClassifierBase): column_indexes[class_probs > 0.5] = 1 return self._le.inverse_transform(column_indexes) - def predict_proba(self, data): + def predict_proba(self, data, output_margin=False, ntree_limit=0): test_dmatrix = DMatrix(data, missing=self.missing) - class_probs = self.booster().predict(test_dmatrix) + class_probs = self.booster().predict(test_dmatrix, + output_margin=output_margin, + ntree_limit=ntree_limit) if self.objective == "multi:softprob": return class_probs else: diff --git a/python-package/xgboost/training.py b/python-package/xgboost/training.py index 1e7294d7b..03e24bdba 100644 --- a/python-package/xgboost/training.py +++ b/python-package/xgboost/training.py @@ -10,7 +10,8 @@ import numpy as np from .core import Booster, STRING_TYPES def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, - early_stopping_rounds=None, evals_result=None, verbose_eval=True): + maximize=False, early_stopping_rounds=None, evals_result=None, + verbose_eval=True, learning_rates=None, xgb_model=None): # pylint: disable=too-many-statements,too-many-branches, attribute-defined-outside-init """Train a booster with given parameters. @@ -29,6 +30,8 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, Customized objective function. feval : function Customized evaluation function. + maximize : bool + Whether to maximize feval. early_stopping_rounds: int Activates early stopping. Validation error needs to decrease at least every round(s) to continue training. @@ -46,13 +49,27 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, verbose_eval : bool If `verbose_eval` then the evaluation metric on the validation set, if given, is printed at each boosting stage. + learning_rates: list or function + Learning rate for each boosting round (yields learning rate decay). + - list l: eta = l[boosting round] + - function f: eta = f(boosting round, num_boost_round) + xgb_model : file name of stored xgb model or 'Booster' instance + Xgb model to be loaded before training (allows training continuation). Returns ------- booster : a trained booster model """ evals = list(evals) - bst = Booster(params, [dtrain] + [d[0] for d in evals]) + ntrees = 0 + if xgb_model is not None: + if not isinstance(xgb_model, STRING_TYPES): + xgb_model = xgb_model.save_raw() + bst = Booster(params, [dtrain] + [d[0] for d in evals], model_file=xgb_model) + ntrees = len(bst.get_dump()) + else: + bst = Booster(params, [dtrain] + [d[0] for d in evals]) + if evals_result is not None: if not isinstance(evals_result, dict): @@ -65,6 +82,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, if not early_stopping_rounds: for i in range(num_boost_round): bst.update(dtrain, i, obj) + ntrees += 1 if len(evals) != 0: bst_eval_set = bst.eval_set(evals, i, feval) if isinstance(bst_eval_set, STRING_TYPES): @@ -78,7 +96,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, res = re.findall("([0-9a-zA-Z@]+[-]*):-?([0-9.]+).", msg) for key in evals_name: evals_idx = evals_name.index(key) - res_per_eval = len(res) / len(evals_name) + res_per_eval = len(res) // len(evals_name) for r in range(res_per_eval): res_item = res[(evals_idx*res_per_eval) + r] res_key = res_item[0] @@ -87,6 +105,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, evals_result[key][res_key].append(res_val) else: evals_result[key][res_key] = [res_val] + bst.best_iteration = (ntrees - 1) return bst else: @@ -94,7 +113,8 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, if len(evals) < 1: raise ValueError('For early stopping you need at least one set in evals.') - sys.stderr.write("Will train until {} error hasn't decreased in {} rounds.\n".format(\ + if verbose_eval: + sys.stderr.write("Will train until {} error hasn't decreased in {} rounds.\n".format(\ evals[-1][1], early_stopping_rounds)) # is params a list of tuples? are we using multiple eval metrics? @@ -110,6 +130,8 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, maximize_metrics = ('auc', 'map', 'ndcg') if any(params['eval_metric'].startswith(x) for x in maximize_metrics): maximize_score = True + if feval is not None: + maximize_score = maximize if maximize_score: best_score = 0.0 @@ -117,10 +139,19 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, best_score = float('inf') best_msg = '' - best_score_i = 0 + best_score_i = ntrees + + if isinstance(learning_rates, list) and len(learning_rates) != num_boost_round: + raise ValueError("Length of list 'learning_rates' has to equal 'num_boost_round'.") for i in range(num_boost_round): + if learning_rates is not None: + if isinstance(learning_rates, list): + bst.set_param({'eta': learning_rates[i]}) + else: + bst.set_param({'eta': learning_rates(i, num_boost_round)}) bst.update(dtrain, i, obj) + ntrees += 1 bst_eval_set = bst.eval_set(evals, i, feval) if isinstance(bst_eval_set, STRING_TYPES): @@ -135,7 +166,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, res = re.findall("([0-9a-zA-Z@]+[-]*):-?([0-9.]+).", msg) for key in evals_name: evals_idx = evals_name.index(key) - res_per_eval = len(res) / len(evals_name) + res_per_eval = len(res) // len(evals_name) for r in range(res_per_eval): res_item = res[(evals_idx*res_per_eval) + r] res_key = res_item[0] @@ -149,7 +180,7 @@ def train(params, dtrain, num_boost_round=10, evals=(), obj=None, feval=None, if (maximize_score and score > best_score) or \ (not maximize_score and score < best_score): best_score = score - best_score_i = i + best_score_i = (ntrees - 1) best_msg = msg elif i - best_score_i >= early_stopping_rounds: sys.stderr.write("Stopping. Best iteration:\n{}\n\n".format(best_msg)) diff --git a/scripts/travis_script.sh b/scripts/travis_script.sh index 3a026966d..1e62b5b46 100755 --- a/scripts/travis_script.sh +++ b/scripts/travis_script.sh @@ -64,7 +64,7 @@ if [ ${TASK} == "python-package" -o ${TASK} == "python-package3" ]; then conda create -n myenv python=2.7 fi source activate myenv - conda install numpy scipy pandas matplotlib nose + conda install numpy scipy pandas matplotlib nose scikit-learn python -m pip install graphviz make all CXX=${CXX} || exit -1 diff --git a/src/gbm/gbtree-inl.hpp b/src/gbm/gbtree-inl.hpp index 9335ef8e7..c06dc51a1 100644 --- a/src/gbm/gbtree-inl.hpp +++ b/src/gbm/gbtree-inl.hpp @@ -138,10 +138,7 @@ class GBTree : public IGradBooster { { nthread = omp_get_num_threads(); } - thread_temp.resize(nthread, tree::RegTree::FVec()); - for (int i = 0; i < nthread; ++i) { - thread_temp[i].Init(mparam.num_feature); - } + InitThreadTemp(nthread); std::vector &preds = *out_preds; const size_t stride = info.num_row * mparam.num_output_group; preds.resize(stride * (mparam.size_leaf_vector+1)); @@ -194,10 +191,7 @@ class GBTree : public IGradBooster { { nthread = omp_get_num_threads(); } - thread_temp.resize(nthread, tree::RegTree::FVec()); - for (int i = 0; i < nthread; ++i) { - thread_temp[i].Init(mparam.num_feature); - } + InitThreadTemp(nthread); this->PredPath(p_fmat, info, out_preds, ntree_limit); } virtual std::vector DumpModel(const utils::FeatMap& fmap, int option) { @@ -391,6 +385,16 @@ class GBTree : public IGradBooster { } } } + // init thread buffers + inline void InitThreadTemp(int nthread) { + int prev_thread_temp_size = thread_temp.size(); + if (prev_thread_temp_size < nthread) { + thread_temp.resize(nthread, tree::RegTree::FVec()); + for (int i = prev_thread_temp_size; i < nthread; ++i) { + thread_temp[i].Init(mparam.num_feature); + } + } + } // --- data structure --- /*! \brief training parameters */ diff --git a/src/tree/param.h b/src/tree/param.h index f06365a17..c6060ffbf 100644 --- a/src/tree/param.h +++ b/src/tree/param.h @@ -72,7 +72,8 @@ struct TrainParam{ opt_dense_col = 1.0f; nthread = 0; size_leaf_vector = 0; - parallel_option = 2; + // enforce parallel option to 0 for now, investigate the other strategy + parallel_option = 0; sketch_eps = 0.1f; sketch_ratio = 2.0f; cache_opt = 1; diff --git a/subtree/rabit/.gitignore b/subtree/rabit/.gitignore index 504802743..121caaafe 100644 --- a/subtree/rabit/.gitignore +++ b/subtree/rabit/.gitignore @@ -34,3 +34,6 @@ *tmp* *.rabit *.mock +dmlc-core +recommonmark +recom diff --git a/subtree/rabit/.travis.yml b/subtree/rabit/.travis.yml new file mode 100644 index 000000000..339f5c692 --- /dev/null +++ b/subtree/rabit/.travis.yml @@ -0,0 +1,51 @@ +# disable sudo to use container based build +sudo: false + +# Use Build Matrix to do lint and build seperately +env: + matrix: + - TASK=lint LINT_LANG=cpp + - TASK=lint LINT_LANG=python + - TASK=doc + - TASK=build CXX=g++ + - TASK=test CXX=g++ + +# dependent apt packages +addons: + apt: + packages: + - doxygen + - libopenmpi-dev + - wget + - git + - libcurl4-openssl-dev + - unzip + - python-numpy + +before_install: + - git clone https://github.com/dmlc/dmlc-core + - export TRAVIS=dmlc-core/scripts/travis/ + - source ${TRAVIS}/travis_setup_env.sh + +install: + - pip install cpplint pylint --user `whoami` + +script: scripts/travis_script.sh + + +before_cache: + - ${TRAVIS}/travis_before_cache.sh + + +cache: + directories: + - ${HOME}/.cache/usr + + +notifications: +# Emails are sent to the committer's git-configured email address by default, + email: + on_success: change + on_failure: always + + diff --git a/subtree/rabit/Makefile b/subtree/rabit/Makefile index e2a96eb43..8c9d9f403 100644 --- a/subtree/rabit/Makefile +++ b/subtree/rabit/Makefile @@ -3,8 +3,19 @@ export CXX = g++ endif export MPICXX = mpicxx export LDFLAGS= -Llib -lrt -export WARNFLAGS= -Wall -Wextra -Wno-unused-parameter -Wno-unknown-pragmas -pedantic -export CFLAGS = -O3 -msse2 -fPIC $(WARNFLAGS) +export WARNFLAGS= -Wall -Wextra -Wno-unused-parameter -Wno-unknown-pragmas -std=c++0x +export CFLAGS = -O3 -msse2 $(WARNFLAGS) + +ifndef WITH_FPIC + WITH_FPIC = 1 +endif +ifeq ($(WITH_FPIC), 1) + CFLAGS += -fPIC +endif + +ifndef LINT_LANG + LINT_LANG="all" +endif # build path BPATH=. @@ -15,7 +26,9 @@ OBJ= $(BPATH)/allreduce_base.o $(BPATH)/allreduce_robust.o $(BPATH)/engine.o $(B SLIB= wrapper/librabit_wrapper.so wrapper/librabit_wrapper_mock.so wrapper/librabit_wrapper_mpi.so ALIB= lib/librabit.a lib/librabit_mpi.a lib/librabit_empty.a lib/librabit_mock.a lib/librabit_base.a HEADERS=src/*.h include/*.h include/rabit/*.h -.PHONY: clean all install mpi python +DMLC=dmlc-core + +.PHONY: clean all install mpi python lint doc doxygen all: lib/librabit.a lib/librabit_mock.a wrapper/librabit_wrapper.so wrapper/librabit_wrapper_mock.so lib/librabit_base.a mpi: lib/librabit_mpi.a wrapper/librabit_wrapper_mpi.so @@ -40,10 +53,10 @@ wrapper/librabit_wrapper.so: $(BPATH)/rabit_wrapper.o lib/librabit.a wrapper/librabit_wrapper_mock.so: $(BPATH)/rabit_wrapper.o lib/librabit_mock.a wrapper/librabit_wrapper_mpi.so: $(BPATH)/rabit_wrapper.o lib/librabit_mpi.a -$(OBJ) : +$(OBJ) : $(CXX) -c $(CFLAGS) -o $@ $(firstword $(filter %.cpp %.c %.cc, $^) ) -$(MPIOBJ) : +$(MPIOBJ) : $(MPICXX) -c $(CFLAGS) -o $@ $(firstword $(filter %.cpp %.c %.cc, $^) ) $(ALIB): @@ -52,6 +65,12 @@ $(ALIB): $(SLIB) : $(CXX) $(CFLAGS) -shared -o $@ $(filter %.cpp %.o %.c %.cc %.a, $^) $(LDFLAGS) -clean: - $(RM) $(OBJ) $(MPIOBJ) $(ALIB) $(MPIALIB) *~ src/*~ include/*~ include/*/*~ wrapper/*~ +lint: + $(DMLC)/scripts/lint.py rabit $(LINT_LANG) src include wrapper + +doc doxygen: + cd include; doxygen ../doc/Doxyfile; cd - + +clean: + $(RM) $(OBJ) $(MPIOBJ) $(ALIB) $(MPIALIB) $(SLIB) *~ src/*~ include/*~ include/*/*~ wrapper/*~ diff --git a/subtree/rabit/README.md b/subtree/rabit/README.md index 1bf5caee4..9302a2199 100644 --- a/subtree/rabit/README.md +++ b/subtree/rabit/README.md @@ -1,6 +1,8 @@ ## rabit: Reliable Allreduce and Broadcast Interface +[![Build Status](https://travis-ci.org/dmlc/rabit.svg?branch=master)](https://travis-ci.org/dmlc/rabit) +[![Documentation Status](https://readthedocs.org/projects/rabit/badge/?version=latest)](http://rabit.readthedocs.org/) -rabit is a light weight library that provides a fault tolerant interface of Allreduce and Broadcast. It is designed to support easy implementations of distributed machine learning programs, many of which fall naturally under the Allreduce abstraction. The goal of rabit is to support ***portable*** , ***scalable*** and ***reliable*** distributed machine learning programs. +rabit is a light weight library that provides a fault tolerant interface of Allreduce and Broadcast. It is designed to support easy implementations of distributed machine learning programs, many of which fall naturally under the Allreduce abstraction. The goal of rabit is to support ***portable*** , ***scalable*** and ***reliable*** distributed machine learning programs. * [Tutorial](guide) * [API Documentation](http://homes.cs.washington.edu/~tqchen/rabit/doc) diff --git a/subtree/rabit/doc/.gitignore b/subtree/rabit/doc/.gitignore index 9036e38b3..95f88be43 100644 --- a/subtree/rabit/doc/.gitignore +++ b/subtree/rabit/doc/.gitignore @@ -1,3 +1,5 @@ html latex *.sh +_* +doxygen diff --git a/subtree/rabit/doc/Doxyfile b/subtree/rabit/doc/Doxyfile index 694bc35d3..2c9c64ea7 100644 --- a/subtree/rabit/doc/Doxyfile +++ b/subtree/rabit/doc/Doxyfile @@ -8,7 +8,7 @@ PROJECT_NAME = "rabit" PROJECT_NUMBER = PROJECT_BRIEF = PROJECT_LOGO = -OUTPUT_DIRECTORY = ../doc +OUTPUT_DIRECTORY = ../doc/doxygen CREATE_SUBDIRS = NO OUTPUT_LANGUAGE = English BRIEF_MEMBER_DESC = YES @@ -101,8 +101,8 @@ FILE_PATTERNS = RECURSIVE = NO EXCLUDE = EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = *-inl.hpp -EXCLUDE_SYMBOLS = +EXCLUDE_PATTERNS = *-inl.hpp +EXCLUDE_SYMBOLS = EXAMPLE_PATH = EXAMPLE_PATTERNS = EXAMPLE_RECURSIVE = NO @@ -216,7 +216,7 @@ MAN_LINKS = NO #--------------------------------------------------------------------------- # configuration options related to the XML output #--------------------------------------------------------------------------- -GENERATE_XML = NO +GENERATE_XML = YES XML_OUTPUT = xml XML_SCHEMA = XML_DTD = diff --git a/subtree/rabit/doc/Makefile b/subtree/rabit/doc/Makefile new file mode 100644 index 000000000..40bba2a28 --- /dev/null +++ b/subtree/rabit/doc/Makefile @@ -0,0 +1,192 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " applehelp to make an Apple Help Book" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/rabit.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/rabit.qhc" + +applehelp: + $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp + @echo + @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." + @echo "N.B. You won't be able to view it unless you put it in" \ + "~/Library/Documentation/Help or install it in your application" \ + "bundle." + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/rabit" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/rabit" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/subtree/rabit/doc/conf.py b/subtree/rabit/doc/conf.py new file mode 100644 index 000000000..ef89de489 --- /dev/null +++ b/subtree/rabit/doc/conf.py @@ -0,0 +1,184 @@ +# -*- coding: utf-8 -*- +# +# documentation build configuration file, created by +# sphinx-quickstart on Thu Jul 23 19:40:08 2015. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. +import sys +import os, subprocess +import shlex +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) +libpath = os.path.join(curr_path, '../wrapper/') +sys.path.insert(0, os.path.join(curr_path, '../wrapper/')) +sys.path.insert(0, curr_path) +from sphinx_util import MarkdownParser, AutoStructify + +# -- General configuration ------------------------------------------------ + +# General information about the project. +project = u'rabit' +copyright = u'2015, rabit developers' +author = u'rabit developers' +github_doc_root = 'https://github.com/dmlc/rabit/tree/master/doc/' + +# add markdown parser +MarkdownParser.github_doc_root = github_doc_root +source_parsers = { + '.md': MarkdownParser, +} +# Version information. +import rabit + +version = rabit.__version__ +release = rabit.__version__ + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.napoleon', + 'sphinx.ext.mathjax', + 'breathe', +] + +# Use breathe to include doxygen documents +breathe_projects = {'rabit' : 'doxygen/xml/'} +breathe_default_project = 'rabit' + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# source_suffix = ['.rst', '.md'] +source_suffix = ['.rst', '.md'] + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +#keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# html_theme = 'alabaster' + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Output file base name for HTML help builder. +htmlhelp_basename = project + 'doc' + +# -- Options for LaTeX output --------------------------------------------- +latex_elements = { +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'rabit.tex', project, + author, 'manual'), +] + +# hook for doxygen +def run_doxygen(folder): + """Run the doxygen make command in the designated folder.""" + try: + retcode = subprocess.call("cd %s; make doxygen" % folder, shell=True) + if retcode < 0: + sys.stderr.write("doxygen terminated by signal %s" % (-retcode)) + except OSError as e: + sys.stderr.write("doxygen execution failed: %s" % e) + + +def run_build_lib(folder): + """Run the doxygen make command in the designated folder.""" + try: + retcode = subprocess.call("cd %s; make" % folder, shell=True) + retcode = subprocess.call("rm -rf _build/html/doxygen", shell=True) + retcode = subprocess.call("mkdir _build", shell=True) + retcode = subprocess.call("mkdir _build/html", shell=True) + retcode = subprocess.call("cp -rf doxygen/html _build/html/doxygen", shell=True) + if retcode < 0: + sys.stderr.write("build terminated by signal %s" % (-retcode)) + except OSError as e: + sys.stderr.write("build execution failed: %s" % e) + + +def generate_doxygen_xml(app): + """Run the doxygen make commands if we're on the ReadTheDocs server""" + read_the_docs_build = os.environ.get('READTHEDOCS', None) == 'True' + if read_the_docs_build: + run_doxygen('..') + sys.stderr.write('Check if shared lib exists\n') + run_build_lib('..') + sys.stderr.write('The wrapper path: %s\n' % str(os.listdir('../wrapper'))) + rabit._loadlib() + + +def setup(app): + # Add hook for building doxygen xml when needed + app.connect("builder-inited", generate_doxygen_xml) + app.add_config_value('recommonmark_config', { + 'url_resolver': lambda url: github_doc_root + url, + }, True) + app.add_transform(AutoStructify) diff --git a/subtree/rabit/doc/cpp_api.md b/subtree/rabit/doc/cpp_api.md new file mode 100644 index 000000000..c6184aa08 --- /dev/null +++ b/subtree/rabit/doc/cpp_api.md @@ -0,0 +1,9 @@ +C++ Library API of Rabit +======================== +This page contains document of Library API of rabit. + +```eval_rst +.. toctree:: + +.. doxygennamespace:: rabit +``` diff --git a/subtree/rabit/guide/README.md b/subtree/rabit/doc/guide.md similarity index 89% rename from subtree/rabit/guide/README.md rename to subtree/rabit/doc/guide.md index 26cace131..e2bfa5ce8 100644 --- a/subtree/rabit/guide/README.md +++ b/subtree/rabit/doc/guide.md @@ -1,10 +1,9 @@ Tutorial -===== +======== This is rabit's tutorial, a ***Reliable Allreduce and Broadcast Interface***. +All the example codes are in the [guide](https://github.com/dmlc/rabit/blob/master/guide/) folder of the project. To run the examples locally, you will need to build them with ```make```. -Please also refer to the [API Documentation](http://homes.cs.washington.edu/~tqchen/rabit/doc) for further details. - **List of Topics** * [What is Allreduce](#what-is-allreduce) * [Common Use Case](#common-use-case) @@ -20,9 +19,9 @@ Please also refer to the [API Documentation](http://homes.cs.washington.edu/~tqc * [Fault Tolerance](#fault-tolerance) What is Allreduce -===== +----------------- The main methods provided by rabit are Allreduce and Broadcast. Allreduce performs reduction across different computation nodes, -and returns the result to every node. To understand the behavior of the function, consider the following example in [basic.cc](basic.cc) (there is a python example right after this if you are more familiar with python). +and returns the result to every node. To understand the behavior of the function, consider the following example in [basic.cc](../guide/basic.cc) (there is a python example right after this if you are more familiar with python). ```c++ #include using namespace rabit; @@ -32,7 +31,7 @@ int main(int argc, char *argv[]) { rabit::Init(argc, argv); for (int i = 0; i < N; ++i) { a[i] = rabit::GetRank() + i; - } + } printf("@node[%d] before-allreduce: a={%d, %d, %d}\n", rabit::GetRank(), a[0], a[1], a[2]); // allreduce take max of each elements in all processes @@ -42,7 +41,7 @@ int main(int argc, char *argv[]) { // second allreduce that sums everything up Allreduce(&a[0], N); printf("@node[%d] after-allreduce-sum: a={%d, %d, %d}\n", - rabit::GetRank(), a[0], a[1], a[2]); + rabit::GetRank(), a[0], a[1], a[2]); rabit::Finalize(); return 0; } @@ -55,7 +54,7 @@ starts the rabit program with two worker processes. This will start two processes, one process with rank 0 and the other with rank 1, both processes run the same code. The ```rabit::GetRank()``` function returns the rank of current process. -Before the call to Allreduce, process 0 contains the array ```a = {0, 1, 2}```, while process 1 has the array +Before the call to Allreduce, process 0 contains the array ```a = {0, 1, 2}```, while process 1 has the array ```a = {1, 2, 3}```. After the call to Allreduce, the array contents in all processes are replaced by the reduction result (in this case, the maximum value in each position across all the processes). So, after the Allreduce call, the result will become ```a = {1, 2, 3}```. @@ -63,7 +62,7 @@ Rabit provides different reduction operators, for example, if you change ```op: the reduction operation will be a summation, and the result will become ```a = {1, 3, 5}```. You can also run the example with different processes by setting -n to different values. -If you are more familiar with python, you can also use rabit in python. The same example as before can be found in [basic.py](basic.py): +If you are more familiar with python, you can also use rabit in python. The same example as before can be found in [basic.py](../guide/basic.py): ```python import numpy as np @@ -75,7 +74,7 @@ rank = rabit.get_rank() a = np.zeros(n) for i in xrange(n): a[i] = rank + i - + print '@node[%d] before-allreduce: a=%s' % (rank, str(a)) a = rabit.allreduce(a, rabit.MAX) print '@node[%d] after-allreduce-max: a=%s' % (rank, str(a)) @@ -89,7 +88,7 @@ You can run the program using the following command ``` Broadcast is another method provided by rabit besides Allreduce. This function allows one node to broadcast its -local data to all other nodes. The following code in [broadcast.cc](broadcast.cc) broadcasts a string from +local data to all other nodes. The following code in [broadcast.cc](../guide/broadcast.cc) broadcasts a string from node 0 to all other nodes. ```c++ #include @@ -115,7 +114,7 @@ The following command starts the program with three worker processes. ``` Besides strings, rabit also allows to broadcast constant size array and vectors. -The counterpart in python can be found in [broadcast.py](broadcast.py). Here is a snippet so that you can get a better sense of how simple is to use the python library: +The counterpart in python can be found in [broadcast.py](../guide/broadcast.py). Here is a snippet so that you can get a better sense of how simple is to use the python library: ```python import rabit @@ -132,7 +131,7 @@ rabit.finalize() ``` Common Use Case -===== +--------------- Many distributed machine learning algorithms involve splitting the data into different nodes, computing statistics locally, and finally aggregating them. Such workflow is usually done repetitively through many iterations before the algorithm converges. Allreduce naturally meets the structure of such programs, common use cases include: @@ -144,7 +143,7 @@ common use cases include: Rabit is a reliable and portable library for distributed machine learning programs, that allow programs to run reliably on different platforms. Use Rabit API -==== +------------- This section introduces topics about how to use rabit API. You can always refer to [API Documentation](http://homes.cs.washington.edu/~tqchen/rabit/doc) for definition of each functions. This section trys to gives examples of different aspectes of rabit API. @@ -178,16 +177,16 @@ int main(int argc, char *argv[]) { ``` Besides the common Allreduce and Broadcast functions, there are two additional functions: ```LoadCheckPoint``` -and ```CheckPoint```. These two functions are used for fault-tolerance purposes. +and ```CheckPoint```. These two functions are used for fault-tolerance purposes. As mentioned before, traditional machine learning programs involve several iterations. In each iteration, we start with a model, make some calls to Allreduce or Broadcast and update the model. The calling sequence in each iteration does not need to be the same. * When the nodes start from the beginning (i.e. iteration 0), ```LoadCheckPoint``` returns 0, so we can initialize the model. * ```CheckPoint``` saves the model after each iteration. - Efficiency Note: the model is only kept in local memory and no save to disk is performed when calling Checkpoint -* When a node goes down and restarts, ```LoadCheckPoint``` will recover the latest saved model, and -* When a node goes down, the rest of the nodes will block in the call of Allreduce/Broadcast and wait for - the recovery of the failed node until it catches up. +* When a node goes down and restarts, ```LoadCheckPoint``` will recover the latest saved model, and +* When a node goes down, the rest of the nodes will block in the call of Allreduce/Broadcast and wait for + the recovery of the failed node until it catches up. Please see the [Fault Tolerance](#fault-tolerance) section to understand the recovery procedure executed by rabit. @@ -202,8 +201,8 @@ into the data buffer, pass the data to Allreduce function, and get the reduced r from failure, we can directly recover the result from other nodes(see also [Fault Tolerance](#fault-tolerance)) and the data preparation procedure no longer necessary. Rabit Allreduce add an optional parameter preparation function to support such scenario. User can pass in a function that corresponds to the data preparation procedure to Allreduce -calls, and the data preparation function will only be called when necessary. We use [lazy_allreduce.cc](lazy_allreduce.cc) -as an example to demonstrate this feature. It is modified from [basic.cc](basic.cc), and you can compare the two codes. +calls, and the data preparation function will only be called when necessary. We use [lazy_allreduce.cc](../guide/lazy_allreduce.cc) +as an example to demonstrate this feature. It is modified from [basic.cc](../guide/basic.cc), and you can compare the two codes. ```c++ #include using namespace rabit; @@ -216,18 +215,18 @@ int main(int argc, char *argv[]) { printf("@node[%d] run prepare function\n", rabit::GetRank()); for (int i = 0; i < N; ++i) { a[i] = rabit::GetRank() + i; - } + } }; printf("@node[%d] before-allreduce: a={%d, %d, %d}\n", rabit::GetRank(), a[0], a[1], a[2]); // allreduce take max of each elements in all processes - Allreduce(&a[0], N, prepare); + Allreduce(&a[0], N, prepare); printf("@node[%d] after-allreduce-sum: a={%d, %d, %d}\n", - rabit::GetRank(), a[0], a[1], a[2]); + rabit::GetRank(), a[0], a[1], a[2]); // rum second allreduce Allreduce(&a[0], N); printf("@node[%d] after-allreduce-max: a={%d, %d, %d}\n", - rabit::GetRank(), a[0], a[1], a[2]); + rabit::GetRank(), a[0], a[1], a[2]); rabit::Finalize(); return 0; } @@ -242,7 +241,7 @@ the effect when a process goes down. You can run the program using the following The additional arguments ```mock=0,0,1,0``` will cause node 0 to kill itself before second call of Allreduce (see also [mock test](#link-against-mock-test-rabit-library)). You will find that the prepare function's print is only executed once and node 0 will no longer execute the preparation function when it restarts from failure. -You can also find python version of the example in [lazy_allreduce.py](lazy_allreduce.py), and run it using the followin command +You can also find python version of the example in [lazy_allreduce.py](../guide/lazy_allreduce.py), and run it using the followin command ```bash ../tracker/rabit_demo.py -n 2 lazy_allreduce.py mock=0,0,1,0 @@ -250,8 +249,8 @@ You can also find python version of the example in [lazy_allreduce.py](lazy_allr Since lazy preparation function may not be called during execution. User should be careful when using this feature. For example, a possible mistake could be putting some memory allocation code in the lazy preparation function, and the computing memory was not allocated when lazy preparation function is not called. -The example in [lazy_allreduce.cc](lazy_allreduce.cc) provides a simple way to migrate normal prepration code([basic.cc](basic.cc)) to lazy version: wrap the preparation -code with a lambda function, and pass it to allreduce. +The example in [lazy_allreduce.cc](../guide/lazy_allreduce.cc) provides a simple way to migrate normal prepration code([basic.cc](../guide/basic.cc)) to lazy version: wrap the preparation +code with a lambda function, and pass it to allreduce. #### Checkpoint and LazyCheckpoint Common machine learning algorithms usually involves iterative computation. As mentioned in the section ([Structure of a Rabit Program](#structure-of-a-rabit-program)), @@ -263,9 +262,9 @@ There are two model arguments you can pass to Checkpoint and LoadCheckpoint: ``` * ```local_model``` refers to the model that is specifically tied to the current node - For example, in topic modeling, the topic assignments of subset of documents in current node is local model -Because the different nature of the two types of models, different strategy will be used for them. +Because the different nature of the two types of models, different strategy will be used for them. ```global_model``` is simply saved in local memory of each node, while ```local_model``` will replicated to some other -nodes (selected using a ring replication strategy). The checkpoint is only saved in the memory without touching the disk which makes rabit programs more efficient. +nodes (selected using a ring replication strategy). The checkpoint is only saved in the memory without touching the disk which makes rabit programs more efficient. User is encouraged to use ```global_model``` only when is sufficient for better efficiency. To enable a model class to be checked pointed, user can implement a [serialization interface](../include/rabit_serialization.h). The serialization interface already @@ -287,7 +286,7 @@ improve the efficiency of the program. Compile Programs with Rabit -==== +--------------------------- Rabit is a portable library, to use it, you only need to include the rabit header file. * You will need to add the path to [../include](../include) to the header search path of the compiler - Solution 1: add ```-I/path/to/rabit/include``` to the compiler flag in gcc or clang @@ -333,27 +332,27 @@ For example, consider the following script in the test case - Note that ndeath = 1 means this will happen only if node 1 died once, which is our case Running Rabit Jobs -==== -Rabit is a portable library that can run on multiple platforms. +------------------ +Rabit is a portable library that can run on multiple platforms. #### Running Rabit Locally -* You can use [../tracker/rabit_demo.py](../tracker/rabit_demo.py) to start n processes locally +* You can use [../tracker/rabit_demo.py](https://github.com/dmlc/rabit/blob/master/tracker/rabit_demo.py) to start n processes locally * This script will restart the program when it exits with -2, so it can be used for [mock test](#link-against-mock-test-library) #### Running Rabit on Hadoop -* You can use [../tracker/rabit_yarn.py](../tracker/rabit_yarn.py) to run rabit programs as Yarn application +* You can use [../tracker/rabit_yarn.py](https://github.com/dmlc/rabit/blob/master/tracker/rabit_yarn.py) to run rabit programs as Yarn application * This will start rabit programs as yarn applications - This allows multi-threading programs in each node, which can be more efficient - An easy multi-threading solution could be to use OpenMP with rabit code * It is also possible to run rabit program via hadoop streaming, however, YARN is highly recommended. #### Running Rabit using MPI -* You can submit rabit programs to an MPI cluster using [../tracker/rabit_mpi.py](../tracker/rabit_mpi.py). +* You can submit rabit programs to an MPI cluster using [../tracker/rabit_mpi.py](https://github.com/dmlc/rabit/blob/master/tracker/rabit_mpi.py). * If you linked your code against librabit_mpi.a, then you can directly use mpirun to submit the job #### Customize Tracker Script You can also modify the tracker script to allow rabit to run on other platforms. To do so, refer to existing -tracker scripts, such as [../tracker/rabit_hadoop.py](../tracker/rabit_hadoop.py) and [../tracker/rabit_mpi.py](../tracker/rabit_mpi.py) to get a sense of how it is done. +tracker scripts, such as [../tracker/rabit_yarn.py](../tracker/rabit_yarn.py) and [../tracker/rabit_mpi.py](https://github.com/dmlc/rabit/blob/master/tracker/rabit_mpi.py) to get a sense of how it is done. You will need to implement a platform dependent submission function with the following definition ```python @@ -376,7 +375,7 @@ Note that the current rabit tracker does not restart a worker when it dies, the - rabit-yarn provides such functionality in YARN Fault Tolerance -===== +--------------- This section introduces how fault tolerance works in rabit. The following figure shows how rabit deals with failures. diff --git a/subtree/rabit/doc/index.md b/subtree/rabit/doc/index.md new file mode 100644 index 000000000..d209d95ba --- /dev/null +++ b/subtree/rabit/doc/index.md @@ -0,0 +1,24 @@ +Rabit Documentation +===================== +rabit is a light weight library that provides a fault tolerant interface of Allreduce and Broadcast. It is designed to support easy implementations of distributed machine learning programs, many of which fall naturally under the Allreduce abstraction. The goal of rabit is to support **portable** , **scalable** and **reliable** distributed machine learning programs. + +API Documents +------------- +```eval_rst + +.. toctree:: + :maxdepth: 2 + + python_api.md + cpp_api.md + parameters.md + guide.md +``` +Indices and tables +------------------ + +```eval_rst +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` +``` \ No newline at end of file diff --git a/subtree/rabit/doc/mkdoc.sh b/subtree/rabit/doc/mkdoc.sh deleted file mode 100755 index 181e280fb..000000000 --- a/subtree/rabit/doc/mkdoc.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -cd ../include -doxygen ../doc/Doxyfile -cd ../doc diff --git a/subtree/rabit/doc/README.md b/subtree/rabit/doc/parameters.md similarity index 70% rename from subtree/rabit/doc/README.md rename to subtree/rabit/doc/parameters.md index fadc9a1b1..37580d5a1 100644 --- a/subtree/rabit/doc/README.md +++ b/subtree/rabit/doc/parameters.md @@ -1,18 +1,11 @@ -Rabit Documentation -==== -* [Tutorial](../guide) -* [API Documentation](http://homes.cs.washington.edu/~tqchen/rabit/doc) - - You can also run ```./mkdoc.sh``` to make the document locally -* [Parameters](#parameters) - Parameters -==== +========== This section list all the parameters that can be passed to rabit::Init function as argv. -All the parameters are passed in as string in format of ```parameter-name=parameter-value```. +All the parameters are passed in as string in format of ``parameter-name=parameter-value``. In most setting these parameters have default value or will be automatically detected, and do not need to be manually configured. -* rabit_tracker_uri [passed in automatically by tracker] +* rabit_tracker_uri [passed in automatically by tracker] - The uri/ip of rabit tracker * rabit_tracker_port [passed in automatically by tracker] - The port of rabit tracker diff --git a/subtree/rabit/doc/python-requirements.txt b/subtree/rabit/doc/python-requirements.txt new file mode 100644 index 000000000..5970c4367 --- /dev/null +++ b/subtree/rabit/doc/python-requirements.txt @@ -0,0 +1,4 @@ +numpy +breathe +commonmark + diff --git a/subtree/rabit/doc/python_api.md b/subtree/rabit/doc/python_api.md new file mode 100644 index 000000000..8a0eda921 --- /dev/null +++ b/subtree/rabit/doc/python_api.md @@ -0,0 +1,11 @@ +Python API of Rabit +=================== +This page contains document of python API of rabit. + +```eval_rst +.. toctree:: + +.. automodule:: rabit + :members: + :show-inheritance: +``` diff --git a/subtree/rabit/doc/sphinx_util.py b/subtree/rabit/doc/sphinx_util.py new file mode 100644 index 000000000..f6a33ffa3 --- /dev/null +++ b/subtree/rabit/doc/sphinx_util.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +"""Helper utilty function for customization.""" +import sys +import os +import docutils +import subprocess + +if os.environ.get('READTHEDOCS', None) == 'True': + subprocess.call('cd ..; rm -rf recommonmark;' + + 'git clone https://github.com/tqchen/recommonmark', shell=True) + +sys.path.insert(0, os.path.abspath('../recommonmark/')) +from recommonmark import parser, transform + +MarkdownParser = parser.CommonMarkParser +AutoStructify = transform.AutoStructify diff --git a/subtree/rabit/guide/README b/subtree/rabit/guide/README new file mode 100644 index 000000000..2483d683f --- /dev/null +++ b/subtree/rabit/guide/README @@ -0,0 +1 @@ +See tutorial at ../doc/guide.md \ No newline at end of file diff --git a/subtree/rabit/guide/basic.cc b/subtree/rabit/guide/basic.cc index 62c0fc165..a9a729170 100644 --- a/subtree/rabit/guide/basic.cc +++ b/subtree/rabit/guide/basic.cc @@ -5,11 +5,17 @@ * * \author Tianqi Chen */ +#define _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_DEPRECATE +#include #include using namespace rabit; -const int N = 3; int main(int argc, char *argv[]) { - int a[N]; + int N = 3; + if (argc > 1) { + N = atoi(argv[1]); + } + std::vector a(N); rabit::Init(argc, argv); for (int i = 0; i < N; ++i) { a[i] = rabit::GetRank() + i; diff --git a/subtree/rabit/include/dmlc/io.h b/subtree/rabit/include/dmlc/io.h index e273763ca..66d590b2d 100644 --- a/subtree/rabit/include/dmlc/io.h +++ b/subtree/rabit/include/dmlc/io.h @@ -14,6 +14,7 @@ // include uint64_t only to make io standalone #ifdef _MSC_VER +/*! \brief uint64 */ typedef unsigned __int64 uint64_t; #else #include @@ -24,7 +25,7 @@ namespace dmlc { /*! * \brief interface of stream I/O for serialization */ -class Stream { +class Stream { // NOLINT(*) public: /*! * \brief reads data from a stream @@ -71,7 +72,7 @@ class Stream { /*! * \brief writes a string * \param str the string to be written/serialized - */ + */ inline void Write(const std::string &str); /*! * \brief loads a string @@ -94,7 +95,7 @@ class SeekStream: public Stream { * \brief generic factory function * create an SeekStream for read only, * the stream will close the underlying files upon deletion - * error will be reported and the system will exit when create failed + * error will be reported and the system will exit when create failed * \param uri the uri of the input currently we support * hdfs://, s3://, and file:// by default file:// will be used * \param allow_null whether NULL can be returned, or directly report error @@ -107,12 +108,12 @@ class SeekStream: public Stream { /*! \brief interface for serializable objects */ class Serializable { public: - /*! + /*! * \brief load the model from a stream * \param fi stream where to load the model from */ virtual void Load(Stream *fi) = 0; - /*! + /*! * \brief saves the model to a stream * \param fo stream where to save the model to */ @@ -123,7 +124,7 @@ class Serializable { * \brief input split creates that allows reading * of records from split of data, * independent part that covers all the dataset - * + * * see InputSplit::Create for definition of record */ class InputSplit { @@ -141,7 +142,7 @@ class InputSplit { * this is a hint so may not be enforced, * but InputSplit will try adjust its internal buffer * size to the hinted value - * \param chunk_size the chunk size + * \param chunk_size the chunk size */ virtual void HintChunkSize(size_t chunk_size) {} /*! \brief reset the position of InputSplit to beginning */ @@ -150,7 +151,7 @@ class InputSplit { * \brief get the next record, the returning value * is valid until next call to NextRecord or NextChunk * caller can modify the memory content of out_rec - * + * * For text, out_rec contains a single line * For recordio, out_rec contains one record content(with header striped) * @@ -161,11 +162,11 @@ class InputSplit { */ virtual bool NextRecord(Blob *out_rec) = 0; /*! - * \brief get a chunk of memory that can contain multiple records, + * \brief get a chunk of memory that can contain multiple records, * the caller needs to parse the content of the resulting chunk, * for text file, out_chunk can contain data of multiple lines * for recordio, out_chunk can contain multiple records(including headers) - * + * * This function ensures there won't be partial record in the chunk * caller can modify the memory content of out_chunk, * the memory is valid until next call to NextRecord or NextChunk @@ -192,9 +193,10 @@ class InputSplit { * List of possible types: "text", "recordio" * - "text": * text file, each line is treated as a record - * input split will split on \n or \r + * input split will split on '\\n' or '\\r' * - "recordio": * binary recordio file, see recordio.h + * \return a new input split * \sa InputSplit::Type */ static InputSplit* Create(const char *uri, @@ -224,7 +226,7 @@ class ostream : public std::basic_ostream { * \param buffer_size internal streambuf size */ explicit ostream(Stream *stream, - size_t buffer_size = 1 << 10) + size_t buffer_size = (1 << 10)) : std::basic_ostream(NULL), buf_(buffer_size) { this->set_stream(stream); } @@ -240,7 +242,7 @@ class ostream : public std::basic_ostream { buf_.set_stream(stream); this->rdbuf(&buf_); } - + private: // internal streambuf class OutBuf : public std::streambuf { @@ -251,7 +253,7 @@ class ostream : public std::basic_ostream { } // set stream to the buffer inline void set_stream(Stream *stream); - + private: /*! \brief internal stream by StreamBuf */ Stream *stream_; @@ -287,7 +289,7 @@ class istream : public std::basic_istream { * \param buffer_size internal buffer size */ explicit istream(Stream *stream, - size_t buffer_size = 1 << 10) + size_t buffer_size = (1 << 10)) : std::basic_istream(NULL), buf_(buffer_size) { this->set_stream(stream); } @@ -325,7 +327,7 @@ class istream : public std::basic_istream { Stream *stream_; /*! \brief how many bytes we read so far */ size_t bytes_read_; - /*! \brief internal buffer */ + /*! \brief internal buffer */ std::vector buffer_; // override underflow inline int_type underflow(); @@ -402,7 +404,7 @@ inline int ostream::OutBuf::overflow(int c) { // implementations for istream inline void istream::InBuf::set_stream(Stream *stream) { stream_ = stream; - this->setg(&buffer_[0], &buffer_[0], &buffer_[0]); + this->setg(&buffer_[0], &buffer_[0], &buffer_[0]); } inline int istream::InBuf::underflow() { char *bhead = &buffer_[0]; diff --git a/subtree/rabit/include/rabit.h b/subtree/rabit/include/rabit.h index 824b454bb..b0f1df39c 100644 --- a/subtree/rabit/include/rabit.h +++ b/subtree/rabit/include/rabit.h @@ -8,12 +8,18 @@ * rabit.h and serializable.h is all what the user needs to use the rabit interface * \author Tianqi Chen, Ignacio Cano, Tianyi Zhou */ -#ifndef RABIT_RABIT_H_ -#define RABIT_RABIT_H_ +#ifndef RABIT_RABIT_H_ // NOLINT(*) +#define RABIT_RABIT_H_ // NOLINT(*) #include #include + +// whether or not use c++11 support +#ifndef DMLC_USE_CXX11 +#define DMLC_USE_CXX11 (defined(__GXX_EXPERIMENTAL_CXX0X__) ||\ + __cplusplus >= 201103L || defined(_MSC_VER)) +#endif // optionally support of lambda functions in C++11, if available -#if __cplusplus >= 201103L +#if DMLC_USE_CXX11 #include #endif // C++11 // contains definition of Serializable @@ -56,8 +62,8 @@ struct BitOR; * \param argv the array of input arguments */ inline void Init(int argc, char *argv[]); -/*! - * \brief finalizes the rabit engine, call this function after you finished with all the jobs +/*! + * \brief finalizes the rabit engine, call this function after you finished with all the jobs */ inline void Finalize(void); /*! \brief gets rank of the current process */ @@ -71,7 +77,7 @@ inline bool IsDistributed(void); inline std::string GetProcessorName(void); /*! * \brief prints the msg to the tracker, - * this function can be used to communicate progress information to + * this function can be used to communicate progress information to * the user who monitors the tracker * \param msg the message to be printed */ @@ -89,7 +95,7 @@ inline void TrackerPrintf(const char *fmt, ...); /*! * \brief broadcasts a memory region to every node from the root * - * Example: int a = 1; Broadcast(&a, sizeof(a), root); + * Example: int a = 1; Broadcast(&a, sizeof(a), root); * \param sendrecv_data the pointer to the send/receive buffer, * \param size the data size * \param root the process root @@ -113,48 +119,54 @@ inline void Broadcast(std::vector *sendrecv_data, int root); */ inline void Broadcast(std::string *sendrecv_data, int root); /*! - * \brief performs in-place Allreduce on sendrecvbuf + * \brief performs in-place Allreduce on sendrecvbuf * this function is NOT thread-safe * * Example Usage: the following code does an Allreduce and outputs the sum as the result - * vector data(10); - * ... - * Allreduce(&data[0], data.size()); - * ... + * \code{.cpp} + * vector data(10); + * ... + * Allreduce(&data[0], data.size()); + * ... + * \endcode + * * \param sendrecvbuf buffer for both sending and receiving data * \param count number of elements to be reduced * \param prepare_fun Lazy preprocessing function, if it is not NULL, prepare_fun(prepare_arg) * will be called by the function before performing Allreduce in order to initialize the data in sendrecvbuf. * If the result of Allreduce can be recovered directly, then prepare_func will NOT be called - * \param prepare_arg argument used to pass into the lazy preprocessing function - * \tparam OP see namespace op, reduce operator + * \param prepare_arg argument used to pass into the lazy preprocessing function + * \tparam OP see namespace op, reduce operator * \tparam DType data type */ template inline void Allreduce(DType *sendrecvbuf, size_t count, - void (*prepare_fun)(void *arg) = NULL, + void (*prepare_fun)(void *) = NULL, void *prepare_arg = NULL); // C++11 support for lambda prepare function -#if __cplusplus >= 201103L +#if DMLC_USE_CXX11 /*! * \brief performs in-place Allreduce, on sendrecvbuf * with a prepare function specified by a lambda function * - * Example Usage: the following code does an Allreduce and outputs the sum as the result - * vector data(10); - * ... - * Allreduce(&data[0], data.size(), [&]() { - * for (int i = 0; i < 10; ++i) { - * data[i] = i; - * } - * }); + * Example Usage: + * \code{.cpp} + * // the following code does an Allreduce and outputs the sum as the result + * vector data(10); + * ... + * Allreduce(&data[0], data.size(), [&]() { + * for (int i = 0; i < 10; ++i) { + * data[i] = i; + * } + * }); * ... + * \endcode * \param sendrecvbuf buffer for both sending and receiving data * \param count number of elements to be reduced * \param prepare_fun Lazy lambda preprocessing function, prepare_fun() will be invoked * by the function before performing Allreduce in order to initialize the data in sendrecvbuf. * If the result of Allreduce can be recovered directly, then prepare_func will NOT be called - * \tparam OP see namespace op, reduce operator + * \tparam OP see namespace op, reduce operator * \tparam DType data type */ template @@ -168,19 +180,20 @@ inline void Allreduce(DType *sendrecvbuf, size_t count, * is the same in every node * \param local_model pointer to the local model that is specific to the current node/rank * this can be NULL when no local model is needed - * + * * \return the version number of the check point loaded * if returned version == 0, this means no model has been CheckPointed * the p_model is not touched, users should do the necessary initialization by themselves - * - * Common usage example: - * int iter = rabit::LoadCheckPoint(&model); - * if (iter == 0) model.InitParameters(); - * for (i = iter; i < max_iter; ++i) { - * do many things, include allreduce - * rabit::CheckPoint(model); - * } * + * \code{.cpp} + * // Example usage code of LoadCheckPoint + * int iter = rabit::LoadCheckPoint(&model); + * if (iter == 0) model.InitParameters(); + * for (i = iter; i < max_iter; ++i) { + * // do many things, include allreduce + * rabit::CheckPoint(model); + * } + * \endcode * \sa CheckPoint, VersionNumber */ inline int LoadCheckPoint(Serializable *global_model, @@ -188,7 +201,7 @@ inline int LoadCheckPoint(Serializable *global_model, /*! * \brief checkpoints the model, meaning a stage of execution has finished. * every time we call check point, a version number will be increased by one - * + * * \param global_model pointer to the globally shared model/state * when calling this function, the caller needs to guarantee that the global_model * is the same in every node @@ -204,16 +217,16 @@ inline void CheckPoint(const Serializable *global_model, /*! * \brief This function can be used to replace CheckPoint for global_model only, * when certain condition is met (see detailed explanation). - * + * * This is a "lazy" checkpoint such that only the pointer to the global_model is * remembered and no memory copy is taken. To use this function, the user MUST ensure that: * The global_model must remain unchanged until the last call of Allreduce/Broadcast in the current version finishes. - * In other words, the global_model model can be changed only between the last call of + * In other words, the global_model model can be changed only between the last call of * Allreduce/Broadcast and LazyCheckPoint, both in the same version - * + * * For example, suppose the calling sequence is: * LazyCheckPoint, code1, Allreduce, code2, Broadcast, code3, LazyCheckPoint/(or can be CheckPoint) - * + * * Then the user MUST only change the global_model in code3. * * The use of LazyCheckPoint instead of CheckPoint will improve the efficiency of the program. @@ -235,36 +248,36 @@ namespace engine { class ReduceHandle; } // namespace engine /*! - * \brief template class to make customized reduce and all reduce easy - * Do not use reducer directly in the function you call Finalize, + * \brief template class to make customized reduce and all reduce easy + * Do not use reducer directly in the function you call Finalize, * because the destructor can execute after Finalize * \tparam DType data type that to be reduced * \tparam freduce the customized reduction function * DType must be a struct, with no pointer */ -template +template // NOLINT(*) class Reducer { public: Reducer(void); /*! - * \brief customized in-place all reduce operation + * \brief customized in-place all reduce operation * \param sendrecvbuf the in place send-recv buffer * \param count number of elements to be reduced * \param prepare_fun Lazy preprocessing function, if it is not NULL, prepare_fun(prepare_arg) * will be called by the function before performing Allreduce, to initialize the data in sendrecvbuf. * If the result of Allreduce can be recovered directly, then prepare_func will NOT be called - * \param prepare_arg argument used to pass into the lazy preprocessing function + * \param prepare_arg argument used to pass into the lazy preprocessing function */ inline void Allreduce(DType *sendrecvbuf, size_t count, - void (*prepare_fun)(void *arg) = NULL, + void (*prepare_fun)(void *) = NULL, void *prepare_arg = NULL); -#if __cplusplus >= 201103L +#if DMLC_USE_CXX11 /*! * \brief customized in-place all reduce operation, with lambda function as preprocessor * \param sendrecvbuf pointer to the array of objects to be reduced * \param count number of elements to be reduced * \param prepare_fun lambda function executed to prepare the data, if necessary - */ + */ inline void Allreduce(DType *sendrecvbuf, size_t count, std::function prepare_fun); #endif @@ -278,7 +291,7 @@ class Reducer { * this class defines complex reducer handles all the data structure that can be * serialized/deserialized into fixed size buffer * Do not use reducer directly in the function you call Finalize, because the destructor can execute after Finalize - * + * * \tparam DType data type that to be reduced, DType must contain the following functions: * \tparam freduce the customized reduction function * (1) Save(IStream &fs) (2) Load(IStream &fs) (3) Reduce(const DType &src, size_t max_nbyte) @@ -288,7 +301,7 @@ class SerializeReducer { public: SerializeReducer(void); /*! - * \brief customized in-place all reduce operation + * \brief customized in-place all reduce operation * \param sendrecvobj pointer to the array of objects to be reduced * \param max_nbyte maximum amount of memory needed to serialize each object * this includes budget limit for intermediate and final result @@ -296,14 +309,14 @@ class SerializeReducer { * \param prepare_fun Lazy preprocessing function, if it is not NULL, prepare_fun(prepare_arg) * will be called by the function before performing Allreduce, to initialize the data in sendrecvbuf. * If the result of Allreduce can be recovered directly, then the prepare_func will NOT be called - * \param prepare_arg argument used to pass into the lazy preprocessing function + * \param prepare_arg argument used to pass into the lazy preprocessing function */ inline void Allreduce(DType *sendrecvobj, size_t max_nbyte, size_t count, - void (*prepare_fun)(void *arg) = NULL, + void (*prepare_fun)(void *) = NULL, void *prepare_arg = NULL); // C++11 support for lambda prepare function -#if __cplusplus >= 201103L +#if DMLC_USE_CXX11 /*! * \brief customized in-place all reduce operation, with lambda function as preprocessor * \param sendrecvobj pointer to the array of objects to be reduced @@ -311,7 +324,7 @@ class SerializeReducer { * this includes budget limit for intermediate and final result * \param count number of elements to be reduced * \param prepare_fun lambda function executed to prepare the data, if necessary - */ + */ inline void Allreduce(DType *sendrecvobj, size_t max_nbyte, size_t count, std::function prepare_fun); @@ -326,4 +339,4 @@ class SerializeReducer { } // namespace rabit // implementation of template functions #include "./rabit/rabit-inl.h" -#endif // RABIT_RABIT_H_ +#endif // RABIT_RABIT_H_ // NOLINT(*) diff --git a/subtree/rabit/include/rabit/engine.h b/subtree/rabit/include/rabit/engine.h index a2f5da25b..272bbb8ef 100644 --- a/subtree/rabit/include/rabit/engine.h +++ b/subtree/rabit/include/rabit/engine.h @@ -183,7 +183,9 @@ enum DataType { kLong = 4, kULong = 5, kFloat = 6, - kDouble = 7 + kDouble = 7, + kLongLong = 8, + kULongLong = 9 }; } // namespace mpi /*! diff --git a/subtree/rabit/include/rabit/io.h b/subtree/rabit/include/rabit/io.h index a0eb0adb8..7ffca38f2 100644 --- a/subtree/rabit/include/rabit/io.h +++ b/subtree/rabit/include/rabit/io.h @@ -4,8 +4,8 @@ * \brief utilities with different serializable implementations * \author Tianqi Chen */ -#ifndef RABIT_UTILS_IO_H_ -#define RABIT_UTILS_IO_H_ +#ifndef RABIT_IO_H_ +#define RABIT_IO_H_ #include #include #include @@ -51,6 +51,7 @@ struct MemoryFixSizeBuffer : public SeekStream { virtual bool AtEnd(void) const { return curr_ptr_ == buffer_size_; } + private: /*! \brief in memory buffer */ char *p_buffer_; @@ -93,6 +94,7 @@ struct MemoryBufferStream : public SeekStream { virtual bool AtEnd(void) const { return curr_ptr_ == p_buffer_->length(); } + private: /*! \brief in memory buffer */ std::string *p_buffer_; @@ -101,4 +103,4 @@ struct MemoryBufferStream : public SeekStream { }; // class MemoryBufferStream } // namespace utils } // namespace rabit -#endif // RABIT_UTILS_IO_H_ +#endif // RABIT_IO_H_ diff --git a/subtree/rabit/include/rabit/rabit-inl.h b/subtree/rabit/include/rabit/rabit-inl.h index 3d1ec59a8..e82b5a9a0 100644 --- a/subtree/rabit/include/rabit/rabit-inl.h +++ b/subtree/rabit/include/rabit/rabit-inl.h @@ -1,12 +1,15 @@ /*! + * Copyright by Contributors * \file rabit-inl.h * \brief implementation of inline template function for rabit interface * * \author Tianqi Chen */ -#ifndef RABIT_RABIT_INL_H -#define RABIT_RABIT_INL_H +#ifndef RABIT_RABIT_INL_H_ +#define RABIT_RABIT_INL_H_ // use engine for implementation +#include +#include #include "./io.h" #include "./utils.h" #include "../rabit.h" @@ -30,15 +33,15 @@ inline DataType GetType(void) { return kInt; } template<> -inline DataType GetType(void) { +inline DataType GetType(void) { // NOLINT(*) return kUInt; } template<> -inline DataType GetType(void) { +inline DataType GetType(void) { // NOLINT(*) return kLong; } template<> -inline DataType GetType(void) { +inline DataType GetType(void) { // NOLINT(*) return kULong; } template<> @@ -49,47 +52,55 @@ template<> inline DataType GetType(void) { return kDouble; } +template<> +inline DataType GetType(void) { // NOLINT(*) + return kLongLong; +} +template<> +inline DataType GetType(void) { // NOLINT(*) + return kULongLong; +} } // namespace mpi } // namespace engine namespace op { struct Max { - const static engine::mpi::OpType kType = engine::mpi::kMax; + static const engine::mpi::OpType kType = engine::mpi::kMax; template - inline static void Reduce(DType &dst, const DType &src) { + inline static void Reduce(DType &dst, const DType &src) { // NOLINT(*) if (dst < src) dst = src; } }; struct Min { - const static engine::mpi::OpType kType = engine::mpi::kMin; + static const engine::mpi::OpType kType = engine::mpi::kMin; template - inline static void Reduce(DType &dst, const DType &src) { + inline static void Reduce(DType &dst, const DType &src) { // NOLINT(*) if (dst > src) dst = src; } }; struct Sum { - const static engine::mpi::OpType kType = engine::mpi::kSum; + static const engine::mpi::OpType kType = engine::mpi::kSum; template - inline static void Reduce(DType &dst, const DType &src) { + inline static void Reduce(DType &dst, const DType &src) { // NOLINT(*) dst += src; } }; struct BitOR { - const static engine::mpi::OpType kType = engine::mpi::kBitwiseOR; + static const engine::mpi::OpType kType = engine::mpi::kBitwiseOR; template - inline static void Reduce(DType &dst, const DType &src) { + inline static void Reduce(DType &dst, const DType &src) { // NOLINT(*) dst |= src; } }; template inline void Reducer(const void *src_, void *dst_, int len, const MPI::Datatype &dtype) { const DType *src = (const DType*)src_; - DType *dst = (DType*)dst_; + DType *dst = (DType*)dst_; // NOLINT(*) for (int i = 0; i < len; ++i) { OP::Reduce(dst[i], src[i]); } } -} // namespace op +} // namespace op // intialize the rabit engine inline void Init(int argc, char *argv[]) { @@ -144,23 +155,23 @@ inline void Broadcast(std::string *sendrecv_data, int root) { // perform inplace Allreduce template inline void Allreduce(DType *sendrecvbuf, size_t count, - void (*prepare_fun)(void *arg), + void (*prepare_fun)(void *arg), void *prepare_arg) { - engine::Allreduce_(sendrecvbuf, sizeof(DType), count, op::Reducer, + engine::Allreduce_(sendrecvbuf, sizeof(DType), count, op::Reducer, engine::mpi::GetType(), OP::kType, prepare_fun, prepare_arg); } // C++11 support for lambda prepare function -#if __cplusplus >= 201103L +#if DMLC_USE_CXX11 inline void InvokeLambda_(void *fun) { (*static_cast*>(fun))(); } template inline void Allreduce(DType *sendrecvbuf, size_t count, std::function prepare_fun) { - engine::Allreduce_(sendrecvbuf, sizeof(DType), count, op::Reducer, + engine::Allreduce_(sendrecvbuf, sizeof(DType), count, op::Reducer, engine::mpi::GetType(), OP::kType, InvokeLambda_, &prepare_fun); } -#endif // C++11 +#endif // C++11 // print message to the tracker inline void TrackerPrint(const std::string &msg) { @@ -215,15 +226,16 @@ inline void ReducerSafe_(const void *src_, void *dst_, int len_, const MPI::Data } } // function to perform reduction for Reducer -template -inline void ReducerAlign_(const void *src_, void *dst_, int len_, const MPI::Datatype &dtype) { +template // NOLINT(*) +inline void ReducerAlign_(const void *src_, void *dst_, + int len_, const MPI::Datatype &dtype) { const DType *psrc = reinterpret_cast(src_); DType *pdst = reinterpret_cast(dst_); for (int i = 0; i < len_; ++i) { freduce(pdst[i], psrc[i]); } } -template +template // NOLINT(*) inline Reducer::Reducer(void) { // it is safe to directly use handle for aligned data types if (sizeof(DType) == 8 || sizeof(DType) == 4 || sizeof(DType) == 1) { @@ -232,7 +244,7 @@ inline Reducer::Reducer(void) { this->handle_.Init(ReducerSafe_, sizeof(DType)); } } -template +template // NOLINT(*) inline void Reducer::Allreduce(DType *sendrecvbuf, size_t count, void (*prepare_fun)(void *arg), void *prepare_arg) { @@ -240,13 +252,14 @@ inline void Reducer::Allreduce(DType *sendrecvbuf, size_t count, } // function to perform reduction for SerializeReducer template -inline void SerializeReducerFunc_(const void *src_, void *dst_, int len_, const MPI::Datatype &dtype) { +inline void SerializeReducerFunc_(const void *src_, void *dst_, + int len_, const MPI::Datatype &dtype) { int nbytes = engine::ReduceHandle::TypeSize(dtype); // temp space DType tsrc, tdst; for (int i = 0; i < len_; ++i) { - utils::MemoryFixSizeBuffer fsrc((char*)(src_) + i * nbytes, nbytes); - utils::MemoryFixSizeBuffer fdst((char*)(dst_) + i * nbytes, nbytes); + utils::MemoryFixSizeBuffer fsrc((char*)(src_) + i * nbytes, nbytes); // NOLINT(*) + utils::MemoryFixSizeBuffer fdst((char*)(dst_) + i * nbytes, nbytes); // NOLINT(*) tsrc.Load(fsrc); tdst.Load(fdst); // govern const check @@ -288,8 +301,8 @@ inline void SerializeReducer::Allreduce(DType *sendrecvobj, // setup closure SerializeReduceClosure c; c.sendrecvobj = sendrecvobj; c.max_nbyte = max_nbyte; c.count = count; - c.prepare_fun = prepare_fun; c.prepare_arg = prepare_arg; c.p_buffer = &buffer_; - // invoke here + c.prepare_fun = prepare_fun; c.prepare_arg = prepare_arg; c.p_buffer = &buffer_; + // invoke here handle_.Allreduce(BeginPtr(buffer_), max_nbyte, count, SerializeReduceClosure::Invoke, &c); for (size_t i = 0; i < count; ++i) { @@ -298,8 +311,8 @@ inline void SerializeReducer::Allreduce(DType *sendrecvobj, } } -#if __cplusplus >= 201103L -template +#if DMLC_USE_CXX11 +template // NOLINT(*)g inline void Reducer::Allreduce(DType *sendrecvbuf, size_t count, std::function prepare_fun) { this->Allreduce(sendrecvbuf, count, InvokeLambda_, &prepare_fun); @@ -312,4 +325,4 @@ inline void SerializeReducer::Allreduce(DType *sendrecvobj, } #endif } // namespace rabit -#endif +#endif // RABIT_RABIT_INL_H_ diff --git a/subtree/rabit/include/rabit/timer.h b/subtree/rabit/include/rabit/timer.h index 46b7affc4..1f135add6 100644 --- a/subtree/rabit/include/rabit/timer.h +++ b/subtree/rabit/include/rabit/timer.h @@ -1,4 +1,5 @@ /*! + * Copyright by Contributors * \file timer.h * \brief This file defines the utils for timing * \author Tianqi Chen, Nacho, Tianyi @@ -18,7 +19,6 @@ namespace utils { * \brief return time in seconds, not cross platform, avoid to use this in most places */ inline double GetTime(void) { - // TODO: use c++11 chrono when c++11 was available #ifdef __MACH__ clock_serv_t cclock; mach_timespec_t mts; @@ -32,7 +32,6 @@ inline double GetTime(void) { utils::Check(clock_gettime(CLOCK_REALTIME, &ts) == 0, "failed to get time"); return static_cast(ts.tv_sec) + static_cast(ts.tv_nsec) * 1e-9; #else - // TODO: add MSVC macro, and MSVC timer return static_cast(time(NULL)); #endif #endif diff --git a/subtree/rabit/include/rabit/utils.h b/subtree/rabit/include/rabit/utils.h index 0f48fa0fa..28709ee7d 100644 --- a/subtree/rabit/include/rabit/utils.h +++ b/subtree/rabit/include/rabit/utils.h @@ -27,7 +27,7 @@ #else #ifdef _FILE_OFFSET_BITS #if _FILE_OFFSET_BITS == 32 -#pragma message ("Warning: FILE OFFSET BITS defined to be 32 bit") +#pragma message("Warning: FILE OFFSET BITS defined to be 32 bit") #endif #endif @@ -59,17 +59,17 @@ namespace utils { const int kPrintBuffer = 1 << 12; #ifndef RABIT_CUSTOMIZE_MSG_ -/*! +/*! * \brief handling of Assert error, caused by inappropriate input - * \param msg error message + * \param msg error message */ inline void HandleAssertError(const char *msg) { fprintf(stderr, "AssertError:%s\n", msg); exit(-1); } -/*! +/*! * \brief handling of Check error, caused by inappropriate input - * \param msg error message + * \param msg error message */ inline void HandleCheckError(const char *msg) { fprintf(stderr, "%s\n", msg); @@ -163,7 +163,7 @@ inline std::FILE *FopenCheck(const char *fname, const char *flag) { // easy utils that can be directly accessed in xgboost /*! \brief get the beginning address of a vector */ template -inline T *BeginPtr(std::vector &vec) { +inline T *BeginPtr(std::vector &vec) { // NOLINT(*) if (vec.size() == 0) { return NULL; } else { @@ -172,14 +172,14 @@ inline T *BeginPtr(std::vector &vec) { } /*! \brief get the beginning address of a vector */ template -inline const T *BeginPtr(const std::vector &vec) { +inline const T *BeginPtr(const std::vector &vec) { // NOLINT(*) if (vec.size() == 0) { return NULL; } else { return &vec[0]; } } -inline char* BeginPtr(std::string &str) { +inline char* BeginPtr(std::string &str) { // NOLINT(*) if (str.length() == 0) return NULL; return &str[0]; } diff --git a/subtree/rabit/include/rabit_serializable.h b/subtree/rabit/include/rabit_serializable.h index 40266575b..c9199bba1 100644 --- a/subtree/rabit/include/rabit_serializable.h +++ b/subtree/rabit/include/rabit_serializable.h @@ -4,8 +4,8 @@ * \brief defines serializable interface of rabit * \author Tianqi Chen */ -#ifndef RABIT_RABIT_SERIALIZABLE_H_ -#define RABIT_RABIT_SERIALIZABLE_H_ +#ifndef RABIT_SERIALIZABLE_H_ +#define RABIT_SERIALIZABLE_H_ #include #include #include "./rabit/utils.h" @@ -13,15 +13,15 @@ namespace rabit { /*! - * \brief defines stream used in rabit - * see definition of Stream in dmlc/io.h + * \brief defines stream used in rabit + * see definition of Stream in dmlc/io.h */ typedef dmlc::Stream Stream; /*! - * \brief defines serializable objects used in rabit - * see definition of Serializable in dmlc/io.h + * \brief defines serializable objects used in rabit + * see definition of Serializable in dmlc/io.h */ typedef dmlc::Serializable Serializable; } // namespace rabit -#endif // RABIT_RABIT_SERIALIZABLE_H_ +#endif // RABIT_SERIALIZABLE_H_ diff --git a/subtree/rabit/scripts/travis_runtest.sh b/subtree/rabit/scripts/travis_runtest.sh new file mode 100755 index 000000000..f57141c6c --- /dev/null +++ b/subtree/rabit/scripts/travis_runtest.sh @@ -0,0 +1,8 @@ +#!/bin/bash +make -f test.mk model_recover_10_10k || exit -1 +make -f test.mk model_recover_10_10k_die_same || exit -1 +make -f test.mk local_recover_10_10k || exit -1 +make -f test.mk pylocal_recover_10_10k || exit -1 +make -f test.mk lazy_recover_10_10k_die_hard || exit -1 +make -f test.mk lazy_recover_10_10k_die_same || exit -1 +make -f test.mk ringallreduce_10_10k || exit -1 \ No newline at end of file diff --git a/subtree/rabit/scripts/travis_script.sh b/subtree/rabit/scripts/travis_script.sh new file mode 100755 index 000000000..664582906 --- /dev/null +++ b/subtree/rabit/scripts/travis_script.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# main script of travis +if [ ${TASK} == "lint" ]; then + make lint || exit -1 +fi + +if [ ${TASK} == "doc" ]; then + make doc 2>log.txt + (cat log.txt| grep -v ENABLE_PREPROCESSING |grep -v "unsupported tag" |grep warning) && exit -1 +fi + +if [ ${TASK} == "build" ]; then + make all || exit -1 +fi + +if [ ${TASK} == "test" ]; then + cd test + make all || exit -1 + ../scripts/travis_runtest.sh || exit -1 +fi + diff --git a/subtree/rabit/src/allreduce_base.cc b/subtree/rabit/src/allreduce_base.cc index 964738b34..917d1dffb 100644 --- a/subtree/rabit/src/allreduce_base.cc +++ b/subtree/rabit/src/allreduce_base.cc @@ -24,6 +24,7 @@ AllreduceBase::AllreduceBase(void) { nport_trial = 1000; rank = 0; world_size = -1; + connect_retry = 5; hadoop_mode = 0; version_number = 0; // 32 K items @@ -46,6 +47,7 @@ AllreduceBase::AllreduceBase(void) { env_vars.push_back("DMLC_NUM_ATTEMPT"); env_vars.push_back("DMLC_TRACKER_URI"); env_vars.push_back("DMLC_TRACKER_PORT"); + env_vars.push_back("DMLC_WORKER_CONNECT_RETRY"); } // initialization function @@ -94,7 +96,8 @@ void AllreduceBase::Init(void) { } } if (dmlc_role != "worker") { - fprintf(stderr, "Rabit Module currently only work with dmlc worker, quit this program by exit 0\n"); + fprintf(stderr, "Rabit Module currently only work with dmlc worker"\ + ", quit this program by exit 0\n"); exit(0); } // clear the setting before start reconnection @@ -134,7 +137,7 @@ void AllreduceBase::TrackerPrint(const std::string &msg) { // util to parse data with unit suffix inline size_t ParseUnit(const char *name, const char *val) { char unit; - unsigned long amt; + unsigned long amt; // NOLINT(*) int n = sscanf(val, "%lu%c", &amt, &unit); size_t amount = amt; if (n == 2) { @@ -154,7 +157,7 @@ inline size_t ParseUnit(const char *name, const char *val) { } } /*! - * \brief set parameters to the engine + * \brief set parameters to the engine * \param name parameter name * \param val parameter value */ @@ -174,6 +177,9 @@ void AllreduceBase::SetParam(const char *name, const char *val) { if (!strcmp(name, "rabit_reduce_buffer")) { reduce_buffer_size = (ParseUnit(name, val) + 7) >> 3; } + if (!strcmp(name, "DMLC_WORKER_CONNECT_RETRY")) { + connect_retry = atoi(val); + } } /*! * \brief initialize connection to the tracker @@ -184,9 +190,23 @@ utils::TCPSocket AllreduceBase::ConnectTracker(void) const { // get information from tracker utils::TCPSocket tracker; tracker.Create(); - if (!tracker.Connect(utils::SockAddr(tracker_uri.c_str(), tracker_port))) { - utils::Socket::Error("Connect"); - } + + int retry = 0; + do { + fprintf(stderr, "connect to ip: [%s]\n", tracker_uri.c_str()); + if (!tracker.Connect(utils::SockAddr(tracker_uri.c_str(), tracker_port))) { + if (++retry >= connect_retry) { + fprintf(stderr, "connect to (failed): [%s]\n", tracker_uri.c_str()); + utils::Socket::Error("Connect"); + } else { + fprintf(stderr, "retry connect to ip(retry time %d): [%s]\n", retry, tracker_uri.c_str()); + sleep(1); + continue; + } + } + break; + } while (1); + using utils::Assert; Assert(tracker.SendAll(&magic, sizeof(magic)) == sizeof(magic), "ReConnectLink failure 1"); @@ -258,7 +278,7 @@ void AllreduceBase::ReConnectLinks(const char *cmd) { } else { if (!all_links[i].sock.IsClosed()) all_links[i].sock.Close(); } - } + } int ngood = static_cast(good_link.size()); Assert(tracker.SendAll(&ngood, sizeof(ngood)) == sizeof(ngood), "ReConnectLink failure 5"); @@ -359,7 +379,7 @@ void AllreduceBase::ReConnectLinks(const char *cmd) { * The kSuccess TryAllreduce does NOT mean every node have successfully finishes TryAllreduce. * It only means the current node get the correct result of Allreduce. * However, it means every node finishes LAST call(instead of this one) of Allreduce/Bcast - * + * * \param sendrecvbuf_ buffer for both sending and recving data * \param type_nbytes the unit number of bytes the type have * \param count number of elements to be reduced @@ -440,7 +460,7 @@ AllreduceBase::TryAllreduceTree(void *sendrecvbuf_, selecter.WatchRead(links[i].sock); } // size_write <= size_read - if (links[i].size_write != total_size){ + if (links[i].size_write != total_size) { if (links[i].size_write < size_down_in) { selecter.WatchWrite(links[i].sock); } @@ -477,7 +497,7 @@ AllreduceBase::TryAllreduceTree(void *sendrecvbuf_, size_t max_reduce = total_size; for (int i = 0; i < nlink; ++i) { if (i != parent_index) { - max_reduce= std::min(max_reduce, links[i].size_read); + max_reduce = std::min(max_reduce, links[i].size_read); utils::Assert(buffer_size == 0 || buffer_size == links[i].buffer_size, "buffer size inconsistent"); buffer_size = links[i].buffer_size; @@ -513,7 +533,7 @@ AllreduceBase::TryAllreduceTree(void *sendrecvbuf_, if (len != -1) { size_up_out += static_cast(len); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) { return ReportError(&links[parent_index], ret); } @@ -525,7 +545,7 @@ AllreduceBase::TryAllreduceTree(void *sendrecvbuf_, ssize_t len = links[parent_index].sock. Recv(sendrecvbuf + size_down_in, total_size - size_down_in); if (len == 0) { - links[parent_index].sock.Close(); + links[parent_index].sock.Close(); return ReportError(&links[parent_index], kRecvZeroLen); } if (len != -1) { @@ -533,7 +553,7 @@ AllreduceBase::TryAllreduceTree(void *sendrecvbuf_, utils::Assert(size_down_in <= size_up_out, "Allreduce: boundary error"); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) { return ReportError(&links[parent_index], ret); } @@ -670,7 +690,7 @@ AllreduceBase::TryAllgatherRing(void *sendrecvbuf_, size_t total_size, size_t slice_begin, size_t slice_end, size_t size_prev_slice) { - // read from next link and send to prev one + // read from next link and send to prev one LinkRecord &prev = *ring_prev, &next = *ring_next; // need to reply on special rank structure utils::Assert(next.rank == (rank + 1) % world_size && @@ -678,11 +698,11 @@ AllreduceBase::TryAllgatherRing(void *sendrecvbuf_, size_t total_size, "need to assume rank structure"); // send recv buffer char *sendrecvbuf = reinterpret_cast(sendrecvbuf_); - const size_t stop_read = total_size + slice_begin; - const size_t stop_write = total_size + slice_begin - size_prev_slice; + const size_t stop_read = total_size + slice_begin; + const size_t stop_write = total_size + slice_begin - size_prev_slice; size_t write_ptr = slice_begin; size_t read_ptr = slice_end; - + while (true) { // select helper bool finished = true; @@ -709,7 +729,7 @@ AllreduceBase::TryAllgatherRing(void *sendrecvbuf_, size_t total_size, if (len != -1) { read_ptr += static_cast(len); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) return ReportError(&next, ret); } } @@ -723,7 +743,7 @@ AllreduceBase::TryAllgatherRing(void *sendrecvbuf_, size_t total_size, if (len != -1) { write_ptr += static_cast(len); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) return ReportError(&prev, ret); } } @@ -733,7 +753,7 @@ AllreduceBase::TryAllgatherRing(void *sendrecvbuf_, size_t total_size, /*! * \brief perform in-place allreduce, on sendrecvbuf, this function can fail, * and will return the cause of failure - * + * * Ring-based algorithm * * \param sendrecvbuf_ buffer for both sending and recving data @@ -748,7 +768,7 @@ AllreduceBase::TryReduceScatterRing(void *sendrecvbuf_, size_t type_nbytes, size_t count, ReduceFunction reducer) { - // read from next link and send to prev one + // read from next link and send to prev one LinkRecord &prev = *ring_prev, &next = *ring_next; // need to reply on special rank structure utils::Assert(next.rank == (rank + 1) % world_size && @@ -757,7 +777,7 @@ AllreduceBase::TryReduceScatterRing(void *sendrecvbuf_, // total size of message const size_t total_size = type_nbytes * count; size_t n = static_cast(world_size); - size_t step = (count + n - 1) / n; + size_t step = (count + n - 1) / n; size_t r = static_cast(next.rank); size_t write_ptr = std::min(r * step, count) * type_nbytes; size_t read_ptr = std::min((r + 1) * step, count) * type_nbytes; @@ -826,11 +846,11 @@ AllreduceBase::TryReduceScatterRing(void *sendrecvbuf_, if (len != -1) { write_ptr += static_cast(len); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) return ReportError(&prev, ret); } } - } + } return kSuccess; } /*! @@ -857,7 +877,7 @@ AllreduceBase::TryAllreduceRing(void *sendrecvbuf_, size_t end = std::min((rank + 1) * step, count) * type_nbytes; // previous rank int prank = ring_prev->rank; - // get rank of previous + // get rank of previous return TryAllgatherRing (sendrecvbuf_, type_nbytes * count, begin, end, diff --git a/subtree/rabit/src/allreduce_base.h b/subtree/rabit/src/allreduce_base.h index c34eb6042..63acd75d5 100644 --- a/subtree/rabit/src/allreduce_base.h +++ b/subtree/rabit/src/allreduce_base.h @@ -42,7 +42,7 @@ class AllreduceBase : public IEngine { // shutdown the engine virtual void Shutdown(void); /*! - * \brief set parameters to the engine + * \brief set parameters to the engine * \param name parameter name * \param val parameter value */ @@ -72,7 +72,7 @@ class AllreduceBase : public IEngine { return host_uri; } /*! - * \brief perform in-place allreduce, on sendrecvbuf + * \brief perform in-place allreduce, on sendrecvbuf * this function is NOT thread-safe * \param sendrecvbuf_ buffer for both sending and recving data * \param type_nbytes the unit number of bytes the type have @@ -82,7 +82,7 @@ class AllreduceBase : public IEngine { * will be called by the function before performing Allreduce, to intialize the data in sendrecvbuf_. * If the result of Allreduce can be recovered directly, then prepare_func will NOT be called * \param prepare_arg argument used to passed into the lazy preprocessing function - */ + */ virtual void Allreduce(void *sendrecvbuf_, size_t type_nbytes, size_t count, @@ -90,6 +90,7 @@ class AllreduceBase : public IEngine { PreprocFunction prepare_fun = NULL, void *prepare_arg = NULL) { if (prepare_fun != NULL) prepare_fun(prepare_arg); + if (world_size == 1) return; utils::Assert(TryAllreduce(sendrecvbuf_, type_nbytes, count, reducer) == kSuccess, "Allreduce failed"); @@ -101,6 +102,7 @@ class AllreduceBase : public IEngine { * \param root the root worker id to broadcast the data */ virtual void Broadcast(void *sendrecvbuf_, size_t total_size, int root) { + if (world_size == 1) return; utils::Assert(TryBroadcast(sendrecvbuf_, total_size, root) == kSuccess, "Broadcast failed"); } @@ -115,14 +117,14 @@ class AllreduceBase : public IEngine { * \return the version number of check point loaded * if returned version == 0, this means no model has been CheckPointed * the p_model is not touched, user should do necessary initialization by themselves - * + * * Common usage example: * int iter = rabit::LoadCheckPoint(&model); * if (iter == 0) model.InitParameters(); * for (i = iter; i < max_iter; ++i) { * do many things, include allreduce * rabit::CheckPoint(model); - * } + * } * * \sa CheckPoint, VersionNumber */ @@ -133,7 +135,7 @@ class AllreduceBase : public IEngine { /*! * \brief checkpoint the model, meaning we finished a stage of execution * every time we call check point, there is a version number which will increase by one - * + * * \param global_model pointer to the globally shared model/state * when calling this function, the caller need to gauranttees that global_model * is the same in all nodes @@ -153,16 +155,16 @@ class AllreduceBase : public IEngine { /*! * \brief This function can be used to replace CheckPoint for global_model only, * when certain condition is met(see detailed expplaination). - * + * * This is a "lazy" checkpoint such that only the pointer to global_model is * remembered and no memory copy is taken. To use this function, the user MUST ensure that: * The global_model must remain unchanged util last call of Allreduce/Broadcast in current version finishs. - * In another words, global_model model can be changed only between last call of + * In another words, global_model model can be changed only between last call of * Allreduce/Broadcast and LazyCheckPoint in current version - * + * * For example, suppose the calling sequence is: * LazyCheckPoint, code1, Allreduce, code2, Broadcast, code3, LazyCheckPoint - * + * * If user can only changes global_model in code3, then LazyCheckPoint can be used to * improve efficiency of the program. * \param global_model pointer to the globally shared model/state @@ -189,8 +191,8 @@ class AllreduceBase : public IEngine { virtual void InitAfterException(void) { utils::Error("InitAfterException: not implemented"); } - /*! - * \brief report current status to the job tracker + /*! + * \brief report current status to the job tracker * depending on the job tracker we are in */ inline void ReportStatus(void) const { @@ -211,7 +213,7 @@ class AllreduceBase : public IEngine { kRecvZeroLen, /*! \brief a neighbor node go down, the connection is dropped */ kSockError, - /*! + /*! * \brief another node which is not my neighbor go down, * get Out-of-Band exception notification from my neighbor */ @@ -223,7 +225,7 @@ class AllreduceBase : public IEngine { ReturnTypeEnum value; // constructor ReturnType() {} - ReturnType(ReturnTypeEnum value) : value(value){} + ReturnType(ReturnTypeEnum value) : value(value) {} // NOLINT(*) inline bool operator==(const ReturnTypeEnum &v) const { return value == v; } @@ -232,8 +234,13 @@ class AllreduceBase : public IEngine { } }; /*! \brief translate errno to return type */ - inline static ReturnType Errno2Return(int errsv) { - if (errsv == EAGAIN || errsv == EWOULDBLOCK) return kSuccess; + inline static ReturnType Errno2Return() { + int errsv = utils::Socket::GetLastError(); + if (errsv == EAGAIN || errsv == EWOULDBLOCK || errsv == 0) return kSuccess; +#ifdef _WIN32 + if (errsv == WSAEWOULDBLOCK) return kSuccess; + if (errsv == WSAECONNRESET) return kConnReset; +#endif if (errsv == ECONNRESET) return kConnReset; return kSockError; } @@ -253,7 +260,7 @@ class AllreduceBase : public IEngine { // buffer size, in bytes size_t buffer_size; // constructor - LinkRecord(void) + LinkRecord(void) : buffer_head(NULL), buffer_size(0) { } // initialize buffer @@ -297,7 +304,7 @@ class AllreduceBase : public IEngine { if (len == 0) { sock.Close(); return kRecvZeroLen; } - if (len == -1) return Errno2Return(errno); + if (len == -1) return Errno2Return(); size_read += static_cast(len); return kSuccess; } @@ -316,7 +323,7 @@ class AllreduceBase : public IEngine { if (len == 0) { sock.Close(); return kRecvZeroLen; } - if (len == -1) return Errno2Return(errno); + if (len == -1) return Errno2Return(); size_read += static_cast(len); return kSuccess; } @@ -329,7 +336,7 @@ class AllreduceBase : public IEngine { inline ReturnType WriteFromArray(const void *sendbuf_, size_t max_size) { const char *p = static_cast(sendbuf_); ssize_t len = sock.Send(p + size_write, max_size - size_write); - if (len == -1) return Errno2Return(errno); + if (len == -1) return Errno2Return(); size_write += static_cast(len); return kSuccess; } @@ -370,7 +377,7 @@ class AllreduceBase : public IEngine { * The kSuccess TryAllreduce does NOT mean every node have successfully finishes TryAllreduce. * It only means the current node get the correct result of Allreduce. * However, it means every node finishes LAST call(instead of this one) of Allreduce/Bcast - * + * * \param sendrecvbuf_ buffer for both sending and recving data * \param type_nbytes the unit number of bytes the type have * \param count number of elements to be reduced @@ -390,7 +397,7 @@ class AllreduceBase : public IEngine { * \return this function can return kSuccess, kSockError, kGetExcept, see ReturnType for details * \sa ReturnType */ - ReturnType TryBroadcast(void *sendrecvbuf_, size_t size, int root); + ReturnType TryBroadcast(void *sendrecvbuf_, size_t size, int root); /*! * \brief perform in-place allreduce, on sendrecvbuf, * this function implements tree-shape reduction @@ -426,14 +433,14 @@ class AllreduceBase : public IEngine { size_t size_prev_slice); /*! * \brief perform in-place allreduce, reduce on the sendrecvbuf, - * + * * after the function, node k get k-th segment of the reduction result * the k-th segment is defined by [k * step, min((k + 1) * step,count) ) * where step = ceil(count / world_size) * * \param sendrecvbuf_ buffer for both sending and recving data * \param type_nbytes the unit number of bytes the type have - * \param count number of elements to be reduced + * \param count number of elements to be reduced * \param reducer reduce function * \return this function can return kSuccess, kSockError, kGetExcept, see ReturnType for details * \sa ReturnType, TryAllreduce @@ -458,7 +465,7 @@ class AllreduceBase : public IEngine { size_t count, ReduceFunction reducer); /*! - * \brief function used to report error when a link goes wrong + * \brief function used to report error when a link goes wrong * \param link the pointer to the link who causes the error * \param err the error type */ @@ -512,7 +519,9 @@ class AllreduceBase : public IEngine { int rank; // world size int world_size; + // connect retry time + int connect_retry; }; } // namespace engine } // namespace rabit -#endif // RABIT_ALLREDUCE_BASE_H +#endif // RABIT_ALLREDUCE_BASE_H_ diff --git a/subtree/rabit/src/allreduce_mock.h b/subtree/rabit/src/allreduce_mock.h index 4c271e7ba..c3f9f4f1d 100644 --- a/subtree/rabit/src/allreduce_mock.h +++ b/subtree/rabit/src/allreduce_mock.h @@ -1,8 +1,9 @@ /*! + * Copyright by Contributors * \file allreduce_mock.h * \brief Mock test module of AllReduce engine, * insert failures in certain call point, to test if the engine is robust to failure - * + * * \author Ignacio Cano, Tianqi Chen */ #ifndef RABIT_ALLREDUCE_MOCK_H_ @@ -68,7 +69,7 @@ class AllreduceMock : public AllreduceRobust { DummySerializer dum; ComboSerializer com(global_model, local_model); return AllreduceRobust::LoadCheckPoint(&dum, &com); - } + } } virtual void CheckPoint(const Serializable *global_model, const Serializable *local_model) { @@ -100,6 +101,7 @@ class AllreduceMock : public AllreduceRobust { this->Verify(MockKey(rank, version_number, seq_counter, num_trial), "LazyCheckPoint"); AllreduceRobust::LazyCheckPoint(global_model); } + protected: // force checkpoint to local int force_local; @@ -108,7 +110,7 @@ class AllreduceMock : public AllreduceRobust { // sum of allreduce double tsum_allreduce; double time_checkpoint; - + private: struct DummySerializer : public Serializable { virtual void Load(Stream *fi) { @@ -126,7 +128,7 @@ class AllreduceMock : public AllreduceRobust { } ComboSerializer(const Serializable *lhs, const Serializable *rhs) : lhs(NULL), rhs(NULL), c_lhs(lhs), c_rhs(rhs) { - } + } virtual void Load(Stream *fi) { if (lhs != NULL) lhs->Load(fi); if (rhs != NULL) rhs->Load(fi); @@ -143,10 +145,10 @@ class AllreduceMock : public AllreduceRobust { int seqno; int ntrial; MockKey(void) {} - MockKey(int rank, int version, int seqno, int ntrial) + MockKey(int rank, int version, int seqno, int ntrial) : rank(rank), version(version), seqno(seqno), ntrial(ntrial) {} inline bool operator==(const MockKey &b) const { - return rank == b.rank && + return rank == b.rank && version == b.version && seqno == b.seqno && ntrial == b.ntrial; @@ -173,4 +175,4 @@ class AllreduceMock : public AllreduceRobust { }; } // namespace engine } // namespace rabit -#endif // RABIT_ALLREDUCE_MOCK_H_ +#endif // RABIT_ALLREDUCE_MOCK_H_ diff --git a/subtree/rabit/src/allreduce_robust-inl.h b/subtree/rabit/src/allreduce_robust-inl.h index d8cc8dcdd..d3cbc0033 100644 --- a/subtree/rabit/src/allreduce_robust-inl.h +++ b/subtree/rabit/src/allreduce_robust-inl.h @@ -2,17 +2,17 @@ * Copyright (c) 2014 by Contributors * \file allreduce_robust-inl.h * \brief implementation of inline template function in AllreduceRobust - * + * * \author Tianqi Chen */ -#ifndef RABIT_ENGINE_ROBUST_INL_H_ -#define RABIT_ENGINE_ROBUST_INL_H_ +#ifndef RABIT_ALLREDUCE_ROBUST_INL_H_ +#define RABIT_ALLREDUCE_ROBUST_INL_H_ #include namespace rabit { namespace engine { /*! - * \brief run message passing algorithm on the allreduce tree + * \brief run message passing algorithm on the allreduce tree * the result is edge message stored in p_edge_in and p_edge_out * \param node_value the value associated with current node * \param p_edge_in used to store input message from each of the edge @@ -35,7 +35,7 @@ inline AllreduceRobust::ReturnType AllreduceRobust::MsgPassing(const NodeType &node_value, std::vector *p_edge_in, std::vector *p_edge_out, - EdgeType (*func) + EdgeType(*func) (const NodeType &node_value, const std::vector &edge_in, size_t out_index)) { @@ -80,8 +80,16 @@ AllreduceRobust::MsgPassing(const NodeType &node_value, selecter.WatchRead(links[i].sock); } break; - case 1: if (i == parent_index) selecter.WatchWrite(links[i].sock); break; - case 2: if (i == parent_index) selecter.WatchRead(links[i].sock); break; + case 1: + if (i == parent_index) { + selecter.WatchWrite(links[i].sock); + } + break; + case 2: + if (i == parent_index) { + selecter.WatchRead(links[i].sock); + } + break; case 3: if (i != parent_index && links[i].size_write != sizeof(EdgeType)) { selecter.WatchWrite(links[i].sock); @@ -158,4 +166,4 @@ AllreduceRobust::MsgPassing(const NodeType &node_value, } } // namespace engine } // namespace rabit -#endif // RABIT_ENGINE_ROBUST_INL_H_ +#endif // RABIT_ALLREDUCE_ROBUST_INL_H_ diff --git a/subtree/rabit/src/allreduce_robust.cc b/subtree/rabit/src/allreduce_robust.cc index 339603498..175751842 100644 --- a/subtree/rabit/src/allreduce_robust.cc +++ b/subtree/rabit/src/allreduce_robust.cc @@ -27,7 +27,7 @@ AllreduceRobust::AllreduceRobust(void) { result_buffer_round = 1; global_lazycheck = NULL; use_local_model = -1; - recover_counter = 0; + recover_counter = 0; env_vars.push_back("rabit_global_replica"); env_vars.push_back("rabit_local_replica"); } @@ -49,7 +49,7 @@ void AllreduceRobust::Shutdown(void) { AllreduceBase::Shutdown(); } /*! - * \brief set parameters to the engine + * \brief set parameters to the engine * \param name parameter name * \param val parameter value */ @@ -61,7 +61,7 @@ void AllreduceRobust::SetParam(const char *name, const char *val) { } } /*! - * \brief perform in-place allreduce, on sendrecvbuf + * \brief perform in-place allreduce, on sendrecvbuf * this function is NOT thread-safe * \param sendrecvbuf_ buffer for both sending and recving data * \param type_nbytes the unit number of bytes the type have @@ -147,14 +147,14 @@ void AllreduceRobust::Broadcast(void *sendrecvbuf_, size_t total_size, int root) * \return the version number of check point loaded * if returned version == 0, this means no model has been CheckPointed * the p_model is not touched, user should do necessary initialization by themselves - * + * * Common usage example: * int iter = rabit::LoadCheckPoint(&model); * if (iter == 0) model.InitParameters(); * for (i = iter; i < max_iter; ++i) { * do many things, include allreduce * rabit::CheckPoint(model); - * } + * } * * \sa CheckPoint, VersionNumber */ @@ -208,7 +208,7 @@ int AllreduceRobust::LoadCheckPoint(Serializable *global_model, * \brief internal consistency check function, * use check to ensure user always call CheckPoint/LoadCheckPoint * with or without local but not both, this function will set the approperiate settings - * in the first call of LoadCheckPoint/CheckPoint + * in the first call of LoadCheckPoint/CheckPoint * * \param with_local whether the user calls CheckPoint with local model */ @@ -224,14 +224,14 @@ void AllreduceRobust::LocalModelCheck(bool with_local) { num_local_replica = 0; } } else { - utils::Check(use_local_model == int(with_local), + utils::Check(use_local_model == static_cast(with_local), "Can only call Checkpoint/LoadCheckPoint always with"\ "or without local_model, but not mixed case"); } } /*! * \brief internal implementation of checkpoint, support both lazy and normal way - * + * * \param global_model pointer to the globally shared model/state * when calling this function, the caller need to gauranttees that global_model * is the same in all nodes @@ -423,7 +423,7 @@ AllreduceRobust::ReturnType AllreduceRobust::TryResetLinks(void) { * recover links according to the error type reported * if there is no error, return true * \param err_type the type of error happening in the system - * \return true if err_type is kSuccess, false otherwise + * \return true if err_type is kSuccess, false otherwise */ bool AllreduceRobust::CheckAndRecover(ReturnType err_type) { if (err_type == kSuccess) return true; @@ -488,7 +488,7 @@ ShortestDist(const std::pair &node_value, * \brief message passing function, used to decide the * data request from each edge, whether need to request data from certain edge * \param node_value a pair of request_data and best_link - * request_data stores whether current node need to request data + * request_data stores whether current node need to request data * best_link gives the best edge index to fetch the data * \param req_in the data request from incoming edges * \param out_index the edge index of output link @@ -524,7 +524,7 @@ inline char DataRequest(const std::pair &node_value, * * \return this function can return kSuccess/kSockError/kGetExcept, see ReturnType for details * \sa ReturnType - */ + */ AllreduceRobust::ReturnType AllreduceRobust::TryDecideRouting(AllreduceRobust::RecoverType role, size_t *p_size, @@ -586,7 +586,7 @@ AllreduceRobust::TryDecideRouting(AllreduceRobust::RecoverType role, * * \return this function can return kSuccess/kSockError/kGetExcept, see ReturnType for details * \sa ReturnType, TryDecideRouting - */ + */ AllreduceRobust::ReturnType AllreduceRobust::TryRecoverData(RecoverType role, void *sendrecvbuf_, @@ -644,7 +644,7 @@ AllreduceRobust::TryRecoverData(RecoverType role, if (role == kRequestData) { const int pid = recv_link; if (selecter.CheckRead(links[pid].sock)) { - ReturnType ret = links[pid].ReadToArray(sendrecvbuf_, size); + ReturnType ret = links[pid].ReadToArray(sendrecvbuf_, size); if (ret != kSuccess) { return ReportError(&links[pid], ret); } @@ -691,7 +691,7 @@ AllreduceRobust::TryRecoverData(RecoverType role, if (len != -1) { links[i].size_write += len; } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) return ReportError(&links[i], ret); } } @@ -823,10 +823,10 @@ AllreduceRobust::TryGetResult(void *sendrecvbuf, size_t size, int seqno, bool re * \param buf the buffer to store the result * \param size the total size of the buffer * \param flag flag information about the action \sa ActionSummary - * \param seqno sequence number of the action, if it is special action with flag set, + * \param seqno sequence number of the action, if it is special action with flag set, * seqno needs to be set to ActionSummary::kSpecialOp * - * \return if this function can return true or false + * \return if this function can return true or false * - true means buf already set to the * result by recovering procedure, the action is complete, no further action is needed * - false means this is the lastest action that has not yet been executed, need to execute the action @@ -907,7 +907,7 @@ bool AllreduceRobust::RecoverExec(void *buf, size_t size, int flag, int seqno) { * plus replication of states in previous num_local_replica hops in the ring * * The input parameters must contain the valid local states available in current nodes, - * This function try ist best to "complete" the missing parts of local_rptr and local_chkpt + * This function try ist best to "complete" the missing parts of local_rptr and local_chkpt * If there is sufficient information in the ring, when the function returns, local_chkpt will * contain num_local_replica + 1 checkpoints (including the chkpt of this node) * If there is no sufficient information in the ring, this function the number of checkpoints @@ -1161,7 +1161,7 @@ AllreduceRobust::RingPassing(void *sendrecvbuf_, if (len != -1) { read_ptr += static_cast(len); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) return ReportError(&prev, ret); } } @@ -1171,7 +1171,7 @@ AllreduceRobust::RingPassing(void *sendrecvbuf_, if (len != -1) { write_ptr += static_cast(len); } else { - ReturnType ret = Errno2Return(errno); + ReturnType ret = Errno2Return(); if (ret != kSuccess) return ReportError(&prev, ret); } } diff --git a/subtree/rabit/src/allreduce_robust.h b/subtree/rabit/src/allreduce_robust.h index 658d6f8c7..caf2e57af 100644 --- a/subtree/rabit/src/allreduce_robust.h +++ b/subtree/rabit/src/allreduce_robust.h @@ -5,7 +5,7 @@ * using TCP non-block socket and tree-shape reduction. * * This implementation considers the failure of nodes - * + * * \author Tianqi Chen, Ignacio Cano, Tianyi Zhou */ #ifndef RABIT_ALLREDUCE_ROBUST_H_ @@ -28,13 +28,13 @@ class AllreduceRobust : public AllreduceBase { /*! \brief shutdown the engine */ virtual void Shutdown(void); /*! - * \brief set parameters to the engine + * \brief set parameters to the engine * \param name parameter name * \param val parameter value */ virtual void SetParam(const char *name, const char *val); /*! - * \brief perform in-place allreduce, on sendrecvbuf + * \brief perform in-place allreduce, on sendrecvbuf * this function is NOT thread-safe * \param sendrecvbuf_ buffer for both sending and recving data * \param type_nbytes the unit number of bytes the type have @@ -69,14 +69,14 @@ class AllreduceRobust : public AllreduceBase { * \return the version number of check point loaded * if returned version == 0, this means no model has been CheckPointed * the p_model is not touched, user should do necessary initialization by themselves - * + * * Common usage example: * int iter = rabit::LoadCheckPoint(&model); * if (iter == 0) model.InitParameters(); * for (i = iter; i < max_iter; ++i) { * do many things, include allreduce * rabit::CheckPoint(model); - * } + * } * * \sa CheckPoint, VersionNumber */ @@ -85,7 +85,7 @@ class AllreduceRobust : public AllreduceBase { /*! * \brief checkpoint the model, meaning we finished a stage of execution * every time we call check point, there is a version number which will increase by one - * + * * \param global_model pointer to the globally shared model/state * when calling this function, the caller need to gauranttees that global_model * is the same in all nodes @@ -105,16 +105,16 @@ class AllreduceRobust : public AllreduceBase { /*! * \brief This function can be used to replace CheckPoint for global_model only, * when certain condition is met(see detailed expplaination). - * + * * This is a "lazy" checkpoint such that only the pointer to global_model is * remembered and no memory copy is taken. To use this function, the user MUST ensure that: * The global_model must remain unchanged util last call of Allreduce/Broadcast in current version finishs. - * In another words, global_model model can be changed only between last call of + * In another words, global_model model can be changed only between last call of * Allreduce/Broadcast and LazyCheckPoint in current version - * + * * For example, suppose the calling sequence is: * LazyCheckPoint, code1, Allreduce, code2, Broadcast, code3, LazyCheckPoint - * + * * If user can only changes global_model in code3, then LazyCheckPoint can be used to * improve efficiency of the program. * \param global_model pointer to the globally shared model/state @@ -287,6 +287,7 @@ class AllreduceRobust : public AllreduceBase { if (seqno_.size() == 0) return -1; return seqno_.back(); } + private: // sequence number of each std::vector seqno_; @@ -301,14 +302,14 @@ class AllreduceRobust : public AllreduceBase { * \brief internal consistency check function, * use check to ensure user always call CheckPoint/LoadCheckPoint * with or without local but not both, this function will set the approperiate settings - * in the first call of LoadCheckPoint/CheckPoint + * in the first call of LoadCheckPoint/CheckPoint * * \param with_local whether the user calls CheckPoint with local model */ void LocalModelCheck(bool with_local); /*! * \brief internal implementation of checkpoint, support both lazy and normal way - * + * * \param global_model pointer to the globally shared model/state * when calling this function, the caller need to gauranttees that global_model * is the same in all nodes @@ -326,10 +327,10 @@ class AllreduceRobust : public AllreduceBase { * after this function finishes, all the messages received and sent * before in all live links are discarded, * This allows us to get a fresh start after error has happened - * + * * TODO(tqchen): this function is not yet functioning was not used by engine, * simple resetlink and reconnect strategy is used - * + * * \return this function can return kSuccess or kSockError * when kSockError is returned, it simply means there are bad sockets in the links, * and some link recovery proceduer is needed @@ -340,7 +341,7 @@ class AllreduceRobust : public AllreduceBase { * recover links according to the error type reported * if there is no error, return true * \param err_type the type of error happening in the system - * \return true if err_type is kSuccess, false otherwise + * \return true if err_type is kSuccess, false otherwise */ bool CheckAndRecover(ReturnType err_type); /*! @@ -355,7 +356,7 @@ class AllreduceRobust : public AllreduceBase { * \param seqno sequence number of the action, if it is special action with flag set, * seqno needs to be set to ActionSummary::kSpecialOp * - * \return if this function can return true or false + * \return if this function can return true or false * - true means buf already set to the * result by recovering procedure, the action is complete, no further action is needed * - false means this is the lastest action that has not yet been executed, need to execute the action @@ -364,7 +365,7 @@ class AllreduceRobust : public AllreduceBase { int seqno = ActionSummary::kSpecialOp); /*! * \brief try to load check point - * + * * This is a collaborative function called by all nodes * only the nodes with requester set to true really needs to load the check point * other nodes acts as collaborative roles to complete this request @@ -395,7 +396,7 @@ class AllreduceRobust : public AllreduceBase { * \param p_size used to store the size of the message, for node in state kHaveData, * this size must be set correctly before calling the function * for others, this surves as output parameter - + * \param p_recvlink used to store the link current node should recv data from, if necessary * this can be -1, which means current node have the data * \param p_req_in used to store the resulting vector, indicating which link we should send the data to @@ -432,7 +433,7 @@ class AllreduceRobust : public AllreduceBase { * plus replication of states in previous num_local_replica hops in the ring * * The input parameters must contain the valid local states available in current nodes, - * This function try ist best to "complete" the missing parts of local_rptr and local_chkpt + * This function try ist best to "complete" the missing parts of local_rptr and local_chkpt * If there is sufficient information in the ring, when the function returns, local_chkpt will * contain num_local_replica + 1 checkpoints (including the chkpt of this node) * If there is no sufficient information in the ring, this function the number of checkpoints @@ -487,7 +488,7 @@ o * the input state must exactly one saved state(local state of current node) LinkRecord *read_link, LinkRecord *write_link); /*! - * \brief run message passing algorithm on the allreduce tree + * \brief run message passing algorithm on the allreduce tree * the result is edge message stored in p_edge_in and p_edge_out * \param node_value the value associated with current node * \param p_edge_in used to store input message from each of the edge @@ -509,7 +510,7 @@ o * the input state must exactly one saved state(local state of current node) inline ReturnType MsgPassing(const NodeType &node_value, std::vector *p_edge_in, std::vector *p_edge_out, - EdgeType (*func) + EdgeType(*func) (const NodeType &node_value, const std::vector &edge_in, size_t out_index)); diff --git a/subtree/rabit/src/engine.cc b/subtree/rabit/src/engine.cc index c5041642e..0f4770fe2 100644 --- a/subtree/rabit/src/engine.cc +++ b/subtree/rabit/src/engine.cc @@ -3,7 +3,7 @@ * \file engine.cc * \brief this file governs which implementation of engine we are actually using * provides an singleton of engine interface - * + * * \author Tianqi Chen, Ignacio Cano, Tianyi Zhou */ #define _CRT_SECURE_NO_WARNINGS @@ -60,7 +60,7 @@ void Allreduce_(void *sendrecvbuf, } // code for reduce handle -ReduceHandle::ReduceHandle(void) +ReduceHandle::ReduceHandle(void) : handle_(NULL), redfunc_(NULL), htype_(NULL) { } ReduceHandle::~ReduceHandle(void) {} diff --git a/subtree/rabit/src/engine_mpi.cc b/subtree/rabit/src/engine_mpi.cc index 5c8a4c372..11e55335b 100644 --- a/subtree/rabit/src/engine_mpi.cc +++ b/subtree/rabit/src/engine_mpi.cc @@ -3,7 +3,7 @@ * \file engine_mpi.cc * \brief this file gives an implementation of engine interface using MPI, * this will allow rabit program to run with MPI, but do not comes with fault tolerant - * + * * \author Tianqi Chen */ #define _CRT_SECURE_NO_WARNINGS @@ -110,6 +110,8 @@ inline MPI::Datatype GetType(mpi::DataType dtype) { case kULong: return MPI::UNSIGNED_LONG; case kFloat: return MPI::FLOAT; case kDouble: return MPI::DOUBLE; + case kLongLong: return MPI::LONG_LONG; + case kULongLong: return MPI::UNSIGNED_LONG_LONG; } utils::Error("unknown mpi::DataType"); return MPI::CHAR; @@ -141,7 +143,7 @@ void Allreduce_(void *sendrecvbuf, } // code for reduce handle -ReduceHandle::ReduceHandle(void) +ReduceHandle::ReduceHandle(void) : handle_(NULL), redfunc_(NULL), htype_(NULL) { } ReduceHandle::~ReduceHandle(void) { @@ -164,7 +166,7 @@ void ReduceHandle::Init(IEngine::ReduceFunction redfunc, size_t type_nbytes) { if (type_nbytes != 0) { MPI::Datatype *dtype = new MPI::Datatype(); if (type_nbytes % 8 == 0) { - *dtype = MPI::LONG.Create_contiguous(type_nbytes / sizeof(long)); + *dtype = MPI::LONG.Create_contiguous(type_nbytes / sizeof(long)); // NOLINT(*) } else if (type_nbytes % 4 == 0) { *dtype = MPI::INT.Create_contiguous(type_nbytes / sizeof(int)); } else { @@ -193,7 +195,7 @@ void ReduceHandle::Allreduce(void *sendrecvbuf, dtype->Free(); } if (type_nbytes % 8 == 0) { - *dtype = MPI::LONG.Create_contiguous(type_nbytes / sizeof(long)); + *dtype = MPI::LONG.Create_contiguous(type_nbytes / sizeof(long)); // NOLINT(*) } else if (type_nbytes % 4 == 0) { *dtype = MPI::INT.Create_contiguous(type_nbytes / sizeof(int)); } else { diff --git a/subtree/rabit/src/socket.h b/subtree/rabit/src/socket.h index c0eb6278c..6df7a7b78 100644 --- a/subtree/rabit/src/socket.h +++ b/subtree/rabit/src/socket.h @@ -51,7 +51,7 @@ struct SockAddr { utils::Check(gethostname(&buf[0], 256) != -1, "fail to get host name"); return std::string(buf.c_str()); } - /*! + /*! * \brief set the address * \param url the url of the address * \param port the port of address @@ -83,7 +83,7 @@ struct SockAddr { } }; -/*! +/*! * \brief base class containing common operations of TCP and UDP sockets */ class Socket { @@ -94,6 +94,25 @@ class Socket { inline operator SOCKET() const { return sockfd; } + /*! + * \return last error of socket operation + */ + inline static int GetLastError(void) { +#ifdef _WIN32 + return WSAGetLastError(); +#else + return errno; +#endif + } + /*! \return whether last error was would block */ + inline static bool LastErrorWouldBlock(void) { + int errsv = GetLastError(); +#ifdef _WIN32 + return errsv == WSAEWOULDBLOCK; +#else + return errsv == EAGAIN || errsv == EWOULDBLOCK; +#endif + } /*! * \brief start up the socket module * call this before using the sockets @@ -110,15 +129,15 @@ class Socket { } #endif } - /*! + /*! * \brief shutdown the socket module after use, all sockets need to be closed - */ + */ inline static void Finalize(void) { #ifdef _WIN32 WSACleanup(); #endif } - /*! + /*! * \brief set this socket to use non-blocking mode * \param non_block whether set it to be non-block, if it is false * it will set it back to block mode @@ -144,8 +163,8 @@ class Socket { } #endif } - /*! - * \brief bind the socket to an address + /*! + * \brief bind the socket to an address * \param addr */ inline void Bind(const SockAddr &addr) { @@ -154,7 +173,7 @@ class Socket { Socket::Error("Bind"); } } - /*! + /*! * \brief try bind the socket to host, from start_port to end_port * \param start_port starting port number to try * \param end_port ending port number to try @@ -169,11 +188,11 @@ class Socket { return port; } #if defined(_WIN32) - if (WSAGetLastError() != WSAEADDRINUSE) { - Socket::Error("TryBindHost"); - } + if (WSAGetLastError() != WSAEADDRINUSE) { + Socket::Error("TryBindHost"); + } #else - if (errno != EADDRINUSE) { + if (errno != EADDRINUSE) { Socket::Error("TryBindHost"); } #endif @@ -216,8 +235,12 @@ class Socket { } // report an socket error inline static void Error(const char *msg) { - int errsv = errno; + int errsv = GetLastError(); +#ifdef _WIN32 + utils::Error("Socket %s Error:WSAError-code=%d", msg, errsv); +#else utils::Error("Socket %s Error:%s", msg, strerror(errsv)); +#endif } protected: @@ -225,7 +248,7 @@ class Socket { } }; -/*! +/*! * \brief a wrapper of TCP socket that hopefully be cross platform */ class TCPSocket : public Socket{ @@ -238,10 +261,11 @@ class TCPSocket : public Socket{ /*! * \brief enable/disable TCP keepalive * \param keepalive whether to set the keep alive option on - */ + */ inline void SetKeepAlive(bool keepalive) { int opt = static_cast(keepalive); - if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast(&opt), sizeof(opt)) < 0) { + if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, + reinterpret_cast(&opt), sizeof(opt)) < 0) { Socket::Error("SetKeepAlive"); } } @@ -271,12 +295,12 @@ class TCPSocket : public Socket{ return TCPSocket(newfd); } /*! - * \brief decide whether the socket is at OOB mark + * \brief decide whether the socket is at OOB mark * \return 1 if at mark, 0 if not, -1 if an error occured */ inline int AtMark(void) const { #ifdef _WIN32 - unsigned long atmark; + unsigned long atmark; // NOLINT(*) if (ioctlsocket(sockfd, SIOCATMARK, &atmark) != NO_ERROR) return -1; #else int atmark; @@ -284,8 +308,8 @@ class TCPSocket : public Socket{ #endif return static_cast(atmark); } - /*! - * \brief connect to an address + /*! + * \brief connect to an address * \param addr the address to connect to * \return whether connect is successful */ @@ -305,8 +329,8 @@ class TCPSocket : public Socket{ const char *buf = reinterpret_cast(buf_); return send(sockfd, buf, static_cast(len), flag); } - /*! - * \brief receive data using the socket + /*! + * \brief receive data using the socket * \param buf_ the pointer to the buffer * \param len the size of the buffer * \param flags extra flags @@ -330,7 +354,7 @@ class TCPSocket : public Socket{ while (ndone < len) { ssize_t ret = send(sockfd, buf, static_cast(len - ndone), 0); if (ret == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; + if (LastErrorWouldBlock()) return ndone; Socket::Error("SendAll"); } buf += ret; @@ -352,7 +376,7 @@ class TCPSocket : public Socket{ ssize_t ret = recv(sockfd, buf, static_cast(len - ndone), MSG_WAITALL); if (ret == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; + if (LastErrorWouldBlock()) return ndone; Socket::Error("RecvAll"); } if (ret == 0) return ndone; @@ -362,7 +386,7 @@ class TCPSocket : public Socket{ return ndone; } /*! - * \brief send a string over network + * \brief send a string over network * \param str the string to be sent */ inline void SendStr(const std::string &str) { @@ -400,7 +424,7 @@ struct SelectHelper { maxfd = 0; } /*! - * \brief add file descriptor to watch for read + * \brief add file descriptor to watch for read * \param fd file descriptor to be watched */ inline void WatchRead(SOCKET fd) { @@ -450,7 +474,7 @@ struct SelectHelper { * \param timeout the timeout counter, can be 0, which means wait until the event happen * \return 1 if success, 0 if timeout, and -1 if error occurs */ - inline static int WaitExcept(SOCKET fd, long timeout = 0) { + inline static int WaitExcept(SOCKET fd, long timeout = 0) { // NOLINT(*) fd_set wait_set; FD_ZERO(&wait_set); FD_SET(fd, &wait_set); @@ -463,10 +487,10 @@ struct SelectHelper { * \param select_write whether to watch for write event * \param select_except whether to watch for exception event * \param timeout specify timeout in micro-seconds(ms) if equals 0, means select will always block - * \return number of active descriptors selected, + * \return number of active descriptors selected, * return -1 if error occurs */ - inline int Select(long timeout = 0) { + inline int Select(long timeout = 0) { // NOLINT(*) int ret = Select_(static_cast(maxfd + 1), &read_set, &write_set, &except_set, timeout); if (ret == -1) { @@ -477,7 +501,7 @@ struct SelectHelper { private: inline static int Select_(int maxfd, fd_set *rfds, - fd_set *wfds, fd_set *efds, long timeout) { + fd_set *wfds, fd_set *efds, long timeout) { // NOLINT(*) #if !defined(_WIN32) utils::Assert(maxfd < FD_SETSIZE, "maxdf must be smaller than FDSETSIZE"); #endif diff --git a/subtree/rabit/test/Makefile b/subtree/rabit/test/Makefile index a1ff6a854..62e4e17f0 100644 --- a/subtree/rabit/test/Makefile +++ b/subtree/rabit/test/Makefile @@ -2,7 +2,7 @@ export CC = gcc export CXX = g++ export MPICXX = mpicxx export LDFLAGS= -L../lib -pthread -lm -lrt -export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -I../include -std=c++11 +export CFLAGS = -Wall -O3 -msse2 -Wno-unknown-pragmas -fPIC -I../include -std=c++0x # specify tensor path BIN = speed_test model_recover local_recover lazy_recover diff --git a/subtree/rabit/test/test.mk b/subtree/rabit/test/test.mk index be3429bab..282a82bc4 100644 --- a/subtree/rabit/test/test.mk +++ b/subtree/rabit/test/test.mk @@ -1,7 +1,7 @@ # this is a makefile used to show testcases of rabit .PHONY: all -all: +all: model_recover_10_10k model_recover_10_10k_die_same # this experiment test recovery with actually process exit, use keepalive to keep program alive model_recover_10_10k: diff --git a/subtree/rabit/tracker/rabit_tracker.py b/subtree/rabit/tracker/rabit_tracker.py index c8dd896f1..d8e6ae84d 100644 --- a/subtree/rabit/tracker/rabit_tracker.py +++ b/subtree/rabit/tracker/rabit_tracker.py @@ -1,6 +1,6 @@ """ Tracker script for rabit -Implements the tracker control protocol +Implements the tracker control protocol - start rabit jobs - help nodes to establish links with each other @@ -19,13 +19,13 @@ from threading import Thread """ Extension of socket to handle recv and send of special data """ -class ExSocket: +class ExSocket: def __init__(self, sock): self.sock = sock def recvall(self, nbytes): res = [] sock = self.sock - nread = 0 + nread = 0 while nread < nbytes: chunk = self.sock.recv(min(nbytes - nread, 1024)) nread += len(chunk) @@ -106,7 +106,7 @@ class SlaveEntry: for r in conset: self.sock.sendstr(wait_conn[r].host) self.sock.sendint(wait_conn[r].port) - self.sock.sendint(r) + self.sock.sendint(r) nerr = self.sock.recvint() if nerr != 0: continue @@ -121,7 +121,7 @@ class SlaveEntry: wait_conn.pop(r, None) self.wait_accept = len(badset) - len(conset) return rmset - + class Tracker: def __init__(self, port = 9091, port_end = 9999, verbose = True, hostIP = 'auto'): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -132,7 +132,7 @@ class Tracker: break except socket.error: continue - sock.listen(16) + sock.listen(128) self.sock = sock self.verbose = verbose if hostIP == 'auto': @@ -145,7 +145,7 @@ class Tracker: """ get enviroment variables for slaves can be passed in as args or envs - """ + """ if self.hostIP == 'dns': host = socket.gethostname() elif self.hostIP == 'ip': @@ -153,14 +153,14 @@ class Tracker: else: host = self.hostIP return {'rabit_tracker_uri': host, - 'rabit_tracker_port': self.port} + 'rabit_tracker_port': self.port} def get_neighbor(self, rank, nslave): rank = rank + 1 ret = [] if rank > 1: ret.append(rank / 2 - 1) if rank * 2 - 1 < nslave: - ret.append(rank * 2 - 1) + ret.append(rank * 2 - 1) if rank * 2 < nslave: ret.append(rank * 2) return ret @@ -198,10 +198,10 @@ class Tracker: rlst = self.find_share_ring(tree_map, parent_map, 0) assert len(rlst) == len(tree_map) ring_map = {} - nslave = len(tree_map) + nslave = len(tree_map) for r in range(nslave): rprev = (r + nslave - 1) % nslave - rnext = (r + 1) % nslave + rnext = (r + 1) % nslave ring_map[rlst[r]] = (rlst[rprev], rlst[rnext]) return ring_map @@ -231,7 +231,7 @@ class Tracker: else: parent_map_[rmap[k]] = -1 return tree_map_, parent_map_, ring_map_ - + def handle_print(self,slave, msg): sys.stdout.write(msg) @@ -253,14 +253,14 @@ class Tracker: pending = [] # lazy initialize tree_map tree_map = None - + while len(shutdown) != nslave: fd, s_addr = self.sock.accept() s = SlaveEntry(fd, s_addr) if s.cmd == 'print': msg = s.sock.recvstr() self.handle_print(s, msg) - continue + continue if s.cmd == 'shutdown': assert s.rank >= 0 and s.rank not in shutdown assert s.rank not in wait_conn @@ -280,12 +280,12 @@ class Tracker: assert s.world_size == -1 or s.world_size == nslave if s.cmd == 'recover': assert s.rank >= 0 - + rank = s.decide_rank(job_map) # batch assignment of ranks if rank == -1: assert len(todo_nodes) != 0 - pending.append(s) + pending.append(s) if len(pending) == len(todo_nodes): pending.sort(key = lambda x : x.host) for s in pending: diff --git a/subtree/rabit/windows/basic/basic.vcxproj b/subtree/rabit/windows/basic/basic.vcxproj index 4e686584c..109c405ef 100644 --- a/subtree/rabit/windows/basic/basic.vcxproj +++ b/subtree/rabit/windows/basic/basic.vcxproj @@ -100,6 +100,7 @@ true true ..\..\include + MultiThreaded true diff --git a/subtree/rabit/wrapper/rabit.py b/subtree/rabit/wrapper/rabit.py index 6282e5cfd..91ce3e6ae 100644 --- a/subtree/rabit/wrapper/rabit.py +++ b/subtree/rabit/wrapper/rabit.py @@ -1,8 +1,9 @@ """ -Python interface for rabit - Reliable Allreduce and Broadcast Library +Reliable Allreduce and Broadcast Library. + Author: Tianqi Chen """ +# pylint: disable=unused-argument,invalid-name,global-statement,dangerous-default-value, import cPickle as pickle import ctypes import os @@ -10,34 +11,41 @@ import sys import warnings import numpy as np +# version information about the doc +__version__ = '1.0' + if os.name == 'nt': WRAPPER_PATH = os.path.dirname(__file__) + '\\..\\windows\\x64\\Release\\rabit_wrapper%s.dll' else: WRAPPER_PATH = os.path.dirname(__file__) + '/librabit_wrapper%s.so' -rbtlib = None + +_LIB = None # load in xgboost library -def loadlib__(lib = 'standard'): - global rbtlib - if rbtlib != None: - warnings.Warn('rabit.int call was ignored because it has already been initialized', level = 2) +def _loadlib(lib='standard'): + """Load rabit library.""" + global _LIB + if _LIB != None: + warnings.warn('rabit.int call was ignored because it has'\ + ' already been initialized', level=2) return if lib == 'standard': - rbtlib = ctypes.cdll.LoadLibrary(WRAPPER_PATH % '') + _LIB = ctypes.cdll.LoadLibrary(WRAPPER_PATH % '') elif lib == 'mock': - rbtlib = ctypes.cdll.LoadLibrary(WRAPPER_PATH % '_mock') + _LIB = ctypes.cdll.LoadLibrary(WRAPPER_PATH % '_mock') elif lib == 'mpi': - rbtlib = ctypes.cdll.LoadLibrary(WRAPPER_PATH % '_mpi') + _LIB = ctypes.cdll.LoadLibrary(WRAPPER_PATH % '_mpi') else: raise Exception('unknown rabit lib %s, can be standard, mock, mpi' % lib) - rbtlib.RabitGetRank.restype = ctypes.c_int - rbtlib.RabitGetWorldSize.restype = ctypes.c_int - rbtlib.RabitVersionNumber.restype = ctypes.c_int + _LIB.RabitGetRank.restype = ctypes.c_int + _LIB.RabitGetWorldSize.restype = ctypes.c_int + _LIB.RabitVersionNumber.restype = ctypes.c_int -def unloadlib__(): - global rbtlib - del rbtlib - rbtlib = None +def _unloadlib(): + """Unload rabit library.""" + global _LIB + del _LIB + _LIB = None # reduction operators MAX = 0 @@ -45,125 +53,118 @@ MIN = 1 SUM = 2 BITOR = 3 -def check_err__(): - """ - reserved function used to check error - """ - return +def init(args=None, lib='standard'): + """Intialize the rabit module, call this once before using anything. -def init(args = sys.argv, lib = 'standard'): + Parameters + ---------- + args: list of str, optional + The list of arguments used to initialized the rabit + usually you need to pass in sys.argv. + Defaults to sys.argv when it is None. + lib: {'standard', 'mock', 'mpi'} + Type of library we want to load """ - intialize the rabit module, call this once before using anything - Arguments: - args: list(string) [default=sys.argv] - the list of arguments used to initialized the rabit - usually you need to pass in sys.argv - with_mock: boolean [default=False] - Whether initialize the mock test module - """ - loadlib__(lib) + if args is None: + args = sys.argv + _loadlib(lib) arr = (ctypes.c_char_p * len(args))() arr[:] = args - rbtlib.RabitInit(len(args), arr) - check_err__() + _LIB.RabitInit(len(args), arr) def finalize(): + """Finalize the rabit engine. + + Call this function after you finished all jobs. """ - finalize the rabit engine, call this function after you finished all jobs - """ - rbtlib.RabitFinalize() - check_err__() - unloadlib__() + _LIB.RabitFinalize() + _unloadlib() def get_rank(): + """Get rank of current process. + + Returns + ------- + rank : int + Rank of current process. """ - Returns rank of current process - """ - ret = rbtlib.RabitGetRank() - check_err__() + ret = _LIB.RabitGetRank() return ret def get_world_size(): + """Get total number workers. + + Returns + ------- + n : int + Total number of process. """ - Returns get total number of process - """ - ret = rbtlib.RabitGetWorldSize() - check_err__() + ret = _LIB.RabitGetWorldSize() return ret def tracker_print(msg): - """ - print message to the tracker - this function can be used to communicate the information of the progress - to the tracker + """Print message to the tracker. + + This function can be used to communicate the information of + the progress to the tracker + + Parameters + ---------- + msg : str + The message to be printed to tracker. """ if not isinstance(msg, str): msg = str(msg) - rbtlib.RabitTrackerPrint(ctypes.c_char_p(msg).encode('utf-8')) - check_err__() + _LIB.RabitTrackerPrint(ctypes.c_char_p(msg).encode('utf-8')) def get_processor_name(): - """ - Returns the name of processor(host) + """Get the processor name. + + Returns + ------- + name : str + the name of processor(host) """ mxlen = 256 length = ctypes.c_ulong() buf = ctypes.create_string_buffer(mxlen) - rbtlib.RabitGetProcessorName(buf, ctypes.byref(length), - mxlen) - check_err__() + _LIB.RabitGetProcessorName(buf, ctypes.byref(length), mxlen) return buf.value def broadcast(data, root): - """ - broadcast object from one node to all other nodes - this function will return the broadcasted object + """Broadcast object from one node to all other nodes. - Example: the following example broadcast hello from rank 0 to all other nodes - ```python - rabit.init() - n = 3 - rank = rabit.get_rank() - s = None - if rank == 0: - s = {'hello world':100, 2:3} - print '@node[%d] before-broadcast: s=\"%s\"' % (rank, str(s)) - s = rabit.broadcast(s, 0) - print '@node[%d] after-broadcast: s=\"%s\"' % (rank, str(s)) - rabit.finalize() - ``` - - Arguments: - data: anytype that can be pickled - input data, if current rank does not equal root, this can be None - root: int - rank of the node to broadcast data from - Returns: - the result of broadcast + Parameters + ---------- + data : any type that can be pickled + Input data, if current rank does not equal root, this can be None + root : int + Rank of the node to broadcast data from. + + Returns + ------- + object : int + the result of broadcast. """ rank = get_rank() length = ctypes.c_ulong() if root == rank: assert data is not None, 'need to pass in data when broadcasting' - s = pickle.dumps(data, protocol = pickle.HIGHEST_PROTOCOL) + s = pickle.dumps(data, protocol=pickle.HIGHEST_PROTOCOL) length.value = len(s) # run first broadcast - rbtlib.RabitBroadcast(ctypes.byref(length), - ctypes.sizeof(ctypes.c_ulong), - root) - check_err__() + _LIB.RabitBroadcast(ctypes.byref(length), + ctypes.sizeof(ctypes.c_ulong), root) if root != rank: dptr = (ctypes.c_char * length.value)() # run second - rbtlib.RabitBroadcast(ctypes.cast(dptr, ctypes.c_void_p), - length.value, root) - check_err__() + _LIB.RabitBroadcast(ctypes.cast(dptr, ctypes.c_void_p), + length.value, root) data = pickle.loads(dptr.raw) del dptr else: - rbtlib.RabitBroadcast(ctypes.cast(ctypes.c_char_p(s), ctypes.c_void_p), - length.value, root) - check_err__() + _LIB.RabitBroadcast(ctypes.cast(ctypes.c_char_p(s), ctypes.c_void_p), + length.value, root) del s return data @@ -179,20 +180,29 @@ DTYPE_ENUM__ = { np.dtype('float64') : 7 } -def allreduce(data, op, prepare_fun = None): - """ - perform allreduce, return the result, this function is not thread-safe - Arguments: - data: numpy ndarray - input data - op: int - reduction operators, can be MIN, MAX, SUM, BITOR - prepare_fun: lambda data - Lazy preprocessing function, if it is not None, prepare_fun(data) - will be called by the function before performing allreduce, to intialize the data - If the result of Allreduce can be recovered directly, then prepare_fun will NOT be called - Returns: - the result of allreduce, have same shape as data +def allreduce(data, op, prepare_fun=None): + """Perform allreduce, return the result. + + Parameters + ---------- + data: numpy array + Input data. + op: int + Reduction operators, can be MIN, MAX, SUM, BITOR + prepare_fun: function + Lazy preprocessing function, if it is not None, prepare_fun(data) + will be called by the function before performing allreduce, to intialize the data + If the result of Allreduce can be recovered directly, + then prepare_fun will NOT be called + + Returns + ------- + result : array_like + The result of allreduce, have same shape as data + + Notes + ----- + This function is not thread-safe. """ if not isinstance(data, np.ndarray): raise Exception('allreduce only takes in numpy.ndarray') @@ -202,21 +212,21 @@ def allreduce(data, op, prepare_fun = None): if buf.dtype not in DTYPE_ENUM__: raise Exception('data type %s not supported' % str(buf.dtype)) if prepare_fun is None: - rbtlib.RabitAllreduce(buf.ctypes.data_as(ctypes.c_void_p), - buf.size, DTYPE_ENUM__[buf.dtype], - op, None, None) + _LIB.RabitAllreduce(buf.ctypes.data_as(ctypes.c_void_p), + buf.size, DTYPE_ENUM__[buf.dtype], + op, None, None) else: - PFUNC = ctypes.CFUNCTYPE(None, ctypes.c_void_p) + func_ptr = ctypes.CFUNCTYPE(None, ctypes.c_void_p) def pfunc(args): + """prepare function.""" prepare_fun(data) - rbtlib.RabitAllreduce(buf.ctypes.data_as(ctypes.c_void_p), - buf.size, DTYPE_ENUM__[buf.dtype], - op, PFUNC(pfunc), None) - check_err__() + _LIB.RabitAllreduce(buf.ctypes.data_as(ctypes.c_void_p), + buf.size, DTYPE_ENUM__[buf.dtype], + op, func_ptr(pfunc), None) return buf -def load_model__(ptr, length): +def _load_model(ptr, length): """ Internal function used by the module, unpickle a model from a buffer specified by ptr, length @@ -229,78 +239,89 @@ def load_model__(ptr, length): data = (ctypes.c_char * length).from_address(ctypes.addressof(ptr.contents)) return pickle.loads(data.raw) -def load_checkpoint(with_local = False): - """ - load latest check point - Arguments: - with_local: boolean [default = False] - whether the checkpoint contains local model - Returns: +def load_checkpoint(with_local=False): + """Load latest check point. + + Parameters + ---------- + with_local: bool, optional + whether the checkpoint contains local model + + Returns + ------- + tuple : tuple if with_local: return (version, gobal_model, local_model) else return (version, gobal_model) if returned version == 0, this means no model has been CheckPointed and global_model, local_model returned will be None """ - gp = ctypes.POINTER(ctypes.c_char)() + gptr = ctypes.POINTER(ctypes.c_char)() global_len = ctypes.c_ulong() if with_local: - lp = ctypes.POINTER(ctypes.c_char)() + lptr = ctypes.POINTER(ctypes.c_char)() local_len = ctypes.c_ulong() - version = rbtlib.RabitLoadCheckPoint( - ctypes.byref(gp), + version = _LIB.RabitLoadCheckPoint( + ctypes.byref(gptr), ctypes.byref(global_len), - ctypes.byref(lp), + ctypes.byref(lptr), ctypes.byref(local_len)) - check_err__() if version == 0: return (version, None, None) return (version, - load_model__(gp, global_len.value), - load_model__(lp, local_len.value)) + _load_model(gptr, global_len.value), + _load_model(lptr, local_len.value)) else: - version = rbtlib.RabitLoadCheckPoint( - ctypes.byref(gp), + version = _LIB.RabitLoadCheckPoint( + ctypes.byref(gptr), ctypes.byref(global_len), None, None) - check_err__() if version == 0: return (version, None) return (version, - load_model__(gp, global_len.value)) - -def checkpoint(global_model, local_model = None): - """ - checkpoint the model, meaning we finished a stage of execution - every time we call check point, there is a version number which will increase by one + _load_model(gptr, global_len.value)) - Arguments: - global_model: anytype that can be pickled - globally shared model/state when calling this function, - the caller need to gauranttees that global_model is the same in all nodes - local_model: anytype that can be pickled - local model, that is specific to current node/rank. - This can be None when no local state is needed. - local_model requires explicit replication of the model for fault-tolerance, - which will bring replication cost in checkpoint function, - while global_model do not need explicit replication. - It is recommended to use global_model if possible +def checkpoint(global_model, local_model=None): + """Checkpoint the model. + + This means we finished a stage of execution. + Every time we call check point, there is a version number which will increase by one. + + Parameters + ---------- + global_model: anytype that can be pickled + globally shared model/state when calling this function, + the caller need to gauranttees that global_model is the same in all nodes + + local_model: anytype that can be pickled + Local model, that is specific to current node/rank. + This can be None when no local state is needed. + + Notes + ----- + local_model requires explicit replication of the model for fault-tolerance. + This will bring replication cost in checkpoint function. + while global_model do not need explicit replication. + It is recommended to use global_model if possible. """ - sg = pickle.dumps(global_model) + sglobal = pickle.dumps(global_model) if local_model is None: - rbtlib.RabitCheckPoint(sg, len(sg), None, 0) - check_err__() - del sg; + _LIB.RabitCheckPoint(sglobal, len(sglobal), None, 0) + del sglobal else: - sl = pickle.dumps(local_model) - rbtlib.RabitCheckPoint(sg, len(sg), sl, len(sl)) - check_err__() - del sl; del sg; + slocal = pickle.dumps(local_model) + _LIB.RabitCheckPoint(sglobal, len(sglobal), slocal, len(slocal)) + del slocal + del sglobal def version_number(): + """Returns version number of current stored model. + + This means how many calls to CheckPoint we made so far. + + Returns + ------- + version : int + Version number of currently stored model """ - Returns version number of current stored model, - which means how many calls to CheckPoint we made so far - """ - ret = rbtlib.RabitVersionNumber() - check_err__() + ret = _LIB.RabitVersionNumber() return ret diff --git a/subtree/rabit/wrapper/rabit_wrapper.cc b/subtree/rabit/wrapper/rabit_wrapper.cc index 704bf4abc..7025b3ffe 100644 --- a/subtree/rabit/wrapper/rabit_wrapper.cc +++ b/subtree/rabit/wrapper/rabit_wrapper.cc @@ -1,3 +1,4 @@ +// Copyright by Contributors // implementations in ctypes #define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE @@ -28,7 +29,7 @@ struct FHelper { void (*prepare_fun)(void *arg), void *prepare_arg) { utils::Error("DataType does not support bitwise or operation"); - } + } }; template inline void Allreduce_(void *sendrecvbuf_, @@ -60,12 +61,12 @@ inline void Allreduce_(void *sendrecvbuf_, return; case kLong: rabit::Allreduce - (static_cast(sendrecvbuf_), + (static_cast(sendrecvbuf_), // NOLINT(*) count, prepare_fun, prepare_arg); return; case kULong: rabit::Allreduce - (static_cast(sendrecvbuf_), + (static_cast(sendrecvbuf_), // NOLINT(*) count, prepare_fun, prepare_arg); return; case kFloat: @@ -135,7 +136,7 @@ struct ReadWrapper : public Serializable { } virtual void Save(Stream *fo) const { utils::Error("not implemented"); - } + } }; struct WriteWrapper : public Serializable { const char *data; @@ -179,7 +180,7 @@ extern "C" { if (s.length() > max_len) { s.resize(max_len - 1); } - strcpy(out_name, s.c_str()); + strcpy(out_name, s.c_str()); // NOLINT(*) *out_len = static_cast(s.length()); } void RabitBroadcast(void *sendrecv_data, @@ -218,7 +219,7 @@ extern "C" { *out_local_model = BeginPtr(local_buffer); *out_local_len = static_cast(local_buffer.length()); } - return version; + return version; } void RabitCheckPoint(const char *global_model, rbt_ulong global_len, diff --git a/subtree/rabit/wrapper/rabit_wrapper.h b/subtree/rabit/wrapper/rabit_wrapper.h index 39caa70b4..d00a31fda 100644 --- a/subtree/rabit/wrapper/rabit_wrapper.h +++ b/subtree/rabit/wrapper/rabit_wrapper.h @@ -1,18 +1,19 @@ -#ifndef RABIT_WRAPPER_H_ -#define RABIT_WRAPPER_H_ /*! + * Copyright by Contributors * \file rabit_wrapper.h * \author Tianqi Chen * \brief a C style wrapper of rabit * can be used to create wrapper of other languages */ +#ifndef RABIT_WRAPPER_H_ +#define RABIT_WRAPPER_H_ #ifdef _MSC_VER #define RABIT_DLL __declspec(dllexport) #else #define RABIT_DLL #endif // manually define unsign long -typedef unsigned long rbt_ulong; +typedef unsigned long rbt_ulong; // NOLINT(*) #ifdef __cplusplus extern "C" { @@ -23,8 +24,8 @@ extern "C" { * \param argv the array of input arguments */ RABIT_DLL void RabitInit(int argc, char *argv[]); - /*! - * \brief finalize the rabit engine, call this function after you finished all jobs + /*! + * \brief finalize the rabit engine, call this function after you finished all jobs */ RABIT_DLL void RabitFinalize(void); /*! \brief get rank of current process */ @@ -37,9 +38,9 @@ extern "C" { * the user who monitors the tracker * \param msg the message to be printed */ - RABIT_DLL void RabitTrackerPrint(const char *msg); + RABIT_DLL void RabitTrackerPrint(const char *msg); /*! - * \brief get name of processor + * \brief get name of processor * \param out_name hold output string * \param out_len hold length of output string * \param max_len maximum buffer length of input @@ -50,7 +51,7 @@ extern "C" { /*! * \brief broadcast an memory region to all others from root * - * Example: int a = 1; Broadcast(&a, sizeof(a), root); + * Example: int a = 1; Broadcast(&a, sizeof(a), root); * \param sendrecv_data the pointer to send or recive buffer, * \param size the size of the data * \param root the root of process @@ -58,7 +59,7 @@ extern "C" { RABIT_DLL void RabitBroadcast(void *sendrecv_data, rbt_ulong size, int root); /*! - * \brief perform in-place allreduce, on sendrecvbuf + * \brief perform in-place allreduce, on sendrecvbuf * this function is NOT thread-safe * * Example Usage: the following code gives sum of the result @@ -81,14 +82,14 @@ extern "C" { int enum_op, void (*prepare_fun)(void *arg), void *prepare_arg); - + /*! * \brief load latest check point * \param out_global_model hold output of serialized global_model * \param out_global_len the output length of serialized global model * \param out_local_model hold output of serialized local_model, can be NULL * \param out_local_len the output length of serialized local model, can be NULL - * + * * \return the version number of check point loaded * if returned version == 0, this means no model has been CheckPointed * nothing will be touched @@ -100,7 +101,7 @@ extern "C" { /*! * \brief checkpoint the model, meaning we finished a stage of execution * every time we call check point, there is a version number which will increase by one - * + * * \param global_model hold content of serialized global_model * \param global_len the content length of serialized global model * \param local_model hold content of serialized local_model, can be NULL @@ -122,4 +123,4 @@ extern "C" { #ifdef __cplusplus } // C #endif -#endif // XGBOOST_WRAPPER_H_ +#endif // RABIT_WRAPPER_H_ diff --git a/tests/python/test_basic.py b/tests/python/test_basic.py index fa287b247..a8e0d5238 100644 --- a/tests/python/test_basic.py +++ b/tests/python/test_basic.py @@ -3,8 +3,11 @@ import numpy as np import xgboost as xgb import unittest +import matplotlib +matplotlib.use('Agg') dpath = 'demo/data/' +rng = np.random.RandomState(1994) class TestBasic(unittest.TestCase): @@ -197,9 +200,6 @@ class TestBasic(unittest.TestCase): bst2 = xgb.Booster(model_file='xgb.model') # plotting - import matplotlib - matplotlib.use('Agg') - from matplotlib.axes import Axes from graphviz import Digraph @@ -220,7 +220,6 @@ class TestBasic(unittest.TestCase): for p in ax.patches: assert p.get_facecolor() == (1.0, 0, 0, 1.0) # red - ax = xgb.plot_importance(bst2, color=['r', 'r', 'b', 'b'], title=None, xlabel=None, ylabel=None) assert isinstance(ax, Axes) @@ -235,5 +234,63 @@ class TestBasic(unittest.TestCase): g = xgb.to_graphviz(bst2, num_trees=0) assert isinstance(g, Digraph) + ax = xgb.plot_tree(bst2, num_trees=0) assert isinstance(ax, Axes) + + def test_importance_plot_lim(self): + np.random.seed(1) + dm = xgb.DMatrix(np.random.randn(100, 100), label=[0, 1]*50) + bst = xgb.train({}, dm) + assert len(bst.get_fscore()) == 71 + ax = xgb.plot_importance(bst) + assert ax.get_xlim() == (0., 11.) + assert ax.get_ylim() == (-1., 71.) + + ax = xgb.plot_importance(bst, xlim=(0, 5), ylim=(10, 71)) + assert ax.get_xlim() == (0., 5.) + assert ax.get_ylim() == (10., 71.) + + def test_sklearn_api(self): + from sklearn import datasets + from sklearn.cross_validation import train_test_split + + np.random.seed(1) + + iris = datasets.load_iris() + tr_d, te_d, tr_l, te_l = train_test_split(iris.data, iris.target, train_size=120) + + classifier = xgb.XGBClassifier() + classifier.fit(tr_d, tr_l) + + preds = classifier.predict(te_d) + labels = te_l + err = sum([1 for p, l in zip(preds, labels) if p != l]) / len(te_l) + # error must be smaller than 10% + assert err < 0.1 + + def test_sklearn_plotting(self): + from sklearn import datasets + iris = datasets.load_iris() + + classifier = xgb.XGBClassifier() + classifier.fit(iris.data, iris.target) + + import matplotlib + matplotlib.use('Agg') + + from matplotlib.axes import Axes + from graphviz import Digraph + + ax = xgb.plot_importance(classifier) + assert isinstance(ax, Axes) + assert ax.get_title() == 'Feature importance' + assert ax.get_xlabel() == 'F score' + assert ax.get_ylabel() == 'Features' + assert len(ax.patches) == 4 + + g = xgb.to_graphviz(classifier, num_trees=0) + assert isinstance(g, Digraph) + + ax = xgb.plot_tree(classifier, num_trees=0) + assert isinstance(ax, Axes) \ No newline at end of file diff --git a/tests/python/test_early_stopping.py b/tests/python/test_early_stopping.py new file mode 100644 index 000000000..ef2cc1263 --- /dev/null +++ b/tests/python/test_early_stopping.py @@ -0,0 +1,32 @@ +import xgboost as xgb +import numpy as np +from sklearn.datasets import load_digits +from sklearn.cross_validation import KFold, train_test_split +import unittest + +rng = np.random.RandomState(1994) + +class TestEarlyStopping(unittest.TestCase): + + def test_early_stopping_nonparallel(self): + digits = load_digits(2) + X = digits['data'] + y = digits['target'] + X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) + clf1 = xgb.XGBClassifier() + clf1.fit(X_train, y_train, early_stopping_rounds=5, eval_metric="auc", + eval_set=[(X_test, y_test)]) + clf2 = xgb.XGBClassifier() + clf2.fit(X_train, y_train, early_stopping_rounds=4, eval_metric="auc", + eval_set=[(X_test, y_test)]) + # should be the same + assert clf1.best_score == clf2.best_score + assert clf1.best_score != 1 + # check overfit + clf3 = xgb.XGBClassifier() + clf3.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="auc", + eval_set=[(X_test, y_test)]) + assert clf3.best_score == 1 + +# TODO: parallel test for early stopping +# TODO: comment out for now. Will re-visit later \ No newline at end of file diff --git a/tests/python/test_models.py b/tests/python/test_models.py index 8c06d9de9..295765d61 100644 --- a/tests/python/test_models.py +++ b/tests/python/test_models.py @@ -1,39 +1,80 @@ import numpy as np import xgboost as xgb +import unittest dpath = 'demo/data/' dtrain = xgb.DMatrix(dpath + 'agaricus.txt.train') dtest = xgb.DMatrix(dpath + 'agaricus.txt.test') -def test_glm(): - param = {'silent':1, 'objective':'binary:logistic', 'booster':'gblinear', 'alpha': 0.0001, 'lambda': 1 } - watchlist = [(dtest,'eval'), (dtrain,'train')] - num_round = 4 - bst = xgb.train(param, dtrain, num_round, watchlist) - assert isinstance(bst, xgb.core.Booster) - preds = bst.predict(dtest) - labels = dtest.get_label() - err = sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) / float(len(preds)) - assert err < 0.1 +rng = np.random.RandomState(1994) -def test_custom_objective(): - param = {'max_depth':2, 'eta':1, 'silent':1 } - watchlist = [(dtest,'eval'), (dtrain,'train')] - num_round = 2 - def logregobj(preds, dtrain): - labels = dtrain.get_label() - preds = 1.0 / (1.0 + np.exp(-preds)) - grad = preds - labels - hess = preds * (1.0-preds) - return grad, hess - def evalerror(preds, dtrain): - labels = dtrain.get_label() - return 'error', float(sum(labels != (preds > 0.0))) / len(labels) - bst = xgb.train(param, dtrain, num_round, watchlist, logregobj, evalerror) - assert isinstance(bst, xgb.core.Booster) - preds = bst.predict(dtest) - labels = dtest.get_label() - err = sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) / float(len(preds)) - assert err < 0.1 +class TestModels(unittest.TestCase): + + def test_glm(self): + param = {'silent':1, 'objective':'binary:logistic', 'booster':'gblinear', 'alpha': 0.0001, 'lambda': 1 } + watchlist = [(dtest,'eval'), (dtrain,'train')] + num_round = 4 + bst = xgb.train(param, dtrain, num_round, watchlist) + assert isinstance(bst, xgb.core.Booster) + preds = bst.predict(dtest) + labels = dtest.get_label() + err = sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) / float(len(preds)) + assert err < 0.1 + + def test_eta_decay(self): + param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' } + watchlist = [(dtest,'eval'), (dtrain,'train')] + num_round = 2 + # learning_rates as a list + bst = xgb.train(param, dtrain, num_round, watchlist, learning_rates=[0.4, 0.3]) + assert isinstance(bst, xgb.core.Booster) + + # learning_rates as a customized decay function + def eta_decay(ithround, num_boost_round): + return num_boost_round / ithround + bst = xgb.train(param, dtrain, num_round, watchlist, learning_rates=eta_decay) + assert isinstance(bst, xgb.core.Booster) + def test_custom_objective(self): + param = {'max_depth':2, 'eta':1, 'silent':1 } + watchlist = [(dtest,'eval'), (dtrain,'train')] + num_round = 2 + def logregobj(preds, dtrain): + labels = dtrain.get_label() + preds = 1.0 / (1.0 + np.exp(-preds)) + grad = preds - labels + hess = preds * (1.0-preds) + return grad, hess + def evalerror(preds, dtrain): + labels = dtrain.get_label() + return 'error', float(sum(labels != (preds > 0.0))) / len(labels) + + # test custom_objective in training + bst = xgb.train(param, dtrain, num_round, watchlist, logregobj, evalerror) + assert isinstance(bst, xgb.core.Booster) + preds = bst.predict(dtest) + labels = dtest.get_label() + err = sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) / float(len(preds)) + assert err < 0.1 + + # test custom_objective in cross-validation + xgb.cv(param, dtrain, num_round, nfold = 5, seed = 0, + obj = logregobj, feval=evalerror) + + def test_fpreproc(self): + param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'} + num_round = 2 + def fpreproc(dtrain, dtest, param): + label = dtrain.get_label() + ratio = float(np.sum(label == 0)) / np.sum(label==1) + param['scale_pos_weight'] = ratio + return (dtrain, dtest, param) + xgb.cv(param, dtrain, num_round, nfold=5, + metrics={'auc'}, seed = 0, fpreproc = fpreproc) + + def test_show_stdv(self): + param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'} + num_round = 2 + xgb.cv(param, dtrain, num_round, nfold=5, + metrics={'error'}, seed = 0, show_stdv = False) diff --git a/tests/python/test_training_continuation.py b/tests/python/test_training_continuation.py new file mode 100644 index 000000000..fec7a6a62 --- /dev/null +++ b/tests/python/test_training_continuation.py @@ -0,0 +1,52 @@ +import xgboost as xgb +import numpy as np +from sklearn.cross_validation import KFold, train_test_split +from sklearn.metrics import mean_squared_error +from sklearn.grid_search import GridSearchCV +from sklearn.datasets import load_iris, load_digits, load_boston +import unittest + +rng = np.random.RandomState(1337) + +class TestTrainingContinuation(unittest.TestCase): + + xgb_params = { + 'colsample_bytree': 0.7, + 'silent': 1, + 'nthread': 1, + } + + def test_training_continuation(self): + digits = load_digits(2) + X = digits['data'] + y = digits['target'] + + dtrain = xgb.DMatrix(X,label=y) + + gbdt_01 = xgb.train(self.xgb_params, dtrain, num_boost_round=10) + ntrees_01 = len(gbdt_01.get_dump()) + assert ntrees_01 == 10 + + gbdt_02 = xgb.train(self.xgb_params, dtrain, num_boost_round=0) + gbdt_02.save_model('xgb_tc.model') + + gbdt_02a = xgb.train(self.xgb_params, dtrain, num_boost_round=10, xgb_model=gbdt_02) + gbdt_02b = xgb.train(self.xgb_params, dtrain, num_boost_round=10, xgb_model="xgb_tc.model") + ntrees_02a = len(gbdt_02a.get_dump()) + ntrees_02b = len(gbdt_02b.get_dump()) + assert ntrees_02a == 10 + assert ntrees_02b == 10 + assert mean_squared_error(y, gbdt_01.predict(dtrain)) == mean_squared_error(y, gbdt_02a.predict(dtrain)) + assert mean_squared_error(y, gbdt_01.predict(dtrain)) == mean_squared_error(y, gbdt_02b.predict(dtrain)) + + gbdt_03 = xgb.train(self.xgb_params, dtrain, num_boost_round=3) + gbdt_03.save_model('xgb_tc.model') + + gbdt_03a = xgb.train(self.xgb_params, dtrain, num_boost_round=7, xgb_model=gbdt_03) + gbdt_03b = xgb.train(self.xgb_params, dtrain, num_boost_round=7, xgb_model="xgb_tc.model") + ntrees_03a = len(gbdt_03a.get_dump()) + ntrees_03b = len(gbdt_03b.get_dump()) + assert ntrees_03a == 10 + assert ntrees_03b == 10 + assert mean_squared_error(y, gbdt_03a.predict(dtrain)) == mean_squared_error(y, gbdt_03b.predict(dtrain)) + diff --git a/tests/python/test_with_sklearn.py b/tests/python/test_with_sklearn.py new file mode 100644 index 000000000..3e31ddb65 --- /dev/null +++ b/tests/python/test_with_sklearn.py @@ -0,0 +1,64 @@ +import xgboost as xgb +import numpy as np +from sklearn.cross_validation import KFold, train_test_split +from sklearn.metrics import mean_squared_error +from sklearn.grid_search import GridSearchCV +from sklearn.datasets import load_iris, load_digits, load_boston + +rng = np.random.RandomState(1994) + +def test_binary_classification(): + digits = load_digits(2) + y = digits['target'] + X = digits['data'] + kf = KFold(y.shape[0], n_folds=2, shuffle=True, random_state=rng) + for train_index, test_index in kf: + xgb_model = xgb.XGBClassifier().fit(X[train_index],y[train_index]) + preds = xgb_model.predict(X[test_index]) + labels = y[test_index] + err = sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) / float(len(preds)) + assert err < 0.1 + +def test_multiclass_classification(): + iris = load_iris() + y = iris['target'] + X = iris['data'] + kf = KFold(y.shape[0], n_folds=2, shuffle=True, random_state=rng) + for train_index, test_index in kf: + xgb_model = xgb.XGBClassifier().fit(X[train_index],y[train_index]) + preds = xgb_model.predict(X[test_index]) + # test other params in XGBClassifier().fit + preds2 = xgb_model.predict(X[test_index], output_margin=True, ntree_limit=3) + preds3 = xgb_model.predict(X[test_index], output_margin=True, ntree_limit=0) + preds4 = xgb_model.predict(X[test_index], output_margin=False, ntree_limit=3) + labels = y[test_index] + err = sum(1 for i in range(len(preds)) if int(preds[i]>0.5)!=labels[i]) / float(len(preds)) + assert err < 0.4 + +def test_boston_housing_regression(): + boston = load_boston() + y = boston['target'] + X = boston['data'] + kf = KFold(y.shape[0], n_folds=2, shuffle=True, random_state=rng) + for train_index, test_index in kf: + xgb_model = xgb.XGBRegressor().fit(X[train_index],y[train_index]) + preds = xgb_model.predict(X[test_index]) + # test other params in XGBRegressor().fit + preds2 = xgb_model.predict(X[test_index], output_margin=True, ntree_limit=3) + preds3 = xgb_model.predict(X[test_index], output_margin=True, ntree_limit=0) + preds4 = xgb_model.predict(X[test_index], output_margin=False, ntree_limit=3) + labels = y[test_index] + assert mean_squared_error(preds, labels) < 25 + +def test_parameter_tuning(): + boston = load_boston() + y = boston['target'] + X = boston['data'] + xgb_model = xgb.XGBRegressor() + clf = GridSearchCV(xgb_model, + {'max_depth': [2,4,6], + 'n_estimators': [50,100,200]}, verbose=1) + clf.fit(X,y) + assert clf.best_score_ < 0.7 + assert clf.best_params_ == {'n_estimators': 100, 'max_depth': 4} +