Context-gated steering for GPT-2 Small

This repository contains the inference artifact and reproducible experiment code for context-gated residual-stream steering of GPT-2 Small. The base language model is not fine-tuned, so this is not a full language-model checkpoint or a LoRA adapter. The published artifact consists of four normalized steering directions, the gate configuration, and code that applies the intervention through TransformerLens hooks.

Method

The intervention is applied at blocks.6.hook_resid_pre and changes only the final sequence position:

h'_t = h_t + g_t * alpha * v

Here v is an L2-normalized decoder direction from the gpt2-small-res-jb sparse autoencoder and g_t is determined by the method:

  • classic: g_t = 1 at every generation step;
  • random: g_t ~ Bernoulli(0.8) using an RNG independent of token sampling;
  • gate: use the GPT-2 Tuned Lens at layer 6 to estimate the next-token distribution, sum the probability assigned to alphabetic non-function-word tokens, and steer when this content probability is below 0.7.

The gate therefore applies steering mainly when the next step is estimated to produce a service or other non-content token. The score is computed before the steering vector is added.

Steering directions

Concept SAE feature Experimental role
violence 20026 hyperparameter selection
fear 20211 transfer validation
scientific 4971 transfer validation
happy 203 transfer validation

The directions were extracted from W_dec in jbloom/GPT2-Small-SAEs-Reformatted, blocks.6.hook_resid_pre, and normalized to unit L2 norm. They are stored in steering_vectors.safetensors under the concept names shown above.

Installation

Python 3.10 or newer and a CUDA-capable GPU are recommended.

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

On Windows PowerShell, activate the environment with:

.venv\Scripts\Activate.ps1

Download

hf download BorodinDN67/gpt2-small-context-gated-steering \
  --local-dir gpt2-small-context-gated-steering

Replace the repository ID if the artifact is published under a different name.

Run the complete grid

Prepare a JSON file containing 30 story starts:

[
  "On Saturday morning, Clara opened the small bakery and began arranging fresh bread in the window.",
  "The envelope had been waiting on the desk since dawn."
]

Run one concept:

python run_experiment.py \
  --artifact . \
  --prompts prompts.json \
  --concept fear \
  --output-dir results/fear

The same script can load the artifact directly from the Hub:

python run_experiment.py \
  --artifact BorodinDN67/gpt2-small-context-gated-steering \
  --prompts prompts.json \
  --concept scientific \
  --output-dir results/scientific

The configured experiment uses:

methods: classic, random, gate
prompts: 30
seeds: 42, 43
alpha: 0, 5, 10, 15, 20, 30, 35, 40, 45, 50, 60
content-gate threshold: 0.7
random-gate probability: 0.8
max_new_tokens: 150
temperature: 0.9
top_p: 0.9
top_k: 50

Results are written as one JSONL file per method:

results/<concept>/
β”œβ”€β”€ classic/results.jsonl
β”œβ”€β”€ random/results.jsonl
└── gate/results.jsonl

For paired comparison, the baseline and every steering method reuse the same token sampling seed. Random gating uses a separate deterministic generator.

Minimal Python example

from steering import generate_with_method, load_runtime, set_generation_seed

runtime = load_runtime(".", device="cuda")
set_generation_seed(42)

text, diagnostics = generate_with_method(
    runtime=runtime,
    prompt="When the laboratory door opened,",
    concept="scientific",
    method="gate",
    alpha=30.0,
)

print(text)
print(diagnostics)

gate_seed is required only when method="random".

Evaluation summary

The supplied continuations were scored on independent 0–10 concept-strength and fluency scales with Qwen/Qwen3-8B. The table reports descriptive gate operating points selected by minimum Euclidean distance to the ideal point (10, 10) within the supplied scored records.

Concept Gate alpha Concept score ↑ Fluency score ↑ Trigger rate Samples
violence 30 6.86 5.48 0.726 21
fear 30 8.13 6.05 0.780 40
scientific 30 7.15 6.15 0.741 20
happy 15 5.85 6.35 0.754 20

Complete alpha-level aggregates, Pareto flags, standard deviations, observed run settings, and representative examples are stored in metrics.json.

The scored archives cover subsets of the declared 30-prompt protocol. These metrics describe the available scored records and are not presented as a completed rerun of the full public configuration. In particular, the public runner standardizes the random-gate probability at 0.8 for all concepts.

Files

File Purpose
steering_vectors.safetensors Four normalized 768-dimensional steering vectors
config.json Model, SAE, gate, generation, and evaluation configuration
steering.py Artifact loading, content scorer, hooks, and generation functions
run_experiment.py Reproducible classic/random/gate grid runner
metrics.json Aggregated evaluation results and examples
requirements.txt Reproducible Python dependencies

Limitations

  • The experiments use GPT-2 Small, so generation quality is substantially below that of modern language models.
  • A single SAE feature does not necessarily represent a concept completely or exclusively.
  • The function-word vocabulary is a heuristic constructed from GPT-2 token strings; the gate is not a supervised part-of-speech classifier.
  • LLM-as-a-judge scores may be noisy or biased. Alpha-zero scores can therefore vary across independently evaluated files even when the underlying generation matches.
  • The supplied scored subsets are small, and the reported operating points were not selected on a separate final test set.
  • The method has been tested at one model size, one layer, and one SAE release.

Acknowledgements

License

The code and packaged steering vectors are released under the MIT License. Users must also comply with the licenses and terms of the referenced base model and SAE.

Downloads last month
115
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for DaniilBorod/gpt2-small-context-gated-steering

Finetuned
(2259)
this model