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

Fill missing options in toml file wih argparse defaults #91

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/test_job_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class TestJobConfig:
def test_command_line_args(self):
config = JobConfig()
config.parse_args([])
assert config.model.name == "llama"
assert config.training.steps == -1

def test_job_config_file(self):
config = JobConfig()
config.parse_args(["--job.config_file", "./train_configs/debug_model.toml"])
assert config.model.name == "llama"
assert config.training.steps == 10

def test_job_file_does_not_exist(self):
with pytest.raises(FileNotFoundError):
Expand Down
11 changes: 7 additions & 4 deletions torchtrain/config_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.

# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
import argparse
Expand All @@ -17,16 +20,16 @@ class JobConfig:
Semantics:
- Default config is loaded from a toml file. If no toml file is provided,
then the default config is loaded from argparse defaults.
- if toml file has missing keys, they are filled with argparse defaults.
"""

def parse_args(self, args_list: list = sys.argv[1:]):
args = JobConfig.init_args_from_command_line(args_list)
config_file = getattr(args, "job.config_file", None)
if config_file is None:
args_dict = self._args_to_two_level_dict(args)
else:
args_dict = self._args_to_two_level_dict(args)
if config_file is not None:
with open(config_file, "rb") as f:
args_dict = tomllib.load(f)
args_dict |= tomllib.load(f)
Copy link
Contributor

Choose a reason for hiding this comment

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

surprised this line does the magic here 🤔 it's a interesting python feature

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, nested dict gets updated using the | operator or in older version we can use the .update method.

for k, v in args_dict.items():
class_type = type(k.title(), (), v)
setattr(self, k, class_type())
Expand Down
Loading