From 75e3470dcca7dd378d98c9d0a8fe224d4f4dbd7a Mon Sep 17 00:00:00 2001 From: E Date: Fri, 2 Oct 2026 15:55:53 -0400 Subject: [PATCH] =?UTF-8?q?exp48:=20build=20chat=20ids=20with=20the=20toke?= =?UTF-8?q?nizer=20(tokenize=3DTrue)=20=E2=80=94=20Mistral's=20tokenizer?= =?UTF-8?q?=20has=20no=20jinja=20template=20and=20read=20a=20hand-built=20?= =?UTF-8?q?[INST]=20string=20as=20text=20(invalid=20prompts);=20identical?= =?UTF-8?q?=20ids=20for=20Qwen,=20so=204B/14B=20results=20stand?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- exp48_emotion_binding.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/exp48_emotion_binding.py b/exp48_emotion_binding.py index 1539275..3d5bce1 100644 --- a/exp48_emotion_binding.py +++ b/exp48_emotion_binding.py @@ -339,14 +339,17 @@ def main(): state["v"] = None if name is None or dose == 0 else (dose * vec[name]).to(dev) def chat_ids(prompt): + """Chat-formatted input ids, built by the tokenizer itself (tokenize=True) + so special tokens are real tokens: Mistral's tokenizer ships no jinja + template and reads a hand-built "[INST]" string as plain text.""" msgs = [{"role": "user", "content": prompt}] - if tok.chat_template: - text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True, - enable_thinking=False) - else: # no template shipped: Mistral instruct format - text = f"[INST]{prompt}[/INST]" - # the template already carries any BOS token; don't add a second one - return tok(text, return_tensors="pt", add_special_tokens=False).input_ids.to(dev) + ids = tok.apply_chat_template(msgs, add_generation_prompt=True, tokenize=True, + enable_thinking=False) + if hasattr(ids, "keys"): # BatchEncoding in some versions + ids = ids["input_ids"] + if ids and isinstance(ids[0], list): + ids = ids[0] + return torch.tensor([ids], device=dev) def generate(prompt, name, dose): ids = chat_ids(prompt)