Skip to content
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

Getting YAMLs without populating defaults #24

Open
tovacinni opened this issue Dec 29, 2022 · 2 comments
Open

Getting YAMLs without populating defaults #24

tovacinni opened this issue Dec 29, 2022 · 2 comments

Comments

@tovacinni
Copy link

Hi tyro team,

First of all thanks for this super cool configuration library!! It looks awesome.

While reading the docs and playing around with the configurator I had a small question: is it possible to output a yaml file for a hierarchical config without first populating the argument defaults via the command line?

The usecase is as follows:

After defining a (hierarchical) dataclass schema, I want to populate a yaml with all null entries so that I can then populate that yaml to use as the default arguments. Ideally I can follow a flow like: 1. the code looks for a default config 2. if none exists, will populate an empty config 3. users can then populate the defaults inside the yaml, and then override the yaml with CLI arguments.

@brentyi
Copy link
Owner

brentyi commented Dec 29, 2022

Hi @tovacinni!

Just want to make sure I'm fully following: could you provide a concrete example of the behavior you're looking for? In particular, I'm wondering what the outputted empty YAML config file would look like.

This generally sounds possible, although I'd need to think about which parts tyro should be responsible for. In the flow you described, it seems like we're currently only focused on the last step. (overriding with CLI arguments)

@tovacinni
Copy link
Author

What I ended up doing for now is that I wrote a quick dataclass_schema_to_dict function that converts the Config dataclass type to a dictionary:

def dataclass_schema_to_dict(dataclass_class):
    return_dict = {}
    if not dataclasses.is_dataclass(dataclass_class):
        raise Exception("The class {dataclass_class} is not a dataclass.")
    for field in dataclasses.fields(dataclass_class):
        if dataclasses.is_dataclass(field.type):
            obj = dataclass_schema_to_dict(field.type)
            return_dict[field.name] = obj
        else:
            return_dict[field.name] = f"{field.type.__name__}"
    return return_dict

This can then be dumped to a YAML, where in this case I have as default values strings indicating the argument type. Since there is support for docs in dynamic dataclasses, it might actually make more sense to output these as comments along with the docstring.

batch_size: int
grid1:
  base_lod: int
  blas_level: int
  interpolation_type: str
seed: int
train_steps: int

As you say though since these steps are possible without looping in tyro, maybe it doesn't need to be a tyro responsibility- unless there's a more recommended way I might do this that tyro already supports!

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

No branches or pull requests

2 participants