NLA verbalizer for Qwen3-8B, layer 18
This is the activation verbalizer (AV) of a single-layer natural language autoencoder (NLA) for Qwen/Qwen3-8B. Given the residual stream of Qwen3-8B at one token, taken at layer 18, it writes a natural language explanation of that activation.
- The repository root holds the verbalizer after RL (150 iterations).
sft/holds the verbalizer after supervised training only, before RL.- The reconstructor that it was trained with is not included.
It is not useful as a general-purpose language model.
Input
The input is one vector of size 4096: the output of decoder block 18 of Qwen3-8B at a single token
position (hidden_states[19] in transformers, i.e. model.model.layers[18]). The vector is
rescaled to norm 150 and written over the embedding of one marker token in the verbalizer's prompt.
The prompt template, marker character and scale are in nla_meta.yaml. This is the same input
contract as marco-bazzani/Qwen3-8B-nla; only
the layer differs (18 instead of 24).
Example
import re, torch, yaml
from huggingface_hub import snapshot_download
from transformers import AutoModelForCausalLM, AutoTokenizer
torch.set_grad_enabled(False)
AV = snapshot_download("camila-blank/nla-qwen3-8b-layer18", ignore_patterns=["sft/*"])
LAYER = 18
meta = yaml.safe_load(open(f"{AV}/nla_meta.yaml"))
char, scale = meta["tokens"]["injection_char"], meta["extraction"]["injection_scale"]
template = meta["prompt_templates"]["actor"]
# 1. Read Qwen3-8B's residual stream at one token
tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B")
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-8B", dtype=torch.bfloat16, device_map="cuda")
text, position = "The capital of France is Paris, and the capital of Italy is", -1
ids = tok(text, return_tensors="pt", add_special_tokens=False).input_ids.cuda()
v = base(ids, output_hidden_states=True).hidden_states[LAYER + 1][0, position].float()
del base
v = v / v.norm() * scale
# 2. Put the vector into the verbalizer's prompt and sample
av_tok = AutoTokenizer.from_pretrained(AV)
av = AutoModelForCausalLM.from_pretrained(AV, dtype=torch.bfloat16, device_map="cuda")
prompt = av_tok.apply_chat_template([{"role": "user", "content": template.format(injection_char=char)}],
add_generation_prompt=True, return_tensors="pt")
prompt = (prompt["input_ids"] if hasattr(prompt, "keys") else prompt).cuda()
emb = av.get_input_embeddings()(prompt)
(slot,) = (prompt[0] == meta["tokens"]["injection_token_id"]).nonzero().squeeze(1).tolist()
emb[0, slot] = v.to(emb.dtype)
out = av.generate(inputs_embeds=emb, attention_mask=torch.ones_like(prompt), do_sample=True,
temperature=1.0, top_p=1.0, top_k=0, max_new_tokens=200)
text_out = av_tok.decode(out[0], skip_special_tokens=True)
m = re.search(r"<explanation>\s*(.*?)\s*</explanation>", text_out, re.S)
print(m.group(1) if m else text_out)
Training sampled at temperature 1.0 with no top-k or top-p truncation. Explanations vary between samples, so drawing several per activation gives a better picture. The training activations were taken at least 50 tokens into a document, from text tokenized without a chat template.
Training
- Data: the texts and explanations of ceselder/qwen3-8b-nla-L24-finefineweb-100k (100k FineFineWeb documents, 10 positions each, explanations written by Claude Sonnet 4.6 from the text). The activations were re-extracted at layer 18.
- Verbalizer, supervised stage: initialised from the layer-24 verbalizer
marco-bazzani/Qwen3-8B-nla; LoRA (rank 64,
rsLoRA, all attention and MLP projections), one epoch over 243k pairs at batch 256, learning rate
1e-4 with cosine decay. The adapter is merged into the weights in
sft/. - Reconstructor, supervised stage: the first 19 blocks of Qwen3-8B without the final norm, LoRA rank 64, plus a linear head; one epoch over 243k pairs.
- RL stage: 150 iterations of 1,024 rollouts (256 activations, 4 samples each). The reward is
the negative reconstruction error of the reconstructor, with a per-activation baseline, a small
penalty for an unclosed
<explanation>tag and a KL penalty (0.01) to the supervised verbalizer. The reconstructor is trained on the same rollouts. A fresh rank-64 LoRA is trained on top of the merged supervised verbalizer and merged into the weights at the repository root.
This differs from the NLA paper's recipe in using LoRA rather than a full fine-tune, in the warm start from a layer-24 verbalizer, and in a short RL stage whose hyperparameters were not tuned.
Reconstruction
Fraction of variance explained on 256 held-out activations (documents not seen in training), one sampled explanation per activation, scored by the co-trained reconstructor on norm-rescaled vectors against a predict-the-mean baseline:
| checkpoint | variance explained | well-formed outputs |
|---|---|---|
sft/ (before RL) | 43% | 100% |
| root (after RL) | 63% | 99% |
Higher reconstruction does not by itself mean more useful explanations; compare both checkpoints on your own task.
References
- Paper: Natural Language Autoencoders Produce Unsupervised Explanations of LLM Activations
- Reference training code used for the recipe: ceselder/nanoNLA