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:
1 parent
bba260fb52
commit
75e3470dcc
1 file changed
+9
-6
@@ -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)
|
||||||
else: # no template shipped: Mistral instruct format
|
if hasattr(ids, "keys"): # BatchEncoding in some versions
|
||||||
text = f"<s>[INST]{prompt}[/INST]"
|
ids = ids["input_ids"]
|
||||||
# the template already carries any BOS token; don't add a second one
|
if ids and isinstance(ids[0], list):
|
||||||
return tok(text, return_tensors="pt", add_special_tokens=False).input_ids.to(dev)
|
ids = ids[0]
|
||||||
|
return torch.tensor([ids], device=dev)
|
||||||
|
|
||||||
def generate(prompt, name, dose):
|
def generate(prompt, name, dose):
|
||||||
ids = chat_ids(prompt)
|
ids = chat_ids(prompt)
|
||||||
|
|||||||
Reference in new issue
Block a user