TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Antisource/heronaisec/llms.txt
Use this file to discover all available pages before exploring further.
evaluation module provides all inference and metric computation needed to quantify how much backdoor behaviour a model retains after an intervention. It contains no training logic, attack generation, or visualization code. The central primitive is predict(), which runs batched inference under @torch.inference_mode() and returns both hard predictions and soft probabilities. Four scalar metrics are derived from those outputs — clean accuracy, attack success rate, trigger confidence, and retention ratio — and evaluate_model() computes all four in a single call, returning a structured dictionary suitable for direct storage in a results dataframe.
Functions
predict()
Run batched inference on a dataset and return predicted labels and class probabilities. Decorated with@torch.inference_mode() for zero-overhead gradient tracking. Sets the model to model.eval() before iterating over batches. Each batch is moved to the device that the model parameters currently reside on, so CPU and CUDA models are handled transparently. Batches are assembled with DataCollatorWithPadding to avoid fixed-length padding across the entire dataset.
The model to run inference with. Can be on any device; input tensors are moved to match.
A PyTorch-formatted
Dataset (from prepare_for_training()) containing "input_ids", "attention_mask", and "label" columns.The tokenizer used to construct the
DataCollatorWithPadding. Must match the tokenizer used to tokenize dataset.Number of examples per inference batch. Defaults to
32. Reduce for GPU memory constraints.1-D integer array of shape
(N,) containing the argmax predicted class label for each example.2-D float array of shape
(N, num_labels) containing the softmax probability distribution over classes for each example.The
@torch.inference_mode() decorator is more aggressive than torch.no_grad() — it disables both gradient computation and version tracking. Do not wrap calls to predict() inside a gradient-enabled context expecting gradients to flow through it.compute_clean_accuracy()
Compute classification accuracy on clean (uncontaminated) examples. A thin wrapper aroundsklearn.metrics.accuracy_score. The function does not filter by label — it compares every prediction to its corresponding ground-truth label.
1-D integer array of predicted class labels, typically the
predictions output of predict().1-D integer array of ground-truth labels of the same length.
Accuracy in
[0.0, 1.0].compute_attack_success_rate()
Compute the fraction of triggered examples classified as the attacker’s target label. Attack Success Rate (ASR) is the primary measure of backdoor effectiveness. A value of1.0 means every triggered input was classified as target_label; a value of 0.0 means the backdoor has been fully eradicated.
1-D integer array of predicted labels on a triggered (poisoned) evaluation set.
The label the attacker intended the model to predict on triggered inputs.
ASR in
[0.0, 1.0], computed as np.mean(predictions == target_label).compute_trigger_confidence()
Compute the mean softmax probability assigned to the target class on triggered inputs. A complementary metric to ASR that captures the strength of the backdoor signal rather than merely its binary presence. Even when ASR drops (e.g. the model no longer predicts the target label), trigger confidence may remain elevated, indicating residual backdoor behaviour in the model’s internal representations.2-D float array of shape
(N, num_labels), typically the probabilities output of predict() run on a triggered dataset.The attacker’s target label index used to slice the probability column.
Mean probability assigned to
target_label across all triggered examples, in [0.0, 1.0].compute_retention_ratio()
Compute the normalised backdoor persistence relative to a pre-intervention baseline. The Retention Ratio is defined asASR(t) / ASR(0), where ASR(t) is the attack success rate after intervention t and ASR(0) is the baseline ASR before any intervention. A value of 1.0 means the backdoor is fully retained; a value of 0.0 means it has been completely removed. The function returns 0.0 when baseline_asr == 0 to avoid a zero-division error.
ASR after the intervention, from
compute_attack_success_rate().ASR before any intervention (at
ratio=0.0). Used as the denominator.Retention ratio
ASR(t) / ASR(0), or 0.0 if baseline_asr == 0.evaluate_model()
Evaluate a model on clean and triggered datasets and return all four persistence metrics. Runspredict() twice — once on clean_dataset and once on triggered_dataset — then computes and returns all four metrics in a single dict. This is the primary evaluation entry-point for experiment loops.
The model to evaluate. Should be in its final state (post-intervention or post-training).
A PyTorch-formatted
Dataset of clean (uncontaminated) validation examples.A PyTorch-formatted
Dataset where every example has the trigger token injected. Typically produced by apply_attack() on the validation split.The tokenizer used to build the
DataCollatorWithPadding inside predict().The attacker’s target label. Used to compute ASR, trigger confidence, and retention ratio.
The ASR of the original poisoned model before any intervention. Passed directly to
compute_retention_ratio().Inference batch size. Defaults to
32.A dictionary with the following keys:
| Key | Description |
|---|---|
"clean_accuracy" | Accuracy on clean validation examples |
"attack_success_rate" | Fraction of triggered examples predicted as target_label |
"mean_trigger_confidence" | Mean softmax probability for target_label on triggered inputs |
"retention_ratio" | attack_success_rate / baseline_asr |