Skip to content

⛏️ Add cli dict parsing for grpo_config #3082

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
merged 5 commits into from
Apr 8, 2025

Conversation

Tavish9
Copy link
Contributor

@Tavish9 Tavish9 commented Mar 14, 2025

What does this PR do?

Adds dict parsing logic to model_init_kwargs in grpo_config, enabling dynamic configuration via CLI. Users can now pass dictionary-like strings (e.g., --model_init_kwargs '{"torch_dtype":"bfloat16") through command-line arguments, which are automatically parsed into Python dicts for the target fields.

Logic is the same as TrainingArguments.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@qgallouedec
Copy link
Member

Thanks! Does it work is you directly modify transformers.training_args._VALID_DICT_FIELDS instead?

@Tavish9
Copy link
Contributor Author

Tavish9 commented Mar 14, 2025

Yes, but both of transformers.training_args and trl.GRPOConfig should have their independent _VALID_DICT_FIELDS , as private attribute does.

In GRPOConfig.__post_init__, it first post-inits it's _VALID_DICT_FIELDS and then transformers.training_args's

@qgallouedec
Copy link
Member

It seems to work:

from transformers.training_args import _VALID_DICT_FIELDS
from trl import GRPOConfig

_VALID_DICT_FIELDS.append("model_init_kwargs")

args = GRPOConfig("output_dir", model_init_kwargs='{"num_labels": 2}')
print(args.model_init_kwargs)  # {"num_labels": 2}

@qgallouedec
Copy link
Member

To do this properly, the first step would be to convert _VALID_DICT_FIELDS into a class attribute of TrainingArguments in transformers. Are you ready to open such a PR in Transformers?

Then we could do:

# in transformers
class TrainingArguments:
    _VALID_DICT_FIELDS = [...]

# in trl
class GRPOConfig(TrainingArguments):
    _VALID_DICT_FIELDS = TrainingArguments._VALID_DICT_FIELDS + ["model_init_kwargs"]

which eliminates the need to duplicate the post init

@Tavish9
Copy link
Contributor Author

Tavish9 commented Mar 14, 2025

To do this properly, the first step would be to convert _VALID_DICT_FIELDS into a class attribute of TrainingArguments in transformers. Are you ready to open such a PR in Transformers?

Then we could do:

# in transformers
class TrainingArguments:
    _VALID_DICT_FIELDS = [...]

# in trl
class GRPOConfig(TrainingArguments):
    _VALID_DICT_FIELDS = TrainingArguments._VALID_DICT_FIELDS + ["model_init_kwargs"]

which eliminates the need to duplicate the post init

Yes, that was my initial thought as well. However, considering that the transformers defines _VALID_DICT_FIELDS as semi-private, I decided against submitting a PR to their repository. If we follow the semi-private variable approach, each config should ideally have its own variable, even though this might lead to some code duplication in the __post__init__ logic. That said, I’m also open to the idea of modifying the semi-private variable in the transformers to make it a class attribute. However, I’m not sure if the maintainers would be receptive to this change in philosophy.

What's your suggestions?

@qgallouedec
Copy link
Member

Yes I think first modifying transformers is the way to go.

@Tavish9
Copy link
Contributor Author

Tavish9 commented Mar 14, 2025

okay, I would notify you when pr merged. :)

@Tavish9 Tavish9 marked this pull request as draft March 14, 2025 16:42
@Tavish9 Tavish9 force-pushed the grpo_config_extend branch from 0743c5a to 3e44f00 Compare April 1, 2025 10:51
@Tavish9 Tavish9 marked this pull request as ready for review April 1, 2025 10:52
@Tavish9
Copy link
Contributor Author

Tavish9 commented Apr 1, 2025

Hi, @qgallouedec, the PR in Transformers is merged. 🥳

@qgallouedec
Copy link
Member

I just need to review it carefully and ensure backwards compatibility
I'll do it asap.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Tavish9
Copy link
Contributor Author

Tavish9 commented Apr 7, 2025

maybe you need to update the version of transfomers and re-run the test?

@qgallouedec
Copy link
Member

So currently this change isn't backward compatible we need to figure out how to make it backward compatible

@Tavish9
Copy link
Contributor Author

Tavish9 commented Apr 7, 2025

okay, let me try with version checking

@Tavish9 Tavish9 force-pushed the grpo_config_extend branch from 9510b36 to 34f148b Compare April 7, 2025 05:18
Copy link
Member

@qgallouedec qgallouedec left a comment

Choose a reason for hiding this comment

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

Nice!! Thanks

@qgallouedec qgallouedec changed the title add cli dict parsing for grpo_config ⛏️ Add cli dict parsing for grpo_config Apr 8, 2025
@qgallouedec qgallouedec merged commit e03e7ac into huggingface:main Apr 8, 2025
9 checks passed
yxliu-TAMU pushed a commit to mincheolseong/ECEN743-GRPO-Project-Proposal that referenced this pull request Apr 20, 2025
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.

3 participants