Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Update Mistral chat template #918

Merged
merged 2 commits into from
Jun 21, 2024

Conversation

riedgar-ms
Copy link
Collaborator

It appears that the Mistral chat template has had an update, so we need to match this

@codecov-commenter
Copy link

codecov-commenter commented Jun 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@10fccd3). Learn more about missing BASE report.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #918   +/-   ##
=======================================
  Coverage        ?   51.43%           
=======================================
  Files           ?       64           
  Lines           ?     4705           
  Branches        ?        0           
=======================================
  Hits            ?     2420           
  Misses          ?     2285           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@riedgar-ms riedgar-ms merged commit d5d07dc into main Jun 21, 2024
109 checks passed
@riedgar-ms riedgar-ms deleted the riedgar-ms/chat-template-break-01 branch June 21, 2024 17:07
CHAT_TEMPLATE_CACHE[phi3_template] = Phi3ChatTemplate


# --------------------------------------------------
# @@@@ Mistral-7B-Instruct-v0.2 @@@@
# --------------------------------------------------
# [05/08/24] https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2/blob/main/tokenizer_config.json#L42
mistral_7b_instruct_template = "{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token}}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}"
mistral_7b_instruct_template = "{{ bos_token }}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ ' ' + message['content'] + eos_token}}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my phone so I can't comment properly, but the newspace added in the template needs to also be added as part of the get_role_start or get_role_end methods so that we match the new jinja format

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't see that this was already merged so I just fixed it in #919

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hubris on my part to think that this was a nice, simple fix. I shall give some thought to adding tests to catch such oversights in future

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, it was just a single character change! I was able to add it in straight from my phone :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes absolutely! I'm not even sure all the existing ones are totally correct. When I made these template -> class mappings, I would open up a notebook and check if the guidance classes matched what the Transformers tokenizer's "apply_chat_template" method did on a standard user/assistant "messages" array of dicts. i think formalizing a methodology around that into tests would be incredibly valuable

we could later use that testing infra to start helping us build an auto chat_template loader that we use as a fallback if something isn't in our cache (ie something that tries to convert the chat template into the guidance class format -- if that passes the tests we write with a variety of messages arrays, then we know we're good)

@Harsha-Nori
Copy link
Collaborator

I'd also like to think more about if we should force a direct update to the template cache like this, or if we should e.g. have a separate version for the new mistral template update. I'm not sure how frequently model hosts/deployed models will pull new weights/config files down, so this would break them upon a guidance update, while keeping an extra template cache doesn't really cost us anything

@riedgar-ms
Copy link
Collaborator Author

riedgar-ms commented Jun 21, 2024

I'd also like to think more about if we should force a direct update to the template cache like this, or if we should e.g. have a separate version for the new mistral template update. I'm not sure how frequently model hosts/deployed models will pull new weights/config files down, so this would break them upon a guidance update, while keeping an extra template cache doesn't really cost us anything

I'm not sure how that would work, although learning the details of these chat templates is another fixture on my ToDo list. One immediate question: what if the templates only differed in whitespace (e.g. for this particular one, suppose another update added a second space between the quotes). Since we strip out whitespace when matching keys, we wouldn't be able to detect that.

@Harsha-Nori
Copy link
Collaborator

good q -- I'm really not sure that whitespace stripping the way I did it for cache matching was actually the best decision. jinja ignores some whitespaces but not others, so I figured we'd try to go for maximal coverage at the beginning until figuring out something more clever

@Harsha-Nori
Copy link
Collaborator

(because the alternative was for the "space misaligned" template to default to the chatml format which would be way worse than just matching to something reasonably close...)

Dobiichi-Origami pushed a commit to Dobiichi-Origami/guidance that referenced this pull request Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants