Skip to content

Commit 15741c1

Browse files
fix(distributed-demo): make apply_chat_template token-id coercion transformers 4.x AND 5.x safe
#105's demo assumed apply_chat_template returns token ids (transformers 4.x); on transformers 5.x it returns a string -> verifier.prefill got a str and raised 'str object cannot be interpreted as an integer' (hit on the Mac, whose venv runs transformers 5.x for gemma-4). Pass tokenize=True and coerce str/BatchEncoding/ nested-list to a flat List[int]. Verified on the GPU host (transformers 4.57) and re-validated on the Mac (5.x). Co-authored-by: FluffyAIcode <FluffyAIcode@users.noreply.github.com>
1 parent f4a4b86 commit 15741c1

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

scripts/demo_distributed_spec_decode.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,27 @@ async def _run_verifier_node(args: argparse.Namespace) -> int:
171171
dtype=torch.bfloat16, device="cpu",
172172
sink_size=args.sink, window_size=args.window,
173173
))
174-
prompt_ids = verifier.tokenizer.apply_chat_template(
174+
# Echo-style answers are where the n-gram proposer shines; Qwen3's thinking
175+
# preamble is novel text the lookup cannot draft. Templates without the
176+
# variable ignore it harmlessly.
177+
# transformers 4.x returns token ids from apply_chat_template(tokenize=True);
178+
# 5.x can return a string (or a BatchEncoding) — coerce to a flat List[int]
179+
# so this works across both (the Mac engine needs transformers 5.x).
180+
_templated = verifier.tokenizer.apply_chat_template(
175181
[{"role": "user", "content": args.prompt}],
176-
add_generation_prompt=True,
177-
# Echo-style answers are where the n-gram proposer shines;
178-
# Qwen3's thinking preamble is novel text the lookup cannot
179-
# draft. Templates without the variable ignore it harmlessly.
182+
add_generation_prompt=True, tokenize=True,
180183
enable_thinking=args.enable_thinking,
181184
)
185+
if isinstance(_templated, str):
186+
prompt_ids = list(verifier.tokenizer.encode(_templated))
187+
else:
188+
if hasattr(_templated, "input_ids"):
189+
_templated = _templated.input_ids
190+
if hasattr(_templated, "tolist"):
191+
_templated = _templated.tolist()
192+
if _templated and isinstance(_templated[0], (list, tuple)):
193+
_templated = _templated[0]
194+
prompt_ids = [int(x) for x in _templated]
182195

183196
# --- 4. Greedy baseline (same verifier, local only) --------------
184197
t0 = time.perf_counter()

0 commit comments

Comments
 (0)