recall_guard.mia.mcs
recall_guard.mia.mcs
Per-model MCS (Memorization Contamination Score) logistic-regression calibrator.
Implements the mia.mcs component from the honest-model-ranking design.
Satisfies Requirements 5.1, 5.2, 5.3, 5.4:
MCSCalibrator: frozen dataclass holding the trainedLogisticRegressionestimator, the canonicalfeature_orderused during training (sopredict_probacannot accidentally feed the classifier a permuted vector), the held-out AUC, and anis_weakflag set whenholdout_auc < min_auc.train(model_lm, is_memorized, oos_control, baseline, ref_lm, min_auc=0.6, seed=0): runs the model (and optional reference) on every row, computes MIA features, standardises them against the per-model control baseline, splits a held-out portion viasklearn.model_selection.train_test_split(test_size=0.25,stratify=y,random_state=seed), fitsLogisticRegression(class_weight="balanced", solver="liblinear", random_state=seed)on the training half, scoresroc_auc_scoreon the holdout half.MCSCalibrator.predict_proba(features, baseline) -> float, pure: standardisesfeaturesagainstbaseline, builds the classifier input vector infeature_order, and returns thepredict_proba(...)[:, 1]value.
Per-row LM failures (TimeoutError, RuntimeError,
ValueError) are skipped with a single WARNING per skip; every
other code path is pure. The MemGuard penalty rule consumed downstream
is penalized_confidence = raw_confidence * (1 - p_memorized)
(Req 5.4: continuous, not threshold-based).
MCSCalibrator
dataclass
Per-model logistic-regression calibrator for p(memorized | features).
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
The NVIDIA model ID this calibrator was trained for. |
classifier |
LogisticRegression
|
The fitted |
feature_order |
list[str]
|
Canonical order used to flatten the standardised feature dict
into the classifier's input vector. Populated at train time and
consumed verbatim by :meth: |
holdout_auc |
float
|
ROC-AUC score of the trained classifier on the 25% held-out portion of the labelled IS/OOS corpus. Reported in the manifest and the per-model evaluation result (Req 5.2). |
is_weak |
bool
|
|
Source code in recall_guard/mia/mcs.py
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 | |
predict_proba
predict_proba(features, baseline)
Return the calibrated probability of "memorized" for one record.
Standardises features against the model's baseline and
feeds the resulting vector to the trained classifier in
self.feature_order.
Returns:
| Type | Description |
|---|---|
float
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of the four core features standardises to |
Source code in recall_guard/mia/mcs.py
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 | |
train
train(
model_lm,
is_memorized,
oos_control,
baseline,
ref_lm,
min_auc=0.6,
seed=0,
max_workers=1,
)
Train the MCS classifier for one model.
Drives the LM over both labelled corpora (in parallel when
max_workers > 1), fits a logistic regression on the standardised
features, and reports a held-out AUC. Raises ValueError if either
class ends up empty after per-row skips.
Source code in recall_guard/mia/mcs.py
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 372 373 374 375 376 377 | |