-
Notifications
You must be signed in to change notification settings - Fork 55
Add step-wise workflow test #173
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
pan-x-c
merged 3 commits into
agentscope-ai:main
from
hiyuchang:feat/multi_step_workflow_test
Aug 8, 2025
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Test for the general step-wise workflow module""" | ||
| import unittest | ||
| from dataclasses import dataclass, field | ||
| from typing import Dict, Optional | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from torch import Tensor | ||
|
|
||
| from tests.tools import get_unittest_dataset_config | ||
| from trinity.common.experience import EID, Experience | ||
| from trinity.common.workflows.step_wise_workflow import ( | ||
| RewardPropagationWorkflow, | ||
| StepWiseRewardWorkflow, | ||
| ) | ||
| from trinity.common.workflows.workflow import Task | ||
|
|
||
|
|
||
| @dataclass | ||
| class MockResponse: | ||
| response_text: str | ||
| reward: float = 0.0 | ||
| metrics: Optional[Dict[str, float]] = None | ||
| info: Optional[Dict] = None | ||
| unique_id: Optional[str] = "0" | ||
| tokens: Optional[Tensor] = Tensor([0, 0]) | ||
| prompt_length: int = 1 | ||
| eid: EID = field(default_factory=EID) | ||
|
|
||
|
|
||
| class DummyStepWiseRewardWorkflow(StepWiseRewardWorkflow): | ||
| def __init__(self, model, task: Task, auxiliary_models=None): | ||
| super().__init__(task=task, model=model, auxiliary_models=auxiliary_models) | ||
| self.repeat_times = task.repeat_times | ||
| self.max_env_steps = task.workflow_args.get("max_env_steps", 1) | ||
| self.actual_steps = task.workflow_args.get("actual_steps", 1) | ||
|
|
||
| def step(self, step_num: int): | ||
| return step_num < self.actual_steps - 1 | ||
|
|
||
| def reward(self, exps: list[Experience], step_num: int): | ||
| return 0.1 * step_num | ||
|
|
||
| @property | ||
| def max_step_num(self): | ||
| return self.max_env_steps | ||
|
|
||
|
|
||
| class DummyRewardPropagationWorkflow(RewardPropagationWorkflow): | ||
| def __init__(self, model, task: Task, auxiliary_models=None): | ||
| super().__init__(task=task, model=model, auxiliary_models=auxiliary_models) | ||
| self.repeat_times = task.repeat_times | ||
| self.max_env_steps = task.workflow_args.get("max_env_steps", 1) | ||
| self.actual_steps = task.workflow_args.get("actual_steps", 1) | ||
|
|
||
| def step(self, step_num: int): | ||
| return step_num < self.actual_steps - 1 | ||
|
|
||
| def reward(self, exps: list[Experience]): | ||
| return 0.1 * len(exps) | ||
|
|
||
| @property | ||
| def max_step_num(self): | ||
| return self.max_env_steps | ||
|
|
||
|
|
||
| class WorkflowTest(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.model = MagicMock() | ||
| self.model.enable_history = True | ||
| self.model.extract_experience_from_history.side_effect = lambda: [ | ||
| MockResponse(f"The answer is \\boxed{42}") | ||
| ] | ||
| self.taskset_config = get_unittest_dataset_config("countdown") | ||
|
|
||
| def test_step_wise_reward_workflow(self) -> None: | ||
| task = Task( | ||
| workflow=DummyStepWiseRewardWorkflow, | ||
| repeat_times=self.taskset_config.repeat_times, | ||
| workflow_args={"max_env_steps": 10, "actual_steps": 5}, | ||
| ) | ||
| workflow = task.to_workflow(model=self.model) | ||
| experiences = workflow.run() | ||
|
|
||
| self.assertEqual(len(experiences), 5) | ||
| actual_steps = [exp.eid.step for exp in experiences] | ||
| self.assertEqual(actual_steps, [0, 1, 2, 3, 4]) | ||
| actual_rewards = [exp.reward for exp in experiences] | ||
| expected_rewards = [0.0, 0.1, 0.2, 0.3, 0.4] | ||
| for actual, expected in zip(actual_rewards, expected_rewards): | ||
| self.assertAlmostEqual(actual, expected) # type: ignore | ||
|
|
||
| def test_reward_propagation_workflow(self) -> None: | ||
| task = Task( | ||
| workflow=DummyRewardPropagationWorkflow, | ||
| repeat_times=self.taskset_config.repeat_times, | ||
| workflow_args={"max_env_steps": 10, "actual_steps": 5}, | ||
| ) | ||
| workflow = task.to_workflow(model=self.model) | ||
| experiences = workflow.run() | ||
|
|
||
| self.assertEqual(len(experiences), 5) | ||
| actual_steps = [exp.eid.step for exp in experiences] | ||
| self.assertEqual(actual_steps, [0, 1, 2, 3, 4]) | ||
| expected_reward = 0.5 | ||
| for exp in experiences: | ||
| self.assertAlmostEqual(exp.reward, expected_reward) # type: ignore | ||
|
|
||
| def test_workflows_stop_at_max_env_steps(self) -> None: | ||
| task = Task( | ||
| workflow=DummyStepWiseRewardWorkflow, | ||
| repeat_times=self.taskset_config.repeat_times, | ||
| workflow_args={"max_env_steps": 3, "actual_steps": 100}, # actual > max | ||
| ) | ||
| workflow = task.to_workflow(model=self.model) | ||
| experiences = workflow.run() | ||
| self.assertEqual(len(experiences), 3) | ||
|
|
||
| task = Task( | ||
| workflow=DummyRewardPropagationWorkflow, | ||
| repeat_times=self.taskset_config.repeat_times, | ||
| workflow_args={"max_env_steps": 3, "actual_steps": 100}, # actual > max | ||
| ) | ||
| workflow = task.to_workflow(model=self.model) | ||
| experiences = workflow.run() | ||
| self.assertEqual(len(experiences), 3) | ||
|
|
||
| def test_workflows_raise_error(self) -> None: | ||
| self.model.enable_history = False | ||
| task = Task( | ||
| workflow=DummyStepWiseRewardWorkflow, | ||
| repeat_times=self.taskset_config.repeat_times, | ||
| workflow_args={"max_env_steps": 10, "actual_steps": 5}, | ||
| ) | ||
| with self.assertRaises(AssertionError): | ||
| task.to_workflow(model=self.model) | ||
|
|
||
| task = Task( | ||
| workflow=DummyRewardPropagationWorkflow, | ||
| repeat_times=self.taskset_config.repeat_times, | ||
| workflow_args={"max_env_steps": 10, "actual_steps": 5}, | ||
| ) | ||
| with self.assertRaises(AssertionError): | ||
| task.to_workflow(model=self.model) | ||
hiyuchang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.