| 1 |
context("Models from previous versions of XGBoost can be loaded") |
| 2 |
|
| 3 |
metadata <- list( |
| 4 |
kRounds = 4, |
| 5 |
kRows = 1000, |
| 6 |
kCols = 4, |
| 7 |
kForests = 2, |
| 8 |
kMaxDepth = 2, |
| 9 |
kClasses = 3 |
| 10 |
) |
| 11 |
|
| 12 |
run_model_param_check <- function(config) { |
| 13 |
testthat::expect_equal(config$learner$learner_model_param$num_feature, "4") |
| 14 |
testthat::expect_equal(config$learner$learner_train_param$booster, "gbtree") |
| 15 |
} |
| 16 |
|
| 17 |
get_n_rounds <- function(model_file) { |
| 18 |
is_10 <- grepl("1.0.0rc1", model_file, fixed = TRUE) |
| 19 |
if (is_10) { |
| 20 |
2 |
| 21 |
} else { |
| 22 |
metadata$kRounds |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
get_num_tree <- function(booster) { |
| 27 |
dump <- xgb.dump(booster) |
| 28 |
m <- regexec("booster\\[[0-9]+\\]", dump, perl = TRUE) |
| 29 |
m <- regmatches(dump, m) |
| 30 |
num_tree <- Reduce("+", lapply(m, length)) |
| 31 |
num_tree |
| 32 |
} |
| 33 |
|
| 34 |
run_booster_check <- function(booster, model_file) { |
| 35 |
config <- xgb.config(booster) |
| 36 |
run_model_param_check(config) |
| 37 |
is_model <- function(typ) { |
| 38 |
grepl(typ, model_file, fixed = TRUE) |
| 39 |
} |
| 40 |
n_rounds <- get_n_rounds(model_file = model_file) |
| 41 |
if (is_model("cls")) { |
| 42 |
testthat::expect_equal( |
| 43 |
get_num_tree(booster), metadata$kForests * n_rounds * metadata$kClasses |
| 44 |
) |
| 45 |
testthat::expect_equal(get_basescore(config), c(0.5, 0.5, 0.5)) # nolint |
| 46 |
testthat::expect_equal( |
| 47 |
config$learner$learner_train_param$objective, "multi:softmax" |
| 48 |
) |
| 49 |
testthat::expect_equal( |
| 50 |
as.numeric(config$learner$learner_model_param$num_class), |
| 51 |
metadata$kClasses |
| 52 |
) |
| 53 |
} else if (is_model("logitraw")) { |
| 54 |
testthat::expect_equal(get_num_tree(booster), metadata$kForests * n_rounds) |
| 55 |
testthat::expect_equal( |
| 56 |
as.numeric(config$learner$learner_model_param$num_class), 0 |
| 57 |
) |
| 58 |
testthat::expect_equal( |
| 59 |
config$learner$learner_train_param$objective, "binary:logitraw" |
| 60 |
) |
| 61 |
} else if (is_model("logit")) { |
| 62 |
testthat::expect_equal(get_num_tree(booster), metadata$kForests * n_rounds) |
| 63 |
testthat::expect_equal( |
| 64 |
as.numeric(config$learner$learner_model_param$num_class), 0 |
| 65 |
) |
| 66 |
testthat::expect_equal( |
| 67 |
config$learner$learner_train_param$objective, "binary:logistic" |
| 68 |
) |
| 69 |
} else if (is_model("ltr")) { |
| 70 |
testthat::expect_equal(get_num_tree(booster), metadata$kForests * n_rounds) |
| 71 |
testthat::expect_equal( |
| 72 |
config$learner$learner_train_param$objective, "rank:ndcg" |
| 73 |
) |
| 74 |
} else if (is_model("aft")) { |
| 75 |
testthat::expect_equal(get_num_tree(booster), metadata$kForests * n_rounds) |
| 76 |
testthat::expect_equal( |
| 77 |
config$learner$learner_train_param$objective, "survival:aft" |
| 78 |
) |
| 79 |
} else { |
| 80 |
testthat::expect_true(is_model("reg")) |
| 81 |
testthat::expect_equal(get_num_tree(booster), metadata$kForests * n_rounds) |
| 82 |
testthat::expect_equal(get_basescore(config), 0.5) # nolint |
| 83 |
testthat::expect_equal( |
| 84 |
config$learner$learner_train_param$objective, "reg:squarederror" |
| 85 |
) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
test_that("Models from previous versions of XGBoost can be loaded", { |
| 90 |
bucket <- "xgboost-ci-jenkins-artifacts" |
| 91 |
region <- "us-west-2" |
| 92 |
file_name <- "xgboost_model_compatibility_tests-3.0.2.zip" |
| 93 |
zipfile <- tempfile(fileext = ".zip") |
| 94 |
extract_dir <- tempdir() |
| 95 |
result <- tryCatch( |
| 96 |
{ |
| 97 |
download.file( |
| 98 |
paste( |
| 99 |
"https://", bucket, ".s3-", region, ".amazonaws.com/", file_name, |
| 100 |
sep = "" |
| 101 |
), |
| 102 |
destfile = zipfile, mode = "wb", quiet = TRUE |
| 103 |
) |
| 104 |
zipfile |
| 105 |
}, |
| 106 |
error = function(e) { |
| 107 |
print(e) |
| 108 |
NA_character_ |
| 109 |
} |
| 110 |
) |
| 111 |
if (is.na(result)) { |
| 112 |
print("Failed to download old models.") |
| 113 |
return() |
| 114 |
} |
| 115 |
|
| 116 |
unzip(zipfile, exdir = extract_dir, overwrite = TRUE) |
| 117 |
model_dir <- file.path(extract_dir, "models") |
| 118 |
|
| 119 |
pred_data <- xgb.DMatrix( |
| 120 |
matrix(c(0, 0, 0, 0), nrow = 1, ncol = 4), |
| 121 |
nthread = 2 |
| 122 |
) |
| 123 |
|
| 124 |
lapply(list.files(model_dir), function(x) { |
| 125 |
model_file <- file.path(model_dir, x) |
| 126 |
is_skl <- grepl("scikit", model_file, fixed = TRUE) |
| 127 |
if (is_skl) { |
| 128 |
return() |
| 129 |
} |
| 130 |
booster <- xgb.load(model_file) |
| 131 |
xgb.model.parameters(booster) <- list(nthread = 2) |
| 132 |
predict(booster, newdata = pred_data) |
| 133 |
run_booster_check(booster, model_file) |
| 134 |
}) |
| 135 |
}) |