-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: add thinking_budget (version 2) #6208
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
Open
thyecust
wants to merge
15
commits into
sgl-project:main
Choose a base branch
from
thyecust:thinking_budget2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a16ac99
version 1
thyecust 6c2ecdb
version 2
thyecust 14fe85c
Merge branch 'main' into thinking_budget2
sleepcoo 6aca64c
check none
thyecust 186b1aa
remove thinking_budget in CompletionRequest
thyecust 1c741a6
add enable_thinking check
thyecust 30aa15f
support thinking_budgets=0
thyecust 200c598
fix
1c99b26
Merge branch 'main' into thinking_budget2
zhaochenyang20 8830b06
Merge branch 'main' into thinking_budget2
sleepcoo 6a56bfe
Fix typo
40defe9
Merge branch 'main' into thinking_budget2
thyecust ebb710b
Merge branch 'main' into thinking_budget2
sleepcoo 3819825
Merge branch 'main' into thinking_budget2
thyecust 7a56516
Merge branch 'main' into thinking_budget2
thyecust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
""" | ||
Usage: | ||
python3 -m unittest test_thinking_budget.TestThinkingBudget.test_chat_completion_with_thinking_budget_20 | ||
python3 -m unittest test_thinking_budget.TestThinkingBudget.test_chat_completion_with_thinking_budget_200 | ||
""" | ||
|
||
import unittest | ||
|
||
import requests | ||
from transformers import AutoTokenizer | ||
|
||
from sglang.srt.utils import kill_process_tree | ||
from sglang.test.test_utils import ( | ||
DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, | ||
DEFAULT_URL_FOR_TEST, | ||
CustomTestCase, | ||
popen_launch_server, | ||
) | ||
|
||
|
||
class TestThinkingBudget(CustomTestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.model = "Qwen/Qwen3-8B" | ||
cls.tokenizer = AutoTokenizer.from_pretrained(cls.model) | ||
cls.base_url = DEFAULT_URL_FOR_TEST | ||
cls.api_key = "sk-1234" | ||
cls.process = popen_launch_server( | ||
cls.model, | ||
cls.base_url, | ||
timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH, | ||
api_key=cls.api_key, | ||
other_args=[ | ||
"--reasoning-parser", | ||
"qwen3", | ||
], | ||
) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
kill_process_tree(cls.process.pid) | ||
|
||
def test_chat_completion_with_thinking_budget_20(self): | ||
response = requests.post( | ||
f"{self.base_url}/v1/chat/completions", | ||
headers={"Authorization": f"Bearer {self.api_key}"}, | ||
json={ | ||
"model": self.model, | ||
"messages": [ | ||
{"role": "user", "content": "9.11 and 9.8, which is greater?"} | ||
], | ||
"temperature": 0, | ||
"separate_reasoning": True, | ||
"chat_template_kwargs": {"enable_thinking": True}, | ||
"thinking_budget": 20, | ||
}, | ||
) | ||
self.assertEqual(response.status_code, 200, f"Failed with: {response.text}") | ||
data = response.json() | ||
reasoning_content = data["choices"][0]["message"]["reasoning_content"] | ||
tokens = self.tokenizer.encode(reasoning_content) | ||
self.assertEqual( | ||
len(tokens), | ||
20, | ||
f"Reasoning content length: {len(tokens)} not equal to 20, tokens: {tokens}, reasoning_content: {reasoning_content}", | ||
) | ||
|
||
def test_chat_completion_with_thinking_budget_200(self): | ||
response = requests.post( | ||
f"{self.base_url}/v1/chat/completions", | ||
headers={"Authorization": f"Bearer {self.api_key}"}, | ||
json={ | ||
"model": self.model, | ||
"messages": [ | ||
{"role": "user", "content": "9.11 and 9.8, which is greater?"} | ||
], | ||
"temperature": 0, | ||
"separate_reasoning": True, | ||
"chat_template_kwargs": {"enable_thinking": True}, | ||
"thinking_budget": 200, | ||
}, | ||
) | ||
self.assertEqual(response.status_code, 200, f"Failed with: {response.text}") | ||
data = response.json() | ||
reasoning_content = data["choices"][0]["message"]["reasoning_content"] | ||
tokens = self.tokenizer.encode(reasoning_content) | ||
self.assertEqual( | ||
len(tokens), | ||
200, | ||
f"Reasoning content length {len(tokens)} not equal to 200, tokens: {tokens}, reasoning_content: {reasoning_content}", | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For non-reasoning model, we can skip this check? Do we need to identify if the model is a reasoning model from the architect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am thinking about it. Now I don't know how to decide whether a model is reasoning from a request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can think of, there seems to be no better way. The information about can be obtained here is very limited. It is just doing sampling and does not know the specific model architecture.