exp48: build chat ids with the tokenizer (tokenize=True) — Mistral's tokenizer has no jinja template and read a hand-built [INST] string as text (invalid prompts); identical ids for Qwen, so 4B/14B results stand

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EandClaude Opus 5.5 committed 2026-10-02 15:55:53 -04:00
1 parent bba260fb52
commit 75e3470dcc
1 file changed
+10 -7
+10 -7
View File
@@ -339,14 +339,17 @@ def main():
state["v"] = None if name is None or dose == 0 else (dose * vec[name]).to(dev) state["v"] = None if name is None or dose == 0 else (dose * vec[name]).to(dev)
def chat_ids(prompt): 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 "<s>[INST]" string as plain text."""
msgs = [{"role": "user", "content": prompt}] msgs = [{"role": "user", "content": prompt}]
if tok.chat_template: ids = tok.apply_chat_template(msgs, add_generation_prompt=True, tokenize=True,
text = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True, enable_thinking=False)
enable_thinking=False) if hasattr(ids, "keys"): # BatchEncoding in some versions
else: # no template shipped: Mistral instruct format ids = ids["input_ids"]
text = f"<s>[INST]{prompt}[/INST]" if ids and isinstance(ids[0], list):
# the template already carries any BOS token; don't add a second one ids = ids[0]
return tok(text, return_tensors="pt", add_special_tokens=False).input_ids.to(dev) return torch.tensor([ids], device=dev)
def generate(prompt, name, dose): def generate(prompt, name, dose):
ids = chat_ids(prompt) ids = chat_ids(prompt)