recall_guard.harness.scorer
recall_guard.harness.scorer
Public inference-without-recall façade: :class:MemoryGuardedScorer.
This is the stable surface that downstream projects (e.g. Global_Macro_AI_Factors'
macro_framework Track A) consume. It wraps the existing primitives (the NVIDIA
LM client, the per-model control baseline, and the MCS contamination calibrator)
behind two phases:
- :meth:
MemoryGuardedScorer.calibrateruns the model over the control + IS/OOS corpora and trains the per-model calibrator (HTTP-heavy, done once). - :meth:
MemoryGuardedScorer.score/ :meth:score_manyturn one prompt into a :class:GuardedScore: the parsed directional signal, the raw MIA features, the calibratedp_memorized, and the MemGuard-discounted confidence.
The score path reuses the evaluator's parser, the MIA feature computation, the
control-baseline standardisation, and the calibrator's predict_proba verbatim,
so p_memorized is bit-for-bit identical to what the batch harness produces for
the same inputs (Req 3.3). The façade adds no statistics of its own.
One example of consuming this façade: a macro overlay multiplies each
AI-generated Black-Litterman view magnitude by (1 - p_memorized) before
it can move money, which is the same discount memguard_confidence applies
to raw_confidence. A weak or missing score passes the raw exposure
through, and parse failures fall back to the consumer's risk-parity core.
The score is a discount, not a certificate: no model is presumed clean, and
the consumer owns the fallback policy.
Layer note: this module lives in the harness layer (top of the stack), so it may
depend on core, mia, and harness.evaluator. It imports nothing from
harness.plots or portfolio, so re-exporting it from the package root keeps
import recall_guard free of matplotlib/vectorbt (Req 4.1, 4.3).
ConfigurationError
Bases: RuntimeError
Raised when the NIM credential is absent, empty, or rejected (Req 3.7).
Source code in recall_guard/harness/scorer.py
82 83 | |
GuardedScore
dataclass
One guarded inference result.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt_hash |
str
|
First 16 hex chars of |
parse_ok |
bool
|
|
signal |
int | None
|
Parsed direction in |
raw_confidence |
float | None
|
Parsed confidence in |
p_memorized |
float | None
|
Calibrated |
memguard_confidence |
float | None
|
|
features |
MiaFeatures | None
|
The raw :class: |
fail_reason |
str | None
|
One of |
Source code in recall_guard/harness/scorer.py
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 | |
EnsembledScore
dataclass
One prompt scored over many draws.
p_memorized_point is the exposure multiplier. consensus.p_memorized
is the score of one actually-observed draw and is evidence only -- the two
can differ, because the representative draw is selected by rank while the
point estimate is a reduction over all draws. Using the consensus draw's
score to scale exposure would silently substitute a single draw for the
ensemble, which is the thing this feature exists to stop.
sampled_at records when the draws were taken. The sampled distribution
moves between sessions as well as within one, so a consensus has a shelf
life and a cached one is not the same as a fresh one.
p_memorized_point is None exactly when the ensemble failed, and it is
never 0.0 in that case: zero would mean "pass 100% of exposure through",
the opposite of what an unusable measurement should imply.
Source code in recall_guard/harness/scorer.py
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 | |
MemoryGuardedScorer
Calibrated, per-model inference-without-recall scorer.
Construct via :meth:calibrate (which performs the model calls and training),
then call :meth:score / :meth:score_many.
Source code in recall_guard/harness/scorer.py
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
holdout_auc
property
holdout_auc
Held-out IS/OOS separation of the trained calibrator (Req 3.4).
is_weak
property
is_weak
True when holdout_auc is below the calibration gate (Req 3.4).
calibrate
classmethod
calibrate(
*,
api_key,
model,
is_memorized,
oos_control,
reference_model=None,
min_auc=0.6,
min_valid=50,
seed=0,
max_workers=8,
timeout_s=45.0,
min_call_interval_s=0.0,
lm_factory=None,
)
Build the control baseline and train the MCS calibrator for model.
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If |
ValueError
|
If a class has too few usable rows to calibrate / train (Req 3.4). |
Source code in recall_guard/harness/scorer.py
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 | |
score
score(prompt)
Score one prompt into a :class:GuardedScore.
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the NIM endpoint rejects the credential while scoring (Req 3.7). |
Source code in recall_guard/harness/scorer.py
296 297 298 299 300 301 302 303 304 305 306 | |
score_many
score_many(prompts, *, max_workers=8)
Score many prompts (parallel LM calls); preserves input order.
Source code in recall_guard/harness/scorer.py
308 309 310 311 312 313 314 315 316 317 318 319 | |
score_ensemble
score_ensemble(prompt, *, spec, conservative_quantile=None)
Score one prompt over spec.draws draws and reduce the results.
A single scoring is close to uninformative as an exposure multiplier: measured on one identical prompt, its 95% band spans two thirds of the unit interval. Ensembling narrows that, and reports what is left.
Each draw is scored through the unchanged single-draw path and the resulting scores are then reduced. Averaging the intermediate features and scoring once would be a different quantity -- the calibrator is a sigmoid, so the score of the mean is not the mean of the scores.
The point estimate is the mean, because attenuation is linear in the score and the mean is therefore unbiased for expected attenuation. No symmetric trimming is applied: the upper tail of this distribution is the contamination evidence the score exists to report, so trimming it away would discard the signal and shift the estimate toward the risk-increasing side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
EnsembleSpec
|
Explicit configuration. There is no default instance. |
required |
conservative_quantile
|
float | None
|
Optional upper quantile of the score, for a caller who would rather withhold more exposure than risk withholding too little. |
None
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the endpoint rejects the credential while drawing. |
Source code in recall_guard/harness/scorer.py
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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |