INJECTION_CHAR = "㈀" # U+3200 — single token in Qwen tokenizer @torch.no_grad()def verbalize(activation: torch.Tensor, model, tokenizer, max_new_tokens=80) -> str: prompt = ( "You are a meticulous AI researcher investigating activation vectors from a language model. " "Describe the semantic content of the vector enclosed in <concept> tags.\n\n" f"<concept>{INJECTION_CHAR}</concept>\n\nPlease provide an explanation." ) enc = tokenizer( tokenizer.apply_chat_template( [{"role": "user", "content": prompt}], tokenize=False, add_generation_prompt=True ), return_tensors="pt", ).to(model.device) embeds = model.get_input_embeddings()(enc["input_ids"]).clone() inj_pos = (enc["input_ids"][0] == tokenizer.encode(INJECTION_CHAR, add_special_tokens=False)[0]).nonzero()[0, 0] embeds[0, inj_pos] = activation.to(embeds.dtype).to(embeds.device) out = model.generate( inputs_embeds=embeds, attention_mask=enc["attention_mask"], max_new_tokens=max_new_tokens, do_sample=False, pad_token_id=tokenizer.eos_token_id, ) return tokenizer.decode(out[0], skip_special_tokens=True)