Skip to content

[Experimental] Add a MultiStepPadding Operator#511

Open
hiyuchang wants to merge 1 commit intoagentscope-ai:mainfrom
hiyuchang:feat/regularize_multi_turn
Open

[Experimental] Add a MultiStepPadding Operator#511
hiyuchang wants to merge 1 commit intoagentscope-ai:mainfrom
hiyuchang:feat/regularize_multi_turn

Conversation

@hiyuchang
Copy link
Collaborator

Description

As the title says.

Still testing the effect. Do NOT merge this PR directly.

TODO: only works with token-mean.

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 @hiyuchang, 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 introduces an experimental MultiStepPadding operator designed to normalize the length of experience sequences within the trinity framework. By padding shorter rollouts with synthetic placeholder experiences, it aims to facilitate consistent batch processing, particularly in reinforcement learning contexts. The changes also include updates to core experience data structures to properly identify and manage these padded entries, along with an example configuration for its application.

Highlights

  • New MultiStepPadding Operator: Introduced a new MultiStepPadding operator to standardize the length of experience rollouts by adding placeholder experiences up to a specified max_steps.
  • Operator Integration: Integrated the MultiStepPadding operator into the trinity buffer system, making it available for use in experience processing pipelines.
  • Experience Status Tracking: Enhanced the Experience and Experiences data structures to track the 'status' of experiences, differentiating between original and newly added placeholder entries.
  • Example Configuration: Provided an example configuration (multi_step_padding.yaml) demonstrating the usage of the new operator within a FrozenLake environment setup.
  • Effective Experience Statistics: Added utility functions in the trainer to log statistics about effective (non-placeholder) experiences, aiding in debugging and understanding the impact of padding.
Changelog
  • examples/agentscope_frozenlake/agent.py
    • Disabled console output for the agent's OpenAIChatModel instance.
  • examples/agentscope_frozenlake/multi_step_padding.yaml
    • Added a new YAML configuration file for a FrozenLake experiment, specifying multi_step_grpo algorithm with the multi_step_padding operator.
  • trinity/buffer/operators/init.py
    • Registered the MultiStepPadding class in the _OPERATOR_MAP.
  • trinity/buffer/operators/multi_step_operator.py
    • Created a new file implementing the MultiStepPadding class, which processes a list of Experience objects, groups them by task and run, and pads shorter runs with placeholder experiences up to max_steps.
  • trinity/common/experience.py
    • Modified the gather function to collect statuses from experiences.
    • Added a statuses field to the Experiences dataclass.
    • Introduced a new helper function gather_statuses to determine if an experience is a placeholder or effective.
  • trinity/trainer/verl/utils.py
    • Imported gather_statuses.
    • Added a new function print_effective_experience_stats to log the count of effective experiences and a reweight factor.
    • Integrated print_effective_experience_stats into the to_data_proto function.
Activity
  • The author explicitly stated that the feature is 'Still testing the effect' and 'Do NOT merge this PR directly,' indicating it's an experimental work-in-progress.
  • A TODO comment notes that the operator 'only works with token-mean.'
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 an experimental MultiStepPadding operator, designed to pad experience rollouts to a fixed length with placeholder experiences. The implementation is well-structured, including updates to the Experience data structures to handle these placeholders and utility functions to process them. The code is generally of high quality for an experimental feature. I've included a couple of suggestions to enhance code clarity and maintainability.

Comment on lines +71 to +72
rewards = [exp.reward for exp in exps_same_run if exp.reward is not None]
avg_reward = sum(rewards) / len(rewards)
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 if exp.reward is not None check within the list comprehension is redundant. The assert on lines 68-70 already ensures that exp.reward is not None for all experiences in exps_same_run. Removing this redundant check will improve code clarity.

Suggested change
rewards = [exp.reward for exp in exps_same_run if exp.reward is not None]
avg_reward = sum(rewards) / len(rewards)
rewards = [exp.reward for exp in exps_same_run]
avg_reward = sum(rewards) / len(rewards)

Comment on lines +613 to +620
def gather_statuses(experiences) -> Tensor:
statuses = []
for exp in experiences:
if exp.info.get("status", None) == "placeholder":
statuses.append(0)
else:
statuses.append(1)
return torch.tensor(statuses, dtype=torch.bool)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved readability and type safety, consider adding a type hint for the experiences argument. Additionally, the function's body can be refactored into a more concise list comprehension.

def gather_statuses(experiences: List[Experience]) -> Tensor:
    statuses = [0 if exp.info.get("status", None) == "placeholder" else 1 for exp in experiences]
    return torch.tensor(statuses, dtype=torch.bool)

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.

1 participant