Skip to content

Instantly share code, notes, and snippets.

@Laurae2
Last active December 24, 2018 21:34
Show Gist options
  • Save Laurae2/0d1a63c1c334172e690ddd26f1590234 to your computer and use it in GitHub Desktop.
Save Laurae2/0d1a63c1c334172e690ddd26f1590234 to your computer and use it in GitHub Desktop.
RAM usage of xgboost GPU in R
library(xgboost)
set.seed(1)
N <- 1000000
p <- 100
pp <- 25
X <- matrix(runif(N * p), ncol = p)
betas <- 2 * runif(pp) - 1
sel <- sort(sample(p, pp))
m <- X[, sel] %*% betas - 1 + rnorm(N)
y <- rbinom(N, 1, plogis(m))
format(object.size(X), units = "Mb")
trainer <- function(n_cpus, n_gpus, n_iterations, n_depth) {
if (n_gpus == 0) {
pt <- proc.time()
set.seed(11111)
model <- xgb.train(list(objective = "binary:logistic", nthread = n_cpus, eta = 0.10, max_depth = n_depth, max_bin = 64, tree_method = "hist"),
dtrain, nrounds = n_iterations, verbose = 0)
my_time <- proc.time() - pt
} else {
pt <- proc.time()
set.seed(11111)
model <- xgb.train(list(objective = "gpu:binary:logistic", nthread = n_cpus, eta = 0.10, max_depth = n_depth, max_bin = 64, tree_method = "gpu_hist", n_gpus = n_gpus),
dtrain, nrounds = n_iterations, verbose = 0)
my_time <- proc.time() - pt
}
return(my_time)
}
dtrain <- xgb.DMatrix(X, label = y)
wl <- list(test = dtest)
gpus_to_use_old <- 1
gpus_to_use <- 1
n_iters_to_use <- 5
depth_to_use <- 1:12
gc(verbose = FALSE)
if (gpus_to_use != gpus_to_use_old) {
rm(dtrain)
gc(verbose = FALSE)
dtrain <- xgb.DMatrix(X, label = y)
gc(verbose = FALSE)
}
gpus_to_use_old <- gpus_to_use
for (i in depth_to_use) {
trainer(gpus_to_use, gpus_to_use, n_iters_to_use, i)
cat("Record for Depth ", i, "!", sep = "")
Sys.sleep(5)
cat(" | Refreshed!\n", sep = "")
gc(verbose = FALSE)
Sys.sleep(3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment