Downloads · 30 days
11
6% of all-time downloads
DaniilBorod/gpt2-small-context-gated-steering
gpt2-small-context-gated-steering is a text generation model from DaniilBorod. Use it when you need the model to write or continue text. It is set up for transformer-lens. The card lists the license as mit.
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-mod…
Downloads · 30 days
11
6% of all-time downloads
All-time downloads
187
Public
Repo size
12.8 KB
Likes
0
Public
Click a slice to open those files.
.json57 KB · 56%
From the Hugging Face model README
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.
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.
| 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.
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
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.
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.
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".
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.
| 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 |
openai-community/gpt2jbloom/GPT2-Small-SAEs-ReformattedThe 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.