SpecForge icon indicating copy to clipboard operation
SpecForge copied to clipboard

[Feature]Add Parser for Qwen3 think model

Open zyksir opened this issue 2 months ago • 2 comments

Motivation

In the previous PR (#182), @jiapingW discovered that for some Qwen3 thinking models, the output was not as expected — an unexpected <think>\n\n</think>\n\n appeared when enable_thinking was set to False.

This PR creates a new Parser for Qwen3 thinking models. For the non-thinking model(e.g Qwen/Qwen3-30B-A3B-Instruct-2507), you can go with the old qwen parser; while for the new thinking models like Qwen/Qwen3-30B-A3B, you can use qwen3-thinking parser.

Modifications

Related Issues

Accuracy Test

The following code is used for test.

from transformers import AutoTokenizer
from specforge.data.preprocessing import preprocess_conversations
from specforge.data.template import TEMPLATE_REGISTRY
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-30B-A3B", trust_remote_code=True)
messages = [[
    {"role": "user", "content": "Who are you?"},
    {"role": "assistant", "content": "I am a model."},
    {"role": "user", "content": "What is your name?"},
    {"role": "assistant", "content": "My name is QwQ."},
    {"role": "user", "content": "What is 1+1"},
    {"role": "assistant", "content": "=2."}
]]
chat_template = TEMPLATE_REGISTRY.get('qwen3-thinking')
max_length = 2048

conversation = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=False,
        enable_thinking=True,
    )

print(conversation)

conversation = preprocess_conversations(tokenizer, messages, chat_template, max_length)
print([tokenizer.decode(conversation['input_ids'][0].view(-1))])

This is the result:

['<|im_start|>user\nWho are you?<|im_end|>\n<|im_start|>assistant\nI am a model.<|im_end|>\n<|im_start|>user\nWhat is your name?<|im_end|>\n<|im_start|>assistant\nMy name is QwQ.<|im_end|>\n<|im_start|>user\nWhat is 1+1<|im_end|>\n<|im_start|>assistant\n\n\n\n\n=2.<|im_end|>\n'] ['<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\nWho are you?<|im_end|>\n<|im_start|>assistant\nI am a model.<|im_end|>\n<|im_start|>user\nWhat is your name?<|im_end|>\n<|im_start|>assistant\nMy name is QwQ.<|im_end|>\n<|im_start|>user\nWhat is 1+1<|im_end|>\n<|im_start|>assistant\n=2.<|im_end|>\n']

Benchmark & Profiling

Checklist

  • [ ] Format your code according to the Code Formatting with Pre-Commit.
  • [ ] Add unit tests as outlined in the Running Unit Tests.
  • [ ] Update documentation / docstrings / example tutorials as needed, according to Writing Documentation.
  • [ ] Provide throughput / latency benchmark results and accuracy evaluation results as needed, according to Benchmark and Profiling and Accuracy Results.
  • [ ] For reviewers: If you haven't made any contributions to this PR and are only assisting with merging the main branch, please remove yourself as a co-author when merging the PR.
  • [ ] Please feel free to join our Slack channel at https://sgl-fru7574.slack.com/archives/C09784E3EN6 to discuss your PR.

zyksir avatar Oct 21 '25 08:10 zyksir

Summary of Changes

Hello @zyksir, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue with Qwen3 thinking models producing unwanted <think> tags in their output when the thinking feature is disabled. It introduces a specialized Qwen3ThinkingParser and a corresponding chat template to intelligently handle these models. The new parser dynamically configures itself based on whether thinking is enabled, and a general parsing improvement ensures that specified ignored tokens are removed from the conversation, resulting in cleaner and more accurate model output.

Highlights

  • New Parser for Qwen3 Thinking Models: Introduced a new Qwen3ThinkingParser class, inheriting from GeneralParser, specifically designed to handle the unique output characteristics of Qwen3 models that incorporate 'thinking' capabilities.
  • Dynamic Token Handling: The Qwen3ThinkingParser dynamically adjusts the assistant_message_separator and sets an ignored_token based on the enable_thinking flag, ensuring correct parsing and clean output whether the thinking feature is active or not.
  • Removal of Unwanted Tokens: A general mechanism was added to the GeneralParser to remove any specified ignored_token strings from the conversation, directly addressing the issue of unexpected <think> tags appearing in the output.
  • New Chat Template Registration: A new chat template named qwen3-thinking has been registered, linking it to the Qwen3ThinkingParser and explicitly defining the <think>\n\n</think>\n\n string as an ignored_token for removal.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Oct 21 '25 08:10 gemini-code-assist[bot]

I think the implementation of adding an ignore_token is concise and reasonable. That's great!

jiapingW avatar Oct 21 '25 08:10 jiapingW