Skip to main content

contrastive_adjustment

Function contrastive_adjustment 

Source
pub fn contrastive_adjustment(
    base: &[i32],
    expert: &[i32],
    alpha_milli: i32,
    top_k: usize,
) -> LogitAdjustment
Expand description

SafeDecoding-style contrastive adjustment (ADR-013): steer toward the safety expert and away from the base — final = base + α·(expert − base) — returned as additive milli-logit penalties consumed after the grammar mask, before sampling (the ADR-005 order is unchanged).

alpha_milli is the steering strength ×1000 (1000 = 1.0×). Only the top_k highest-base-logit tokens are steered (0 = all): SafeDecoding restricts contrast to the head of the distribution so long-tail noise is not amplified. Deterministic and integer (ADR-008).

Safety/robustness invariants: a negative alpha_milli is clamped to 0 — contrastive steering must never push toward the base/unsafe direction — and the delta math is saturating, so an extreme strength can neither overflow i64 nor wrap on the i32 cast. Plus the usual fail-safes: an empty result when base/expert lengths differ (or are empty), and a natural no-op when expert == base (every delta is zero).