Multilayer NLA verbalizer for Qwen3-8B
This is the activation verbalizer (AV) of a multilayer natural language autoencoder (NLA) for Qwen3-8B. Given the residual stream of Qwen3-8B at one token, it writes a natural language explanation of that activation. It is the multilayer recipe, seed 3, after 500 RL iterations. The reconstructor that it was trained with is not included.
Input
The input is Qwen3-8B's residual stream at a single token position, taken at the output of each of blocks 8 to 35
(28 vectors of size 4096). Each vector is rescaled to a fixed norm and written into the embedding of one of 28
marker tokens in the AV's prompt, block 8 first. The prompt template, marker character and scale are in
nla_meta.yaml.
Example
import re, torch, yaml
from huggingface_hub import snapshot_download
from transformers import AutoModelForCausalLM, AutoTokenizer
torch.set_grad_enabled(False)
AV = snapshot_download("marco-bazzani/nla-qwen3-8b-multilayer")
BLOCKS = range(8, 36)
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").input_ids.cuda()
acts = {}
hooks = [base.model.layers[b].register_forward_hook(
lambda m, i, o, b=b: acts.__setitem__(b, (o[0] if isinstance(o, tuple) else o)[0, position].float()))
for b in BLOCKS]
base(ids)
for h in hooks:
h.remove()
del base
v = torch.stack([acts[b] for b in BLOCKS]) # [28, 4096]
v = v / v.norm(dim=-1, keepdim=True) * scale
# 2. Put the vectors into the AV'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 * len(BLOCKS))}],
add_generation_prompt=True, return_tensors="pt").cuda()
emb = av.get_input_embeddings()(prompt)
slots = (prompt[0] == meta["tokens"]["injection_token_id"]).nonzero().squeeze(1)
assert len(slots) == len(BLOCKS)
emb[0, slots] = 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, 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)
We sampled at temperature 1.0 and top-p 1.0. Explanations vary between samples, so drawing several per activation gives a better picture.