## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----install------------------------------------------------------------------
# library(cuda.ml)
# 
# # Complete backend for training and GPU inference.
# cuda_ml_install()
# 
# # Smaller backend on a CPU-only inference host.
# cuda_ml_install(device = "cpu")

## ----train-random-forest------------------------------------------------------
# library(cuda.ml)
# cuda_ml_install()
# 
# set.seed(1)
# forest <- cuda_ml_rand_forest(
#   class ~ .,
#   data = modeldata::hpc_data,
#   trees = 500
# )
# 
# dir.create("hpc-forest")
# cuda_ml_nvforest_export(
#   forest,
#   directory = "hpc-forest",
#   prefix = "model"
# )

## ----deploy-random-forest-----------------------------------------------------
# library(cuda.ml)
# cuda_ml_install(device = "cpu")
# 
# forest <- cuda_ml_nvforest_import(
#   directory = "hpc-forest",
#   prefix = "model",
#   device = "cpu"
# )
# 
# hpc_predictors <- subset(modeldata::hpc_data, select = -class)
# predict(forest, hpc_predictors[1:5, ], type = "class")
# predict(forest, hpc_predictors[1:5, ], type = "prob")

## ----explicit-formats---------------------------------------------------------
# xgb_ubjson <- cuda_ml_nvforest_load_model(
#   "xgboost-model.ubj",
#   model_type = "xgboost_ubj",
#   device = "cpu"
# )
# xgb_json <- cuda_ml_nvforest_load_model(
#   "xgboost-model.json",
#   model_type = "xgboost_json",
#   device = "cpu"
# )
# xgb_legacy <- cuda_ml_nvforest_load_model(
#   "xgboost-model.model",
#   model_type = "xgboost_legacy",
#   device = "cpu"
# )
# lightgbm_model <- cuda_ml_nvforest_load_model(
#   "lightgbm-model.txt",
#   model_type = "lightgbm",
#   device = "cpu"
# )
# treelite_model <- cuda_ml_nvforest_load_model(
#   "treelite-model.checkpoint",
#   model_type = "treelite_checkpoint",
#   device = "cpu"
# )

## ----inferred-formats---------------------------------------------------------
# xgb_model <- cuda_ml_nvforest_load_model("xgboost-model.ubj", device = "cpu")
# lightgbm_model <- cuda_ml_nvforest_load_model(
#   "lightgbm-model.txt",
#   class_levels = c("no", "yes"),
#   device = "cpu"
# )

## ----devices------------------------------------------------------------------
# cpu_model <- cuda_ml_nvforest_load_model(
#   "model.ubj",
#   device = "cpu"
# )
# 
# gpu_model <- cuda_ml_nvforest_load_model(
#   "model.ubj",
#   device = "gpu",
#   device_id = 0
# )

## ----regression---------------------------------------------------------------
# regression_model <- cuda_ml_nvforest_load_model(
#   "regression.ubj",
#   device = "cpu"
# )
# 
# new_data <- data.frame(
#   feature_1 = c(0.2, 0.8),
#   feature_2 = c(1.5, 0.4)
# )
# 
# regression_predictions <- predict(regression_model, new_data)

## ----classification-----------------------------------------------------------
# classifier <- cuda_ml_nvforest_load_model(
#   "classifier.txt",
#   model_type = "lightgbm",
#   class_levels = c("no", "yes"),
#   device = "cpu"
# )
# 
# class_predictions <- predict(classifier, new_data, type = "class")
# probability_predictions <- predict(classifier, new_data, type = "prob")

## ----inspect------------------------------------------------------------------
# info <- cuda_ml_nvforest_info(classifier)
# info$task_type
# info$num_features
# info$num_trees
# info$device
# info$has_probability_output

## ----leaves-------------------------------------------------------------------
# leaf_ids <- cuda_ml_nvforest_leaf_ids(classifier, new_data)

## ----per-tree-----------------------------------------------------------------
# per_tree <- cuda_ml_nvforest_predict_per_tree(classifier, new_data)

## ----save-state---------------------------------------------------------------
# cuda_ml_serialize(classifier, "classifier.cuda-ml-state")
# classifier <- cuda_ml_unserialize(
#   "classifier.cuda-ml-state",
#   device = "cpu"
# )

