-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[None][fix] Fix GPT-OSS router token identity #16760
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
Merged
SimengLiu-nv
merged 3 commits into
NVIDIA:main
from
SimengLiu-nv:fix-gptoss-harmony-consistency
Jul 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import TYPE_CHECKING, Callable, Optional, cast | ||
|
|
||
| from transformers import PretrainedConfig | ||
|
|
||
| from tensorrt_llm.serve.openai_protocol import ChatCompletionRequest | ||
|
|
||
| if TYPE_CHECKING: | ||
| from tensorrt_llm.serve.harmony_adapter import HarmonyAdapter | ||
|
|
||
| ToolDict = dict[str, object] | ||
|
|
||
|
|
||
| def resolve_model_type_from_config(model_name_or_path: str) -> Optional[str]: | ||
| """Return the checkpoint's declared model type from its config metadata.""" | ||
| config_dict, _ = PretrainedConfig.get_config_dict(model_name_or_path) | ||
| model_type = config_dict.get("model_type") | ||
| return model_type if isinstance(model_type, str) else None | ||
|
|
||
|
|
||
| def uses_harmony_tokenization( | ||
| use_harmony: Optional[bool] = None, | ||
| model_type: Optional[str] = None, | ||
| model_type_resolver: Optional[Callable[[], Optional[str]]] = None, | ||
| ) -> bool: | ||
| if os.getenv("DISABLE_HARMONY_ADAPTER", "0") == "1": | ||
| return False | ||
| if use_harmony is not None: | ||
| return use_harmony | ||
| if model_type is None and model_type_resolver is not None: | ||
| model_type = model_type_resolver() | ||
| return model_type == "gpt_oss" | ||
|
|
||
|
|
||
| def get_chat_completion_tool_dicts( | ||
| request: ChatCompletionRequest, empty_as_none: bool = False | ||
| ) -> Optional[list[ToolDict]]: | ||
| if request.tools is None or (empty_as_none and not request.tools): | ||
| return None | ||
| tools: list[ToolDict] = [] | ||
| for tool in request.tools: | ||
| if hasattr(tool, "model_dump"): | ||
| tools.append(cast(ToolDict, tool.model_dump())) | ||
| elif isinstance(tool, dict): | ||
| tools.append(cast(ToolDict, tool)) | ||
| else: | ||
| raise TypeError(f"Unsupported tool type: {type(tool).__name__}") | ||
| return tools | ||
|
|
||
|
|
||
| def tokenize_harmony_chat_request( | ||
| request: ChatCompletionRequest, | ||
| harmony_adapter: Optional["HarmonyAdapter"] = None, | ||
| set_prompt_token_ids: bool = False, | ||
| ) -> list[int]: | ||
| if request.prompt_token_ids is not None: | ||
| return request.prompt_token_ids | ||
|
|
||
| from tensorrt_llm.serve import harmony_adapter as harmony_adapter_module | ||
|
|
||
| adapter = harmony_adapter or harmony_adapter_module.get_harmony_adapter() | ||
| result = adapter.openai_to_harmony_tokens( | ||
| request.messages, | ||
| get_chat_completion_tool_dicts(request, empty_as_none=True), | ||
| reasoning_effort=harmony_adapter_module.maybe_transform_reasoning_effort( | ||
| request.reasoning_effort | ||
| ), | ||
| tool_choice=request.tool_choice, | ||
| ) | ||
| if set_prompt_token_ids: | ||
| request.prompt_token_ids = result | ||
| return result | ||
|
|
||
|
|
||
| def render_chat_request_for_tokenizer( | ||
| request: ChatCompletionRequest, tokenizer: object | ||
| ) -> str | list[int]: | ||
| chat_template_kwargs = ( | ||
| dict(request.chat_template_kwargs) if getattr(request, "chat_template_kwargs", None) else {} | ||
| ) | ||
| chat_template_kwargs["tools"] = get_chat_completion_tool_dicts(request) | ||
| chat_template_kwargs["documents"] = request.documents | ||
| if request.chat_template is not None: | ||
| chat_template_kwargs["chat_template"] = request.chat_template | ||
| rendered = tokenizer.apply_chat_template( | ||
| [msg if isinstance(msg, dict) else dict(msg) for msg in request.messages], | ||
| add_generation_prompt=request.add_generation_prompt, | ||
| tokenize=False, | ||
| return_dict=False, | ||
| **chat_template_kwargs, | ||
| ) | ||
| if isinstance(rendered, str): | ||
| return rendered | ||
| return list(rendered) | ||
|
|
||
|
|
||
| def tokenize_chat_request_for_serving( | ||
| request: ChatCompletionRequest, | ||
| tokenizer_factory: Callable[[], object], | ||
| encode_rendered: Callable[[str, object], list[int]], | ||
| use_harmony: Optional[bool] = None, | ||
| model_type: Optional[str] = None, | ||
| model_type_resolver: Optional[Callable[[], Optional[str]]] = None, | ||
| harmony_adapter: Optional["HarmonyAdapter"] = None, | ||
| set_prompt_token_ids: bool = True, | ||
| ) -> list[int]: | ||
| if request.prompt_token_ids is not None: | ||
| return request.prompt_token_ids | ||
|
|
||
| if uses_harmony_tokenization( | ||
| use_harmony=use_harmony, | ||
| model_type=model_type, | ||
| model_type_resolver=model_type_resolver, | ||
| ): | ||
| return tokenize_harmony_chat_request( | ||
| request, | ||
| harmony_adapter=harmony_adapter, | ||
| set_prompt_token_ids=set_prompt_token_ids, | ||
| ) | ||
|
|
||
| tokenizer = tokenizer_factory() | ||
| rendered = render_chat_request_for_tokenizer(request, tokenizer) | ||
| result = encode_rendered(rendered, tokenizer) if isinstance(rendered, str) else rendered | ||
| if set_prompt_token_ids: | ||
| request.prompt_token_ids = result | ||
| return result |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.