-
Notifications
You must be signed in to change notification settings - Fork 274
/
xgboost.rs
371 lines (341 loc) · 15.8 KB
/
xgboost.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/// XGBoost implementation.
///
/// XGBoost is a family of gradient-boosted decision tree algorithms,
/// that are very effective on real-world datasets.
///
/// It uses its own dense matrix.
use anyhow::{anyhow, Result};
use xgboost::parameters::tree::*;
use xgboost::parameters::*;
use xgboost::{Booster, DMatrix};
use crate::orm::dataset::Dataset;
use crate::orm::Hyperparams;
use crate::bindings::Bindings;
use pgrx::*;
#[pg_extern]
fn xgboost_version() -> String {
String::from("1.62")
}
fn get_dart_params(hyperparams: &Hyperparams) -> dart::DartBoosterParameters {
let mut params = dart::DartBoosterParametersBuilder::default();
for (key, value) in hyperparams {
match key.as_str() {
"rate_drop" => params.rate_drop(value.as_f64().unwrap() as f32),
"one_drop" => params.one_drop(value.as_bool().unwrap()),
"skip_drop" => params.skip_drop(value.as_f64().unwrap() as f32),
"sample_type" => match value.as_str().unwrap() {
"uniform" => params.sample_type(dart::SampleType::Uniform),
"weighted" => params.sample_type(dart::SampleType::Weighted),
_ => panic!("Unknown {:?}: {:?}", key, value),
},
"normalize_type" => match value.as_str().unwrap() {
"tree" => params.normalize_type(dart::NormalizeType::Tree),
"forest" => params.normalize_type(dart::NormalizeType::Forest),
_ => panic!("Unknown {:?}: {:?}", key, value),
},
"booster" | "n_estimators" | "boost_rounds" => &mut params, // Valid but not relevant to this section
"nthread" => &mut params,
_ => panic!("Unknown {:?}: {:?}", key, value),
};
}
params.build().unwrap()
}
fn get_linear_params(hyperparams: &Hyperparams) -> linear::LinearBoosterParameters {
let mut params = linear::LinearBoosterParametersBuilder::default();
for (key, value) in hyperparams {
match key.as_str() {
"alpha" | "reg_alpha" => params.alpha(value.as_f64().unwrap() as f32),
"lambda" | "reg_lambda" => params.lambda(value.as_f64().unwrap() as f32),
"updater" => match value.as_str().unwrap() {
"shotgun" => params.updater(linear::LinearUpdate::Shotgun),
"coord_descent" => params.updater(linear::LinearUpdate::CoordDescent),
_ => panic!("Unknown {:?}: {:?}", key, value),
},
"booster" | "n_estimators" | "boost_rounds" => &mut params, // Valid but not relevant to this section
"nthread" => &mut params,
_ => panic!("Unknown {:?}: {:?}", key, value),
};
}
params.build().unwrap()
}
fn get_tree_params(hyperparams: &Hyperparams) -> tree::TreeBoosterParameters {
let mut params = tree::TreeBoosterParametersBuilder::default();
for (key, value) in hyperparams {
match key.as_str() {
"eta" | "learning_rate" => params.eta(value.as_f64().unwrap() as f32),
"gamma" | "min_split_loss" => params.gamma(value.as_f64().unwrap() as f32),
"max_depth" => params.max_depth(value.as_u64().unwrap() as u32),
"min_child_weight" => params.min_child_weight(value.as_f64().unwrap() as f32),
"max_delta_step" => params.max_delta_step(value.as_f64().unwrap() as f32),
"subsample" => params.subsample(value.as_f64().unwrap() as f32),
"colsample_bytree" => params.colsample_bytree(value.as_f64().unwrap() as f32),
"colsample_bylevel" => params.colsample_bylevel(value.as_f64().unwrap() as f32),
"lambda" | "reg_lambda" => params.lambda(value.as_f64().unwrap() as f32),
"alpha" | "reg_alpha" => params.alpha(value.as_f64().unwrap() as f32),
"tree_method" => match value.as_str().unwrap() {
"auto" => params.tree_method(TreeMethod::Auto),
"exact" => params.tree_method(TreeMethod::Exact),
"approx" => params.tree_method(TreeMethod::Approx),
"hist" => params.tree_method(TreeMethod::Hist),
"gpu_exact" => params.tree_method(TreeMethod::GpuExact),
"gpu_hist" => params.tree_method(TreeMethod::GpuHist),
_ => panic!("Unknown hyperparameter {:?}: {:?}", key, value),
},
"sketch_eps" => params.sketch_eps(value.as_f64().unwrap() as f32),
"scale_pos_weight" => params.scale_pos_weight(value.as_f64().unwrap() as f32),
"updater" => match value.as_array() {
Some(array) => {
let mut v = Vec::new();
for value in array {
match value.as_str().unwrap() {
"grow_col_maker" => v.push(TreeUpdater::GrowColMaker),
"dist_col" => v.push(TreeUpdater::DistCol),
"grow_hist_maker" => v.push(TreeUpdater::GrowHistMaker),
"grow_local_hist_maker" => v.push(TreeUpdater::GrowLocalHistMaker),
"grow_sk_maker" => v.push(TreeUpdater::GrowSkMaker),
"sync" => v.push(TreeUpdater::Sync),
"refresh" => v.push(TreeUpdater::Refresh),
"prune" => v.push(TreeUpdater::Prune),
_ => panic!("Unknown hyperparameter {:?}: {:?}", key, value),
}
}
params.updater(v)
}
_ => panic!("updater should be a JSON array. Got: {:?}", value),
},
"refresh_leaf" => params.refresh_leaf(value.as_bool().unwrap()),
"process_type" => match value.as_str().unwrap() {
"default" => params.process_type(ProcessType::Default),
"update" => params.process_type(ProcessType::Update),
_ => panic!("Unknown hyperparameter {:?}: {:?}", key, value),
},
"grow_policy" => match value.as_str().unwrap() {
"depthwise" => params.grow_policy(GrowPolicy::Depthwise),
"loss_guide" => params.grow_policy(GrowPolicy::LossGuide),
_ => panic!("Unknown hyperparameter {:?}: {:?}", key, value),
},
"predictor" => match value.as_str().unwrap() {
"cpu" => params.predictor(Predictor::Cpu),
"gpu" => params.predictor(Predictor::Gpu),
_ => panic!("Unknown hyperparameter {:?}: {:?}", key, value),
},
"max_leaves" => params.max_leaves(value.as_u64().unwrap() as u32),
"max_bin" => params.max_bin(value.as_u64().unwrap() as u32),
"booster" | "n_estimators" | "boost_rounds" | "eval_metric" | "objective" => &mut params, // Valid but not relevant to this section
"nthread" => &mut params,
"random_state" => &mut params,
_ => panic!("Unknown hyperparameter {:?}: {:?}", key, value),
};
}
params.build().unwrap()
}
pub fn fit_regression(dataset: &Dataset, hyperparams: &Hyperparams) -> Result<Box<dyn Bindings>> {
fit(dataset, hyperparams, learning::Objective::RegLinear)
}
pub fn fit_classification(dataset: &Dataset, hyperparams: &Hyperparams) -> Result<Box<dyn Bindings>> {
fit(
dataset,
hyperparams,
learning::Objective::MultiSoftprob(dataset.num_distinct_labels.try_into().unwrap()),
)
}
fn eval_metric_from_string(name: &str) -> learning::EvaluationMetric {
match name {
"rmse" => learning::EvaluationMetric::RMSE,
"mae" => learning::EvaluationMetric::MAE,
"logloss" => learning::EvaluationMetric::LogLoss,
"merror" => learning::EvaluationMetric::MultiClassErrorRate,
"mlogloss" => learning::EvaluationMetric::MultiClassLogLoss,
"auc" => learning::EvaluationMetric::AUC,
"ndcg" => learning::EvaluationMetric::NDCG,
"ndcg-" => learning::EvaluationMetric::NDCGNegative,
"map" => learning::EvaluationMetric::MAP,
"map-" => learning::EvaluationMetric::MAPNegative,
"poisson-nloglik" => learning::EvaluationMetric::PoissonLogLoss,
"gamma-nloglik" => learning::EvaluationMetric::GammaLogLoss,
"cox-nloglik" => learning::EvaluationMetric::CoxLogLoss,
"gamma-deviance" => learning::EvaluationMetric::GammaDeviance,
"tweedie-nloglik" => learning::EvaluationMetric::TweedieLogLoss,
_ => error!("Unknown eval_metric: {:?}", name),
}
}
fn objective_from_string(name: &str, dataset: &Dataset) -> learning::Objective {
match name {
"reg:linear" => learning::Objective::RegLinear,
"reg:logistic" => learning::Objective::RegLogistic,
"binary:logistic" => learning::Objective::BinaryLogistic,
"binary:logitraw" => learning::Objective::BinaryLogisticRaw,
"gpu:reg:linear" => learning::Objective::GpuRegLinear,
"gpu:reg:logistic" => learning::Objective::GpuRegLogistic,
"gpu:binary:logistic" => learning::Objective::GpuBinaryLogistic,
"gpu:binary:logitraw" => learning::Objective::GpuBinaryLogisticRaw,
"count:poisson" => learning::Objective::CountPoisson,
"survival:cox" => learning::Objective::SurvivalCox,
"multi:softmax" => learning::Objective::MultiSoftmax(dataset.num_distinct_labels.try_into().unwrap()),
"multi:softprob" => learning::Objective::MultiSoftprob(dataset.num_distinct_labels.try_into().unwrap()),
"rank:pairwise" => learning::Objective::RankPairwise,
"reg:gamma" => learning::Objective::RegGamma,
"reg:tweedie" => learning::Objective::RegTweedie(Some(dataset.num_distinct_labels as f32)),
_ => error!("Unknown objective: {:?}", name),
}
}
fn fit(dataset: &Dataset, hyperparams: &Hyperparams, objective: learning::Objective) -> Result<Box<dyn Bindings>> {
// split the train/test data into DMatrix
let mut dtrain = DMatrix::from_dense(&dataset.x_train, dataset.num_train_rows).unwrap();
let mut dtest = DMatrix::from_dense(&dataset.x_test, dataset.num_test_rows).unwrap();
dtrain.set_labels(&dataset.y_train).unwrap();
dtest.set_labels(&dataset.y_test).unwrap();
// specify datasets to evaluate against during training
let evaluation_sets = &[(&dtrain, "train"), (&dtest, "test")];
let seed = match hyperparams.get("random_state") {
Some(value) => value.as_u64().unwrap(),
None => 0,
};
let eval_metrics = match hyperparams.get("eval_metric") {
Some(metrics) => {
if metrics.is_array() {
learning::Metrics::Custom(
metrics
.as_array()
.unwrap()
.iter()
.map(|metric| eval_metric_from_string(metric.as_str().unwrap()))
.collect(),
)
} else {
learning::Metrics::Custom(Vec::from([eval_metric_from_string(metrics.as_str().unwrap())]))
}
}
None => learning::Metrics::Auto,
};
let learning_params = match learning::LearningTaskParametersBuilder::default()
.objective(match hyperparams.get("objective") {
Some(value) => objective_from_string(value.as_str().unwrap(), dataset),
None => objective,
})
.eval_metrics(eval_metrics)
.seed(seed)
.build()
{
Ok(params) => params,
Err(e) => error!("Failed to parse learning params:\n\n{}", e),
};
// overall configuration for Booster
let booster_params = match BoosterParametersBuilder::default()
.learning_params(learning_params)
.booster_type(match hyperparams.get("booster") {
Some(value) => match value.as_str().unwrap() {
"gbtree" => BoosterType::Tree(get_tree_params(hyperparams)),
"linear" => BoosterType::Linear(get_linear_params(hyperparams)),
"dart" => BoosterType::Dart(get_dart_params(hyperparams)),
_ => panic!("Unknown booster: {:?}", value),
},
_ => BoosterType::Tree(get_tree_params(hyperparams)),
})
.threads(
hyperparams
.get("nthread")
.map(|value| value.as_i64().expect("nthread must be an integer") as u32),
)
.verbose(true)
.build()
{
Ok(params) => params,
Err(e) => error!("Failed to configure booster:\n\n{}", e),
};
let mut builder = TrainingParametersBuilder::default();
// number of training iterations is aliased
match hyperparams.get("n_estimators") {
Some(value) => builder.boost_rounds(value.as_u64().unwrap() as u32),
None => match hyperparams.get("boost_rounds") {
Some(value) => builder.boost_rounds(value.as_u64().unwrap() as u32),
None => &mut builder,
},
};
let params = match builder
// dataset to train with
.dtrain(&dtrain)
// optional datasets to evaluate against in each iteration
.evaluation_sets(Some(evaluation_sets))
// model parameters
.booster_params(booster_params)
.build()
{
Ok(params) => params,
Err(e) => error!("Failed to create training parameters:\n\n{}", e),
};
// train model, and print evaluation data
let booster = match Booster::train(¶ms) {
Ok(booster) => booster,
Err(e) => error!("Failed to train model:\n\n{}", e),
};
Ok(Box::new(Estimator { estimator: booster }))
}
pub struct Estimator {
estimator: xgboost::Booster,
}
unsafe impl Send for Estimator {}
unsafe impl Sync for Estimator {}
impl std::fmt::Debug for Estimator {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
formatter.debug_struct("Estimator").finish()
}
}
impl Bindings for Estimator {
fn predict(&self, features: &[f32], num_features: usize, num_classes: usize) -> Result<Vec<f32>> {
let x = DMatrix::from_dense(features, features.len() / num_features)?;
let y = self.estimator.predict(&x)?;
Ok(match num_classes {
0 => y,
_ => y
.chunks(num_classes)
.map(|probabilities| {
probabilities
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.total_cmp(b))
.map(|(index, _)| index)
.unwrap() as f32
})
.collect::<Vec<f32>>(),
})
}
fn predict_proba(&self, features: &[f32], num_features: usize) -> Result<Vec<f32>> {
let x = DMatrix::from_dense(features, features.len() / num_features)?;
Ok(self.estimator.predict(&x)?)
}
/// Serialize self to bytes
fn to_bytes(&self) -> Result<Vec<u8>> {
let r: u64 = rand::random();
let path = format!("/tmp/pgml_{}.bin", r);
self.estimator.save(std::path::Path::new(&path))?;
let bytes = std::fs::read(&path)?;
std::fs::remove_file(&path)?;
Ok(bytes)
}
/// Deserialize self from bytes, with additional context
fn from_bytes(bytes: &[u8]) -> Result<Box<dyn Bindings>>
where
Self: Sized,
{
let mut estimator = Booster::load_buffer(bytes);
if estimator.is_err() {
// backward compatibility w/ 2.0.0
estimator = Booster::load_buffer(&bytes[16..]);
}
let mut estimator = estimator?;
// Get concurrency setting
let concurrency: i64 = Spi::get_one(
"
SELECT COALESCE(
current_setting('pgml.predict_concurrency', true),
'2'
)::bigint",
)?
.unwrap();
estimator
.set_param("nthread", &concurrency.to_string())
.map_err(|e| anyhow!("could not set nthread XGBoost parameter: {e}"))?;
Ok(Box::new(Estimator { estimator }))
}
}