Skip to content

Conversation

DarkLight1337
Copy link
Member

@DarkLight1337 DarkLight1337 commented Sep 10, 2024

The expected inputs and outputs for mistral tokenizer are different from HF's tokenizers. So, in this PR I have split them into different functions to avoid introducing many union types.

cc @patrickvonplaten since you originally worked on using mistral tokenizer in vLLM

Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can do one of these:

  • Add ready label to the PR
  • Enable auto-merge.

🚀

Comment on lines +383 to +385
elif part_type == "refusal":
text = _RefusalParser(part)["refusal"]
texts.append(text)
Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that _RefusalParser got left out previously, so I'm adding it here.

Comment on lines +440 to +452
def _postprocess_messages(messages: List[ConversationMessage]) -> None:
# per the Transformers docs & maintainers, tool call arguments in
# assistant-role messages with tool_calls need to be dicts not JSON str -
# this is how tool-use chat templates will expect them moving forwards
# so, for messages that have tool_calls, parse the string (which we get
# from openAI format) to dict
for message in messages:
if (message["role"] == "assistant" and "tool_calls" in message
and isinstance(message["tool_calls"], list)):

for item in message["tool_calls"]:
item["function"]["arguments"] = json.loads(
item["function"]["arguments"])
Copy link
Member Author

Choose a reason for hiding this comment

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

I assume that mistral tokenizers will be able to handle tool calls internally since the output conversation will not be used by mistral tokenizer.

Comment on lines +397 to +411
prompt: Union[str, List[int]]
if isinstance(tokenizer, MistralTokenizer):
prompt = apply_mistral_chat_template(
tokenizer,
messages=messages,
chat_template=chat_template,
add_generation_prompt=add_generation_prompt,
)
else:
prompt = apply_hf_chat_template(
tokenizer,
conversation=conversation,
chat_template=chat_template,
add_generation_prompt=add_generation_prompt,
)
Copy link
Member Author

@DarkLight1337 DarkLight1337 Sep 10, 2024

Choose a reason for hiding this comment

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

The main part of this PR. Notice that mistral tokenizer uses messages while HF tokenizer uses conversation. This is cleaner than having different parsing logic inside parse_chat_messages as it avoids the need to handle different types of conversation when generating the output request.

Comment on lines +516 to 521
chat_template: Optional[str],
**kwargs: Any,
) -> List[int]:
return tokenizer.apply_chat_template(
messages=messages,
chat_template=chat_template,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
chat_template: Optional[str],
**kwargs: Any,
) -> List[int]:
return tokenizer.apply_chat_template(
messages=messages,
chat_template=chat_template,
**kwargs: Any,
) -> List[int]:
return tokenizer.apply_chat_template(
messages=messages,

maybe out of scope for this PR, but mistral tokenizers will actually never need a chat_template

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, let's do this in another PR.

Copy link
Collaborator

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the PR @DarkLight1337 - that's indeed much cleaner!
Think we don't need to pass the chat_template to the mistral tokenizer function at all anymore, but also happy to tackle this in another PR

@DarkLight1337 DarkLight1337 enabled auto-merge (squash) September 10, 2024 15:07
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Sep 10, 2024
@DarkLight1337 DarkLight1337 merged commit 8c054b7 into vllm-project:main Sep 10, 2024
69 checks passed
@DarkLight1337 DarkLight1337 deleted the mixtral-tokenization branch September 11, 2024 02:36
dtrifiro pushed a commit to opendatahub-io/vllm that referenced this pull request Sep 12, 2024
Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
garg-amit pushed a commit to garg-amit/vllm that referenced this pull request Oct 28, 2024
LeiWang1999 pushed a commit to LeiWang1999/vllm-bitblas that referenced this pull request Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants