Skip to content

Conversation

@chenyushuo
Copy link
Collaborator

Description

As the title says.

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has passed all tests
  • Docstrings have been added/updated in Google Style
  • Documentation has been updated
  • Code is ready for review

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @chenyushuo, I'm Gemini Code Assist1! 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 significantly refactors the inference model architecture by introducing a new BaseInferenceModel class. This change aims to improve code organization and reduce redundancy by abstracting common functionalities such as chat template application, prompt truncation, and message-to-experience conversion into a shared base class. Concrete model implementations like TinkerModel and vLLMRolloutModel now inherit from this base class, simplifying their structure and making them more maintainable by delegating these common tasks.

Highlights

  • Introduction of BaseInferenceModel: A new BaseInferenceModel class has been introduced to centralize common logic shared across different inference model implementations, promoting code reuse and maintainability.
  • Centralized Chat Template Application: The logic for applying chat templates, previously duplicated in concrete model classes, has been moved into a new apply_chat_template method within BaseInferenceModel.
  • Consolidated Prompt Truncation: Prompt truncation logic is now encapsulated in the _handle_prompt_truncation method of BaseInferenceModel, reducing redundancy and simplifying prompt handling.
  • Unified Message-to-Experience Conversion: The convert_messages_to_experience method has been relocated to BaseInferenceModel, ensuring consistent conversion of messages into experience objects across all derived inference models.
  • Simplified Derived Models: TinkerModel and vLLMRolloutModel now inherit from BaseInferenceModel, allowing them to leverage the shared functionalities and significantly reducing their individual codebases by removing duplicated logic.
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 👍 and 👎 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.

Footnotes

  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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a solid refactoring by creating a BaseInferenceModel to abstract away common logic from TinkerModel and vLLMRolloutModel. This change significantly reduces code duplication and enhances maintainability. The implementation is well-executed. I have a couple of minor suggestions to further improve the readability of the new base class.

Comment on lines +145 to +157
token_ids = token_ids[: self.config.max_prompt_tokens + 1] # leave one for response
return [
Experience(
tokens=token_ids,
logprobs=torch.zeros(1, dtype=torch.float32),
prompt_length=len(token_ids) - 1,
prompt_text=self.tokenizer.decode(token_ids[:-1]),
response_text=self.tokenizer.decode(token_ids[-1]),
truncate_status="prompt_truncated",
reward=0.0,
)
for _ in range(kwargs.get("n", 1))
], False
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic for handling prompt truncation and creating a dummy Experience object is a bit dense. Using an explicit variable for the prompt token count would make the code more self-documenting and easier to understand at a glance.

Suggested change
token_ids = token_ids[: self.config.max_prompt_tokens + 1] # leave one for response
return [
Experience(
tokens=token_ids,
logprobs=torch.zeros(1, dtype=torch.float32),
prompt_length=len(token_ids) - 1,
prompt_text=self.tokenizer.decode(token_ids[:-1]),
response_text=self.tokenizer.decode(token_ids[-1]),
truncate_status="prompt_truncated",
reward=0.0,
)
for _ in range(kwargs.get("n", 1))
], False
prompt_token_count = self.config.max_prompt_tokens
token_ids = token_ids[:prompt_token_count + 1]
return [
Experience(
tokens=token_ids,
logprobs=torch.zeros(1, dtype=torch.float32),
prompt_length=prompt_token_count,
prompt_text=self.tokenizer.decode(token_ids[:-1]),
response_text=self.tokenizer.decode(token_ids[-1]),
truncate_status="prompt_truncated",
reward=0.0,
)
for _ in range(kwargs.get("n", 1))
], False

Comment on lines +182 to +189
if self.config.max_model_len is not None and self.config.max_model_len > 0:
if len(token_ids) > self.config.max_model_len - 1:
truncate_status = "response_truncated"
self.logger.warning(
f"Warning: {len(token_ids)=} exceeds the length limit {(self.config.max_model_len - 1)=}"
)
token_ids = token_ids[: self.config.max_model_len - 1]
action_mask = action_mask[: self.config.max_model_len - 1]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The use of max_model_len - 1 is a bit of a magic number. Introducing a local variable for the maximum length and adding a comment to explain the -1 would improve code clarity and maintainability, making it easier for future developers to understand the rationale behind this truncation logic.

Suggested change
if self.config.max_model_len is not None and self.config.max_model_len > 0:
if len(token_ids) > self.config.max_model_len - 1:
truncate_status = "response_truncated"
self.logger.warning(
f"Warning: {len(token_ids)=} exceeds the length limit {(self.config.max_model_len - 1)=}"
)
token_ids = token_ids[: self.config.max_model_len - 1]
action_mask = action_mask[: self.config.max_model_len - 1]
if self.config.max_model_len is not None and self.config.max_model_len > 0:
# The -1 is to leave space for at least one token to be generated for logprobs calculation.
max_len = self.config.max_model_len - 1
if len(token_ids) > max_len:
truncate_status = "response_truncated"
self.logger.warning(
f"Warning: {len(token_ids)=} exceeds the length limit {max_len=}"
)
token_ids = token_ids[:max_len]
action_mask = action_mask[:max_len]

@pan-x-c
Copy link
Collaborator

pan-x-c commented Jan 20, 2026

/unittest-module-common

@github-actions
Copy link

Summary

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Other ❓ Flaky 🍂 Duration ⏱️
42 41 0 1 0 0 14m 12s

Skipped

Tests Status
tests/common/vllm_test.py::TestTinkerAsyncAPIServer::test_api_async skipped ⏭️

Tests

Test Name Status Flaky Duration
tests/common/config_test.py::TestConfig::test_all_examples_are_valid 36.3s
tests/common/config_test.py::TestConfig::test_chat_template_path 73ms
tests/common/config_test.py::TestConfig::test_config_flatten 30ms
tests/common/config_test.py::TestConfig::test_continue_from_checkpoint_is_valid 430ms
tests/common/config_test.py::TestConfig::test_default_workflow 72ms
tests/common/config_test.py::TestConfig::test_load_default_config 4.6s
tests/common/config_test.py::TestConfig::test_max_token_len_per_gpu_set_correctly 72ms
tests/common/config_test.py::TestConfig::test_optimizer_config_propagation 72ms
tests/common/config_test.py::TestConfig::test_update_config_from_ray_cluster 1.6s
tests/common/experience_test.py::TestEID::test_eid_properties 1ms
tests/common/experience_test.py::TestExperience::test_action_mask_and_logprobs_type 1ms
tests/common/experience_test.py::TestExperience::test_assertions 1ms
tests/common/experience_test.py::TestExperience::test_dpo_experience 1ms
tests/common/experience_test.py::TestExperience::test_gather 1ms
tests/common/experience_test.py::TestExperience::test_gather_with_token_level_reward 1ms
tests/common/experience_test.py::TestExperience::test_hf_datasets_conversion 13ms
tests/common/experience_test.py::TestExperience::test_multi_turn_experience 1ms
tests/common/experience_test.py::TestExperience::test_serialize_deserialize 1ms
tests/common/experience_test.py::TestExperience::test_single_turn_experience 1ms
tests/common/experience_test.py::TestExperience::test_to_dict 1ms
tests/common/experience_test.py::TestExperienceConversion::test_batch_conversion 1ms
tests/common/experience_test.py::TestExperienceConversion::test_dpo_experience_batch_conversion 1ms
tests/common/experience_test.py::TestExperienceConversion::test_experience_model_experience_conversion 1ms
tests/common/experience_test.py::TestExperienceConversion::test_gather_experiences_with_custom_fields 1ms
tests/common/experience_test.py::TestExperienceConversion::test_multiturn_experience_batch_converstion 1ms
tests/common/vllm_test.py::ModelWrapperTest_0::test_generate 1m 16s
tests/common/vllm_test.py::ModelWrapperTest_1::test_generate 1m 5s
tests/common/vllm_test.py::ModelWrapperTest_2::test_generate 1m 11s
tests/common/vllm_test.py::TestModelLen_0::test_model_len 58.3s
tests/common/vllm_test.py::TestModelLen_1::test_model_len 21.9s
tests/common/vllm_test.py::TestModelLen_2::test_model_len 51.8s
tests/common/vllm_test.py::TestModelLenWithoutPromptTruncation::test_model_len 51.3s
tests/common/vllm_test.py::TestAPIServer::test_api 24.3s
tests/common/vllm_test.py::TestLogprobs::test_logprobs_api 21.7s
tests/common/vllm_test.py::TestAsyncAPIServer::test_api_async 54.4s
tests/common/vllm_test.py::TestTinkerAsyncAPIServer::test_api_async ⏭️ 1ms
tests/common/vllm_test.py::TestTokenizer::test_action_mask 298ms
tests/common/vllm_test.py::TestTokenizer::test_action_mask_with_tools 337ms
tests/common/vllm_test.py::TestAPIServerToolCall_0_deepseek_r1::test_api_tool_calls 53.2s
tests/common/vllm_test.py::TestAPIServerToolCall_1::test_api_tool_calls 22.1s
tests/common/vllm_test.py::TestSuperLongGeneration::test_generate 2m 36s
tests/common/vllm_test.py::TestTinkerAPI::test_tinker_api 1m 13s

Github Test Reporter by CTRF 💚

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.

2 participants