Skip to content

Use dataclass instead of TypedDict for config #1675

Open

Description

@xuantengh

Currently, nemo-rl uses TypedDict to host the config options:

class GRPOConfig(TypedDict):
num_prompts_per_step: int
num_generations_per_prompt: int
max_num_epochs: int
max_num_steps: int
max_rollout_turns: int
normalize_rewards: bool
use_leave_one_out_baseline: bool
val_period: int
val_batch_size: int
val_at_start: bool
max_val_samples: int
seed: int
async_grpo: NotRequired[AsyncGRPOConfig]
overlong_filtering: NotRequired[bool]
# whether to enable dynamic sampling, i.e.
# whether to discard prompts whose rewards have zero standard deviation
use_dynamic_sampling: bool
# When using dynamic sampling, the maximum number of batches to generate
# before throwing an error
dynamic_sampling_max_gen_batches: NotRequired[int]
# When using dynamic sampling, generation prompt batch size will equal
# num_prompts_per_step * batch_multiplier
batch_multiplier: NotRequired[float]
reward_shaping: RewardShapingConfig
reward_scaling: RewardScalingConfig
class GRPOSaveState(TypedDict):
consumed_samples: int
current_step: int
current_epoch: int
total_steps: int
total_valid_tokens: int # Track total number of non-padding tokens during training
val_reward: NotRequired[
float
] # Optional field - may not be present during training
def _default_grpo_save_state() -> GRPOSaveState:
return {
"consumed_samples": 0,
"current_step": 0,
"current_epoch": 0,
"total_steps": 0,
"total_valid_tokens": 0,
"val_reward": -99999999.0,
}
class GRPOLoggerConfig(LoggerConfig):
num_val_samples_to_print: int # number of val samples to print to stdout
class MasterConfig(TypedDict):
policy: PolicyConfig
loss_fn: ClippedPGLossConfig
env: dict[str, Any]
data: DataConfig
grpo: GRPOConfig
logger: GRPOLoggerConfig
cluster: ClusterConfig
checkpointing: CheckpointingConfig

We can consider to use dataclass to host these options like:

@dataclass
class GRPOConfig:
    num_prompts_per_step: int
    num_generations_per_prompt: int
    max_num_epochs: int
    max_num_steps: int
    # ...

It has several benifits, like, more friendly for type checking for pyrefly/mypy, and ease of development, e.g., you can directly access config.logger.log_dir with type hints in IDE/editor, instead of typing config["logger"]["log_dir"]. We may also add some runtime validation logic in the __post_init__ dunder for dataclass.

The dataclass could be converted from omegaconf using to_object method.

Activity

  1. added
    enhancement
    on Dec 21, 2025
  2. coderabbitai commented on Dec 21, 2025

    @coderabbitai
    Contributor

    📝 CodeRabbit Plan Mode

    Generate an implementation plan and prompts that you can use with your favorite coding agent.

    • Create Plan
    Examples


    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord or schedule a call!

  3. terrykong commented on Dec 21, 2025

    @terrykong
    Collaborator

    Hi @xuantengh . I expected this question to come to us eventually :)

    So I'm not opposed to dataclasses, in fact there are benefits to using dataclasses (e.g., dot getters). Let me first share some of the initial motivation for typeddicts and maybe we can see if we can come out of this with anything actionable.

    Initially, we wanted something very minimal and flexible. An example would be if someone has a yaml that looks different from what we expect because:

    1. they want to have extra keys because they are extending the framework (imagine a fork where GRPO has more config options)
    2. using a job config yaml from an older nemo-rl but in newer nemo-rl code (ex: deprecated flags still present)

    we wouldn't want to error out because the structure was different, so long as they had the necessary keys/values. TypedDict's being hints gave us enough to document the config and let a static type hint tool analyze our code, while not getting in the way with "version skew" of configs.

    Dataclasses offer lots of goodies and are more friendly to IDEs plus having a way to post init, so I agree those would be nice, but one thing they do not offer out of the box is this key mismatch.

    It's definitely possible to design a solution with dataclasses that deals with this key mismatch, but I'd like to understand what thing we can help solve by doing so. Is this nice-to-have for you or blocking you in some way? Would love to hear more about how this can help

  4. xuantengh commented on Dec 22, 2025

    @xuantengh
    ContributorAuthor

    Thanks for your patient explanation about the backward compatiblity consideration for chosing TypedDict.

    Is this nice-to-have for you or blocking you in some way?

    Yeah, it is just a nice-to-have feature. It does not block anything, and I'm happy with TypedDict as well. Feel free to close this if this is not planned at least right now.

  5. terrykong commented on Dec 22, 2025

    @terrykong
    Collaborator

    No worries. Thanks for raising. I think we can keep this issue open for now in case there are more opinions on this.

  6. added
    waiting-on-maintainers
    and removed
    needs-follow-up
    on Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

community-requestenhancementt-dataclasswaiting-on-maintainersx-tencent

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions