recall_guard.mia.features
recall_guard.mia.features
MIA feature computation for one (model, prompt, response) record.
Implements the mia.features component from the honest-model-ranking
design. Computes the five MIA features defined by Requirements 4.1, 4.2,
and 4.3:
loss: mean negative logprob of the realised tokens.min_k: mean of the bottom-K clipped logprobs.min_k_pp: mean of the bottom-K per-position z-scores against each token'stop_logprobsdistribution (Min-K%++).zlib_ratio:-sum(clipped_logprobs) / len(zlib.compress(response, 9)).ref_delta:loss_self - loss_ref(Nonewhen no reference run).
Pure function with no I/O and no global state. Numerical stability is enforced
by clipping individual logprobs to a finite floor (LOGPROB_FLOOR)
before any averaging, and by flooring per-position standard deviation at
1e-6 for the Min-K%++ z-score.
LOGPROB_FLOOR
module-attribute
LOGPROB_FLOOR = -30.0
Lower bound for individual logprob values, applied before averaging.
Prevents a single -inf (or extremely negative) per-token logprob from
poisoning loss / min_k / zlib_ratio / ref_delta.
MiaFeatures
dataclass
Five MIA features for one (model, prompt, response) record.
Attributes:
| Name | Type | Description |
|---|---|---|
loss |
float
|
Mean negative logprob of the realised tokens (clipped at floor). Low loss means the model found the text easy to predict, which is what stored text looks like. |
min_k |
float
|
Mean of the bottom |
min_k_pp |
float
|
Mean of the bottom-K per-position z-scores (Min-K%++). Same idea as
|
zlib_ratio |
float
|
|
ref_delta |
float | None
|
|
Source code in recall_guard/mia/features.py
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 | |
compute_mia_features
compute_mia_features(
response, logprobs, ref_logprobs, k=0.2
)
Compute the five MIA features for one record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
str
|
The model's emitted text. Used only for the zlib-ratio denominator. |
required |
logprobs
|
list[TokenLogprob]
|
Per-token logprob entries from |
required |
ref_logprobs
|
list[TokenLogprob] | None
|
Per-token logprobs from a reference model on the same prompt; or
|
required |
k
|
float
|
Fraction of tokens used for the bottom-K slice in Min-K% and Min-K%++. Defaults to 0.2 (the paper's setting). |
0.2
|
Returns:
| Type | Description |
|---|---|
MiaFeatures
|
Frozen dataclass with all five features. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in recall_guard/mia/features.py
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 | |