generation_config.json stops on <EOS_TOKEN> (id 3), but chat turns end with <|END_OF_TURN_TOKEN|> (id 6)

#15
by Oakshat - opened

Hi, thanks for releasing Tiny Aya. I found a small mismatch in the stop token settings that wastes generation time.

What I see

generation_config.json and config.json set eos_token_id to 3 (<EOS_TOKEN>), but the tokenizer's EOS token is <|END_OF_TURN_TOKEN|> (id 6), which is what ends an assistant turn in the chat template.

from transformers import AutoTokenizer, GenerationConfig
m = "CohereLabs/tiny-aya-global"
print(GenerationConfig.from_pretrained(m).eos_token_id)  # 3
print(AutoTokenizer.from_pretrained(m).eos_token_id)     # 6

Checked on revision af89d219.

Effect

With model.generate(...) and the default generation config, generation only stops on token 3. In my runs with greedy decoding, the model finished its answer with token 6 and then kept emitting token 6 until max_new_tokens. The extra tokens are special tokens, so they disappear when decoding with skip_special_tokens=True and the text looks normal. The cost is hidden: every answer uses the full token budget, and a library that only returns decoded text cannot tell that the answer finished early.

After passing both ids as stop tokens, one batch of four answers went from about 30 s to about 7 s on my machine (768 new tokens allowed).

Workaround

model.generate(**inputs, eos_token_id=[3, 6], ...)

Suggested fix

Set "eos_token_id": [3, 6] in generation_config.json, so chat generation stops at the end of the turn by default. It may be worth checking the other Tiny Aya variants for the same setting.

Cohere Labs org

Hey @Oakshat Thanks for the detailed report!

We couldn't reproduce the runaway generation. With transformers 5.23, using apply_chat_template(..., add_generation_prompt=True) and the default generation config, the model ends every turn with <|END_RESPONSE|><EOS_TOKEN> and stops on id 3, both greedy and sampled, single and batched. Could you share your full script, your transformers/torch versions, and the raw generated ids from one affected answer? Thanks!

Thanks @alexrs , you are right, and sorry for the noise. I reran it today and the base model stops correctly. My first report mixed up the base model with my own fine-tune.

Setup: transformers 5.19.0, torch 2.14.1, MPS, bf16, revision af89d219, apply_chat_template(..., add_generation_prompt=True), greedy, default generation config, max_new_tokens=200.

  • Base model, "What is the capital of France?": The capital of France is Paris.<|END_RESPONSE|><EOS_TOKEN>, last ids [..., 261001, 3], stops.
  • My LoRA fine-tune of tiny-aya-global (SFT on chat data), same prompt: ends with <|END_RESPONSE|><|END_OF_TURN_TOKEN|>, then repeats id 6 until max_new_tokens (last ids [6, 6, 6, 6, 6, 6]).

I think the cause is the chat template. A finished assistant turn renders as <|START_RESPONSE|>Hello.<|END_RESPONSE|><|END_OF_TURN_TOKEN|>, so SFT on chat-template data teaches the model to end with id 6. The base model ends with id 3, and generation_config.json stops only on 3. tokenizer.eos_token is also id 6.

So this affects fine-tunes, not the released model. If it seems worth it, adding 6 to eos_token_id in generation_config.json would make such fine-tunes stop by default. Otherwise feel free to close this.

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
m = "CohereLabs/tiny-aya-global"  # or the fine-tuned checkpoint
tok = AutoTokenizer.from_pretrained(m, revision="af89d219")
model = AutoModelForCausalLM.from_pretrained(m, revision="af89d219", dtype=torch.bfloat16).to("mps")
x = tok.apply_chat_template([{"role": "user", "content": "What is the capital of France?"}],
                            add_generation_prompt=True, return_tensors="pt", return_dict=True).to("mps")
out = model.generate(**x, do_sample=False, max_new_tokens=200)[0, x["input_ids"].shape[1]:].tolist()
print(len(out), out[-6:])
print(tok.decode(out, skip_special_tokens=False))
Cohere Labs org

Thanks for following up and for tracking this down. We reproduced it: a LoRA fine-tune of tiny-aya-global on chat-template data learns to end turns with <|END_OF_TURN_TOKEN|> (id 6), and with the default generation config it never stops.

With eos_token_id: [3, 6] it stops right after the answer, and the released model's output is unchanged. We have added id 6 to generation_config.json for Global, Earth, Fire and Water. Adapters loaded on the base will pick it up automatically. Merged fine-tunes keep their own config, so set "eos_token_id": [3, 6] in theirs, or pass it to generate().

Sign up or log in to comment