Skip to main content

Overview

Steering vectors provide a non-destructive, reversible alternative to weight projection for suppressing refusal behavior at inference time. Instead of modifying weights permanently, hooks are installed on transformer layers that add or subtract scaled direction vectors from the residual stream during the forward pass.
Advantages over weight projection:
  • Reversible — hooks can be removed between requests
  • Tunable at inference time without re-running the pipeline
  • Composable — multiple vectors can be combined
  • No permanent weight changes
Trade-offs:
  • Slight per-token inference overhead
  • Requires wrapping the forward pass with hooks
  • Effect is per-token, not permanent

SteeringVector


SteeringVectorFactory

Static factory class for constructing SteeringVector instances from various sources.

from_refusal_direction()

Create a steering vector from a pre-computed refusal direction.
torch.Tensor
required
(hidden_dim,) refusal direction vector (will be normalized internally).
int | None
default:"None"
Layer the direction was extracted from (metadata only).
float
default:"-1.0"
Steering strength. alpha=-1.0 steers away from refusal (suppresses it). Use alpha=+1.0 to reinforce refusal.
Returns SteeringVector with label="refusal".

from_contrastive_pairs()

Create a steering vector from contrastive activation pairs via difference-in-means.
list[torch.Tensor]
required
Activations from “positive” concept examples (e.g., harmful prompts that trigger refusal).
list[torch.Tensor]
required
Activations from “negative” concept examples (e.g., harmless prompts).
str
default:"contrastive"
Human-readable label for the vector.
float
default:"1.0"
Default steering strength.
Returns SteeringVector with metadata including n_positive, n_negative, and raw_magnitude.

combine()

Combine multiple steering vectors into one via weighted sum (then re-normalized).
list[SteeringVector]
required
List of SteeringVector to combine.
list[float] | None
default:"None"
Per-vector weights. If None, equal weights are used.
str
default:"combined"
Label for the resulting vector.

SteeringConfig

Configuration passed to SteeringHookManager.install().
list[SteeringVector]
required
One or more steering vectors to apply.
list[int]
required
Layer indices at which to install hooks.
float
default:"1.0"
Global scaling factor applied to all vectors at all layers.
dict[int, float]
default:"{}"
Per-layer alpha overrides. Takes precedence over global alpha for the specified layers.
str
default:"all"
Which token positions to steer. One of:
  • "all" — all positions (broadest effect)
  • "last" — final token only (most targeted)
  • "first" — first token only
bool
default:"True"
Normalize vectors to unit norm before scaling by alpha.

SteeringHookManager

Manages the lifecycle of inference-time steering hooks on a model.

Constructor

No constructor arguments. Creates a new manager with no active hooks.

install()

Install steering hooks on the model. Calls remove() first to clean up any existing hooks.
nn.Module
required
The transformer model.
SteeringConfig
required
Steering configuration specifying vectors, layers, and alphas.
list[nn.Module] | None
default:"None"
Explicit layer module list. If None, auto-detected from common transformer attribute paths (model.layers, transformer.h, etc.).
Returns SteeringResult with: hooks_installed (int), total_steered_layers (int), config.

remove()

Remove all installed hooks and reset the active flag.

is_active

True if hooks are currently installed.

SteeringResult


Code Examples