An open source, non-profit web search engine
Prediction should be safe in XGBoost
Daoud Clarke84547dfa287d
diff --git a/mwmbl_rank/src/lib.rs b/mwmbl_rank/src/lib.rsindex 7d3480e..21dd7a1 100644--- a/mwmbl_rank/src/lib.rs+++ b/mwmbl_rank/src/lib.rs@@ -46,9 +46,9 @@ fn py_dict_to_record(obj: &Bound<'_, PyAny>) -> PyResult<DocumentRecord> {////// Each record dict must have keys: query, url, title, extract, score.///-/// Note: marked `unsendable` because the underlying XGBoost Booster handle-/// is a raw C pointer that does not implement Send.-#[pyclass(name = "RustXGBPipeline", unsendable)]+/// XGBPipeline implements Send (see pipeline.rs), so this class is safe to use+/// from multiple Python threads (e.g. Django worker threads).+#[pyclass(name = "RustXGBPipeline")]pub struct PyXGBPipeline {inner: XGBPipeline,}diff --git a/mwmbl_rank/src/pipeline.rs b/mwmbl_rank/src/pipeline.rsindex b99da24..f365149 100644--- a/mwmbl_rank/src/pipeline.rs+++ b/mwmbl_rank/src/pipeline.rs@@ -66,6 +66,12 @@ pub fn extract_features_batch(records: &[DocumentRecord]) -> Vec<f32> {/// The Rust XGBoost pipeline./// Holds the trained booster and hyperparameters.+///+/// # Safety+/// `xgb::Booster` wraps a raw C pointer (`BoosterHandle`) which is `!Send` by default.+/// XGBoost's C API is documented to be safe for concurrent read-only access (prediction),+/// and we only mutate the booster during `fit()` / `load_model()`, which are called+/// single-threadedly before the object is shared. We therefore assert `Send` manually.pub struct XGBPipeline {pub threshold: f32,pub scale_pos_weight: f32,@@ -78,6 +84,9 @@ pub struct XGBPipeline {booster: Option<Booster>,}+// SAFETY: see doc-comment on XGBPipeline above.+unsafe impl Send for XGBPipeline {}+impl XGBPipeline {pub fn new(threshold: f32, scale_pos_weight: f32, reg_lambda: f32, num_rounds: u32) -> Self {XGBPipeline {