# Why does tokenizer.apply\_chat\_template() add multiple eos tokens?

**URL:** <https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417>\
**Category:** Intermediate\
**Created:** [September 12, 2024, 6:32am UTC](https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417 "2024-09-12T06:32:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chahnwoo](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Chahnwoo](https://discuss.huggingface.co/u/Chahnwoo)\
**Post date:** [September 12, 2024, 6:32am UTC](https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417/1 "2024-09-12T06:32:25Z")

</div>

The [LLaMA-3.1-8B-Instruct model](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct/blob/main/tokenizer_config.json) uses the chat template defined below:

```auto
  "chat_template": "{% set loop_messages = messages %}{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}{% if loop.index0 == 0 %}{% set content = bos_token + content %}{% endif %}{{ content }}{% endfor %}{% if add_generation_prompt %}{{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}{% endif %}"

```

To the best of my knowledge, the segment below adds an `eos_token` to the end of every conversation turn:

```auto
{% for message in loop_messages %}{% set content = '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'+ message['content'] | trim + '<|eot_id|>' %}

```

This is my first time working with multi-turn conversation data, and I am wondering why an `eos_token` is added to the end of every turn. Wouldn’t training on data like this give the model a mistaken understanding that text can be generated even after the `eos_token`?

Or does this not matter during inference because the LLM programmatically cuts generation once the `eos_token` has been generated?

---

<div class="post-metadata">

**Author:** ![swtb](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@swtb](https://discuss.huggingface.co/u/swtb)\
**Post date:** [September 12, 2024, 2:47pm UTC](https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417/2 "2024-09-12T14:47:45Z")

</div>

The models have been trained with the EOS token positioned in this way to create the illusion of a multi-turn conversation. Therefore, sticking to the prompt template used during the instruction finetune will cause the model to also stick to its expected behaviour.

The EOS token then effectively helps the model to understand that whomever was speaking is now done.

In some examples I have found that omitting the EOS token in my query caused the model to attempt to **complete** my query. Whereas adding the EOS token caused the model to **reply** to my query.

As with all things, experiment an see what happens.

---

<div class="post-metadata">

**Author:** ![Chahnwoo](https://avatars.discourse-cdn.com/v4/letter/c/a88e57/32.png) [@Chahnwoo](https://discuss.huggingface.co/u/Chahnwoo)\
**Post date:** [September 13, 2024, 2:20am UTC](https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417/3 "2024-09-13T02:20:58Z")

</div>

@swtb  
It’s interesting that you noticed this difference:

> [@swtb](#):
>
> In some examples I have found that omitting the EOS token in my query caused the model to attempt to **complete** my query. Whereas adding the EOS token caused the model to **reply** to my query.

I’ve been processing what you’ve said, and your explanation that the `eos_token` of the `Llama-3.1-8B-Instruct` model serves a different purpose from that of the `Llama-3.1-8B` base model does make intuitive sense to me.

As you’ve suggested, it would seem that structuring multi-turn conversations in this way would induce the model to learn that the `eos_token` marks the end of a turn, rather than the end of a model generation.

I’ve begun thinking that this may be why the base model and instruct models have different `eos_token` by default. The base model has the `eos_token` set to:

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/3/0/30edcedf538a66bac2970e54882ebbb677491c93.png)

While the instruct model has the `eos_token` set to:

![image](https://us1.discourse-cdn.com/hellohellohello/original/3X/3/a/3a020eeca2aea3a0cc99ccaafae0b5a8c61ee8a9.png)

Perhaps this was an intentional choice by Meta, so as to avoid **“overwriting”** the information the Llama model had learned about the `<|end_of_text|>` token? By setting a different `eos_token` and ensuring that the `chat_template` made use of `<|eot_id|>`, perhaps they were able to preserve what was previously learned about the `<|end_of_text|>` token while inducing the behavior they desired.

If this interpretation seems off in any way, please let me know!

---

<div class="post-metadata">

**Author:** ![swtb](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@swtb](https://discuss.huggingface.co/u/swtb)\
**Post date:** [September 19, 2024, 12:00pm UTC](https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417/4 "2024-09-19T12:00:27Z")

</div>

@Chahnwoo I think you are on the right lines. If I wanted to train certain behaviour into a model using special tokens and markers I would definitely use a new token rather than reusing and old one.

We for sure want to leverage the base models language understanding for the instruction tuning. Almost as an additive task that relies on the deeper knowledge in the pretraining.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](https://discuss.huggingface.co/u/system)\
**Post date:** [September 20, 2024, 12:07am UTC](https://discuss.huggingface.co/t/why-does-tokenizer-apply-chat-template-add-multiple-eos-tokens/106417/5 "2024-09-20T00:07:27Z")

</div>

Closed on September 20, 2024, 12:07am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
