Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

New XGB rust model; more parameters for XGB eval

Daoud Clarke694026b87030
diff --git a/devdata/rankeval-2026-04/model.xgb b/devdata/rankeval-2026-04/model.xgb
new file mode 100644
index 0000000..bf22e9e
Binary files /dev/null and b/devdata/rankeval-2026-04/model.xgb differ
diff --git a/mwmbl/rankeval/evaluation/evaluate_remote.py b/mwmbl/rankeval/evaluation/evaluate_remote.py
index 4332023..ab914b5 100644
--- a/mwmbl/rankeval/evaluation/evaluate_remote.py
+++ b/mwmbl/rankeval/evaluation/evaluate_remote.py
@@ -36,7 +36,7 @@ def run():
# ranker = HeuristicRanker(RemoteIndex(), DummyCompleter())
# ranker = HeuristicAndWikiRanker(RemoteIndex(), DummyCompleter(), max_wiki_results=3)
model = MwmblRankingModel(ranker)
- evaluate(model, fraction=0.1, use_test=False)
+ evaluate(model, use_test=True)
def single_query(query: str):
diff --git a/mwmbl/rankeval/ltr/evaluate.py b/mwmbl/rankeval/ltr/evaluate.py
index e3551c3..dd8245d 100644
--- a/mwmbl/rankeval/ltr/evaluate.py
+++ b/mwmbl/rankeval/ltr/evaluate.py
@@ -73,10 +73,56 @@ def run():
parser.add_argument('--predictor', required=True, choices=sorted(PREDICTORS))
parser.add_argument("--binary-labels", required=False, action="store_true")
parser.add_argument('--note', required=True)
+ parser.add_argument('--max-depth', type=int, default=None, help='XGB max_depth parameter')
+ parser.add_argument('--scale-pos-weight', type=float, default=None, help='XGB scale_pos_weight parameter')
+ parser.add_argument('--n-estimators', type=int, default=None, help='XGB n_estimators parameter')
+ parser.add_argument('--min-child-weight', type=float, default=None, help='XGB min_child_weight parameter')
+ parser.add_argument('--gamma', type=float, default=None, help='XGB gamma (min_split_loss) parameter')
+ parser.add_argument('--reg-gamma', type=float, default=None, help='XGB reg_gamma parameter')
+ parser.add_argument('--reg-lambda', type=float, default=None, help='XGB reg_lambda parameter')
+ parser.add_argument('--subsample', type=float, default=None, help='XGB subsample parameter')
args = parser.parse_args()
- predictor = PREDICTORS[args.predictor]
+ # Build a dict of only the XGB params that were explicitly provided,
+ # so they override the hardcoded defaults in each predictor constructor.
+ xgb_params = {k: v for k, v in {
+ 'max_depth': args.max_depth,
+ 'scale_pos_weight': args.scale_pos_weight,
+ 'n_estimators': args.n_estimators,
+ 'min_child_weight': args.min_child_weight,
+ 'gamma': args.gamma,
+ 'reg_gamma': args.reg_gamma,
+ 'subsample': args.subsample,
+ }.items() if v is not None}
+
+ rust_params = {k: v for k, v in {
+ 'scale_pos_weight': args.scale_pos_weight,
+ 'num_rounds': args.n_estimators,
+ 'max_depth': args.max_depth,
+ 'min_child_weight': args.min_child_weight,
+ 'gamma': args.gamma,
+ 'reg_lambda': args.reg_lambda,
+ 'subsample': args.subsample,
+ }.items() if v is not None}
+
+ # Merge hardcoded defaults with any CLI overrides (CLI wins).
+ xgb_classifier_params = {'scale_pos_weight': 0.1, 'reg_lambda': 2, **xgb_params}
+ xgb_ranker_params = {'reg_lambda': 2, **xgb_params}
+ rust_pipeline_params = {'scale_pos_weight': 0.1, 'reg_lambda': 2.0, 'num_rounds': 100, **rust_params}
+
+ predictors = {
+ 'random': RandomRegressor(),
+ 'constant': DummyRegressor(),
+ 'decision_tree': make_pipeline(FeatureExtractor(), ThresholdPredictor(0.0, DecisionTreeClassifier())),
+ 'xgb': make_pipeline(FeatureExtractor(), ThresholdPredictor(0.0, XGBClassifier(**xgb_classifier_params))),
+ 'xgb_rust': RustXGBPipeline(threshold=0.0, **rust_pipeline_params),
+ 'xgb_limit_terms': RankingPredictor(FeatureExtractor(), ThresholdPredictor(0.0, XGBClassifier(**xgb_classifier_params))),
+ 'xgb_regressor': make_pipeline(FeatureExtractor(), XGBRegressor(objective="reg:pseudohubererror", **xgb_params)),
+ 'xgb_ranker': make_pipeline(FeatureExtractor(), XGBRanker(objective="rank:ndcg", **xgb_ranker_params)),
+ }
+
+ predictor = predictors[args.predictor]
dataset = pd.read_csv(LEARNING_TO_RANK_DATASET_PATH, lineterminator='\n')
if args.binary_labels:
diff --git a/mwmbl/tinysearchengine/ltr.py b/mwmbl/tinysearchengine/ltr.py
index dce1a22..fc218d4 100644
--- a/mwmbl/tinysearchengine/ltr.py
+++ b/mwmbl/tinysearchengine/ltr.py
@@ -97,6 +97,14 @@ class RustXGBPipeline(BaseEstimator, RegressorMixin):
XGBoost reg_lambda hyperparameter (default 2.0).
num_rounds : int
Number of XGBoost boosting rounds (default 100).
+ max_depth : int or None
+ XGBoost max_depth hyperparameter (default None, uses XGBoost default of 6).
+ min_child_weight : float or None
+ XGBoost min_child_weight hyperparameter (default None, uses XGBoost default of 1.0).
+ gamma : float or None
+ XGBoost gamma (min_split_loss) hyperparameter (default None, uses XGBoost default of 0.0).
+ subsample : float or None
+ XGBoost subsample hyperparameter (default None, uses XGBoost default of 1.0).
"""
def __init__(
@@ -105,16 +113,28 @@ class RustXGBPipeline(BaseEstimator, RegressorMixin):
scale_pos_weight: float = 0.1,
reg_lambda: float = 2.0,
num_rounds: int = 100,
+ max_depth: int | None = None,
+ min_child_weight: float | None = None,
+ gamma: float | None = None,
+ subsample: float | None = None,
):
self.threshold = threshold
self.scale_pos_weight = scale_pos_weight
self.reg_lambda = reg_lambda
self.num_rounds = num_rounds
+ self.max_depth = max_depth
+ self.min_child_weight = min_child_weight
+ self.gamma = gamma
+ self.subsample = subsample
self._inner = mwmbl_rank.RustXGBPipeline(
threshold=self.threshold,
scale_pos_weight=self.scale_pos_weight,
reg_lambda=self.reg_lambda,
num_rounds=self.num_rounds,
+ max_depth=self.max_depth,
+ min_child_weight=self.min_child_weight,
+ gamma=self.gamma,
+ subsample=self.subsample,
)
@staticmethod
@@ -186,6 +206,10 @@ class RustXGBPipeline(BaseEstimator, RegressorMixin):
scale_pos_weight: float = 0.1,
reg_lambda: float = 2.0,
num_rounds: int = 100,
+ max_depth: int | None = None,
+ min_child_weight: float | None = None,
+ gamma: float | None = None,
+ subsample: float | None = None,
) -> 'RustXGBPipeline':
"""Load a pre-trained model from disk and return a ready-to-predict pipeline."""
pipeline = cls(
@@ -193,6 +217,10 @@ class RustXGBPipeline(BaseEstimator, RegressorMixin):
scale_pos_weight=scale_pos_weight,
reg_lambda=reg_lambda,
num_rounds=num_rounds,
+ max_depth=max_depth,
+ min_child_weight=min_child_weight,
+ gamma=gamma,
+ subsample=subsample,
)
pipeline.load_model(path)
return pipeline
@@ -202,5 +230,9 @@ class RustXGBPipeline(BaseEstimator, RegressorMixin):
f"RustXGBPipeline(threshold={self.threshold}, "
f"scale_pos_weight={self.scale_pos_weight}, "
f"reg_lambda={self.reg_lambda}, "
- f"num_rounds={self.num_rounds})"
+ f"num_rounds={self.num_rounds}, "
+ f"max_depth={self.max_depth}, "
+ f"min_child_weight={self.min_child_weight}, "
+ f"gamma={self.gamma}, "
+ f"subsample={self.subsample})"
)
diff --git a/mwmbl/tinysearchengine/ltr_rank.py b/mwmbl/tinysearchengine/ltr_rank.py
index a3f2438..6938358 100644
--- a/mwmbl/tinysearchengine/ltr_rank.py
+++ b/mwmbl/tinysearchengine/ltr_rank.py
@@ -71,8 +71,13 @@ class LTRRanker(Ranker):
} for page in top_pages]
predictions = self.model.predict(data)
- indexes = np.argsort(predictions)[::-1]
- return [top_pages[i] for i in indexes if predictions[i] > 0.0]
+ mask = predictions > 0.0
+ filtered_predictions = predictions[mask]
+ filtered_pages = np.array(top_pages)[mask]
+
+ # Vectorized sorting
+ indices = np.argsort(filtered_predictions)[::-1]
+ return filtered_pages[indices].tolist()
def external_search(self, query: str) -> list[Document]:
if self.include_wiki:
diff --git a/mwmbl_rank/Cargo.toml b/mwmbl_rank/Cargo.toml
index e03e50b..6580448 100644
--- a/mwmbl_rank/Cargo.toml
+++ b/mwmbl_rank/Cargo.toml
@@ -17,7 +17,12 @@ once_cell = "1"
url = "2"
[profile.release]
-lto = true
+debug = true
+opt-level = 1
+lto = false
codegen-units = 1
-opt-level = 3
-# opt-level = 1
+panic = "abort" # Simplifies the call stack by removing unwinding logic
+
+# lto = true
+# codegen-units = 1
+# opt-level = 3
diff --git a/mwmbl_rank/src/lib.rs b/mwmbl_rank/src/lib.rs
index 4516d0c..7d3480e 100644
--- a/mwmbl_rank/src/lib.rs
+++ b/mwmbl_rank/src/lib.rs
@@ -62,11 +62,27 @@ impl PyXGBPipeline {
/// scale_pos_weight: XGBoost scale_pos_weight (default 0.1)
/// reg_lambda: XGBoost reg_lambda (default 2.0)
/// num_rounds: number of boosting rounds (default 100)
+ /// max_depth: XGBoost max_depth (default None → XGBoost default of 6)
+ /// min_child_weight: XGBoost min_child_weight (default None → XGBoost default of 1.0)
+ /// gamma: XGBoost gamma / min_split_loss (default None → XGBoost default of 0.0)
+ /// subsample: XGBoost subsample (default None → XGBoost default of 1.0)
#[new]
- #[pyo3(signature = (threshold=0.0, scale_pos_weight=0.1, reg_lambda=2.0, num_rounds=100))]
- fn new(threshold: f32, scale_pos_weight: f32, reg_lambda: f32, num_rounds: u32) -> Self {
+ #[pyo3(signature = (threshold=0.0, scale_pos_weight=0.1, reg_lambda=2.0, num_rounds=100, max_depth=None, min_child_weight=None, gamma=None, subsample=None))]
+ fn new(
+ threshold: f32,
+ scale_pos_weight: f32,
+ reg_lambda: f32,
+ num_rounds: u32,
+ max_depth: Option<u32>,
+ min_child_weight: Option<f32>,
+ gamma: Option<f32>,
+ subsample: Option<f32>,
+ ) -> Self {
PyXGBPipeline {
- inner: XGBPipeline::new(threshold, scale_pos_weight, reg_lambda, num_rounds),
+ inner: XGBPipeline::with_params(
+ threshold, scale_pos_weight, reg_lambda, num_rounds,
+ max_depth, min_child_weight, gamma, subsample,
+ ),
}
}
@@ -128,11 +144,15 @@ impl PyXGBPipeline {
fn __repr__(&self) -> String {
format!(
- "RustXGBPipeline(threshold={}, scale_pos_weight={}, reg_lambda={}, num_rounds={})",
+ "RustXGBPipeline(threshold={}, scale_pos_weight={}, reg_lambda={}, num_rounds={}, max_depth={:?}, min_child_weight={:?}, gamma={:?}, subsample={:?})",
self.inner.threshold,
self.inner.scale_pos_weight,
self.inner.reg_lambda,
self.inner.num_rounds,
+ self.inner.max_depth,
+ self.inner.min_child_weight,
+ self.inner.gamma,
+ self.inner.subsample,
)
}
}
diff --git a/mwmbl_rank/src/pipeline.rs b/mwmbl_rank/src/pipeline.rs
index 73281af..b99da24 100644
--- a/mwmbl_rank/src/pipeline.rs
+++ b/mwmbl_rank/src/pipeline.rs
@@ -71,6 +71,10 @@ pub struct XGBPipeline {
pub scale_pos_weight: f32,
pub reg_lambda: f32,
pub num_rounds: u32,
+ pub max_depth: Option<u32>,
+ pub min_child_weight: Option<f32>,
+ pub gamma: Option<f32>,
+ pub subsample: Option<f32>,
booster: Option<Booster>,
}
@@ -81,6 +85,33 @@ impl XGBPipeline {
scale_pos_weight,
reg_lambda,
num_rounds,
+ max_depth: None,
+ min_child_weight: None,
+ gamma: None,
+ subsample: None,
+ booster: None,
+ }
+ }
+
+ pub fn with_params(
+ threshold: f32,
+ scale_pos_weight: f32,
+ reg_lambda: f32,
+ num_rounds: u32,
+ max_depth: Option<u32>,
+ min_child_weight: Option<f32>,
+ gamma: Option<f32>,
+ subsample: Option<f32>,
+ ) -> Self {
+ XGBPipeline {
+ threshold,
+ scale_pos_weight,
+ reg_lambda,
+ num_rounds,
+ max_depth,
+ min_child_weight,
+ gamma,
+ subsample,
booster: None,
}
}
@@ -143,10 +174,24 @@ impl XGBPipeline {
// Build tree parameters: lambda (reg_lambda) is f32 in xgb 3.0.5
// Explicitly set tree_method=exact to match XGBoost 2.x behaviour
// (XGBoost 3.0 changed the default from exact→hist for small datasets).
- let tree_params = parameters::tree::TreeBoosterParametersBuilder::default()
+ let mut tree_builder = parameters::tree::TreeBoosterParametersBuilder::default();
+ tree_builder
.lambda(self.reg_lambda)
.scale_pos_weight(self.scale_pos_weight)
- .tree_method(parameters::tree::TreeMethod::Exact)
+ .tree_method(parameters::tree::TreeMethod::Exact);
+ if let Some(v) = self.max_depth {
+ tree_builder.max_depth(v);
+ }
+ if let Some(v) = self.min_child_weight {
+ tree_builder.min_child_weight(v);
+ }
+ if let Some(v) = self.gamma {
+ tree_builder.gamma(v);
+ }
+ if let Some(v) = self.subsample {
+ tree_builder.subsample(v);
+ }
+ let tree_params = tree_builder
.build()
.map_err(|e| format!("Failed to build tree params: {}", e))?;
@@ -227,8 +272,15 @@ impl XGBPipeline {
scale_pos_weight: f32,
reg_lambda: f32,
num_rounds: u32,
+ max_depth: Option<u32>,
+ min_child_weight: Option<f32>,
+ gamma: Option<f32>,
+ subsample: Option<f32>,
) -> Result<Self, String> {
- let mut pipeline = XGBPipeline::new(threshold, scale_pos_weight, reg_lambda, num_rounds);
+ let mut pipeline = XGBPipeline::with_params(
+ threshold, scale_pos_weight, reg_lambda, num_rounds,
+ max_depth, min_child_weight, gamma, subsample,
+ );
pipeline.load_model(path)?;
Ok(pipeline)
}