API Reference: Utils¶
Device detection¶
auditml.utils.device
¶
Device detection and management for AuditML.
get_device(preference: str = 'auto') -> torch.device
¶
Return the best available torch.device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preference
|
str
|
|
'auto'
|
Returns:
| Type | Description |
|---|---|
device
|
|
Source code in src/auditml/utils/device.py
device_info() -> dict[str, str | bool]
¶
Return a dict of device/hardware information.
Source code in src/auditml/utils/device.py
Rust acceleration¶
auditml.utils.rust_accel
¶
Rust acceleration helpers with transparent NumPy fallback.
AuditML ships an optional Rust extension (auditml_rust) that provides
10-15x speedups for two compute-intensive operations:
find_best_threshold— scans all possible thresholds to find the one that maximises binary classification accuracy (used by ThresholdMIA).compute_ssim/batch_ssim— Structural Similarity Index between pixel arrays (used by ModelInversion to measure reconstruction quality).
If the Rust extension is not available (e.g. fresh clone without building), this module falls back to equivalent NumPy implementations automatically. No code outside this file needs to change.
Build the Rust extension
cd rust/
maturin build --release --out ../dist
pip install ../dist/auditml_rust-*.whl
Or for development (rebuilds on every change): maturin develop --release # must run from rust/ directory
rust_status() -> str
¶
Return a human-readable string indicating whether Rust acceleration is active.
Source code in src/auditml/utils/rust_accel.py
find_best_threshold(scores: np.ndarray, labels: np.ndarray) -> tuple[float, float]
¶
Find the threshold on scores that maximises binary accuracy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
ndarray
|
1-D float array of per-sample confidence scores. |
required |
labels
|
ndarray
|
1-D int array of ground-truth binary labels (1=member, 0=non-member). |
required |
Returns:
| Type | Description |
|---|---|
(best_threshold, best_accuracy)
|
|
Source code in src/auditml/utils/rust_accel.py
compute_ssim(img_a: np.ndarray, img_b: np.ndarray) -> float
¶
Compute SSIM between two images (flat float arrays, values in [0,1]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_a
|
ndarray
|
Reference image, flattened to 1-D, pixel values in [0, 1]. |
required |
img_b
|
ndarray
|
Reconstructed image, same shape as img_a. |
required |
Returns:
| Type | Description |
|---|---|
float
|
SSIM in [-1, 1]. 1.0 = identical. |
Source code in src/auditml/utils/rust_accel.py
batch_ssim(originals: np.ndarray | list[np.ndarray], reconstructed: np.ndarray | list[np.ndarray]) -> list[float]
¶
Compute SSIM for a batch of (reference, reconstructed) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
originals
|
ndarray | list[ndarray]
|
List of reference images (each a flat float array). |
required |
reconstructed
|
ndarray | list[ndarray]
|
List of reconstructed images (same length as originals). |
required |
Returns:
| Type | Description |
|---|---|
list[float]
|
SSIM score per pair. |
Source code in src/auditml/utils/rust_accel.py
Reproducibility¶
auditml.utils.reproducibility
¶
Reproducibility utilities for AuditML.
Call set_seed before any training or data splitting to ensure
deterministic, reproducible results across runs.
set_seed(seed: int = 42) -> None
¶
Set random seeds for full reproducibility.
Configures Python, NumPy, and PyTorch (CPU + CUDA) random number generators and enables deterministic cuDNN behaviour.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Integer seed value. |
42
|
Source code in src/auditml/utils/reproducibility.py
Experiment utilities¶
auditml.utils.experiment
¶
Experiment tracking for AuditML.
ExperimentLogger manages the output directory for a single experiment
run, saving configs, metrics, and system information in a structured
layout::
results/<experiment_name>/
├── config.yaml
├── system_info.json
├── metrics.csv
└── logs/
└── <experiment_name>.log
ExperimentLogger
¶
Manages artefacts for a single experiment run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_name
|
str
|
Human-readable experiment identifier. |
'audit'
|
output_dir
|
str | Path
|
Root directory under which the experiment folder is created. |
'./results'
|
Source code in src/auditml/utils/experiment.py
34 35 36 37 38 39 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 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 129 130 131 | |
log_config(config: dict[str, Any]) -> None
¶
Write the experiment config to config.yaml.
Source code in src/auditml/utils/experiment.py
log_system_info() -> dict[str, Any]
¶
Capture and save system/hardware information.
Source code in src/auditml/utils/experiment.py
log_metrics(metrics: dict[str, Any], step: int | None = None) -> None
¶
Record a metrics snapshot.
Logged to both the Python logger and an in-memory list that can
be flushed to CSV with save_metrics.
Source code in src/auditml/utils/experiment.py
save_metrics() -> Path
¶
Flush accumulated metrics to metrics.csv.
Source code in src/auditml/utils/experiment.py
log_model_summary(model: Any) -> None
¶
Log architecture and parameter counts.