Math and Logic Specification¶
This document makes the implemented math and decision logic explicit for
alignment-risk.
Primary references:
- [AIC-2026] The Geometry of Alignment Collapse: When Fine-Tuning Breaks Safety
PDF:https://arxiv.org/pdf/2602.15799 - [ALIGNGUARD-2025] AlignGuard-LoRA: Alignment-Preserving Fine-Tuning via Fisher-Guided Decomposition and Riemannian-Geodesic Collision Regularization
PDF:https://arxiv.org/pdf/2508.02079
For module-to-paper section mapping, see docs/SOURCES.md.
1. Notation¶
- Model parameters (selected trainable subset): \(\theta \in \mathbb{R}^n\).
- Per-sample safety loss: \(\ell_s(\theta; x_i)\).
- Fine-tuning loss: \(\ell_f(\theta; b)\).
- Per-sample safety gradient: \(g_i = \nabla_\theta \ell_s(\theta; x_i)\).
- Gradient matrix: \(G \in \mathbb{R}^{m \times n}\), row \(i\) is \(g_i^\top\).
- Empirical Fisher approximation: \(F \approx \frac{1}{m}G^\top G\).
- Fisher subspace basis: \(B \in \mathbb{R}^{n \times d}\) (columns orthonormal).
- Top Fisher eigenvalues: \(\lambda_1,\dots,\lambda_d \ge 0\).
- Initial update estimate from one fine-tune batch: \(\Delta\theta_0\).
- Number of batches used in curvature averaging: \(N_b\).
- Learning rate: \(\eta\).
- Time/step variable in forecast: \(t\) (derived from step index and
step_size).
2. Parameter Selection Logic¶
The implementation first selects trainable parameters, optionally by explicit allowlist:
fullmode: use all selected trainable parameters.loramode:- if
fisher.parameter_namesis provided, that allowlist is used directly in the caller-provided order; - explicit allowlists are validated strictly: unknown, non-trainable, or duplicate names raise;
- otherwise names are filtered by markers (
lora_,lora_A,lora_B); - if no LoRA match and
require_lora_match=True, raise. - if no LoRA match and
require_lora_match=False, return a skipped report.
3. Vectorization Utilities¶
- Parameters are flattened by concatenation in a fixed order.
- If a gradient/tensor is
None, it is replaced with zeros of matching shape. - Length mismatch between tensor list and reference parameter list raises.
- Batch helpers recursively:
- move tensors to a target device;
- infer batch size from first non-scalar tensor encountered;
- preserve scalar metadata tensors (do not slice them).
4. Fisher Estimation¶
4.1 Per-sample gradient collection¶
Given max_samples = m:
- loop backend:
- iterates over each sample in each batch;
- computes \(\nabla_\theta \ell_s\) via standard backward/autograd.
- vmap backend:
- builds functional model call and vectorized gradient function;
- computes per-sample gradients in chunks (
vmap_chunk_size). - auto backend:
- on first batch, compares loop vs vmap probe gradients (
auto_probe_*); - if close, uses vmap; otherwise warns and uses loop fallback.
All collected gradients are promoted to float32 before CPU-side spectral ops when needed (fp16/bf16 safety).
4.2 Fisher diagonal and module scores¶
With gradient matrix \(G\):
Module score for module \(M\):
where \(J_M\) are flattened parameter indices for that module prefix, and
\(s_M\) corresponds to module_score(M) in code.
Top sensitive weights are the top-k entries of \(\operatorname{diag}(F)\).
4.3 Subspace extraction¶
Config option subspace_method:
svd- \(G = U\Sigma V^\top\)
- eigenvalues of \(\frac{1}{m}G^\top G\): \(\lambda_i = \sigma_i^2/m\)
-
basis columns: first \(d\) columns of \(V\).
-
randomized_svd - draw Gaussian sketch \(\Omega \in \mathbb{R}^{n \times k}\);
- \(Y = G\Omega\), optional power iterations \(Y \leftarrow G(G^\top Y)\);
- QR: \(Y = QR\);
- reduced matrix: \(\widetilde{G} = Q^\top G\);
-
SVD of \(\widetilde{G}\) gives approximate right singular vectors/eigenpairs.
-
diag_topk - choose top-\(d\) diagonal indices from \(\operatorname{diag}(F)\);
- basis is canonical one-hot columns at those indices.
4.4 Rank selection by explained variance¶
Let candidate eigenvalues be \(\lambda_1,\dots,\lambda_r\) after method-specific preselection.
- If
target_explained_variance is None: keepmax_rank. - If target \(\le 0\): keep 1.
- If target \(\ge 1\): keep
max_rank. - If clipped total energy \(\sum_{i=1}^{r}\max(\lambda_i,0)\le 0\): keep
max_rank. - Else choose smallest \(d\) such that:
where \(\tau\) is target_explained_variance.
5. Initial Overlap Risk¶
Given update vector \(\Delta\theta_0\) and subspace basis \(B\):
with \(\varepsilon=10^{-12}\) for numerical safety and clamped to \([0,1]\).
trigger_curvature_check is true when:
where \(\tau_{\mathrm{orth}}\) corresponds to orthogonality_threshold.
6. Curvature Coupling¶
Sample-weighted mean fine-tuning loss over up to max_batches:
where \(\bar{\ell}_f(\theta; b)\) is the mean loss of batch \(b\) and \(n_b\) is that batch's sample count.
Then:
Directional Hessian-vector product is computed as:
via autograd on \(\langle g, \operatorname{stopgrad}(g)\rangle\).
Fisher-weighted overlap magnitudes:
Also returned: \(\|a\|_2\) and \(\|P_M(a)\|_2\).
7. Forecast Model¶
Pipeline scales local terms to step-scale terms:
Stable curvature floor used as effective \(\lambda_{\min}\):
Forecast equations (times = steps * step_size):
Collapse step is the first index where:
where \(L_{\mathrm{collapse}}\) corresponds to collapse_loss_threshold.
8. Adaptive Curvature Refinement Logic¶
If enabled and adaptive_curvature_max_batches > curvature.max_batches:
- If no collapse yet, refine when terminal estimated loss exceeds:
- If collapse exists, refine when collapse index is late in horizon:
Refinement reruns curvature with:
where:
L_collapseiscollapse_loss_threshold.phi_refineisadaptive_curvature_trigger_fraction.s_collapseiscollapse_step.s_maxismax_steps.b_curviscurvature.max_batches.b_adaptisadaptive_curvature_max_batches.
9. Trust-Region Warning Logic¶
A local-validity warning is appended when:
where \(r_{\mathrm{trust}}\) is trust_region_warning_ratio.
10. Skipped LoRA Report (Non-Strict Mode)¶
If mode is lora and no matching trainable LoRA parameters with
require_lora_match=False, the pipeline returns:
- empty subspace;
- zeroed risk/curvature/forecast arrays;
collapse_step=Noneandcollapse_time=None;- warning that analysis was skipped.
11. AlignGuard-Style Mitigation Objective¶
Given current selected LoRA parameter vector \(\theta\) and reference vector \(\theta_{\text{ref}}\):
Decompose by Fisher subspace basis \(B\):
11.1 Alignment penalty¶
11.2 Task-stability penalty¶
Choose diagonal metric \(h\) by h_type:
- identity: \(h_j=1\),
- fisher diagonal: \(h_j = \max(\operatorname{diag}(F)_j, 0)\),
- custom: user-provided nonnegative diagonal.
11.3 Collision penalties¶
Riemannian overlap term:
Geodesic overlap term:
Blended collision penalty:
Total regularized objective:
12. CLI/Demo Logic¶
- CLI exposes
democommand with mode in{full, lora}. - Demo builds a small synthetic model/data setup and runs full pipeline.
- Output artifacts:
- module sensitivity bar chart,
- forecast curve with collapse marker when present.
13. Output Contracts¶
RiskAssessmentReport fields:
subspace: slices, Fisher eigenpairs, diagonal, module scores, top weights.initial_risk: overlap metrics and curvature-trigger flag.curvature: \(\hat{\gamma}\), \(\hat{\epsilon}\), acceleration norms.forecast: arrays for steps, time, drift, quartic term, estimated loss, collapse indices.warning: human-readable decision summary.
14. Scope and Approximation Notes¶
- Uses empirical Fisher from finite sample gradients, not exact Fisher.
- Forecast is a practical lower-bound style approximation with configurable thresholds/horizons.
- Auto backend may deliberately fall back to loop when parity probes fail.
- Mixed precision is handled by explicit promotion/alignment where spectral and projection computations require consistent dtypes.