-
Notifications
You must be signed in to change notification settings - Fork 47
Refactor MathWorkflows #60
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
Closed
garyzhang99
wants to merge
7
commits into
modelscope:main
from
garyzhang99:dev/separate_math_workflows
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf9845b
change math workflows
a1c4cc1
add new math workflows
ffd317a
fix styles
758a086
pass all tests
garyzhang99 f8088b4
add workflows to init
garyzhang99 d0c64a3
pass pre-commit
garyzhang99 66d5c43
remove chinese comments and typos
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """We include seprate the math workflows in this file.""" | ||
| from functools import partial | ||
| from typing import List, Optional | ||
|
|
||
| import openai | ||
|
|
||
| from trinity.common.models.model import ModelWrapper | ||
| from trinity.common.rewards.reward_fn import MathBoxedRewardFn, MathRewardFn | ||
| from trinity.common.workflows.workflow import ( | ||
| WORKFLOWS, | ||
| BaseModelWorkflow, | ||
| SimpleWorkflow, | ||
| Task, | ||
| ) | ||
|
|
||
| PREDEFINED_MATH_SYSTEM_PROMPTS = { | ||
| "deepseek_like": """A conversation between User and Assistant. The user asks a question, and the Assistant solves it. The assistant first thinks about the reasoning process in the mind and then provides the user with the answer. The reasoning process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively, i.e., | ||
| <think> reasoning process here </think> | ||
| <answer> answer here </answer>.""", | ||
| "boxed_with_think": """You are a helpful assistant that solves MATH problems. You should first thinks about the reasoning process in mind and then provides the user with the answer. You should present your reasoning process using the format: <think>\n ...your reasoning process here... </think>\n first. You should always include your final answer in \\boxed{} as closed-form results.""", | ||
| "boxed_no_think": """Please reason step by step, and put your final answer within \\boxed{}.""", | ||
| } | ||
|
|
||
|
|
||
| @WORKFLOWS.register_module("math_workflow") | ||
| class MathWorkflow(SimpleWorkflow): | ||
| """A workflow for math tasks""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| model: ModelWrapper, | ||
| task: Task, | ||
| auxiliary_models: Optional[List[openai.OpenAI]] = None, | ||
| ): | ||
| self.reset(task) | ||
| super().__init__( | ||
| model=model, | ||
| task=task, | ||
| auxiliary_models=auxiliary_models, | ||
| ) | ||
|
|
||
| def reset(self, task: Task): | ||
| if task.format_args.system_prompt is None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is basically the same as the old |
||
| task.format_args.system_prompt = PREDEFINED_MATH_SYSTEM_PROMPTS["deepseek_like"] | ||
| if task.format_args.system_prompt in PREDEFINED_MATH_SYSTEM_PROMPTS.keys(): | ||
| task.format_args.system_prompt = PREDEFINED_MATH_SYSTEM_PROMPTS[ | ||
| task.format_args.system_prompt | ||
| ] | ||
|
|
||
| have_boxed_pattern = "boxed{" in task.format_args.system_prompt | ||
| if not have_boxed_pattern: | ||
| task.reward_fn = MathRewardFn | ||
| else: | ||
| have_think_pattern = ( | ||
| "<think>" in task.format_args.system_prompt | ||
| and "</think>" in task.format_args.system_prompt | ||
| ) | ||
| task.reward_fn = partial(MathBoxedRewardFn, have_think_pattern=have_think_pattern) | ||
|
|
||
| # call the SimpleWorkflow.reset | ||
| super().reset(task) | ||
|
|
||
|
|
||
| @WORKFLOWS.register_module("math_based_model_workflow") | ||
| class MathBasedModelWorkflow(BaseModelWorkflow): | ||
| """A workflow for math tasks, using base model""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| model: ModelWrapper, | ||
| task: Task, | ||
| auxiliary_models: Optional[List[openai.OpenAI]] = None, | ||
| ): | ||
| self.reset(task) | ||
| super().__init__( | ||
| model=model, | ||
| task=task, | ||
| auxiliary_models=auxiliary_models, | ||
| ) | ||
|
|
||
| def reset(self, task: Task): | ||
| if task.format_args.system_prompt is None: | ||
| task.format_args.system_prompt = PREDEFINED_MATH_SYSTEM_PROMPTS["deepseek_like"] | ||
| if task.format_args.system_prompt in PREDEFINED_MATH_SYSTEM_PROMPTS.keys(): | ||
| task.format_args.system_prompt = PREDEFINED_MATH_SYSTEM_PROMPTS[ | ||
| task.format_args.system_prompt | ||
| ] | ||
|
|
||
| have_boxed_pattern = "boxed{" in task.format_args.system_prompt | ||
| if not have_boxed_pattern: | ||
| task.reward_fn = MathRewardFn | ||
| else: | ||
| have_think_pattern = ( | ||
| "</think>" in task.format_args.system_prompt | ||
| and "</think>" in task.format_args.system_prompt | ||
| ) | ||
| task.reward_fn = partial(MathBoxedRewardFn, have_think_pattern=have_think_pattern) | ||
|
|
||
| # call the SimpleWorkflow.reset | ||
| super().reset(task) | ||
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
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.
We should add a
workflow_args: Dictfield to the__init__method ofWorkflowinterface