If a hidden state contains a direction that encodes a concept, you can push the model along that direction and change its behavior without any training. The standard recipe finds the direction from contrast pairs. Run the model on prompts that show the concept and on matching prompts that do not, record the residual stream at some layer, and take the difference of the mean activations. Adding a multiple of that vector to the residual stream at inference time steers generation toward the concept, and subtracting it steers away. The multiplier controls strength: too small does nothing and too large breaks fluency.
Implement steering_vector and apply_steering.
pos_acts and neg_acts are (n, d) and (m, d) activation arrays from contrast prompts. steering_vector(pos_acts, neg_acts, normalize) returns mean(pos) - mean(neg), divided by its L2 norm when normalize=True (and left alone if the norm is zero).apply_steering(resid, v, alpha, positions=None) returns a copy of resid (shape (T, d)) with alpha * v added at the listed token positions, or at all positions when positions is None.resid must not be modified.positions is a list of integer indices.Slice assignment on a copy: out[positions] += alpha * v works for a list of positions without repeats.
Normalizing makes alpha comparable between layers whose activations have different scales.
No results yet
Run your code to check it against the examples, or submit to run every test.