Skip to content

Commit e5d3a2a

Browse files
vcruz305ashhart
authored andcommitted
fix: Flash Next on EXL3 checkpoints stops at the end of the chat turn
The CUDA engine took its stop ids from config.json: the top-level eos_token_id, else text_config's. EXL3 packs of Flash Next (turboderp 3.05bpw_h5_ng5, CYBER-FROST SAGE 3.5 bpw) have no top-level id, and text_config names <|endoftext|> (248044) only; the turn's end, <|im_end|> (248046), is listed in generation_config.json alone. Served chat replies ran past their turn: a thinking-on tool request streamed "<|im_end|>\n<|im_start|>user..." as content, repeated the call three times and leaked <think> into the answer. Config.read now adds generation_config.json's eos_token_id to config.json's, in order and without duplicates. The MLX checkpoint, whose top level already lists both, is unchanged.
1 parent 2fbebf7 commit e5d3a2a

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎src/tensorfold/families/qwen4_exp/cuda/weights.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@
1919
from .qmm import Q4, dequantize, make_q4, stack_q4
2020

2121

22+
def stop_ids(configured: Any, generation: Path) -> tuple[int, ...]:
23+
"""config.json's end-of-reply ids, then generation_config.json's it lacks (EXL3 packs keep <|im_end|> there)."""
24+
25+
def ids(value: Any) -> list[int]:
26+
return [] if value is None else [int(e) for e in value] if isinstance(value, list) else [int(value)]
27+
28+
found = ids(configured)
29+
if generation.exists():
30+
found += ids(json.loads(generation.read_text()).get("eos_token_id"))
31+
out = tuple(dict.fromkeys(found))
32+
if not out:
33+
raise ValueError("no eos_token_id in config.json or generation_config.json")
34+
return out
35+
36+
2237
@dataclass
2338
class Config:
2439
hidden: int
@@ -68,8 +83,7 @@ def read(cls, model_dir: str | Path) -> "Config":
6883
head_dim = int(t.get("head_dim") or t["hidden_size"] // t["num_attention_heads"])
6984
partial = float(rope.get("partial_rotary_factor", t.get("partial_rotary_factor", 0.25)))
7085
teos = t.get("eos_token_id")
71-
eos = raw.get("eos_token_id", teos)
72-
eos = tuple(int(e) for e in eos) if isinstance(eos, list) else (int(eos),)
86+
eos = stop_ids(raw.get("eos_token_id", teos), Path(model_dir) / "generation_config.json")
7387
quant = raw.get("quantization") or raw.get("quantization_config") or {}
7488
return cls(
7589
hidden=int(t["hidden_size"]), layers=int(t["num_hidden_layers"]),

‎tests/test_flashnext_eos.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Flash Next on CUDA stops where the checkpoint says a reply ends, reading generation_config.json too (CPU only)."""
2+
3+
import json
4+
5+
import pytest
6+
7+
pytest.importorskip("torch")
8+
9+
from tensorfold.families.qwen4_exp.cuda.weights import Config, stop_ids # noqa: E402
10+
11+
IM_END, ENDOFTEXT = 248046, 248044
12+
TEXT = {
13+
"hidden_size": 64, "num_hidden_layers": 2, "layer_types": ["linear_attention", "full_attention"],
14+
"vocab_size": 248320, "rms_norm_eps": 1e-6, "num_attention_heads": 4, "num_key_value_heads": 2, "head_dim": 16,
15+
"linear_num_key_heads": 2, "linear_num_value_heads": 4, "linear_key_head_dim": 16, "linear_value_head_dim": 16,
16+
"linear_conv_kernel_dim": 4, "num_experts": 8, "num_experts_per_tok": 2, "moe_intermediate_size": 32,
17+
"shared_expert_intermediate_size": 32,
18+
}
19+
20+
21+
def checkpoint(tmp_path, config, generation=None):
22+
(tmp_path / "config.json").write_text(json.dumps(config))
23+
if generation is not None:
24+
(tmp_path / "generation_config.json").write_text(json.dumps(generation))
25+
return tmp_path
26+
27+
28+
def test_exl3_pack_stops_at_im_end_from_generation_config(tmp_path):
29+
# the EXL3 packs' layout: no top-level eos_token_id, text_config names <|endoftext|> only
30+
d = checkpoint(tmp_path, {"model_type": "qwen4_exp", "text_config": {**TEXT, "eos_token_id": ENDOFTEXT}},
31+
{"eos_token_id": [IM_END, ENDOFTEXT], "bos_token_id": ENDOFTEXT})
32+
c = Config.read(d)
33+
assert c.eos == (ENDOFTEXT, IM_END)
34+
assert c.ple_eos == ENDOFTEXT # the n-gram tables' end id still comes from text_config
35+
36+
37+
def test_top_level_ids_keep_their_order(tmp_path):
38+
# the MLX checkpoint's layout: the top level already lists both
39+
d = checkpoint(tmp_path, {"model_type": "qwen4_exp", "eos_token_id": [IM_END, ENDOFTEXT],
40+
"text_config": {**TEXT, "eos_token_id": ENDOFTEXT}},
41+
{"eos_token_id": [IM_END, ENDOFTEXT]})
42+
assert Config.read(d).eos == (IM_END, ENDOFTEXT)
43+
44+
45+
def test_without_generation_config_the_config_ids_are_used(tmp_path):
46+
d = checkpoint(tmp_path, {"model_type": "qwen4_exp", "text_config": {**TEXT, "eos_token_id": ENDOFTEXT}})
47+
assert Config.read(d).eos == (ENDOFTEXT,)
48+
49+
50+
def test_scalar_and_missing_values(tmp_path):
51+
gen = tmp_path / "generation_config.json"
52+
gen.write_text(json.dumps({"eos_token_id": IM_END}))
53+
assert stop_ids(ENDOFTEXT, gen) == (ENDOFTEXT, IM_END)
54+
gen.write_text(json.dumps({"temperature": 1.0}))
55+
assert stop_ids([ENDOFTEXT], gen) == (ENDOFTEXT,)
56+
with pytest.raises(ValueError, match="eos_token_id"):
57+
stop_ids(None, tmp_path / "absent.json")

0 commit comments

Comments
 (0)