Skip to content

Add argparse_utils and enhance typer_utils for megatron runner#769

Open
fzyzcjy wants to merge 5 commits intomainfrom
ac8452/1/3
Open

Add argparse_utils and enhance typer_utils for megatron runner#769
fzyzcjy wants to merge 5 commits intomainfrom
ac8452/1/3

Conversation

@fzyzcjy
Copy link
Collaborator

@fzyzcjy fzyzcjy commented Mar 20, 2026

No description provided.

fzyzcjy added 4 commits March 20, 2026 18:47
Add dumper CLI arguments (--dumper-enable, --dumper-dir, per-phase config),
dumper_utils.py for SGLang/Megatron dumper integration, model.py hooks
for forward-only and forward-backward phases, rollout env var plumbing,
source patcher wiring in training actors, and basic e2e test.
Add _maybe_apply_dumper_overrides to disable heartbeats, force single
rollout, and disable eval/save when --dumper-enable is set.
Add conftest_dumper.py with shared source patcher YAML configs and
comparator helpers. Expand test_dumper.py with full MoE parallelism
coverage, field verification, and cross-framework (SGLang vs Megatron)
activation comparison. Update dumper_utils.py to nest engine dumps under
engines/ subdirectory.
Add DataclassArgparseBridge for dataclass-to-argparse conversion,
refactor typer_utils with dataclass_cli decorator, and add
parallel_utils for running parallel CLI commands. Include tests.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 improves the handling of command-line arguments by introducing new utilities that bridge Python dataclasses with popular CLI parsing libraries like argparse and Typer. The changes aim to simplify the definition and management of application configurations, making it more robust and developer-friendly for building command-line interfaces.

Highlights

  • New Argparse Utility: Added argparse_utils.py which introduces DataclassArgparseBridge to seamlessly convert Python dataclasses into argparse arguments and vice-versa, supporting various scalar types and optional fields.
  • Enhanced Typer Utility: Refactored typer_utils.py's dataclass_cli decorator to support more flexible usage (bare and parameterized), integrate dataclasses.field metadata for CLI help text, and improve type handling for better CLI generation.
  • Comprehensive Testing: Included extensive unit tests for both the new argparse_utils module and the enhanced typer_utils decorator, covering various scenarios including default values, optional arguments, boolean flags, enum types, and error handling.
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.

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 helpful DataclassArgparseBridge and enhances typer_utils with more features and robust testing. The changes are well-structured and the new utilities are accompanied by comprehensive tests. I've found a couple of areas for improvement in argparse_utils.py to enhance compatibility and correctness, particularly around type checking for optional types and handling of boolean flags with True defaults.

Comment on lines +24 to +33
def _is_optional(tp: type) -> tuple[bool, type | None]:
if not isinstance(tp, types.UnionType):
return False, None

args: tuple[type, ...] = tp.__args__
non_none: list[type] = [a for a in args if a is not type(None)]
if type(None) not in args or len(non_none) != 1:
return False, None

return True, non_none[0]
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 current implementation of _is_optional using isinstance(tp, types.UnionType) is only compatible with Python 3.10+. To support typing.Union from older Python versions, it's better to use typing.get_origin and typing.get_args. This will make the utility more robust across different Python versions.

Suggested change
def _is_optional(tp: type) -> tuple[bool, type | None]:
if not isinstance(tp, types.UnionType):
return False, None
args: tuple[type, ...] = tp.__args__
non_none: list[type] = [a for a in args if a is not type(None)]
if type(None) not in args or len(non_none) != 1:
return False, None
return True, non_none[0]
def _is_optional(tp: type) -> tuple[bool, type | None]:
origin = get_origin(tp)
if origin is Union or (hasattr(types, "UnionType") and origin is types.UnionType):
args = get_args(tp)
non_none: list[type] = [a for a in args if a is not type(None)]
if type(None) in args and len(non_none) == 1:
return True, non_none[0]
return False, None

Comment on lines +86 to +88
if _is_bool(tp):
group.add_argument(flag, dest=dest, action="store_true", default=False)
continue
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 current handling of boolean fields assumes they default to False. If a dataclass field is defined with my_flag: bool = True, this implementation will incorrectly set the argument's default to False. This means if the flag isn't provided on the command line, the value will be False, ignoring the dataclass default.

A more robust approach would be to respect the dataclass default, for example by using argparse.BooleanOptionalAction (Python 3.9+) or manually implementing both --flag and --no-flag arguments. This would likely require updating to_cli_args as well to be consistent.

Co-authored-by: Yueming Yuan <112649537+yueming-yuan@users.noreply.github.com>
@fzyzcjy fzyzcjy changed the base branch from ac8452/1/2 to main March 20, 2026 13:58
@fzyzcjy fzyzcjy requested a review from yushengsu-thu as a code owner March 20, 2026 13:58
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